From 4f74252ce9a8ae3e651cc9e61dc280da82f12d9b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 10:15:28 -0500 Subject: add test-indicator.cc --- tests/test-indicator.cc | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/test-indicator.cc (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc new file mode 100644 index 0000000..2f16176 --- /dev/null +++ b/tests/test-indicator.cc @@ -0,0 +1,52 @@ +/* +Copyright 2012 Canonical Ltd. + +Authors: + Charles Kerr + +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 . +*/ + +#include + +#include "device.h" +#include "indicator-power.h" + +namespace +{ + void ensure_glib_initialized () + { + static bool initialized = false; + + if (G_UNLIKELY(!initialized)) + { + initialized = true; + g_type_init(); + } + } +} + +/*** +**** +***/ + +TEST(IndicatorTest, GObjectNew) +{ + ensure_glib_initialized (); + + GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_TYPE, NULL)); + ASSERT_TRUE (o != NULL); + ASSERT_TRUE (IS_INDICATOR_POWER(o)); + g_clear_pointer (&o, g_object_unref); +} + -- cgit v1.2.3 From f18b60b0ce389bee7fc4784b6714f28567397b42 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 11:32:16 -0500 Subject: remove the g_clear_pointer() calls s.t. things will build and run on alesage's Jenkins setup running Precise --- tests/test-indicator.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 2f16176..e3533e9 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -47,6 +47,6 @@ TEST(IndicatorTest, GObjectNew) GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_TYPE, NULL)); ASSERT_TRUE (o != NULL); ASSERT_TRUE (IS_INDICATOR_POWER(o)); - g_clear_pointer (&o, g_object_unref); + g_object_unref (o); } -- cgit v1.2.3 From d8484ce5caf9fd5f4efb30368ccc9b70b1f38710 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 May 2012 16:50:58 -0500 Subject: first draft of getting GSettings working in the unit tests before the schema is installed. --- tests/test-indicator.cc | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index e3533e9..af494ee 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -22,6 +22,10 @@ with this program. If not, see . #include "device.h" #include "indicator-power.h" +/*** +**** +***/ + namespace { void ensure_glib_initialized () @@ -40,7 +44,27 @@ namespace **** ***/ -TEST(IndicatorTest, GObjectNew) +class IndicatorTest : public ::testing::Test +{ + protected: + + virtual void SetUp() + { + ensure_glib_initialized (); + g_setenv( "GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE); + g_message ("GSETTINGS_SCHEMA_DIR is %s", g_getenv("GSETTINGS_SCHEMA_DIR")); + } + + virtual void TearDown() + { + } +}; + +/*** +**** +***/ + +TEST_F(IndicatorTest, GObjectNew) { ensure_glib_initialized (); @@ -49,4 +73,3 @@ TEST(IndicatorTest, GObjectNew) ASSERT_TRUE (IS_INDICATOR_POWER(o)); g_object_unref (o); } - -- cgit v1.2.3 From 34c756aa7a894ee9c12f3dce0b4ae6e4ac6a30a8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 25 May 2012 14:58:56 -0500 Subject: modify IndicatorPower to use IndicatorPowerDevices internally --- tests/test-indicator.cc | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index af494ee..3e9d97b 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -48,15 +48,32 @@ class IndicatorTest : public ::testing::Test { protected: + IndicatorPowerDevice * ac_device; + IndicatorPowerDevice * battery_device; + virtual void SetUp() { ensure_glib_initialized (); + g_setenv( "GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE); - g_message ("GSETTINGS_SCHEMA_DIR is %s", g_getenv("GSETTINGS_SCHEMA_DIR")); + + ac_device = indicator_power_device_new ( + "/org/freedesktop/UPower/devices/line_power_AC", + UP_DEVICE_KIND_LINE_POWER, + ". GThemedIcon ac-adapter-symbolic ac-adapter ", + 0.0, UP_DEVICE_STATE_UNKNOWN, 0); + + battery_device = indicator_power_device_new ( + "/org/freedesktop/UPower/devices/battery_BAT0", + UP_DEVICE_KIND_BATTERY, + ". GThemedIcon battery-good-symbolic gpm-battery-060 battery-good ", + 52.871712, UP_DEVICE_STATE_DISCHARGING, 8834); } virtual void TearDown() { + g_clear_object (&battery_device); + g_clear_object (&ac_device); } }; @@ -66,10 +83,18 @@ class IndicatorTest : public ::testing::Test TEST_F(IndicatorTest, GObjectNew) { - ensure_glib_initialized (); - GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_TYPE, NULL)); ASSERT_TRUE (o != NULL); ASSERT_TRUE (IS_INDICATOR_POWER(o)); g_object_unref (o); } + +TEST_F(IndicatorTest, SetDevices) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + IndicatorPowerDevice * devices[] = { ac_device, battery_device }; + + indicator_power_set_devices (power, devices, G_N_ELEMENTS(devices)); + + g_object_unref (power); +} -- cgit v1.2.3 From 6bbfe3a79da52a5d03224bdb66faf0d41385158d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 25 May 2012 15:02:03 -0500 Subject: make Jenkins happy --- tests/test-indicator.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 3e9d97b..c3f502a 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -72,8 +72,8 @@ class IndicatorTest : public ::testing::Test virtual void TearDown() { - g_clear_object (&battery_device); - g_clear_object (&ac_device); + g_object_unref (battery_device); + g_object_unref (ac_device); } }; -- cgit v1.2.3 From 676aef0608d08a7d2086742e0dbd6b05e2d548bd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 14:16:26 -0500 Subject: first draft of adding tests for a discharging battery --- tests/test-indicator.cc | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index c3f502a..4b39579 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -98,3 +98,38 @@ TEST_F(IndicatorTest, SetDevices) g_object_unref (power); } + +TEST_F(IndicatorTest, DischargingStrings) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + // give the indicator a discharging battery with 1 hour of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + INDICATOR_POWER_DEVICE_TIME, guint64(60*60), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + + GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + IndicatorObjectEntry * entry = static_cast(entries->data); + ASSERT_STREQ(entry->accessible_desc, "Battery (1 hour left (50%))"); + ASSERT_STREQ(entry->name_hint, "indicator-power"); + g_list_free (entries); + + // give the indicator a discharging battery with over 12 hours of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + INDICATOR_POWER_DEVICE_TIME, guint64(60*60*12 + 1), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + g_list_free (entries); + + // cleanup + g_object_unref (power); +} -- cgit v1.2.3 From e0d1b3299edde62797fe6fa08be3740f24c7d38e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 14:35:22 -0500 Subject: add coverage for various charging/discharging/charged states --- tests/test-indicator.cc | 104 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 4b39579..ad619e2 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -110,7 +110,6 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); indicator_power_set_devices (power, &battery_device, 1); - GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); ASSERT_EQ (g_list_length(entries), 1); IndicatorObjectEntry * entry = static_cast(entries->data); @@ -118,16 +117,115 @@ TEST_F(IndicatorTest, DischargingStrings) ASSERT_STREQ(entry->name_hint, "indicator-power"); g_list_free (entries); + // give the indicator a discharging battery with 2 hours of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, + INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery (2 hours left (100%))"); + g_list_free (entries); + // give the indicator a discharging battery with over 12 hours of life left g_object_set (battery_device, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*12 + 1), NULL); indicator_power_set_devices (power, &battery_device, 1); entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); ASSERT_EQ (g_list_length(entries), 1); entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery"); + g_list_free (entries); + + // give the indicator a discharging battery with 29 seconds left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_TIME, guint64(29), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery (Unknown time left (100%))"); + g_list_free (entries); + + // what happens if the time estimate isn't available + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery (50%)"); + g_list_free (entries); + + // what happens if the time estimate AND percentage isn't available + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0, + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery (not present)"); + g_list_free (entries); + + // cleanup + g_object_unref (power); +} + +TEST_F(IndicatorTest, ChargingStrings) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + // give the indicator a discharging battery with 1 hour of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + INDICATOR_POWER_DEVICE_TIME, guint64(60*60), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + IndicatorObjectEntry * entry = static_cast(entries->data); + ASSERT_STREQ(entry->accessible_desc, "Battery (1 hour to charge (50%))"); + g_list_free (entries); + + // give the indicator a discharging battery with 2 hours of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + entry = static_cast(entries->data); + ASSERT_STREQ (entry->accessible_desc, "Battery (2 hours to charge (50%))"); + g_list_free (entries); + + // cleanup + g_object_unref (power); +} + +TEST_F(IndicatorTest, ChargedStrings) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + // give the indicator a discharging battery with 1 hour of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED, + INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + ASSERT_EQ (g_list_length(entries), 1); + IndicatorObjectEntry * entry = static_cast(entries->data); + ASSERT_STREQ(entry->accessible_desc, "Battery (charged)"); g_list_free (entries); // cleanup -- cgit v1.2.3 From 29ec6d23c13cbe7487232b79700c5d5406c26014 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 15:42:58 -0500 Subject: extract-method to simplify testing the accessible text --- tests/test-indicator.cc | 92 ++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 47 deletions(-) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index ad619e2..52e319f 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -75,6 +75,16 @@ class IndicatorTest : public ::testing::Test g_object_unref (battery_device); g_object_unref (ac_device); } + + const char* GetAccessibleDesc (IndicatorPower * power) const + { + GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); + g_assert (g_list_length(entries) == 1); + IndicatorObjectEntry * entry = static_cast(entries->data); + const char * ret = entry->accessible_desc; + g_list_free (entries); + return ret; + } }; /*** @@ -109,13 +119,8 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); - indicator_power_set_devices (power, &battery_device, 1); - GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - IndicatorObjectEntry * entry = static_cast(entries->data); - ASSERT_STREQ(entry->accessible_desc, "Battery (1 hour left (50%))"); - ASSERT_STREQ(entry->name_hint, "indicator-power"); - g_list_free (entries); + indicator_power_set_devices (power, &battery_device, 1); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour left (50%))"); // give the indicator a discharging battery with 2 hours of life left g_object_set (battery_device, @@ -123,33 +128,21 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery (2 hours left (100%))"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours left (100%))"); // give the indicator a discharging battery with over 12 hours of life left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*12 + 1), NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery"); // give the indicator a discharging battery with 29 seconds left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(29), NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery (Unknown time left (100%))"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (Unknown time left (100%))"); // what happens if the time estimate isn't available g_object_set (battery_device, @@ -157,11 +150,7 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery (50%)"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (50%)"); // what happens if the time estimate AND percentage isn't available g_object_set (battery_device, @@ -169,11 +158,7 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0, NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery (not present)"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (not present)"); // cleanup g_object_unref (power); @@ -190,22 +175,14 @@ TEST_F(IndicatorTest, ChargingStrings) INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); indicator_power_set_devices (power, &battery_device, 1); - GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - IndicatorObjectEntry * entry = static_cast(entries->data); - ASSERT_STREQ(entry->accessible_desc, "Battery (1 hour to charge (50%))"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour to charge (50%))"); // give the indicator a discharging battery with 2 hours of life left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), NULL); indicator_power_set_devices (power, &battery_device, 1); - entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - entry = static_cast(entries->data); - ASSERT_STREQ (entry->accessible_desc, "Battery (2 hours to charge (50%))"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours to charge (50%))"); // cleanup g_object_unref (power); @@ -222,12 +199,33 @@ TEST_F(IndicatorTest, ChargedStrings) INDICATOR_POWER_DEVICE_TIME, guint64(0), NULL); indicator_power_set_devices (power, &battery_device, 1); - GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power)); - ASSERT_EQ (g_list_length(entries), 1); - IndicatorObjectEntry * entry = static_cast(entries->data); - ASSERT_STREQ(entry->accessible_desc, "Battery (charged)"); - g_list_free (entries); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (charged)"); + + // cleanup + g_object_unref (power); +} + +TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED, + INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, + INDICATOR_POWER_DEVICE_TIME, guint64(0), + NULL); + IndicatorPowerDevice * bad_battery_device = indicator_power_device_new ( + "/org/freedesktop/UPower/devices/battery_BAT0", + UP_DEVICE_KIND_BATTERY, + ". GThemedIcon battery-good-symbolic gpm-battery-060 battery-good ", + 53, UP_DEVICE_STATE_CHARGING, 0); + + IndicatorPowerDevice * devices[] = { battery_device, bad_battery_device }; + indicator_power_set_devices (power, devices, G_N_ELEMENTS(devices)); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (53%)"); // cleanup g_object_unref (power); + g_object_unref (bad_battery_device); } + -- cgit v1.2.3 From aeb6ab59af1f325261db754aade493a949d5a1e3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 16:26:02 -0500 Subject: call g_object_run_dispose() in a standalone test to get coverage on the NULL / non-NULL branches of dispose()'s g_clear_pointer() calls --- tests/test-indicator.cc | 1 + 1 file changed, 1 insertion(+) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 52e319f..dedf54c 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -96,6 +96,7 @@ TEST_F(IndicatorTest, GObjectNew) GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_TYPE, NULL)); ASSERT_TRUE (o != NULL); ASSERT_TRUE (IS_INDICATOR_POWER(o)); + g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls g_object_unref (o); } -- cgit v1.2.3 From eda15ff15c3603deb748daaa59c9b28a576a9c4d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 17:10:48 -0500 Subject: add coverage for DBusListener get_property() --- tests/test-indicator.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index dedf54c..2229d77 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -19,6 +19,7 @@ with this program. If not, see . #include +#include "dbus-listener.h" #include "device.h" #include "indicator-power.h" @@ -230,3 +231,19 @@ TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) g_object_unref (bad_battery_device); } +TEST_F(IndicatorTest, DbusListenerGetProperty) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + GObject * dbus_listener = G_OBJECT(g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, + INDICATOR_POWER_DBUS_LISTENER_INDICATOR, power, + NULL)); + GObject * indicator = NULL; + g_object_get (dbus_listener, + INDICATOR_POWER_DBUS_LISTENER_INDICATOR, &indicator, + NULL); + ASSERT_EQ(INDICATOR_POWER(indicator), power); + + // cleanup + g_object_unref (dbus_listener); + g_object_unref (power); +} -- cgit v1.2.3 From 2f1281d4faa6bed2a0be484bd722504df290880c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 26 May 2012 17:49:13 -0500 Subject: add coverage for charging with >1 minute but <1 hour left --- tests/test-indicator.cc | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 2229d77..5875df6 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -115,6 +115,15 @@ TEST_F(IndicatorTest, DischargingStrings) { IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + // give the indicator a discharging battery with 30 minutes of life left + g_object_set (battery_device, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + INDICATOR_POWER_DEVICE_TIME, guint64(60*30), + NULL); + indicator_power_set_devices (power, &battery_device, 1); + ASSERT_STREQ (GetAccessibleDesc(power), "Battery (30 minutes left (50%))"); + // give the indicator a discharging battery with 1 hour of life left g_object_set (battery_device, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, -- cgit v1.2.3 From 23a2672a1a763cdef67ccc5cb9f8fec7daf021be Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 27 May 2012 09:22:28 -0500 Subject: use signals to decouple i-power and dbus-listener --- tests/test-indicator.cc | 60 +++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 32 deletions(-) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 5875df6..50ae2c1 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -103,17 +103,22 @@ TEST_F(IndicatorTest, GObjectNew) TEST_F(IndicatorTest, SetDevices) { + GSList * devices; IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); - IndicatorPowerDevice * devices[] = { ac_device, battery_device }; - indicator_power_set_devices (power, devices, G_N_ELEMENTS(devices)); - + devices = NULL; + devices = g_slist_append (devices, ac_device); + devices = g_slist_append (devices, battery_device); + indicator_power_set_devices (power, devices); + g_slist_free (devices); + g_object_unref (power); } TEST_F(IndicatorTest, DischargingStrings) { IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + GSList * devices = g_slist_append (NULL, battery_device); // give the indicator a discharging battery with 30 minutes of life left g_object_set (battery_device, @@ -121,7 +126,7 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*30), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (30 minutes left (50%))"); // give the indicator a discharging battery with 1 hour of life left @@ -130,7 +135,7 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour left (50%))"); // give the indicator a discharging battery with 2 hours of life left @@ -138,21 +143,21 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours left (100%))"); // give the indicator a discharging battery with over 12 hours of life left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*12 + 1), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery"); // give the indicator a discharging battery with 29 seconds left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(29), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (Unknown time left (100%))"); // what happens if the time estimate isn't available @@ -160,7 +165,7 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_TIME, guint64(0), INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (50%)"); // what happens if the time estimate AND percentage isn't available @@ -168,16 +173,18 @@ TEST_F(IndicatorTest, DischargingStrings) INDICATOR_POWER_DEVICE_TIME, guint64(0), INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0, NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (not present)"); // cleanup + g_slist_free (devices); g_object_unref (power); } TEST_F(IndicatorTest, ChargingStrings) { IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + GSList * devices = g_slist_prepend (NULL, battery_device); // give the indicator a discharging battery with 1 hour of life left g_object_set (battery_device, @@ -185,23 +192,25 @@ TEST_F(IndicatorTest, ChargingStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour to charge (50%))"); // give the indicator a discharging battery with 2 hours of life left g_object_set (battery_device, INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours to charge (50%))"); // cleanup + g_slist_free (devices); g_object_unref (power); } TEST_F(IndicatorTest, ChargedStrings) { IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + GSList * devices = g_slist_append (NULL, battery_device); // give the indicator a discharging battery with 1 hour of life left g_object_set (battery_device, @@ -209,10 +218,11 @@ TEST_F(IndicatorTest, ChargedStrings) INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0, INDICATOR_POWER_DEVICE_TIME, guint64(0), NULL); - indicator_power_set_devices (power, &battery_device, 1); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (charged)"); // cleanup + g_slist_free (devices); g_object_unref (power); } @@ -231,28 +241,14 @@ TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) ". GThemedIcon battery-good-symbolic gpm-battery-060 battery-good ", 53, UP_DEVICE_STATE_CHARGING, 0); - IndicatorPowerDevice * devices[] = { battery_device, bad_battery_device }; - indicator_power_set_devices (power, devices, G_N_ELEMENTS(devices)); + GSList * devices = NULL; + devices = g_slist_append (devices, battery_device); + devices = g_slist_append (devices, bad_battery_device); + indicator_power_set_devices (power, devices); ASSERT_STREQ (GetAccessibleDesc(power), "Battery (53%)"); // cleanup + g_slist_free (devices); g_object_unref (power); g_object_unref (bad_battery_device); } - -TEST_F(IndicatorTest, DbusListenerGetProperty) -{ - IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); - GObject * dbus_listener = G_OBJECT(g_object_new (INDICATOR_POWER_DBUS_LISTENER_TYPE, - INDICATOR_POWER_DBUS_LISTENER_INDICATOR, power, - NULL)); - GObject * indicator = NULL; - g_object_get (dbus_listener, - INDICATOR_POWER_DBUS_LISTENER_INDICATOR, &indicator, - NULL); - ASSERT_EQ(INDICATOR_POWER(indicator), power); - - // cleanup - g_object_unref (dbus_listener); - g_object_unref (power); -} -- cgit v1.2.3 From 7b8cd0982a266d589a7dcc0a76b04776d72e1bf4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 28 May 2012 19:37:43 -0500 Subject: add more tests to improve coverage: different device types, no primary device --- tests/test-indicator.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 50ae2c1..32d6f4f 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -252,3 +252,64 @@ TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) g_object_unref (power); g_object_unref (bad_battery_device); } + +TEST_F(IndicatorTest, OtherDevices) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + g_object_ref (battery_device); + GSList * devices = g_slist_append (NULL, battery_device); + + devices = g_slist_append (devices, indicator_power_device_new ( + "/org/freedesktop/UPower/devices/mouse", UP_DEVICE_KIND_MOUSE, + "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + devices = g_slist_append (devices, indicator_power_device_new ( + "/org/freedesktop/UPower/devices/ups", UP_DEVICE_KIND_UPS, + "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + devices = g_slist_append (devices, indicator_power_device_new ( + "/org/freedesktop/UPower/devices/keyboard", UP_DEVICE_KIND_KEYBOARD, + "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + devices = g_slist_append (devices, indicator_power_device_new ( + "/org/freedesktop/UPower/devices/pda", UP_DEVICE_KIND_PDA, + "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + devices = g_slist_append (devices, indicator_power_device_new ( + "/org/freedesktop/UPower/devices/phone", UP_DEVICE_KIND_PHONE, + "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + devices = g_slist_append (devices, indicator_power_device_new ( + "/org/freedesktop/UPower/devices/monitor", UP_DEVICE_KIND_MONITOR, + "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + devices = g_slist_append (devices, indicator_power_device_new ( + "/org/freedesktop/UPower/devices/media_player", UP_DEVICE_KIND_MEDIA_PLAYER, + "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + devices = g_slist_append (devices, indicator_power_device_new ( + "/org/freedesktop/UPower/devices/tablet", UP_DEVICE_KIND_TABLET, + "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + devices = g_slist_append (devices, indicator_power_device_new ( + "/org/freedesktop/UPower/devices/computer", UP_DEVICE_KIND_COMPUTER, + "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + devices = g_slist_append (devices, indicator_power_device_new ( + "/org/freedesktop/UPower/devices/unknown", UP_DEVICE_KIND_UNKNOWN, + "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + + indicator_power_set_devices (power, devices); + + // FIXME: this tests to confirm the code doesn't crash, + // but further tests would be helpful + + // cleanup + g_slist_free_full (devices, g_object_unref); + g_object_unref (power); +} + +TEST_F(IndicatorTest, NoDevices) +{ + IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL)); + + indicator_power_set_devices (power, NULL); + + // FIXME: this tests to confirm the code doesn't crash, + // but further tests would be helpful + + // cleanup + g_object_unref (power); +} -- cgit v1.2.3 From 97867c31fd3f4767fe891de31786b7b32d6dd35a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 May 2012 13:08:27 -0500 Subject: remove IndicatorPowerDevice's now-unused device icon string. --- tests/test-indicator.cc | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'tests/test-indicator.cc') diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index 32d6f4f..b9f7321 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -61,13 +61,11 @@ class IndicatorTest : public ::testing::Test ac_device = indicator_power_device_new ( "/org/freedesktop/UPower/devices/line_power_AC", UP_DEVICE_KIND_LINE_POWER, - ". GThemedIcon ac-adapter-symbolic ac-adapter ", 0.0, UP_DEVICE_STATE_UNKNOWN, 0); battery_device = indicator_power_device_new ( "/org/freedesktop/UPower/devices/battery_BAT0", UP_DEVICE_KIND_BATTERY, - ". GThemedIcon battery-good-symbolic gpm-battery-060 battery-good ", 52.871712, UP_DEVICE_STATE_DISCHARGING, 8834); } @@ -238,7 +236,6 @@ TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft) IndicatorPowerDevice * bad_battery_device = indicator_power_device_new ( "/org/freedesktop/UPower/devices/battery_BAT0", UP_DEVICE_KIND_BATTERY, - ". GThemedIcon battery-good-symbolic gpm-battery-060 battery-good ", 53, UP_DEVICE_STATE_CHARGING, 0); GSList * devices = NULL; @@ -262,34 +259,34 @@ TEST_F(IndicatorTest, OtherDevices) devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/mouse", UP_DEVICE_KIND_MOUSE, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/ups", UP_DEVICE_KIND_UPS, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/keyboard", UP_DEVICE_KIND_KEYBOARD, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/pda", UP_DEVICE_KIND_PDA, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/phone", UP_DEVICE_KIND_PHONE, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/monitor", UP_DEVICE_KIND_MONITOR, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/media_player", UP_DEVICE_KIND_MEDIA_PLAYER, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/tablet", UP_DEVICE_KIND_TABLET, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/computer", UP_DEVICE_KIND_COMPUTER, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); devices = g_slist_append (devices, indicator_power_device_new ( "/org/freedesktop/UPower/devices/unknown", UP_DEVICE_KIND_UNKNOWN, - "unused", 0, UP_DEVICE_STATE_UNKNOWN, 0)); + 0, UP_DEVICE_STATE_UNKNOWN, 0)); indicator_power_set_devices (power, devices); -- cgit v1.2.3