4073ae
From 9645cbffa8ba1a08b73fdae50b31125d11aa5684 Mon Sep 17 00:00:00 2001
4073ae
From: Bastien Nocera <hadess@hadess.net>
4073ae
Date: Mon, 9 Aug 2021 23:19:17 +0200
4073ae
Subject: [PATCH 1/4] gio: Add portal version of GPowerProfileMonitor
4073ae
4073ae
---
4073ae
 docs/reference/gio/meson.build   |   1 +
4073ae
 gio/giomodule.c                  |   2 +
4073ae
 gio/gpowerprofilemonitorportal.c | 182 +++++++++++++++++++++++++++++++
4073ae
 gio/gpowerprofilemonitorportal.h |  31 ++++++
4073ae
 gio/meson.build                  |   1 +
4073ae
 5 files changed, 217 insertions(+)
4073ae
 create mode 100644 gio/gpowerprofilemonitorportal.c
4073ae
 create mode 100644 gio/gpowerprofilemonitorportal.h
4073ae
4073ae
diff --git a/docs/reference/gio/meson.build b/docs/reference/gio/meson.build
4073ae
index fbabd25ca..9aaafeed5 100644
4073ae
--- a/docs/reference/gio/meson.build
4073ae
+++ b/docs/reference/gio/meson.build
4073ae
@@ -66,6 +66,7 @@ if get_option('gtk_doc')
4073ae
     'gpollfilemonitor.h',
4073ae
     'gportalsupport.h',
4073ae
     'gpowerprofilemonitordbus.h',
4073ae
+    'gpowerprofilemonitorportal.h',
4073ae
     'gproxyresolverportal.h',
4073ae
     'gregistrysettingsbackend.h',
4073ae
     'gresourcefile.h',
4073ae
diff --git a/gio/giomodule.c b/gio/giomodule.c
4073ae
index dfd895717..d34037a45 100644
4073ae
--- a/gio/giomodule.c
4073ae
+++ b/gio/giomodule.c
4073ae
@@ -50,6 +50,7 @@
4073ae
 #include "gmemorymonitordbus.h"
4073ae
 #include "gpowerprofilemonitor.h"
4073ae
 #include "gpowerprofilemonitordbus.h"
4073ae
+#include "gpowerprofilemonitorportal.h"
4073ae
 #ifdef G_OS_WIN32
4073ae
 #include "gregistrysettingsbackend.h"
4073ae
 #include "giowin32-priv.h"
4073ae
@@ -1305,6 +1306,7 @@ _g_io_modules_ensure_loaded (void)
4073ae
       g_type_ensure (g_memory_monitor_dbus_get_type ());
4073ae
       g_type_ensure (g_memory_monitor_portal_get_type ());
4073ae
       g_type_ensure (g_network_monitor_portal_get_type ());
4073ae
+      g_type_ensure (g_power_profile_monitor_portal_get_type ());
4073ae
       g_type_ensure (g_proxy_resolver_portal_get_type ());
4073ae
 #endif
4073ae
 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
