From 7c0d274cb162da46198c63f288b67b1a54e70238 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 28 Oct 2012 01:48:30 +0200 Subject: expand DeviceTest.ChoosePrimary to test choosing from a wider variety of device states. --- tests/test-device.cc | 111 ++++++++++++++++++++------------------------------- 1 file changed, 44 insertions(+), 67 deletions(-) (limited to 'tests') diff --git a/tests/test-device.cc b/tests/test-device.cc index b5fa466..a32e0e5 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -532,22 +532,12 @@ TEST_F(DeviceTest, Labels) 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; + GSList * device_list; IndicatorPowerDevice * a; IndicatorPowerDevice * b; @@ -562,62 +552,49 @@ TEST_F(DeviceTest, ChoosePrimary) 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)); - - /* discharging always comes before charging */ - set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 50, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 50.0); - ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); - set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 50, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 50, 50.0); - ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); - - /* charging always comes before charged */ - set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 50, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_FULLY_CHARGED, 50, 50.0); - ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); - set_device_charge_state (a, UP_DEVICE_STATE_FULLY_CHARGED, 50, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 50.0); - ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); + /* device states + time left to {discharge,charge} + % of charge left, + sorted in order of preference wrt the spec's criteria. + So tests[i] should be picked over any test with an index greater than i */ + struct { + int state; + guint64 time; + double percentage; + } tests[] = { + { UP_DEVICE_STATE_DISCHARGING, 49, 50.0 }, + { UP_DEVICE_STATE_DISCHARGING, 50, 50.0 }, + { UP_DEVICE_STATE_DISCHARGING, 50, 100.0 }, + { UP_DEVICE_STATE_DISCHARGING, 51, 50.0 }, + { UP_DEVICE_STATE_CHARGING, 50, 50.0 }, + { UP_DEVICE_STATE_CHARGING, 49, 50.0 }, + { UP_DEVICE_STATE_CHARGING, 49, 100.0 }, + { UP_DEVICE_STATE_CHARGING, 48, 50.0 }, + { UP_DEVICE_STATE_FULLY_CHARGED, 0, 50.0 } + }; + + device_list = NULL; + device_list = g_slist_append (device_list, a); + device_list = g_slist_append (device_list, b); + for (int i=0, n=G_N_ELEMENTS(tests); i