From b7b7ffd56c91ae9622ffebfec4ea4400c96d2fce Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 26 Oct 2012 10:02:14 +0200 Subject: Select the primary device based on the spec's criteria. Add tests to confirm. --- tests/test-device.cc | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'tests/test-device.cc') diff --git a/tests/test-device.cc b/tests/test-device.cc index 4c6a6f2..c310a58 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -19,6 +19,7 @@ with this program. If not, see . #include #include "device.h" +#include "indicator-power.h" namespace { @@ -530,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 differnt 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); +} -- cgit v1.2.3