[gnome-settings-daemon/wip/benzea/backlight: 5/9] power: Test backlight directly using umockdev



commit 63ca148891ba1525a692a41bbfe9c104c7727c31
Author: Benjamin Berg <bberg redhat com>
Date:   Fri Apr 20 14:53:03 2018 +0200

    power: Test backlight directly using umockdev
    
    This replaces the special cases and temporary files hack used beforehand
    and enables testing of the async logic in the backlight code.

 .gitlab-ci.yml                      |  2 +-
 plugins/power/gsd-backlight.c       | 55 ++++++++++++-------------------------
 plugins/power/meson.build           |  3 +-
 plugins/power/test-backlight-helper |  5 ++++
 plugins/power/test.py               | 46 +++++++++++++++++++++++--------
 5 files changed, 60 insertions(+), 51 deletions(-)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 23e0ed2c..cf476f95 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -12,7 +12,7 @@ variables:
                 libXtst-devel libwacom-devel meson NetworkManager-libnm-devel nss-devel perl-interpreter
                 polkit-devel pulseaudio-libs-devel pygobject3 python3-dbusmock upower-devel wayland-devel
                 which xorg-x11-drv-wacom-devel xorg-x11-server-Xvfb xorg-x11-utils mutter
-                mesa-dri-drivers
+                mesa-dri-drivers umockdev
 
 
 before_script:
diff --git a/plugins/power/gsd-backlight.c b/plugins/power/gsd-backlight.c
index 39693a75..259a0de7 100644
--- a/plugins/power/gsd-backlight.c
+++ b/plugins/power/gsd-backlight.c
@@ -25,8 +25,6 @@
 #include "gsd-power-constants.h"
 #include "gsd-power-manager.h"
 
-#define MOCK_BRIGHTNESS_FILE "GSD_MOCK_brightness"
-
 #ifdef HAVE_GUDEV
 #include <gudev/gudev.h>
 #endif /* HAVE_GUDEV */