4073ae
diff --git a/gio/gpowerprofilemonitorportal.c b/gio/gpowerprofilemonitorportal.c
4073ae
new file mode 100644
4073ae
index 000000000..bb1b4fd15
4073ae
--- /dev/null
4073ae
+++ b/gio/gpowerprofilemonitorportal.c
4073ae
@@ -0,0 +1,182 @@
4073ae
+/* GIO - GLib Input, Output and Streaming Library
4073ae
+ *
4073ae
+ * Copyright 2021 Red Hat, Inc.
4073ae
+ *
4073ae
+ * This library is free software; you can redistribute it and/or
4073ae
+ * modify it under the terms of the GNU Lesser General Public
4073ae
+ * License as published by the Free Software Foundation; either
4073ae
+ * version 2.1 of the License, or (at your option) any later version.
4073ae
+ *
4073ae
+ * This library is distributed in the hope that it will be useful,
4073ae
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4073ae
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4073ae
+ * Lesser General Public License for more details.
4073ae
+ *
4073ae
+ * You should have received a copy of the GNU Lesser General
4073ae
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
4073ae
+ */
4073ae
+
4073ae
+#include "config.h"
4073ae
+
4073ae
+#include "gpowerprofilemonitor.h"
4073ae
+#include "gpowerprofilemonitorportal.h"
4073ae
+#include "gdbuserror.h"
4073ae
+#include "gdbusproxy.h"
4073ae
+#include "ginitable.h"
4073ae
+#include "gioerror.h"
4073ae
+#include "giomodule-priv.h"
4073ae
+#include "gportalsupport.h"
4073ae
+
4073ae
+#define G_POWER_PROFILE_MONITOR_PORTAL_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable))
4073ae
+
4073ae
+static void g_power_profile_monitor_portal_iface_init (GPowerProfileMonitorInterface *iface);
4073ae
+static void g_power_profile_monitor_portal_initable_iface_init (GInitableIface *iface);
4073ae
+
4073ae
+typedef enum
4073ae
+{
4073ae
+  PROP_POWER_SAVER_ENABLED = 1,
4073ae
+} GPowerProfileMonitorPortalProperty;
4073ae
+
4073ae
+struct _GPowerProfileMonitorPortal
4073ae
+{
4073ae
+  GObject parent_instance;
4073ae
+
4073ae
+  GDBusProxy *proxy;
4073ae
+  gulong signal_id;
4073ae
+  gboolean power_saver_enabled;
4073ae
+};
4073ae
+
4073ae
+G_DEFINE_TYPE_WITH_CODE (GPowerProfileMonitorPortal, g_power_profile_monitor_portal, G_TYPE_OBJECT,
4073ae
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
4073ae
+                                                g_power_profile_monitor_portal_initable_iface_init)
4073ae
+                         G_IMPLEMENT_INTERFACE (G_TYPE_POWER_PROFILE_MONITOR,
4073ae
+                                                g_power_profile_monitor_portal_iface_init)
4073ae
+                         _g_io_modules_ensure_extension_points_registered ();
4073ae
+                         g_io_extension_point_implement (G_POWER_PROFILE_MONITOR_EXTENSION_POINT_NAME,
4073ae
+                                                         g_define_type_id,
4073ae
+                                                         "portal",
4073ae
+                                                         40))
4073ae
+
4073ae
+static void
4073ae
+g_power_profile_monitor_portal_init (GPowerProfileMonitorPortal *portal)
4073ae
+{
4073ae
+}
4073ae
+
4073ae
+static void
4073ae
+proxy_properties_changed (GDBusProxy *proxy,
4073ae
+                          GVariant   *changed_properties,
4073ae
+                          GStrv       invalidated_properties,
4073ae
+                          gpointer    user_data)
4073ae
+{
4073ae
+  GPowerProfileMonitorPortal *ppm = user_data;
4073ae
+  gboolean power_saver_enabled;
4073ae
+
4073ae
+  if (!g_variant_lookup (changed_properties, "power-saver-enabled", "b", &power_saver_enabled))
4073ae
+    return;
4073ae
+
4073ae
+  if (power_saver_enabled == ppm->power_saver_enabled)
4073ae
+    return;
4073ae
+
4073ae
+  ppm->power_saver_enabled = power_saver_enabled;
4073ae
+  g_object_notify (G_OBJECT (ppm), "power-saver-enabled");
4073ae
+}
4073ae
+
4073ae
+static void
4073ae
+g_power_profile_monitor_portal_get_property (GObject    *object,
4073ae
+                                             guint       prop_id,
4073ae
+                                             GValue     *value,
4073ae
+                                             GParamSpec *pspec)
4073ae
+{
4073ae
+  GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (object);
4073ae
+
4073ae
+  switch ((GPowerProfileMonitorPortalProperty) prop_id)
4073ae
+    {
4073ae
+    case PROP_POWER_SAVER_ENABLED:
4073ae
+      g_value_set_boolean (value, ppm->power_saver_enabled);
4073ae
+      break;
4073ae
+
4073ae
+    default:
4073ae
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4073ae
+    }
4073ae
+}
4073ae
+
4073ae
+static gboolean
4073ae
+g_power_profile_monitor_portal_initable_init (GInitable     *initable,
4073ae
+                                              GCancellable  *cancellable,
4073ae
+                                              GError       **error)
4073ae
+{
4073ae
+  GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (initable);
4073ae
+  GDBusProxy *proxy;
4073ae
+  gchar *name_owner;
4073ae
+
4073ae
+  if (!glib_should_use_portal ())
4073ae
+    {
4073ae
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not using portals");
4073ae
+      return FALSE;
4073ae
+    }
4073ae
+
4073ae
+  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
4073ae
+                                         G_DBUS_PROXY_FLAGS_NONE,
4073ae
+                                         NULL,
4073ae
+                                         "org.freedesktop.portal.Desktop",
4073ae
+                                         "/org/freedesktop/portal/desktop",
4073ae
+                                         "org.freedesktop.portal.PowerProfileMonitor",
4073ae
+                                         cancellable,
4073ae
+                                         error);
4073ae
+  if (!proxy)
4073ae
+    return FALSE;
4073ae
+
4073ae
+  name_owner = g_dbus_proxy_get_name_owner (proxy);
4073ae
+
4073ae
+  if (name_owner == NULL)
4073ae
+    {
4073ae
+      g_object_unref (proxy);
4073ae
+      g_set_error (error,
4073ae
+                   G_DBUS_ERROR,
4073ae
+                   G_DBUS_ERROR_NAME_HAS_NO_OWNER,
4073ae
+                   "Desktop portal not found");
4073ae
+      return FALSE;
4073ae
+    }
4073ae
+
4073ae
+  g_free (name_owner);
4073ae
+
4073ae
+  ppm->signal_id = g_signal_connect (proxy, "g-properties-changed",
4073ae
+                                     G_CALLBACK (proxy_properties_changed), ppm);
4073ae
+
4073ae
+  ppm->proxy = g_steal_pointer (&proxy);
4073ae
+
4073ae
+  return TRUE;
4073ae
+}
4073ae
+
4073ae
+static void
4073ae
+g_power_profile_monitor_portal_finalize (GObject *object)
4073ae
+{
4073ae
+  GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (object);
4073ae
+
4073ae
+  g_clear_signal_handler (&ppm->signal_id, ppm->proxy);
4073ae
+  g_clear_object (&ppm->proxy);
4073ae
+
4073ae
+  G_OBJECT_CLASS (g_power_profile_monitor_portal_parent_class)->finalize (object);
4073ae
+}
4073ae
+
4073ae
+static void
4073ae
+g_power_profile_monitor_portal_class_init (GPowerProfileMonitorPortalClass *nl_class)
4073ae
+{
4073ae
+  GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);
4073ae
+
4073ae
+  gobject_class->get_property = g_power_profile_monitor_portal_get_property;
4073ae
+  gobject_class->finalize  = g_power_profile_monitor_portal_finalize;
4073ae
+
4073ae
+  g_object_class_override_property (gobject_class, PROP_POWER_SAVER_ENABLED, "power-saver-enabled");
4073ae
+}
4073ae
+
4073ae
+static void
4073ae
+g_power_profile_monitor_portal_iface_init (GPowerProfileMonitorInterface *monitor_iface)
4073ae
+{
4073ae
+}
4073ae
+
4073ae
+static void
4073ae
+g_power_profile_monitor_portal_initable_iface_init (GInitableIface *iface)
4073ae
+{
4073ae
+  iface->init = g_power_profile_monitor_portal_initable_init;
4073ae
+}
4073ae
diff --git a/gio/gpowerprofilemonitorportal.h b/gio/gpowerprofilemonitorportal.h
4073ae
new file mode 100644
4073ae
index 000000000..b91a14610
4073ae
--- /dev/null
4073ae
+++ b/gio/gpowerprofilemonitorportal.h
4073ae
@@ -0,0 +1,31 @@
4073ae
+/* GIO - GLib Input, Output and Streaming Library
4073ae
+ *
4073ae
+ * Copyright 2021 Red Hat, Inc.
4073ae
+ *
4073ae
+ * This library is free software; you can redistribute it and/or
4073ae
+ * modify it under the terms of the GNU Lesser General Public
4073ae
+ * License as published by the Free Software Foundation; either
4073ae
+ * version 2.1 of the License, or (at your option) any later version.
4073ae
+ *
4073ae
+ * This library is distributed in the hope that it will be useful,
4073ae
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4073ae
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4073ae
+ * Lesser General Public License for more details.
4073ae
+ *
4073ae
+ * You should have received a copy of the GNU Lesser General
4073ae
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
4073ae
+ */
4073ae
+
4073ae
+#ifndef __G_POWER_PROFILE_MONITOR_PORTAL_H__
4073ae
+#define __G_POWER_PROFILE_MONITOR_PORTAL_H__
4073ae
+
4073ae
+#include <glib-object.h>
4073ae
+
4073ae
+G_BEGIN_DECLS
4073ae
+
4073ae
+#define G_TYPE_POWER_PROFILE_MONITOR_PORTAL         (g_power_profile_monitor_portal_get_type ())
4073ae
+G_DECLARE_FINAL_TYPE (GPowerProfileMonitorPortal, g_power_profile_monitor_portal, G, POWER_PROFILE_MONITOR_PORTAL, GObject)
4073ae
+
4073ae
+G_END_DECLS
4073ae
+
4073ae
+#endif /* __G_POWER_PROFILE_MONITOR_PORTAL_H__ */
4073ae
diff --git a/gio/meson.build b/gio/meson.build
4073ae
index d5838ed8a..ac3373f2b 100644
4073ae
--- a/gio/meson.build
4073ae
+++ b/gio/meson.build
4073ae
@@ -383,6 +383,7 @@ if host_system != 'windows'
4073ae
     'gopenuriportal.c',
