diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-device.cc | 111 |
1 files changed, 44 insertions, 67 deletions
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<n; i++) + { + for (int j=i+1; j<n; j++) + { + g_object_set (a, INDICATOR_POWER_DEVICE_STATE, tests[i].state, + INDICATOR_POWER_DEVICE_TIME, guint64(tests[i].time), + INDICATOR_POWER_DEVICE_PERCENTAGE, tests[i].percentage, + NULL); + g_object_set (b, INDICATOR_POWER_DEVICE_STATE, tests[j].state, + INDICATOR_POWER_DEVICE_TIME, guint64(tests[j].time), + INDICATOR_POWER_DEVICE_PERCENTAGE, tests[j].percentage, + NULL); + ASSERT_EQ (a, indicator_power_choose_primary_device(device_list)); + + /* reverse the list to check that list order doesn't matter */ + device_list = g_slist_reverse (device_list); + ASSERT_EQ (a, indicator_power_choose_primary_device(device_list)); + } + } + // cleanup - g_slist_free_full (devices, g_object_unref); + g_slist_free_full (device_list, g_object_unref); } |