summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBastien Nocera <hadess@hadess.net>2022-02-16 09:22:30 +0100
committerBastien Nocera <hadess@hadess.net>2022-02-16 11:36:02 +0000
commita3766b3e781e97792d1e0e0201092cfc89e46dbe (patch)
treede0b4264a52c31cd112b85ff9d58e95981497e29 /tests
parentf0f94273b3fe61ed6e2d84e5874048d760f165c4 (diff)
downloadgnome-bluetooth-a3766b3e781e97792d1e0e0201092cfc89e46dbe.tar.gz
tests: Implement battery test in C
That will make it easier to have control over the object references.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/integration-test.py27
-rw-r--r--tests/meson.build12
-rw-r--r--tests/test_battery.c58
3 files changed, 79 insertions, 18 deletions
diff --git a/tests/integration-test.py b/tests/integration-test.py
index c1cbf903..2dbbee89 100755
--- a/tests/integration-test.py
+++ b/tests/integration-test.py
@@ -436,22 +436,6 @@ class OopTests(dbusmock.DBusTestCase):
self.assertEqual(device.props.alias, 'My other device')
self.assertEqual(device.props.connectable, False)
- def test_battery(self):
- # Make a new client that tries to connect to UPower
- client = GnomeBluetoothPriv.Client.new()
-
- self.wait_for_mainloop()
- list_store = client.get_devices()
- self.assertEqual(list_store.get_n_items(), 2)
-
- device = list_store.get_item(0)
- self.assertEqual(int(device.props.battery_type), int(GnomeBluetoothPriv.BatteryType.PERCENTAGE))
- self.assertEqual(device.props.battery_percentage, 66)
-
- device = list_store.get_item(1)
- self.assertEqual(int(device.props.battery_type), int(GnomeBluetoothPriv.BatteryType.COARSE))
- self.assertEqual(device.props.battery_percentage, 55)
- self.assertEqual(device.props.battery_level, 6)
class Tests(dbusmock.DBusTestCase):
@@ -465,6 +449,7 @@ class Tests(dbusmock.DBusTestCase):
'bluez5', {})
cls.exec_path = [sys.argv[0]]
+ cls.exec_dir = builddir + '/tests/'
if os.getenv('VALGRIND') != None:
cls.exec_path = ['valgrind'] + cls.exec_path
@@ -479,11 +464,19 @@ class Tests(dbusmock.DBusTestCase):
self.dbusmock_bluez = dbus.Interface(self.obj_bluez, 'org.bluez.Mock')
def run_test_process(self):
+ c_tests = ['test_battery']
# Get the calling function's name
test_name = inspect.stack()[1][3]
print(f"Running out-of-process test {test_name}")
# And run the test with the same name in the OopTests class in a separate process
- out = subprocess.run(self.exec_path + ['OopTests.' + test_name], capture_output=True)
+ if test_name in c_tests:
+ if os.getenv('VALGRIND') != None:
+ out = subprocess.run(['valgrind', self.exec_dir + test_name], capture_output=True)
+ else:
+ out = subprocess.run([self.exec_dir + test_name], capture_output=True)
+ else:
+ out = subprocess.run(self.exec_path + ['OopTests.' + test_name], capture_output=True)
+
self.assertEqual(out.returncode, 0, "Running test " + test_name + " failed:" + out.stderr.decode('UTF-8') + '\n\n\nSTDOUT:\n' + out.stdout.decode('UTF-8'))
if os.getenv('VALGRIND') != None:
print(out.stderr.decode('UTF-8'))
diff --git a/tests/meson.build b/tests/meson.build
index d7d87764..70aeb72a 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,11 +1,21 @@
if enable_gir and has_dbusmock
+ c_execs = []
+ c_exec = executable('test_battery',
+ 'test_battery.c',
+ include_directories: lib_inc,
+ dependencies: deps,
+ c_args: cflags,
+ link_with: libgnome_bluetooth,
+ )
+ c_execs += [c_exec]
+
integration_test = find_program('integration-test.py')
envs = environment()
envs.set ('top_builddir', meson.global_build_root())
envs.set ('top_srcdir', meson.global_source_root())
- test_deps = [ gnomebt_priv_gir, ]
+ test_deps = [ gnomebt_priv_gir, c_exec ]
python3 = find_program('python3')
unittest_inspector = find_program('unittest_inspector.py')
diff --git a/tests/test_battery.c b/tests/test_battery.c
new file mode 100644
index 00000000..938af139
--- /dev/null
+++ b/tests/test_battery.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2022 Bastien Nocera <hadess@hadess.net>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include <bluetooth-client.h>
+#include <bluetooth-device.h>
+#include <libupower-glib/upower.h>
+
+int main (int argc, char **argv)
+{
+ BluetoothClient *client;
+ GListStore *list_store;
+
+ client = bluetooth_client_new ();
+ list_store = bluetooth_client_get_devices (client);
+
+ /* Wait for bluez */
+ while (g_list_model_get_n_items (G_LIST_MODEL (list_store)) != 2)
+ g_main_context_iteration (NULL, TRUE);
+ g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (list_store)), ==, 2);
+
+ BluetoothDevice *device;
+ BluetoothBatteryType battery_type;
+ double battery_percentage;
+ device = g_list_model_get_item (G_LIST_MODEL (list_store), 0);
+ /* Wait for upower */
+ g_object_get (G_OBJECT (device), "battery-type", &battery_type, NULL);
+ while (battery_type != BLUETOOTH_BATTERY_TYPE_PERCENTAGE) {
+ g_main_context_iteration (NULL, TRUE);
+ g_object_get (G_OBJECT (device), "battery-type", &battery_type, NULL);
+ }
+ g_object_get (G_OBJECT (device),
+ "battery-type", &battery_type,
+ "battery-percentage", &battery_percentage,
+ NULL);
+ g_assert_cmpuint (battery_type, ==, BLUETOOTH_BATTERY_TYPE_PERCENTAGE);
+ g_assert_cmpfloat (battery_percentage, ==, 66.0);
+ g_object_unref (G_OBJECT (device));
+
+ UpDeviceLevel battery_level;
+ device = g_list_model_get_item (G_LIST_MODEL (list_store), 1);
+ g_object_get (G_OBJECT (device),
+ "battery-type", &battery_type,
+ "battery-percentage", &battery_percentage,
+ "battery-level", &battery_level,
+ NULL);
+ g_assert_cmpuint (battery_type, ==, BLUETOOTH_BATTERY_TYPE_COARSE);
+ g_assert_cmpfloat (battery_percentage, ==, 55.0);
+ g_assert_cmpuint (battery_level, ==, UP_DEVICE_LEVEL_NORMAL);
+ g_object_unref (G_OBJECT (device));
+
+ g_clear_object (&client);
+ g_clear_object (&list_store);
+
+ return 0;
+}