4073ae
     'gmemorymonitorportal.c',
4073ae
     'gnetworkmonitorportal.c',
4073ae
+    'gpowerprofilemonitorportal.c',
4073ae
     'gproxyresolverportal.c',
4073ae
     'gtrashportal.c',
4073ae
     'gportalsupport.c',
4073ae
-- 
4073ae
GitLab
4073ae
4073ae
4073ae
From 18eb29897d80bf662d58bd11a89617ddd7ebfeed Mon Sep 17 00:00:00 2001
4073ae
From: Bastien Nocera <hadess@hadess.net>
4073ae
Date: Tue, 10 Aug 2021 10:58:53 +0200
4073ae
Subject: [PATCH 2/4] gio: Add GPowerProfileMonitor tests
4073ae
4073ae
Tests both the portal and direct D-Bus variants.
4073ae
---
4073ae
 gio/tests/meson.build                        |  14 ++-
4073ae
 gio/tests/power-profile-monitor-dbus.py.in   | 107 ++++++++++++++++
4073ae
 gio/tests/power-profile-monitor-portal.py.in | 126 +++++++++++++++++++
4073ae
 3 files changed, 241 insertions(+), 6 deletions(-)
4073ae
 create mode 100755 gio/tests/power-profile-monitor-dbus.py.in
