Back to index

indicator-power  12.10.0
test-indicator.cc
Go to the documentation of this file.
00001 /*
00002 Copyright 2012 Canonical Ltd.
00003 
00004 Authors:
00005     Charles Kerr <charles.kerr@canonical.com>
00006 
00007 This program is free software: you can redistribute it and/or modify it 
00008 under the terms of the GNU General Public License version 3, as published 
00009 by the Free Software Foundation.
00010 
00011 This program is distributed in the hope that it will be useful, but 
00012 WITHOUT ANY WARRANTY; without even the implied warranties of 
00013 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
00014 PURPOSE.  See the GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License along 
00017 with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #include <gtest/gtest.h>
00021 
00022 #include "dbus-listener.h"
00023 #include "device.h"
00024 #include "indicator-power.h"
00025 
00026 /***
00027 ****
00028 ***/
00029 
00030 namespace
00031 {
00032   void ensure_glib_initialized ()
00033   {
00034     static bool initialized = false;
00035 
00036     if (G_UNLIKELY(!initialized))
00037     {
00038       initialized = true;
00039       g_type_init();
00040     }
00041   }
00042 }
00043 
00044 /***
00045 ****
00046 ***/
00047 
00048 class IndicatorTest : public ::testing::Test
00049 {
00050   protected:
00051 
00052     IndicatorPowerDevice * ac_device;
00053     IndicatorPowerDevice * battery_device;
00054 
00055     virtual void SetUp()
00056     {
00057       ensure_glib_initialized ();
00058          
00059       g_setenv( "GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE);
00060 
00061       ac_device = indicator_power_device_new (
00062         "/org/freedesktop/UPower/devices/line_power_AC",
00063         UP_DEVICE_KIND_LINE_POWER,
00064         0.0, UP_DEVICE_STATE_UNKNOWN, 0);
00065 
00066       battery_device = indicator_power_device_new (
00067         "/org/freedesktop/UPower/devices/battery_BAT0",
00068         UP_DEVICE_KIND_BATTERY,
00069         52.871712, UP_DEVICE_STATE_DISCHARGING, 8834);
00070     }
00071 
00072     virtual void TearDown()
00073     {
00074       g_object_unref (battery_device);
00075       g_object_unref (ac_device);
00076     }
00077 
00078     const char* GetAccessibleDesc (IndicatorPower * power) const
00079     {
00080       GList * entries = indicator_object_get_entries (INDICATOR_OBJECT(power));
00081       g_assert (g_list_length(entries) == 1); 
00082       IndicatorObjectEntry * entry = static_cast<IndicatorObjectEntry*>(entries->data);
00083       const char * ret = entry->accessible_desc;
00084       g_list_free (entries);
00085       return ret;
00086     }
00087 };
00088 
00089 /***
00090 ****
00091 ***/
00092 
00093 TEST_F(IndicatorTest, GObjectNew)
00094 {
00095   GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_TYPE, NULL));
00096   ASSERT_TRUE (o != NULL);
00097   ASSERT_TRUE (IS_INDICATOR_POWER(o));
00098   g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls
00099   g_object_unref (o);
00100 }
00101 
00102 TEST_F(IndicatorTest, SetDevices)
00103 {
00104   GSList * devices;
00105   IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
00106 
00107   devices = NULL;
00108   devices = g_slist_append (devices, ac_device);
00109   devices = g_slist_append (devices, battery_device);
00110   indicator_power_set_devices (power, devices);
00111   g_slist_free (devices); 
00112 
00113   g_object_unref (power);
00114 }
00115 
00116 TEST_F(IndicatorTest, DischargingStrings)
00117 {
00118   IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
00119   GSList * devices = g_slist_append (NULL, battery_device);
00120 
00121   // give the indicator a discharging battery with 30 minutes of life left
00122   g_object_set (battery_device,
00123                 INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00124                 INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
00125                 INDICATOR_POWER_DEVICE_TIME, guint64(60*30),
00126                 NULL);
00127   indicator_power_set_devices (power, devices);
00128   ASSERT_STREQ (GetAccessibleDesc(power), "Battery (30 minutes left (50%))");
00129 
00130   // give the indicator a discharging battery with 1 hour of life left
00131   g_object_set (battery_device,
00132                 INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00133                 INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
00134                 INDICATOR_POWER_DEVICE_TIME, guint64(60*60),
00135                 NULL);
00136   indicator_power_set_devices (power, devices);
00137   ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour left (50%))");
00138 
00139   // give the indicator a discharging battery with 2 hours of life left
00140   g_object_set (battery_device,
00141                 INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0,
00142                 INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2),
00143                 NULL);
00144   indicator_power_set_devices (power, devices);
00145   ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours left (100%))");
00146 
00147   // give the indicator a discharging battery with over 12 hours of life left
00148   g_object_set (battery_device,
00149                 INDICATOR_POWER_DEVICE_TIME, guint64(60*60*12 + 1),
00150                 NULL);
00151   indicator_power_set_devices (power, devices);
00152   ASSERT_STREQ (GetAccessibleDesc(power), "Battery");
00153 
00154   // give the indicator a discharging battery with 29 seconds left
00155   g_object_set (battery_device,
00156                 INDICATOR_POWER_DEVICE_TIME, guint64(29),
00157                 NULL);
00158   indicator_power_set_devices (power, devices);
00159   ASSERT_STREQ (GetAccessibleDesc(power), "Battery (Unknown time left (100%))");
00160 
00161   // what happens if the time estimate isn't available
00162   g_object_set (battery_device,
00163                 INDICATOR_POWER_DEVICE_TIME, guint64(0),
00164                 INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
00165                 NULL);
00166   indicator_power_set_devices (power, devices);
00167   ASSERT_STREQ (GetAccessibleDesc(power), "Battery (50%)");
00168 
00169   // what happens if the time estimate AND percentage isn't available
00170   g_object_set (battery_device,
00171                 INDICATOR_POWER_DEVICE_TIME, guint64(0),
00172                 INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0,
00173                 NULL);
00174   indicator_power_set_devices (power, devices);
00175   ASSERT_STREQ (GetAccessibleDesc(power), "Battery (not present)");
00176 
00177   // cleanup
00178   g_slist_free (devices);
00179   g_object_unref (power);
00180 }
00181 
00182 TEST_F(IndicatorTest, ChargingStrings)
00183 {
00184   IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
00185   GSList * devices = g_slist_prepend (NULL, battery_device);
00186 
00187   // give the indicator a discharging battery with 1 hour of life left
00188   g_object_set (battery_device,
00189                 INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
00190                 INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
00191                 INDICATOR_POWER_DEVICE_TIME, guint64(60*60),
00192                 NULL);
00193   indicator_power_set_devices (power, devices);
00194   ASSERT_STREQ (GetAccessibleDesc(power), "Battery (1 hour to charge (50%))");
00195 
00196   // give the indicator a discharging battery with 2 hours of life left
00197   g_object_set (battery_device,
00198                 INDICATOR_POWER_DEVICE_TIME, guint64(60*60*2),
00199                 NULL);
00200   indicator_power_set_devices (power, devices);
00201   ASSERT_STREQ (GetAccessibleDesc(power), "Battery (2 hours to charge (50%))");
00202 
00203   // cleanup
00204   g_slist_free (devices);
00205   g_object_unref (power);
00206 }
00207 
00208 TEST_F(IndicatorTest, ChargedStrings)
00209 {
00210   IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
00211   GSList * devices = g_slist_append (NULL, battery_device);
00212 
00213   // give the indicator a discharging battery with 1 hour of life left
00214   g_object_set (battery_device,
00215                 INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED,
00216                 INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0,
00217                 INDICATOR_POWER_DEVICE_TIME, guint64(0),
00218                 NULL);
00219   indicator_power_set_devices (power, devices);
00220   ASSERT_STREQ (GetAccessibleDesc(power), "Battery (charged)");
00221 
00222   // cleanup
00223   g_slist_free (devices);
00224   g_object_unref (power);
00225 }
00226 
00227 TEST_F(IndicatorTest, AvoidChargingBatteriesWithZeroSecondsLeft)
00228 {
00229   IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
00230 
00231   g_object_set (battery_device,
00232                 INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED,
00233                 INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0,
00234                 INDICATOR_POWER_DEVICE_TIME, guint64(0),
00235                 NULL);
00236   IndicatorPowerDevice * bad_battery_device  = indicator_power_device_new (
00237     "/org/freedesktop/UPower/devices/battery_BAT0",
00238     UP_DEVICE_KIND_BATTERY,
00239     53, UP_DEVICE_STATE_CHARGING, 0);
00240 
00241   GSList * devices = NULL;
00242   devices = g_slist_append (devices, battery_device);
00243   devices = g_slist_append (devices, bad_battery_device);
00244   indicator_power_set_devices (power, devices);
00245   ASSERT_STREQ (GetAccessibleDesc(power), "Battery (53%)");
00246 
00247   // cleanup
00248   g_slist_free (devices);
00249   g_object_unref (power);
00250   g_object_unref (bad_battery_device);
00251 }
00252 
00253 TEST_F(IndicatorTest, OtherDevices)
00254 {
00255   IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
00256 
00257   g_object_ref (battery_device);
00258   GSList * devices = g_slist_append (NULL, battery_device);
00259 
00260   devices = g_slist_append (devices, indicator_power_device_new (
00261     "/org/freedesktop/UPower/devices/mouse", UP_DEVICE_KIND_MOUSE,
00262     0, UP_DEVICE_STATE_UNKNOWN, 0));
00263   devices = g_slist_append (devices, indicator_power_device_new (
00264     "/org/freedesktop/UPower/devices/ups", UP_DEVICE_KIND_UPS,
00265     0, UP_DEVICE_STATE_UNKNOWN, 0));
00266   devices = g_slist_append (devices, indicator_power_device_new (
00267     "/org/freedesktop/UPower/devices/keyboard", UP_DEVICE_KIND_KEYBOARD,
00268     0, UP_DEVICE_STATE_UNKNOWN, 0));
00269   devices = g_slist_append (devices, indicator_power_device_new (
00270     "/org/freedesktop/UPower/devices/pda", UP_DEVICE_KIND_PDA,
00271     0, UP_DEVICE_STATE_UNKNOWN, 0));
00272   devices = g_slist_append (devices, indicator_power_device_new (
00273     "/org/freedesktop/UPower/devices/phone", UP_DEVICE_KIND_PHONE,
00274     0, UP_DEVICE_STATE_UNKNOWN, 0));
00275   devices = g_slist_append (devices, indicator_power_device_new (
00276     "/org/freedesktop/UPower/devices/monitor", UP_DEVICE_KIND_MONITOR,
00277     0, UP_DEVICE_STATE_UNKNOWN, 0));
00278   devices = g_slist_append (devices, indicator_power_device_new (
00279     "/org/freedesktop/UPower/devices/media_player", UP_DEVICE_KIND_MEDIA_PLAYER,
00280     0, UP_DEVICE_STATE_UNKNOWN, 0));
00281   devices = g_slist_append (devices, indicator_power_device_new (
00282     "/org/freedesktop/UPower/devices/tablet", UP_DEVICE_KIND_TABLET,
00283     0, UP_DEVICE_STATE_UNKNOWN, 0));
00284   devices = g_slist_append (devices, indicator_power_device_new (
00285     "/org/freedesktop/UPower/devices/computer", UP_DEVICE_KIND_COMPUTER,
00286     0, UP_DEVICE_STATE_UNKNOWN, 0));
00287   devices = g_slist_append (devices, indicator_power_device_new (
00288     "/org/freedesktop/UPower/devices/unknown", UP_DEVICE_KIND_UNKNOWN,
00289     0, UP_DEVICE_STATE_UNKNOWN, 0));
00290 
00291   indicator_power_set_devices (power, devices);
00292 
00293   // FIXME: this tests to confirm the code doesn't crash,
00294   // but further tests would be helpful
00295 
00296   // cleanup
00297   g_slist_free_full (devices, g_object_unref);
00298   g_object_unref (power);
00299 }
00300 
00301 TEST_F(IndicatorTest, NoDevices)
00302 {
00303   IndicatorPower * power = INDICATOR_POWER(g_object_new (INDICATOR_POWER_TYPE, NULL));
00304 
00305   indicator_power_set_devices (power, NULL);
00306 
00307   // FIXME: this tests to confirm the code doesn't crash,
00308   // but further tests would be helpful
00309 
00310   // cleanup
00311   g_object_unref (power);
00312 }