diff options
-rw-r--r-- | data/com.canonical.indicator.power.gschema.xml.in | 24 | ||||
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/device-provider-mock.c (renamed from tests/device-provider-mock.c) | 2 | ||||
-rw-r--r-- | src/device-provider-mock.h (renamed from tests/device-provider-mock.h) | 0 | ||||
-rw-r--r-- | src/main.c | 70 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 9 | ||||
-rw-r--r-- | tests/indicator-power-service-cmdline-battery.cc | 127 |
7 files changed, 91 insertions, 142 deletions
diff --git a/data/com.canonical.indicator.power.gschema.xml.in b/data/com.canonical.indicator.power.gschema.xml.in index 4fd2620..e9bfcf5 100644 --- a/data/com.canonical.indicator.power.gschema.xml.in +++ b/data/com.canonical.indicator.power.gschema.xml.in @@ -20,5 +20,29 @@ <_summary>When to show the battery status in the menu bar.</_summary> <_description>Options for when to show battery status. Valid options are "present", "charge", and "never".</_description> </key> + + <key name="mock-battery-enabled" type="b"> + <default>false</default> + <_summary>Whether to use a mock battery for testing.</_summary> + <_description>When enabled, a single mock battery will be used instead of monitoring upower.</_description> + </key> + <key name="mock-battery-level" type="i"> + <range min="0" max="100"/> + <default>50</default> + <_summary>The charge level of the mock battery.</_summary> + <_description>The charge level of the mock battery.</_description> + </key> + <key name="mock-battery-charging" type="b"> + <default>false</default> + <_summary>Charge state of the mock battery</_summary> + <_description>Whether the mock battery is charging or discharging</_description> + </key> + <key name="mock-battery-minutes-left" type="i"> + <range min="0" max="420"/> + <default>30</default> + <_summary>Time remaining on the battery</_summary> + <_description>Minutes left until the battery finishes charging/discharging</_description> + </key> + </schema> </schemalist> diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f7efb80..00b0ee2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,7 @@ add_definitions(-DG_LOG_DOMAIN="Indicator-Power") # handwritten sources set(SERVICE_MANUAL_SOURCES brightness.c + device-provider-mock.c device-provider-upower.c device-provider.c device.c diff --git a/tests/device-provider-mock.c b/src/device-provider-mock.c index afca178..ccb80f0 100644 --- a/tests/device-provider-mock.c +++ b/src/device-provider-mock.c @@ -81,7 +81,7 @@ indicator_power_device_provider_interface_init (IndicatorPowerDeviceProviderInte } static void -indicator_power_device_provider_mock_init (IndicatorPowerDeviceProviderMock * self) +indicator_power_device_provider_mock_init (IndicatorPowerDeviceProviderMock * self G_GNUC_UNUSED) { } diff --git a/tests/device-provider-mock.h b/src/device-provider-mock.h index 4d06924..4d06924 100644 --- a/tests/device-provider-mock.h +++ b/src/device-provider-mock.h @@ -24,10 +24,63 @@ #include <gio/gio.h> #include "device.h" +#include "device-provider-mock.h" #include "device-provider-upower.h" #include "service.h" /*** +**** MOCK +***/ + +static IndicatorPowerDevice* +get_mock_battery(GSettings * settings) +{ + const double percent = g_settings_get_int(settings, "mock-battery-level"); + + const UpDeviceState state = g_settings_get_boolean(settings, "mock-battery-charging") + ? UP_DEVICE_STATE_CHARGING + : UP_DEVICE_STATE_DISCHARGING; + + const int seconds_left = g_settings_get_int(settings, "mock-battery-minutes-left") * 60; + + return indicator_power_device_new ("/some/path", + UP_DEVICE_KIND_BATTERY, + percent, + state, + seconds_left); +} + +static IndicatorPowerDeviceProvider* +get_device_provider(GSettings * settings) +{ + IndicatorPowerDeviceProvider * provider = NULL; + + if (g_settings_get_boolean(settings, "mock-battery-enabled")) + { + IndicatorPowerDevice * battery = get_mock_battery(settings); + provider = indicator_power_device_provider_mock_new (); + indicator_power_device_provider_add_device (INDICATOR_POWER_DEVICE_PROVIDER_MOCK(provider), battery); + g_object_unref (battery); + } + else + { + provider = indicator_power_device_provider_upower_new (); + } + + return provider; +} + +static void +on_mock_settings_changed(GSettings* settings, + gchar * key G_GNUC_UNUSED, + gpointer service) +{ + IndicatorPowerDeviceProvider* provider = get_device_provider(settings); + indicator_power_service_set_device_provider (INDICATOR_POWER_SERVICE(service), provider); + g_object_unref(provider); +} + +/*** **** ***/ @@ -41,18 +94,25 @@ on_name_lost (gpointer instance G_GNUC_UNUSED, gpointer loop) int main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) { - IndicatorPowerDeviceProvider * device_provider; - IndicatorPowerService * service; + GSettings * settings; GMainLoop * loop; + IndicatorPowerService * service; /* boilerplate i18n */ setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); textdomain (GETTEXT_PACKAGE); + /* mock settings */ + service = indicator_power_service_new (NULL); + settings = g_settings_new ("com.canonical.indicator.power"); + g_signal_connect(settings, "changed::mock-battery-enabled", G_CALLBACK(on_mock_settings_changed), service); + g_signal_connect(settings, "changed::mock-battery-level", G_CALLBACK(on_mock_settings_changed), service); + g_signal_connect(settings, "changed::mock-battery-charging", G_CALLBACK(on_mock_settings_changed), service); + g_signal_connect(settings, "changed::mock-battery-minutes-left", G_CALLBACK(on_mock_settings_changed), service); + on_mock_settings_changed(settings, NULL, service); + /* run */ - device_provider = indicator_power_device_provider_upower_new (); - service = indicator_power_service_new (device_provider); loop = g_main_loop_new (NULL, FALSE); g_signal_connect (service, INDICATOR_POWER_SERVICE_SIGNAL_NAME_LOST, G_CALLBACK(on_name_lost), loop); @@ -61,6 +121,6 @@ main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) /* cleanup */ g_main_loop_unref (loop); g_clear_object (&service); - g_clear_object (&device_provider); + g_clear_object (&settings); return 0; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a0d24af..02ecb1b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -50,12 +50,3 @@ endfunction() add_test_by_name(test-notify) add_test(NAME dear-reader-the-next-test-takes-80-seconds COMMAND true) add_test_by_name(test-device) - -### -### - -set (APP_NAME indicator-power-service-cmdline-battery) -add_executable (${APP_NAME} ${APP_NAME}.cc device-provider-mock.c) -add_dependencies (${APP_NAME} libindicatorpowerservice) -target_link_libraries (${APP_NAME} indicatorpowerservice ${SERVICE_DEPS_LIBRARIES}) - diff --git a/tests/indicator-power-service-cmdline-battery.cc b/tests/indicator-power-service-cmdline-battery.cc deleted file mode 100644 index 50ed2bb..0000000 --- a/tests/indicator-power-service-cmdline-battery.cc +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 2014 Canonical Ltd. - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - * - * Authors: - * Charles Kerr <charles.kerr@canonical.com> - */ - -#include <cstdlib> - -#include <locale.h> // setlocale() -#include <libintl.h> // bindtextdomain() -#include <unistd.h> // STDIN_FILENO - -#include <gio/gio.h> - -#include "device-provider-mock.h" - -#include "service.h" - -/*** -**** -***/ - -static void -on_name_lost (gpointer instance G_GNUC_UNUSED, gpointer loop) -{ - g_message ("exiting: service couldn't acquire or lost ownership of busname"); - g_main_loop_quit (static_cast<GMainLoop*>(loop)); -} - -static IndicatorPowerDevice * battery = nullptr; - -static GMainLoop * loop = nullptr; - -static gboolean on_command_stream_available (GIOChannel *source, - GIOCondition /*condition*/, - gpointer /*user_data*/) -{ - gchar * str = nullptr; - GError * error = nullptr; - auto status = g_io_channel_read_line (source, &str, nullptr, nullptr, &error); - g_assert_no_error (error); - - if (status == G_IO_STATUS_NORMAL) - { - g_strstrip (str); - - if (!g_strcmp0 (str, "charging")) - { - g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, nullptr); - } - else if (!g_strcmp0 (str, "discharging")) - { - g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, nullptr); - } - else - { - g_object_set (battery, INDICATOR_POWER_DEVICE_PERCENTAGE, atof(str), nullptr); - } - } - else if (status == G_IO_STATUS_EOF) - { - g_main_loop_quit (loop); - } - - g_free (str); - return G_SOURCE_CONTINUE; -} - -/* this is basically indicator-power-service with a custom provider */ -int -main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) -{ - g_print("This test app has the same code as indicator-power-service\n" - "except instead of listening to UPower, it has a fake battery\n" - "which you can edit with keyboard inputs. Supported commands:\n" - "1. A number in [0..100] to set battery level\n" - "2. 'charging'\n" - "3. 'discharging'\n" - "4. ctrl-c to exit\n"); - - IndicatorPowerDeviceProvider * device_provider; - IndicatorPowerService * service; - - g_assert(g_setenv("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, true)); - g_assert(g_setenv("GSETTINGS_BACKEND", "memory", true)); - - /* boilerplate i18n */ - setlocale (LC_ALL, ""); - bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); - textdomain (GETTEXT_PACKAGE); - - /* read lines from the command line */ - auto channel = g_io_channel_unix_new (STDIN_FILENO); - auto watch_tag = g_io_add_watch (channel, G_IO_IN, on_command_stream_available, nullptr); - - /* run */ - battery = indicator_power_device_new ("/some/path", UP_DEVICE_KIND_BATTERY, 50.0, UP_DEVICE_STATE_DISCHARGING, 30*60); - device_provider = indicator_power_device_provider_mock_new (); - indicator_power_device_provider_add_device (INDICATOR_POWER_DEVICE_PROVIDER_MOCK(device_provider), battery); - service = indicator_power_service_new (device_provider); - loop = g_main_loop_new (NULL, FALSE); - g_signal_connect (service, INDICATOR_POWER_SERVICE_SIGNAL_NAME_LOST, - G_CALLBACK(on_name_lost), loop); - g_main_loop_run (loop); - - /* cleanup */ - g_main_loop_unref (loop); - g_source_remove (watch_tag); - g_io_channel_unref (channel); - g_clear_object (&service); - g_clear_object (&device_provider); - g_clear_object (&battery); - return 0; -} |