4073ae
 create mode 100755 gio/tests/power-profile-monitor-portal.py.in
4073ae
4073ae
diff --git a/gio/tests/meson.build b/gio/tests/meson.build
4073ae
index fc2055101..5dbfb8e60 100644
4073ae
--- a/gio/tests/meson.build
4073ae
+++ b/gio/tests/meson.build
4073ae
@@ -541,27 +541,29 @@ if installed_tests_enabled
4073ae
   install_subdir('static-link', install_dir : installed_tests_execdir)
4073ae
   install_data('static-link.py', install_dir : installed_tests_execdir)
4073ae
 
4073ae
-  memory_monitor_tests = [
4073ae
+  monitor_tests = [
4073ae
     'memory-monitor-dbus',
4073ae
     'memory-monitor-portal',
4073ae
+    'power-profile-monitor-dbus',
4073ae
+    'power-profile-monitor-portal'
4073ae
   ]
4073ae
 
4073ae
-  foreach memory_monitor_test : memory_monitor_tests
4073ae
+  foreach monitor_test : monitor_tests
4073ae
     cdata = configuration_data()
4073ae
     cdata.set('installed_tests_dir', installed_tests_execdir)
4073ae
-    cdata.set('program', memory_monitor_test + '.py')
4073ae
+    cdata.set('program', monitor_test + '.py')
4073ae
     cdata.set('env', '')
4073ae
     configure_file(
4073ae
       input: installed_tests_template_tap,
4073ae
-      output: memory_monitor_test + '.test',
4073ae
+      output: monitor_test + '.test',
4073ae
       install_dir: installed_tests_metadir,
4073ae
       configuration: cdata
4073ae
     )
4073ae
     cdata = configuration_data()
4073ae
     cdata.set('libexecdir', join_paths(glib_prefix, get_option('libexecdir')))
4073ae
     configure_file(
4073ae
-      input: memory_monitor_test + '.py.in',
4073ae
-      output: memory_monitor_test + '.py',
4073ae
+      input: monitor_test + '.py.in',
4073ae
+      output: monitor_test + '.py',
4073ae
       install_dir : installed_tests_execdir,
4073ae
       configuration: cdata,
4073ae
     )
4073ae
diff --git a/gio/tests/power-profile-monitor-dbus.py.in b/gio/tests/power-profile-monitor-dbus.py.in
4073ae
new file mode 100755
4073ae
index 000000000..06e594f4a
4073ae
--- /dev/null
4073ae
+++ b/gio/tests/power-profile-monitor-dbus.py.in
4073ae
@@ -0,0 +1,107 @@
4073ae
+#!/usr/bin/python3
4073ae
+
4073ae
+# This program is free software; you can redistribute it and/or modify it under
4073ae
+# the terms of the GNU Lesser General Public License as published by the Free
4073ae
+# Software Foundation; either version 3 of the License, or (at your option) any
4073ae
+# later version.  See http://www.gnu.org/copyleft/lgpl.html for the full text
4073ae
+# of the license.
4073ae
+
4073ae
+__author__ = 'Bastien Nocera'
4073ae
+__email__ = 'hadess@hadess.net'
4073ae
+__copyright__ = '(c) 2019, 2021 Red Hat Inc.'
4073ae
+__license__ = 'LGPL 3+'
4073ae
+
4073ae
+import unittest
4073ae
+import sys
4073ae
+import subprocess
4073ae
+import fcntl
4073ae
+import os
4073ae
+import time
4073ae
+
4073ae
+import taptestrunner
4073ae
+
4073ae
+try:
4073ae
+    # Do all non-standard imports here so we can skip the tests if any
4073ae
+    # needed packages are not available.
4073ae
+    import dbus
4073ae
+    import dbus.mainloop.glib
4073ae
+    import dbusmock
4073ae
+    from gi.repository import GLib
4073ae
+    from gi.repository import Gio
4073ae
+
4073ae
+    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
4073ae
+
4073ae
+    class TestPowerProfileMonitor(dbusmock.DBusTestCase):
4073ae
+        '''Test GPowerProfileMonitorDBus'''
4073ae
+
4073ae
+        @classmethod
4073ae
+        def setUpClass(klass):
4073ae
+            klass.start_system_bus()
4073ae
+            klass.dbus_con = klass.get_dbus(True)
4073ae
+
4073ae
+        def setUp(self):
4073ae
+            try:
4073ae
+                Gio.PowerProfileMonitor
4073ae
+            except AttributeError:
4073ae
+                raise unittest.SkipTest('Power Profile Monitor not in '
4073ae
+                                        'introspection data. Requires '
4073ae
+                                        'GObject-Introspection ≥ 1.63.2') # FIXME version
4073ae
+            try:
4073ae
+                (self.p_mock, self.obj_ppd) = self.spawn_server_template(
4073ae
+                    'power_profiles_daemon', {}, stdout=subprocess.PIPE)
4073ae
+            except ModuleNotFoundError:
4073ae
+                raise unittest.SkipTest("power-profiles-daemon dbusmock template not "
4073ae
+                                        "found. Requires dbusmock > 0.23.1.") # FIXME version
4073ae
+            # set log to nonblocking
4073ae
+            flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL)
4073ae
+            fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
4073ae
+            self.power_saver_enabled = False
4073ae
+            self.dbus_props = dbus.Interface(self.obj_ppd, dbus.PROPERTIES_IFACE)
4073ae
+            self.power_profile_monitor = Gio.PowerProfileMonitor.dup_default()
4073ae
+            self.power_profile_monitor.connect("notify::power-saver-enabled", self.power_saver_enabled_cb)
4073ae
+            self.mainloop = GLib.MainLoop()
4073ae
+            self.main_context = self.mainloop.get_context()
4073ae
+
4073ae
+        def tearDown(self):
4073ae
+            self.p_mock.terminate()
4073ae
+            self.p_mock.wait()
4073ae
+
4073ae
+        def assertEventually(self, condition, message=None, timeout=50):
4073ae
+            '''Assert that condition function eventually returns True.
4073ae
+
4073ae
+            Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
4073ae
+            printed on failure.
4073ae
+            '''
4073ae
+            while timeout >= 0:
4073ae
+                context = GLib.MainContext.default()
4073ae
+                while context.iteration(False):
4073ae
+                    pass
4073ae
+                if condition():
4073ae
+                    break
4073ae
+                timeout -= 1
4073ae
+                time.sleep(0.1)
4073ae
+            else:
4073ae
+                self.fail(message or 'timed out waiting for ' + str(condition))
4073ae
+
4073ae
+        def power_saver_enabled_cb(self, spec, data):
4073ae
+            self.power_saver_enabled = self.power_profile_monitor.get_power_saver_enabled()
4073ae
+            self.main_context.wakeup()
4073ae
+
4073ae
+        def test_power_profile_power_saver_enabled(self):
4073ae
+            '''power-saver-enabled property'''
4073ae
+
4073ae
+            self.assertEqual(self.power_profile_monitor.get_power_saver_enabled(), False)
4073ae
+            self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('power-saver', variant_level=1))
4073ae
+            self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 10)
4073ae
+
4073ae
+            self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('balanced', variant_level=1))
4073ae
+            self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 10)
4073ae
+
4073ae
+except ImportError as e:
4073ae
+    @unittest.skip("Cannot import %s" % e.name)
4073ae
+    class TestPowerProfileMonitor(unittest.TestCase):
4073ae
+        def test_power_profile_power_saver_enabled(self):
4073ae
+            pass
4073ae
+
4073ae
+if __name__ == '__main__':
4073ae
+    unittest.main(testRunner=taptestrunner.TAPTestRunner())
4073ae
diff --git a/gio/tests/power-profile-monitor-portal.py.in b/gio/tests/power-profile-monitor-portal.py.in
4073ae
new file mode 100755
4073ae
index 000000000..960a62232
4073ae
--- /dev/null
4073ae
+++ b/gio/tests/power-profile-monitor-portal.py.in
4073ae
@@ -0,0 +1,126 @@
4073ae
+#!/usr/bin/python3
4073ae
+
4073ae
+# This program is free software; you can redistribute it and/or modify it under
4073ae
+# the terms of the GNU Lesser General Public License as published by the Free
4073ae
+# Software Foundation; either version 3 of the License, or (at your option) any
4073ae
+# later version.  See http://www.gnu.org/copyleft/lgpl.html for the full text
4073ae
+# of the license.
4073ae
+
4073ae
+__author__ = 'Bastien Nocera'
4073ae
+__email__ = 'hadess@hadess.net'
4073ae
+__copyright__ = '(c) 2021 Red Hat Inc.'
4073ae
+__license__ = 'LGPL 3+'
4073ae
+
4073ae
+import unittest
4073ae
+import sys
4073ae
+import subprocess
4073ae
+import fcntl
4073ae
+import os
4073ae
+import time
4073ae
+
4073ae
+import taptestrunner
4073ae
+
4073ae
+try:
4073ae
+    # Do all non-standard imports here so we can skip the tests if any
4073ae
+    # needed packages are not available.
4073ae
+    import dbus
4073ae
+    import dbus.mainloop.glib
4073ae
+    import dbusmock
4073ae
+    from gi.repository import GLib
4073ae
+    from gi.repository import Gio
4073ae
+
4073ae
+    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
4073ae
+
4073ae
+    # XDG_DESKTOP_PORTAL_PATH = os.path.expanduser("~/.cache/jhbuild/build/xdg-desktop-portal/xdg-desktop-portal")
4073ae
+    XDG_DESKTOP_PORTAL_PATH = "@libexecdir@/xdg-desktop-portal"
4073ae
+
4073ae
+    class TestPowerProfileMonitorPortal(dbusmock.DBusTestCase):
4073ae
+        '''Test GPowerProfileMonitorPortal'''
4073ae
+
4073ae
+        @classmethod
4073ae
+        def setUpClass(klass):
4073ae
+            klass.start_system_bus()
4073ae
+            klass.dbus_con = klass.get_dbus(True)
4073ae
+            # Start session bus so that xdg-desktop-portal can run on it
4073ae
+            klass.start_session_bus()
4073ae
+
4073ae
+        def setUp(self):
4073ae
+            try:
4073ae
+                Gio.PowerProfileMonitor
4073ae
+            except AttributeError:
4073ae
+                raise unittest.SkipTest('Power Profile Monitor not in '
4073ae
+                                        'introspection data. Requires '
4073ae
+                                        'GObject-Introspection > 1.69.0')
4073ae
+            try:
4073ae
+                (self.p_mock, self.obj_ppd) = self.spawn_server_template(
4073ae
+                    'power_profiles_daemon', {}, stdout=subprocess.PIPE)
4073ae
+            except ModuleNotFoundError:
4073ae
+                raise unittest.SkipTest("power-profiles-daemon dbusmock template not "
4073ae
+                                        "found. Requires dbusmock > 0.23.1.")
4073ae
+            # set log to nonblocking
4073ae
+            flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL)
4073ae
+            fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
4073ae
+            self.power_saver_enabled = False
4073ae
+            self.dbus_props = dbus.Interface(self.obj_ppd, dbus.PROPERTIES_IFACE)
4073ae
+            try:
4073ae
+                self.xdp = subprocess.Popen([XDG_DESKTOP_PORTAL_PATH])
4073ae
+            except FileNotFoundError:
4073ae
+                raise unittest.SkipTest("xdg-desktop-portal not available")
4073ae
+
4073ae
+            try:
4073ae
+                self.wait_for_bus_object('org.freedesktop.portal.Desktop',
4073ae
+                                        '/org/freedesktop/portal/desktop')
4073ae
+            except:
4073ae
+                raise
4073ae
+            # subprocess.Popen(['gdbus', 'monitor', '--session', '--dest', 'org.freedesktop.portal.Desktop'])
4073ae
+
4073ae
+            os.environ['GTK_USE_PORTAL'] = "1"
4073ae
+            self.power_profile_monitor = Gio.PowerProfileMonitor.dup_default()
4073ae
+            assert("GPowerProfileMonitorPortal" in str(self.power_profile_monitor))
4073ae
+            self.power_profile_monitor.connect("notify::power-saver-enabled", self.power_saver_enabled_cb)
4073ae
+            self.mainloop = GLib.MainLoop()
4073ae
+            self.main_context = self.mainloop.get_context()
4073ae
+
4073ae
+        def tearDown(self):
4073ae
+            self.p_mock.terminate()
4073ae
+            self.p_mock.wait()
4073ae
+
4073ae
+        def assertEventually(self, condition, message=None, timeout=50):
4073ae
+            '''Assert that condition function eventually returns True.
4073ae
+
4073ae
+            Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
4073ae
+            printed on failure.
4073ae
+            '''
4073ae
+            while timeout >= 0:
4073ae
+                context = GLib.MainContext.default()
4073ae
+                while context.iteration(False):
4073ae
+                    pass
4073ae
+                if condition():
4073ae
+                    break
4073ae
+                timeout -= 1
4073ae
+                time.sleep(0.1)
4073ae
+            else:
4073ae
+                self.fail(message or 'timed out waiting for ' + str(condition))
4073ae
+
4073ae
+        def power_saver_enabled_cb(self, spec, data):
4073ae
+            self.power_saver_enabled = self.power_profile_monitor.get_power_saver_enabled()
4073ae
+            self.main_context.wakeup()
4073ae
+
4073ae
+        def test_power_profile_power_saver_enabled_portal(self):
4073ae
+            '''power-saver-enabled property'''
4073ae
+
4073ae
+            self.assertEqual(self.power_profile_monitor.get_power_saver_enabled(), False)
4073ae
+            self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('power-saver', variant_level=1))
4073ae
+            self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 10)
4073ae
+
4073ae
+            self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('balanced', variant_level=1))
4073ae
+            self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 10)
4073ae
+
4073ae
+except ImportError as e:
4073ae
+    @unittest.skip("Cannot import %s" % e.name)
4073ae
+    class TestPowerProfileMonitorPortal(unittest.TestCase):
4073ae
+        def test_power_profile_power_saver_enabled_portal(self):
4073ae
+            pass
4073ae
+
4073ae
+if __name__ == '__main__':
4073ae
+    unittest.main(testRunner=taptestrunner.TAPTestRunner())
4073ae
-- 
4073ae
GitLab
4073ae
4073ae
4073ae
From 66acea8418eb3d8e46bb6f93dc0c3f13a1f7822b Mon Sep 17 00:00:00 2001
4073ae
From: Bastien Nocera <hadess@hadess.net>
4073ae
Date: Wed, 11 Aug 2021 15:37:40 +0200
4073ae
Subject: [PATCH 3/4] gio: Remove left-over debug statement from memory monitor
4073ae
 portal test