@@ -341,6 +339,7 @@ gsd_backlight_run_set_helper (GsdBacklight *backlight, GTask *task)
 {
         GSubprocess *proc = NULL;
         BacklightHelperData *data = g_task_get_task_data (task);
+        const gchar *gsd_backlight_helper = NULL;
         GError *error = NULL;
 
         g_assert (backlight->active_task == NULL);
@@ -349,12 +348,23 @@ gsd_backlight_run_set_helper (GsdBacklight *backlight, GTask *task)
         if (data->value_str == NULL)
                 data->value_str = g_strdup_printf ("%d", data->value);
 
-        proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE,
-                                 &error,
-                                 "pkexec", 
-                                 LIBEXECDIR "/gsd-backlight-helper",
-                                 g_udev_device_get_sysfs_path (backlight->udev_device),
-                                 data->value_str, NULL);
+        /* This is solely for use by the test environment. If given, execute
+         * this helper instead of the internal helper using pkexec */
+        gsd_backlight_helper = g_getenv ("GSD_BACKLIGHT_HELPER");
+        if (!gsd_backlight_helper) {
+                proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE,
+                                         &error,
+                                         "pkexec",
+                                         LIBEXECDIR "/gsd-backlight-helper",
+                                         g_udev_device_get_sysfs_path (backlight->udev_device),
+                                         data->value_str, NULL);
+        } else {
+                proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE,
+                                         &error,
+                                         gsd_backlight_helper,
+                                         g_udev_device_get_sysfs_path (backlight->udev_device),
+                                         data->value_str, NULL);
+        }
 
         if (proc == NULL) {
                 gsd_backlight_set_helper_return (backlight, task, -1, error);
@@ -452,22 +462,6 @@ gsd_backlight_set_brightness_val_async (GsdBacklight *backlight,
 
         task = g_task_new (backlight, cancellable, callback, user_data);
 
-        if (is_mocked ()) {
-                g_autofree gchar *contents = NULL;
-                g_debug ("Setting mock brightness: %d", value);
-
-                contents = g_strdup_printf ("%d", value);
-                if (!g_file_set_contents (MOCK_BRIGHTNESS_FILE, contents, -1, &error)) {
-                        g_warning ("Setting mock brightness failed: %s", error->message);
-                        g_task_return_error (task, error);
-                }
-                priv->brightness_val = priv->brightness_target;
-                g_object_notify_by_pspec (G_OBJECT (backlight), props[PROP_BRIGHTNESS]);
-                g_task_return_int (task, gsd_backlight_get_brightness (backlight, NULL));
-
-                return;
-        }
-
 #ifdef HAVE_GUDEV
         if (backlight->udev_device != NULL) {
                 BacklightHelperData *task_data;
@@ -720,19 +714,6 @@ gsd_backlight_initable_init (GInitable       *initable,
                 return FALSE;
         }
 
-        /* If mocked, set as available and set the brightness (which will also
-         * create the file for the test environment). */
-        if (is_mocked ()) {
-                g_debug ("Using mock for backlight.");
-                priv->available = TRUE;
-                priv->brightness_min = 0;
-                priv->brightness_max = 100;
-
-                gsd_backlight_set_brightness_async (backlight, GSD_MOCK_DEFAULT_BRIGHTNESS, NULL, NULL, 
NULL);
-
-                goto done;
-        }
-
 #ifdef HAVE_GUDEV
         /* Try finding a udev device. */
         if (gsd_backlight_udev_init (backlight))
diff --git a/plugins/power/meson.build b/plugins/power/meson.build
index caeb8e47..0158a466 100644
--- a/plugins/power/meson.build
+++ b/plugins/power/meson.build
@@ -120,7 +120,8 @@ test_py = find_program('test.py')
 envs = [
 #  'G_DEBUG=fatal_warnings',
   'BUILDDIR=' + meson.current_build_dir(),
-  'TOP_BUILDDIR=' + meson.build_root()
+  'TOP_BUILDDIR=' + meson.build_root(),
+  'LD_PRELOAD=libumockdev-preload.so.0',
 ]
 
 test(
diff --git a/plugins/power/test-backlight-helper b/plugins/power/test-backlight-helper
new file mode 100755
index 00000000..7aba9b76
--- /dev/null
+++ b/plugins/power/test-backlight-helper
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+# Simulate a slow call and just write the given brightness value to the device
+sleep 0.1
+echo "$2" >"$1/brightness"
diff --git a/plugins/power/test.py b/plugins/power/test.py
index 28addb28..f79218cd 100755
--- a/plugins/power/test.py
+++ b/plugins/power/test.py
@@ -27,10 +27,12 @@ import dbus
 
 import gi
 gi.require_version('UPowerGlib', '1.0')
+gi.require_version('UMockdev', '1.0')
 
 from gi.repository import Gio
 from gi.repository import GLib
 from gi.repository import UPowerGlib
+from gi.repository import UMockdev
 
 class PowerPluginTest(gsdtestcase.GSDTestCase):
     '''Test the power plugin'''
@@ -43,6 +45,14 @@ class PowerPluginTest(gsdtestcase.GSDTestCase):
         self.start_logind()
         self.daemon_death_expected = False
 
+
+        # Setup umockdev testbed
+        self.testbed = UMockdev.Testbed.new()
+        os.environ['UMOCKDEV_DIR'] = self.testbed.get_root_dir()
+
+        # Create a mock backlight device
+        self.add_backlight()
+
         # start mock upowerd
         (self.upowerd, self.obj_upower) = self.spawn_server_template(
             'upower', {'DaemonVersion': '0.99', 'OnBattery': True, 'LidIsClosed': False}, 
stdout=subprocess.PIPE)
@@ -96,11 +106,12 @@ class PowerPluginTest(gsdtestcase.GSDTestCase):
         self.plugin_log_write = open(os.path.join(self.workdir, 'plugin_power.log'), 'wb', buffering=0)
         # avoid painfully long delays of actions for tests
         env = os.environ.copy()
-        # Disable the use of the PolicyKit helper for brightness
-        env['GSD_DISABLE_BACKLIGHT_HELPER'] = '1'
         # Disable PulseAudio output from libcanberra
         env['CANBERRA_DRIVER'] = 'null'
 
+        # Use dummy script as testing backlight helper
+        env['GSD_BACKLIGHT_HELPER'] = os.path.join (project_root, 'plugins', 'power', 
'test-backlight-helper')
+
         self.daemon = subprocess.Popen(
             [os.path.join(builddir, 'gsd-power'), '--verbose'],
             # comment out this line if you want to see the logs in real time
@@ -156,10 +167,7 @@ class PowerPluginTest(gsdtestcase.GSDTestCase):
         except OSError:
             pass
 
-        try:
-            os.unlink('GSD_MOCK_brightness')
-        except OSError:
-            pass
+        del self.testbed
 
         # we check this at the end so that the other cleanup always happens
         self.assertTrue(daemon_running or self.daemon_death_expected, 'daemon died during the test')
@@ -198,12 +206,26 @@ class PowerPluginTest(gsdtestcase.GSDTestCase):
     def get_status(self):
         return self.obj_session_presence_props.Get('org.gnome.SessionManager.Presence', 'status')
 
-    def get_brightness(self):
-        try:
-            (success, ret) = GLib.file_get_contents ('GSD_MOCK_brightness')
-        except:
-            return gsdpowerconstants.GSD_MOCK_DEFAULT_BRIGHTNESS
-        return int(ret)
+    def add_backlight(self, _type="raw", brightness=50, max_brightness=100):
+        # Undo mangling done in GSD
+        if max_brightness >= 99:
+            max_brightness += 1
+            brightness += 1
+
+        # This needs to be done before starting gsd-power!
+        self.backlight = self.testbed.add_device('backlight', 'mock_backlight', None,
+                                                 ['type', _type,
+                                                  'max_brightness', str(max_brightness),
+                                                  'brightness', str(brightness)],
+                                                 [])
+
+    def get_brightness(self, max_brightness=100):
+        # self.backlight contains the leading slash, so os.path.join doesn't quite work
+        res = int(open(os.path.join(self.testbed.get_root_dir() + self.backlight, 'brightness')).read())
+        # Undo mangling done in GSD
+        if max_brightness >= 99:
+            res -= 1
+        return res
 
     def set_has_external_monitor(self, external):
         if external:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]