[gnome-settings-daemon] power: Make power plugin "mock" support a run-time check
- From: Bastien Nocera <hadess src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-settings-daemon] power: Make power plugin "mock" support a run-time check
- Date: Tue, 4 Oct 2016 09:50:25 +0000 (UTC)
commit 5fc7cb646bcae91d80d0aa90ab5fdd0fc8f513a6
Author: Bastien Nocera <hadess hadess net>
Date: Thu Sep 29 14:08:22 2016 +0200
power: Make power plugin "mock" support a run-time check
This adds runtime mock support, replacing the compile-time mock
support we used before, which will allow re-using a stand-alone program
rather than an especially built binary.
https://bugzilla.gnome.org/show_bug.cgi?id=741688
plugins/power/Makefile.am | 2 +-
plugins/power/gpm-common.c | 66 ++++++++++++++++++++++++++++---------------
plugins/power/test.py | 1 +
3 files changed, 45 insertions(+), 24 deletions(-)
---
diff --git a/plugins/power/Makefile.am b/plugins/power/Makefile.am
index 149a4ec..cd8e6ba 100644
--- a/plugins/power/Makefile.am
+++ b/plugins/power/Makefile.am
@@ -78,7 +78,7 @@ gsd_test_power_SOURCES = \
test-power.c
gsd_test_power_CFLAGS = $(libpower_la_CFLAGS)
-gsd_test_power_CPPFLAGS = $(libpower_la_CPPFLAGS) -DGSD_MOCK=1
+gsd_test_power_CPPFLAGS = $(libpower_la_CPPFLAGS)
gsd_test_power_LDADD = \
-lm \
$(top_builddir)/plugins/common/libcommon.la \
diff --git a/plugins/power/gpm-common.c b/plugins/power/gpm-common.c
index ed962e2..7ccf0be 100644
--- a/plugins/power/gpm-common.c
+++ b/plugins/power/gpm-common.c
@@ -326,7 +326,24 @@ out:
return output;
}
-#ifdef GSD_MOCK
+static gpointer
+parse_mocked (gpointer data)
+{
+ const char *mocked;
+ mocked = g_getenv ("GSD_MOCKED");
+ if (!mocked)
+ return GINT_TO_POINTER (FALSE);
+ return GINT_TO_POINTER (TRUE);
+}
+
+static gboolean
+is_mocked (void)
+{
+ static GOnce mocked_once = G_ONCE_INIT;
+ g_once (&mocked_once, parse_mocked, NULL);
+ return GPOINTER_TO_INT (mocked_once.retval);
+}
+
static void
backlight_set_mock_value (gint value)
{
@@ -369,16 +386,14 @@ backlight_get_mock_value (enum BacklightHelperCommand command)
return ret;
}
-#endif /* GSD_MOCK */
gboolean
backlight_available (GnomeRRScreen *rr_screen)
{
char *path;
-#ifdef GSD_MOCK
- return TRUE;
-#endif
+ if (is_mocked ())
+ return TRUE;
#ifndef __linux__
return (get_primary_output (rr_screen) != NULL);
@@ -452,9 +467,8 @@ backlight_helper_get_value (enum BacklightHelperCommand command, GError **error)
gint64 value = -1;
gchar *endptr = NULL;
-#ifdef GSD_MOCK
- return backlight_get_mock_value (command);
-#endif
+ if (is_mocked ())
+ return backlight_get_mock_value (command);
#ifndef __linux__
/* non-Linux platforms won't have /sys/class/backlight */
@@ -535,10 +549,10 @@ backlight_helper_set_value (gint value,
gint exit_status = 0;
gchar *vstr = NULL;
-#ifdef GSD_MOCK
- backlight_set_mock_value (value);
- return TRUE;
-#endif
+ if (is_mocked ()) {
+ backlight_set_mock_value (value);
+ return TRUE;
+ }
#ifndef __linux__
/* non-Linux platforms won't have /sys/class/backlight */
@@ -825,7 +839,6 @@ randr_output_is_on (GnomeRROutput *output)
return gnome_rr_crtc_get_current_mode (crtc) != NULL;
}
-#ifdef GSD_MOCK
static void
mock_monitor_changed (GFileMonitor *monitor,
GFile *file,
@@ -845,31 +858,27 @@ screen_destroyed (gpointer user_data,
{
g_object_unref (G_OBJECT (user_data));
}
-#endif /* GSD_MOCK */
void
watch_external_monitor (GnomeRRScreen *screen)
{
-#ifdef GSD_MOCK
GFile *file;
GFileMonitor *monitor;
+ if (!is_mocked ())
+ return;
+
file = g_file_new_for_commandline_arg ("GSD_MOCK_EXTERNAL_MONITOR");
monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, NULL);
g_object_unref (file);
g_signal_connect (monitor, "changed",
G_CALLBACK (mock_monitor_changed), screen);
g_object_weak_ref (G_OBJECT (screen), screen_destroyed, monitor);
-#endif /* GSD_MOCK */
}
-gboolean
-external_monitor_is_connected (GnomeRRScreen *screen)
+static gboolean
+mock_external_monitor_is_connected (GnomeRRScreen *screen)
{
- GnomeRROutput **outputs;
- guint i;
-
-#ifdef GSD_MOCK
char *mock_external_monitor_contents;
if (g_file_get_contents ("GSD_MOCK_EXTERNAL_MONITOR", &mock_external_monitor_contents, NULL, NULL)) {
@@ -886,7 +895,18 @@ external_monitor_is_connected (GnomeRRScreen *screen)
g_error ("Unhandled value for GSD_MOCK_EXTERNAL_MONITOR contents: %s",
mock_external_monitor_contents);
g_free (mock_external_monitor_contents);
}
-#endif /* GSD_MOCK */
+
+ return FALSE;
+}
+
+gboolean
+external_monitor_is_connected (GnomeRRScreen *screen)
+{
+ GnomeRROutput **outputs;
+ guint i;
+
+ if (is_mocked ())
+ return mock_external_monitor_is_connected (screen);
g_assert (screen != NULL);
diff --git a/plugins/power/test.py b/plugins/power/test.py
index 0ff4b1f..082ad80 100755
--- a/plugins/power/test.py
+++ b/plugins/power/test.py
@@ -31,6 +31,7 @@ class PowerPluginTest(gsdtestcase.GSDTestCase):
'''Test the power plugin'''
def setUp(self):
+ os.environ.set('GSD_MOCK', '1')
self.check_logind_gnome_session()
self.start_logind()
self.daemon_death_expected = False
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]