4073ae
4073ae
---
4073ae
 gio/tests/memory-monitor-portal.py.in | 1 -
4073ae
 1 file changed, 1 deletion(-)
4073ae
4073ae
diff --git a/gio/tests/memory-monitor-portal.py.in b/gio/tests/memory-monitor-portal.py.in
4073ae
index cb4a960eb..f5fd2283f 100755
4073ae
--- a/gio/tests/memory-monitor-portal.py.in
4073ae
+++ b/gio/tests/memory-monitor-portal.py.in
4073ae
@@ -31,7 +31,6 @@ try:
4073ae
 
4073ae
     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
4073ae
 
4073ae
-    # XDG_DESKTOP_PORTAL_PATH = os.path.expanduser("~/.cache/jhbuild/build/xdg-desktop-portal/xdg-desktop-portal")
4073ae
     XDG_DESKTOP_PORTAL_PATH = "@libexecdir@/xdg-desktop-portal"
4073ae
 
4073ae
     class TestLowMemoryMonitorPortal(dbusmock.DBusTestCase):
4073ae
-- 
4073ae
GitLab
4073ae
4073ae
4073ae
From 2e9842cafc73a7fb94cfde7937e125e1a91f35f8 Mon Sep 17 00:00:00 2001
4073ae
From: Bastien Nocera <hadess@hadess.net>
4073ae
Date: Wed, 11 Aug 2021 15:38:12 +0200
4073ae
Subject: [PATCH 4/4] gio: Simplify memory monitor tests by using
4073ae
 assertEventually() helper
