diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2012-10-26 14:48:54 +0200 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2012-10-26 14:48:54 +0200 |
commit | 6d19670d7d438f2c58c9d819e87b72060af16bc5 (patch) | |
tree | 97a5d4f51d4dbe3f9a312c2c784c9c9b06b42713 /tests | |
parent | 3a0758a29fdae434142e1a67329ff2b65fee3c6b (diff) | |
parent | e246512c0c503b9a80051ae96a9f48d5b7ddb733 (diff) | |
download | ayatana-indicator-power-6d19670d7d438f2c58c9d819e87b72060af16bc5.tar.gz ayatana-indicator-power-6d19670d7d438f2c58c9d819e87b72060af16bc5.tar.bz2 ayatana-indicator-power-6d19670d7d438f2c58c9d819e87b72060af16bc5.zip |
choose the correct primary device, and choose the correct icon for it.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-device.cc | 416 | ||||
-rw-r--r-- | tests/test-indicator.cc | 14 |
2 files changed, 285 insertions, 145 deletions
diff --git a/tests/test-device.cc b/tests/test-device.cc index eb087dc..88c43fc 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -19,6 +19,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>. #include <gtest/gtest.h> #include "device.h" +#include "indicator-power.h" namespace { @@ -239,154 +240,205 @@ TEST_F(DeviceTest, IconNames) check_icon_names (device, "gpm-monitor-symbolic;" "gpm-monitor"); - // empty battery - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_EMPTY, - NULL); - check_icon_names (device, "battery-empty-symbolic;" - "gpm-battery-empty;" - "gpm-battery-000;" - "battery-empty"); - - // charged battery - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED, - NULL); - check_icon_names (device, "battery-full-charged-symbolic;" - "battery-full-charging-symbolic;" - "gpm-battery-full;" - "gpm-battery-100;" - "battery-full-charged;" - "battery-full-charging"); - - // charging battery, 95% - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0, - NULL); - check_icon_names (device, "battery-caution-charging-symbolic;" - "gpm-battery-000-charging;" - "battery-caution-charging"); - - // charging battery, 85% - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0, - NULL); - check_icon_names (device, "battery-caution-charging-symbolic;" - "gpm-battery-000-charging;" - "battery-caution-charging"); - - // charging battery, 50% - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, - NULL); - check_icon_names (device, "battery-caution-charging-symbolic;" - "gpm-battery-000-charging;" - "battery-caution-charging"); - - // charging battery, 25% - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, - NULL); - check_icon_names (device, "battery-caution-charging-symbolic;" - "gpm-battery-000-charging;" - "battery-caution-charging"); - - // charging battery, 5% - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, - NULL); - check_icon_names (device, "battery-caution-charging-symbolic;" - "gpm-battery-000-charging;" - "battery-caution-charging"); - - - // discharging battery, 95% - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0, - NULL); - check_icon_names (device, "battery-100;" - "gpm-battery-100;" - "battery-full-symbolic;" - "battery-full"); - - // discharging battery, 85% - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0, - NULL); - check_icon_names (device, "battery-080;" - "gpm-battery-080;" - "battery-full-symbolic;" - "battery-full"); - - // discharging battery, 50% -- 1 hour left - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, - INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), - NULL); - check_icon_names (device, "battery-060;" - "gpm-battery-060;" - "battery-good-symbolic;" - "battery-good"); - - // discharging battery, 25% -- 1 hour left - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, - INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), - NULL); - check_icon_names (device, "battery-040;" - "gpm-battery-040;" - "battery-good-symbolic;" - "battery-good"); - - // discharging battery, 25% -- 15 minutes left - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, - INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15), - NULL); - check_icon_names (device, "battery-020;" - "gpm-battery-020;" - "battery-low-symbolic;" - "battery-low"); - - // discharging battery, 5% -- 1 hour left - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, - INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), - NULL); - check_icon_names (device, "battery-040;" - "gpm-battery-040;" - "battery-good-symbolic;" - "battery-good"); + // devices that hold a charge + struct { + int kind; + const gchar * kind_str; + } devices[] = { + { UP_DEVICE_KIND_BATTERY, "battery" }, + { UP_DEVICE_KIND_UPS, "ups" }, + { UP_DEVICE_KIND_MOUSE, "mouse" }, + { UP_DEVICE_KIND_KEYBOARD, "keyboard" }, + { UP_DEVICE_KIND_PHONE, "phone" } + }; + + GString * expected = g_string_new (NULL); + for (int i=0, n=G_N_ELEMENTS(devices); i<n; i++) + { + const int kind = devices[i].kind; + const gchar * kind_str = devices[i].kind_str; - // discharging battery, 5% -- 15 minutes left - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, - INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, - INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15), + // empty + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_EMPTY, + NULL); + + g_string_append_printf (expected, "%s-empty-symbolic;", kind_str); + g_string_append_printf (expected, "gpm-%s-empty;", kind_str); + g_string_append_printf (expected, "gpm-%s-000;", kind_str); + g_string_append_printf (expected, "%s-empty", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // charged + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED, + NULL); + g_string_append_printf (expected, "%s-full-charged-symbolic;", kind_str); + g_string_append_printf (expected, "%s-full-charging-symbolic;", kind_str); + g_string_append_printf (expected, "gpm-%s-full;", kind_str); + g_string_append_printf (expected, "gpm-%s-100;", kind_str); + g_string_append_printf (expected, "%s-full-charged;", kind_str); + g_string_append_printf (expected, "%s-full-charging", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // charging, 95% + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0, + NULL); + + g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str); + g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str); + g_string_append_printf (expected, "%s-caution-charging", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // charging, 85% + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0, + NULL); + g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str); + g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str); + g_string_append_printf (expected, "%s-caution-charging", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // charging, 50% + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + NULL); + g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str); + g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str); + g_string_append_printf (expected, "%s-caution-charging", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // charging, 25% + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, + NULL); + g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str); + g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str); + g_string_append_printf (expected, "%s-caution-charging", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // charging, 5% + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, + NULL); + g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str); + g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str); + g_string_append_printf (expected, "%s-caution-charging", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // discharging, 95% + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0, NULL); - check_icon_names (device, "battery-000;" - "gpm-battery-000;" - "battery-caution-symbolic;" - "battery-caution"); - - // state unknown - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY, - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, + g_string_append_printf (expected, "%s-100;", kind_str); + g_string_append_printf (expected, "gpm-%s-100;", kind_str); + g_string_append_printf (expected, "%s-full-symbolic;", kind_str); + g_string_append_printf (expected, "%s-full", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // discharging, 85% + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0, + NULL); + g_string_append_printf (expected, "%s-080;", kind_str); + g_string_append_printf (expected, "gpm-%s-080;", kind_str); + g_string_append_printf (expected, "%s-full-symbolic;", kind_str); + g_string_append_printf (expected, "%s-full", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // discharging, 50% -- 1 hour left + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, + INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), + NULL); + g_string_append_printf (expected, "%s-060;", kind_str); + g_string_append_printf (expected, "gpm-%s-060;", kind_str); + g_string_append_printf (expected, "%s-good-symbolic;", kind_str); + g_string_append_printf (expected, "%s-good", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // discharging, 25% -- 1 hour left + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, + INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), + NULL); + g_string_append_printf (expected, "%s-040;", kind_str); + g_string_append_printf (expected, "gpm-%s-040;", kind_str); + g_string_append_printf (expected, "%s-good-symbolic;", kind_str); + g_string_append_printf (expected, "%s-good", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // discharging, 25% -- 15 minutes left + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, + INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15), + NULL); + g_string_append_printf (expected, "%s-020;", kind_str); + g_string_append_printf (expected, "gpm-%s-020;", kind_str); + g_string_append_printf (expected, "%s-low-symbolic;", kind_str); + g_string_append_printf (expected, "%s-low", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // discharging, 5% -- 1 hour left + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, + INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), NULL); - check_icon_names (device, "battery-missing-symbolic;" - "gpm-battery-missing;" - "battery-missing"); + g_string_append_printf (expected, "%s-040;", kind_str); + g_string_append_printf (expected, "gpm-%s-040;", kind_str); + g_string_append_printf (expected, "%s-good-symbolic;", kind_str); + g_string_append_printf (expected, "%s-good", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // discharging, 5% -- 15 minutes left + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, + INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, + INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15), + NULL); + g_string_append_printf (expected, "%s-000;", kind_str); + g_string_append_printf (expected, "gpm-%s-000;", kind_str); + g_string_append_printf (expected, "%s-caution-symbolic;", kind_str); + g_string_append_printf (expected, "%s-caution", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + + // state unknown + g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, + INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, + NULL); + g_string_append_printf (expected, "%s-missing-symbolic;", kind_str); + g_string_append_printf (expected, "gpm-%s-missing;", kind_str); + g_string_append_printf (expected, "%s-missing", kind_str); + check_icon_names (device, expected->str); + g_string_truncate (expected, 0); + } + g_string_free (expected, TRUE); // cleanup g_object_unref(o); @@ -479,3 +531,77 @@ TEST_F(DeviceTest, Labels) g_setenv ("LANG", real_lang, TRUE); g_free (real_lang); } + +static void +set_device_charge_state (IndicatorPowerDevice * device, int state, int time, double pct) +{ + g_object_set (device, INDICATOR_POWER_DEVICE_STATE, state, + INDICATOR_POWER_DEVICE_PERCENTAGE, pct, + INDICATOR_POWER_DEVICE_TIME, guint64(time), + NULL); +} + + +/* The menu title should tell you at a glance what you need to know most: what + device will lose power soonest (and optionally when), or otherwise which + device will take longest to charge (and optionally how long it will take). */ +TEST_F(DeviceTest, ChoosePrimary) +{ + GSList * devices; + IndicatorPowerDevice * a; + IndicatorPowerDevice * b; + + a = indicator_power_device_new ("/org/freedesktop/UPower/devices/mouse", + UP_DEVICE_KIND_MOUSE, + 0.0, + UP_DEVICE_STATE_DISCHARGING, + 0); + b = indicator_power_device_new ("/org/freedesktop/UPower/devices/battery", + UP_DEVICE_KIND_BATTERY, + 0.0, + UP_DEVICE_STATE_DISCHARGING, + 0); + + devices = NULL; + devices = g_slist_append (devices, a); + devices = g_slist_append (devices, b); + + /* Both discharging, same charge %, different times left before empty. + Confirm that the one with less time is chosen. */ + set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 99, 50.0); + set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 50.0); + ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); + + /* Both discharging, different charge % and times left. + Confirm that the one with less time is chosen. */ + set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 99, 50.0); + set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 49.0); + ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); + + /* Both discharging, different charge %, same times left. + Confirm that the one with less charge is chosen. */ + set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 100, 49.0); + set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 50.0); + ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); + + /* Both are charging, have the same charge percentage, and different times left (to charge). + * Confirm that the one with the most time left is chosen. */ + set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 49, 50.0); + set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 50.0); + ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); + + /* Both are charging, with different charges and time left. + Confirm that the one with the most time left is chosen. */ + set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 49, 50.0); + set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 49.0); + ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); + + /* Both are charging, have the same time left, and different charges. + * Confirm that the one with less charge is chosen. */ + set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 50, 50.0); + set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 49.0); + ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); + + // cleanup + g_slist_free_full (devices, g_object_unref); +} diff --git a/tests/test-indicator.cc b/tests/test-indicator.cc index b9f7321..824a5ac 100644 --- a/tests/test-indicator.cc +++ b/tests/test-indicator.cc @@ -29,6 +29,16 @@ with this program. If not, see <http://www.gnu.org/licenses/>. namespace { + void quiet_log_func (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) + { + // instantiating an indicator w/o a window causes lots + // of glib/gtk warnings... silence them so that they don't + // obscure any other warnings generated by the tests. + } + void ensure_glib_initialized () { static bool initialized = false; @@ -37,6 +47,8 @@ namespace { initialized = true; g_type_init(); + g_log_set_handler ("Gtk", (GLogLevelFlags)(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING), quiet_log_func, NULL); + g_log_set_handler ("GLib-GObject", (GLogLevelFlags)(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING), quiet_log_func, NULL); } } } @@ -71,6 +83,8 @@ class IndicatorTest : public ::testing::Test virtual void TearDown() { + ASSERT_EQ (1, G_OBJECT(battery_device)->ref_count); + ASSERT_EQ (1, G_OBJECT(ac_device)->ref_count); g_object_unref (battery_device); g_object_unref (ac_device); } |