4073ae
4073ae
assertEventually is a helper used in a number of projects that use
4073ae
dbusmock.
4073ae
4073ae
See https://github.com/martinpitt/python-dbusmock/issues/82
4073ae
---
4073ae
 gio/tests/memory-monitor-dbus.py.in   | 31 ++++++++++++++++-----------
4073ae
 gio/tests/memory-monitor-portal.py.in | 31 ++++++++++++++++-----------
4073ae
 2 files changed, 38 insertions(+), 24 deletions(-)
4073ae
4073ae
diff --git a/gio/tests/memory-monitor-dbus.py.in b/gio/tests/memory-monitor-dbus.py.in
4073ae
index 7823e7309..e8ac28faf 100755
4073ae
--- a/gio/tests/memory-monitor-dbus.py.in
4073ae
+++ b/gio/tests/memory-monitor-dbus.py.in
4073ae
@@ -66,6 +66,23 @@ try:
4073ae
             self.p_mock.terminate()
4073ae
             self.p_mock.wait()
4073ae
 
4073ae
+        def assertEventually(self, condition, message=None, timeout=50):
4073ae
+            '''Assert that condition function eventually returns True.
4073ae
+
4073ae
+            Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
4073ae
+            printed on failure.
4073ae
+            '''
4073ae
+            while timeout >= 0:
4073ae
+                context = GLib.MainContext.default()
4073ae
+                while context.iteration(False):
4073ae
+                    pass
4073ae
+                if condition():
4073ae
+                    break
4073ae
+                timeout -= 1
4073ae
+                time.sleep(0.1)
4073ae
+            else:
4073ae
+                self.fail(message or 'timed out waiting for ' + str(condition))
4073ae
+
4073ae
         def memory_warning_cb(self, monitor, level):
4073ae
             self.last_warning = level
4073ae
             self.main_context.wakeup()
4073ae
@@ -82,21 +99,11 @@ try:
4073ae
 
4073ae
             self.dbusmock.EmitWarning(100)
4073ae
             # Wait 2 seconds or until warning
4073ae
-            timeout = 2
4073ae
-            while timeout > 0 and self.last_warning != 100:
4073ae
-                time.sleep(0.5)
4073ae
-                timeout -= 0.5
4073ae
-                self.main_context.iteration(False)
4073ae
-            self.assertEqual(self.last_warning, 100)
4073ae
+            self.assertEventually(self.last_warning == 100, "'100' low-memory warning not received", 20)
4073ae
 
4073ae
             self.dbusmock.EmitWarning(255)
4073ae
             # Wait 2 seconds or until warning
4073ae
-            timeout = 2
4073ae
-            while timeout > 0 and self.last_warning != 255:
4073ae
-                time.sleep(0.5)
4073ae
-                timeout -= 0.5
4073ae
-                self.main_context.iteration(False)
4073ae
-            self.assertEqual(self.last_warning, 255)
4073ae
+            self.assertEventually(self.last_warning == 255, "'255' low-memory warning not received", 20)
4073ae
 
4073ae
 except ImportError as e:
4073ae
     @unittest.skip("Cannot import %s" % e.name)
4073ae
diff --git a/gio/tests/memory-monitor-portal.py.in b/gio/tests/memory-monitor-portal.py.in
4073ae
index f5fd2283f..36d5094d3 100755
4073ae
--- a/gio/tests/memory-monitor-portal.py.in
4073ae
+++ b/gio/tests/memory-monitor-portal.py.in
4073ae
@@ -84,6 +84,23 @@ try:
4073ae
             self.p_mock.terminate()
4073ae
             self.p_mock.wait()
4073ae
 
4073ae
+        def assertEventually(self, condition, message=None, timeout=50):
4073ae
+            '''Assert that condition function eventually returns True.
4073ae
+
4073ae
+            Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
4073ae
+            printed on failure.
4073ae
+            '''
4073ae
+            while timeout >= 0:
4073ae
+                context = GLib.MainContext.default()
4073ae
+                while context.iteration(False):
4073ae
+                    pass
4073ae
+                if condition():
4073ae
+                    break
4073ae
+                timeout -= 1
4073ae
+                time.sleep(0.1)
4073ae
+            else:
4073ae
+                self.fail(message or 'timed out waiting for ' + str(condition))
4073ae
+
4073ae
         def portal_memory_warning_cb(self, monitor, level):
4073ae
             self.last_warning = level
4073ae
             self.main_context.wakeup()
4073ae
@@ -100,21 +117,11 @@ try:
4073ae
 
4073ae
             self.dbusmock.EmitWarning(100)
4073ae
             # Wait 2 seconds or until warning
4073ae
-            timeout = 2
4073ae
-            while timeout > 0 and self.last_warning != 100:
4073ae
-                time.sleep(0.5)
4073ae
-                timeout -= 0.5
4073ae
-                self.main_context.iteration(False)
4073ae
-            self.assertEqual(self.last_warning, 100)
4073ae
+            self.assertEventually(self.last_warning == 100, "'100' low-memory warning not received", 20)
4073ae
 
4073ae
             self.dbusmock.EmitWarning(255)
4073ae
             # Wait 2 seconds or until warning
4073ae
-            timeout = 2
4073ae
-            while timeout > 0 and self.last_warning != 255:
4073ae
-                time.sleep(0.5)
4073ae
-                timeout -= 0.5
4073ae
-                self.main_context.iteration(False)
4073ae
-            self.assertEqual(self.last_warning, 255)
4073ae
+            self.assertEventually(self.last_warning == 255, "'255' low-memory warning not received", 20)
4073ae
 
4073ae
 except ImportError as e:
4073ae
     @unittest.skip("Cannot import %s" % e.name)
4073ae
-- 
4073ae
GitLab
4073ae