Back to index

indicator-power  12.10.0
test-device.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 #include "device.h"
00022 
00023 namespace
00024 {
00025   void ensure_glib_initialized ()
00026   {
00027     static bool initialized = false;
00028 
00029     if (G_UNLIKELY(!initialized))
00030     {
00031       initialized = true;
00032       g_type_init();
00033     }
00034   }
00035 }
00036 
00037 class DeviceTest : public ::testing::Test
00038 {
00039   protected:
00040 
00041     virtual void SetUp()
00042     {
00043       ensure_glib_initialized ();
00044     }
00045 
00046     virtual void TearDown()
00047     {
00048     }
00049 
00050   protected:
00051 
00052     void check_icon_names (const IndicatorPowerDevice * device, const char * expected)
00053     {
00054       char ** names = indicator_power_device_get_icon_names (device);
00055       char * str = g_strjoinv (";", names);
00056       ASSERT_STREQ (expected, str);
00057       g_free (str);
00058       g_strfreev (names);
00059     }
00060 
00061     void check_strings (const IndicatorPowerDevice * device,
00062                         const char * expected_timestring,
00063                         const char * expected_details,
00064                         const char * expected_accessible)
00065     {
00066       char * timestring = NULL;
00067       char * details = NULL;
00068       char * accessible = NULL;
00069 
00070       indicator_power_device_get_time_details (device, &timestring, &details, &accessible);
00071       EXPECT_STREQ (expected_timestring, timestring);
00072       EXPECT_STREQ (expected_details, details);
00073       EXPECT_STREQ (expected_accessible, accessible);
00074 
00075       g_free (accessible);
00076       g_free (details);
00077       g_free (timestring);
00078     }
00079 };
00080 
00081 
00082 /***
00083 ****
00084 ***/
00085 
00086 TEST_F(DeviceTest, GObjectNew)
00087 {
00088   ensure_glib_initialized ();
00089 
00090   GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL));
00091   ASSERT_TRUE (o != NULL);
00092   ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(o));
00093   g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls
00094   g_object_unref (o);
00095 }
00096 
00097 TEST_F(DeviceTest, Properties)
00098 {
00099   int i;
00100   gdouble d;
00101   GObject * o;
00102   gchar * str;
00103   guint64 u64;
00104   const gchar * key;
00105 
00106   ensure_glib_initialized ();
00107 
00108   o = G_OBJECT (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL));
00109   ASSERT_TRUE (o != NULL);
00110   ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(o));
00111 
00112   // Test getting & setting a Device's properties.
00113 
00114   // KIND
00115   key = INDICATOR_POWER_DEVICE_KIND;
00116   g_object_set (o, key, UP_DEVICE_KIND_BATTERY, NULL);
00117   g_object_get (o, key, &i, NULL);
00118   ASSERT_EQ (i, UP_DEVICE_KIND_BATTERY);
00119 
00120   // STATE
00121   key = INDICATOR_POWER_DEVICE_STATE;
00122   g_object_set (o, key, UP_DEVICE_STATE_CHARGING, NULL);
00123   g_object_get (o, key, &i, NULL);
00124   ASSERT_EQ (i, UP_DEVICE_STATE_CHARGING);
00125 
00126   // OBJECT_PATH
00127   key = INDICATOR_POWER_DEVICE_OBJECT_PATH;
00128   g_object_set (o, key, "/object/path", NULL);
00129   g_object_get (o, key, &str, NULL);
00130   ASSERT_STREQ (str, "/object/path");
00131   g_free (str);
00132 
00133   // PERCENTAGE
00134   key = INDICATOR_POWER_DEVICE_PERCENTAGE;
00135   g_object_set (o, key, 50.0, NULL);
00136   g_object_get (o, key, &d, NULL);
00137   ASSERT_EQ((int)d, 50);
00138 
00139   // TIME
00140   key = INDICATOR_POWER_DEVICE_TIME;
00141   g_object_set (o, key, (guint64)30, NULL);
00142   g_object_get (o, key, &u64, NULL);
00143   ASSERT_EQ(u64, 30);
00144 
00145   // cleanup
00146   g_object_unref (o);
00147 }
00148 
00149 TEST_F(DeviceTest, New)
00150 {
00151   ensure_glib_initialized ();
00152 
00153   IndicatorPowerDevice * device = indicator_power_device_new ("/object/path",
00154                                                               UP_DEVICE_KIND_BATTERY,
00155                                                               50.0,
00156                                                               UP_DEVICE_STATE_CHARGING,
00157                                                               30);
00158   ASSERT_TRUE (device != NULL);
00159   ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device));
00160   ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_BATTERY);
00161   ASSERT_EQ (indicator_power_device_get_state(device), UP_DEVICE_STATE_CHARGING);
00162   ASSERT_STREQ (indicator_power_device_get_object_path(device), "/object/path");
00163   ASSERT_EQ ((int)indicator_power_device_get_percentage(device), 50);
00164   ASSERT_EQ (indicator_power_device_get_time(device), 30);
00165 
00166   // cleanup
00167   g_object_unref (device);
00168 }
00169 
00170 TEST_F(DeviceTest, NewFromVariant)
00171 {
00172   ensure_glib_initialized ();
00173 
00174   GVariant * variant = g_variant_new ("(susdut)",
00175                                       "/object/path",
00176                                       UP_DEVICE_KIND_BATTERY,
00177                                       "icon",
00178                                       50.0,
00179                                       UP_DEVICE_STATE_CHARGING,
00180                                       30);
00181   IndicatorPowerDevice * device = indicator_power_device_new_from_variant (variant);
00182   ASSERT_TRUE (variant != NULL);
00183   ASSERT_TRUE (device != NULL);
00184   ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device));
00185   ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_BATTERY);
00186   ASSERT_EQ (indicator_power_device_get_state(device), UP_DEVICE_STATE_CHARGING);
00187   ASSERT_STREQ (indicator_power_device_get_object_path(device), "/object/path");
00188   ASSERT_EQ ((int)indicator_power_device_get_percentage(device), 50);
00189   ASSERT_EQ (indicator_power_device_get_time(device), 30);
00190 
00191   // cleanup
00192   g_object_unref (device);
00193   g_variant_unref (variant);
00194 }
00195 
00196 TEST_F(DeviceTest, BadAccessors)
00197 {
00198   ensure_glib_initialized ();
00199 
00200   // test that these functions can handle being passed NULL pointers
00201   IndicatorPowerDevice * device = NULL;
00202   indicator_power_device_get_kind (device);
00203   indicator_power_device_get_time (device);
00204   indicator_power_device_get_state (device);
00205   indicator_power_device_get_percentage (device);
00206   indicator_power_device_get_object_path (device);
00207 
00208   // test that these functions can handle being passed non-device GObjects
00209   device = reinterpret_cast<IndicatorPowerDevice*>(g_cancellable_new ());
00210   indicator_power_device_get_kind (device);
00211   indicator_power_device_get_time (device);
00212   indicator_power_device_get_state (device);
00213   indicator_power_device_get_percentage (device);
00214   indicator_power_device_get_object_path (device);
00215   g_object_unref (device);
00216 }
00217 
00218 /***
00219 ****
00220 ***/
00221 
00222 TEST_F(DeviceTest, IconNames)
00223 {
00224   IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL));
00225   GObject * o = G_OBJECT(device);
00226 
00227   // bad arguments
00228   ASSERT_TRUE (indicator_power_device_get_icon_names (NULL) == NULL);
00229 
00230   // power
00231   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER,
00232                    NULL);
00233   check_icon_names (device, "ac-adapter-symbolic;"
00234                             "ac-adapter");
00235 
00236   // monitor
00237   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_MONITOR,
00238                    NULL);
00239   check_icon_names (device, "gpm-monitor-symbolic;"
00240                             "gpm-monitor");
00241 
00242   // empty battery
00243   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00244                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_EMPTY,
00245                    NULL);
00246   check_icon_names (device, "battery-empty-symbolic;"
00247                             "gpm-battery-empty;"
00248                             "gpm-battery-000;"
00249                             "battery-empty");
00250 
00251   // charged battery
00252   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00253                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED,
00254                    NULL);
00255   check_icon_names (device, "battery-full-charged-symbolic;battery-full-charging-symbolic;"
00256                             "gpm-battery-full;gpm-battery-100;"
00257                             "battery-full-charged;battery-full-charging");
00258 
00259   // charging battery, 95%
00260   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00261                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
00262                    INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0,
00263                    NULL);
00264   check_icon_names (device, "battery-caution-charging-symbolic;"
00265                             "gpm-battery-000-charging;"
00266                             "battery-caution-charging");
00267 
00268   // charging battery, 85%
00269   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00270                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
00271                    INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0,
00272                    NULL);
00273   check_icon_names (device, "battery-caution-charging-symbolic;"
00274                             "gpm-battery-000-charging;"
00275                             "battery-caution-charging");
00276 
00277   // charging battery, 50%
00278   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00279                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
00280                    INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
00281                    NULL);
00282   check_icon_names (device, "battery-caution-charging-symbolic;"
00283                             "gpm-battery-000-charging;"
00284                             "battery-caution-charging");
00285 
00286   // charging battery, 25%
00287   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00288                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
00289                    INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
00290                    NULL);
00291   check_icon_names (device, "battery-caution-charging-symbolic;"
00292                             "gpm-battery-000-charging;"
00293                             "battery-caution-charging");
00294 
00295   // charging battery, 5%
00296   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00297                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
00298                    INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
00299                    NULL);
00300   check_icon_names (device, "battery-caution-charging-symbolic;"
00301                             "gpm-battery-000-charging;"
00302                             "battery-caution-charging");
00303 
00304 
00305   // discharging battery, 95%
00306   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00307                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00308                    INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0,
00309                    NULL);
00310   check_icon_names (device, "battery-full-symbolic;"
00311                             "gpm-battery-100;"
00312                             "battery-full");
00313 
00314   // discharging battery, 85%
00315   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00316                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00317                    INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0,
00318                    NULL);
00319   check_icon_names (device, "battery-full-symbolic;"
00320                             "gpm-battery-080;"
00321                             "battery-full");
00322 
00323   // discharging battery, 50% -- 1 hour left
00324   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00325                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00326                    INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
00327                    INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
00328                    NULL);
00329   check_icon_names (device, "battery-good-symbolic;"
00330                             "gpm-battery-060;"
00331                             "battery-good");
00332 
00333   // discharging battery, 25% -- 1 hour left
00334   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00335                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00336                    INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
00337                    INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
00338                    NULL);
00339   check_icon_names (device, "battery-good-symbolic;"
00340                             "gpm-battery-040;"
00341                             "battery-good");
00342 
00343   // discharging battery, 25% -- 15 minutes left
00344   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00345                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00346                    INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
00347                    INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15),
00348                    NULL);
00349   check_icon_names (device, "battery-low-symbolic;"
00350                             "gpm-battery-020;"
00351                             "battery-low");
00352 
00353   // discharging battery, 5% -- 1 hour left
00354   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00355                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00356                    INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
00357                    INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
00358                    NULL);
00359   check_icon_names (device, "battery-good-symbolic;"
00360                             "gpm-battery-040;"
00361                             "battery-good");
00362 
00363   // discharging battery, 5% -- 15 minutes left
00364   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00365                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00366                    INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
00367                    INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15),
00368                    NULL);
00369   check_icon_names (device, "battery-caution-symbolic;"
00370                             "gpm-battery-000;"
00371                             "battery-caution"); 
00372   // state unknown
00373   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00374                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, 
00375                    NULL);
00376   check_icon_names (device, "battery-missing-symbolic;"
00377                             "gpm-battery-missing;"
00378                             "battery-missing");
00379 
00380   // cleanup
00381   g_object_unref(o);
00382 }
00383 
00384 
00385 TEST_F(DeviceTest, Labels)
00386 {
00387   // set our language so that i18n won't break these tests
00388   char * real_lang = g_strdup(g_getenv ("LANG"));
00389   g_setenv ("LANG", "en_US.UTF-8", TRUE);
00390 
00391   // bad args: NULL device
00392   check_strings (NULL, NULL, NULL, NULL);
00393 
00394   // bad args: a GObject that isn't a device
00395   GObject * o = G_OBJECT(g_cancellable_new());
00396   check_strings ((IndicatorPowerDevice*)o, NULL, NULL, NULL);
00397   g_object_unref (o);
00398 
00403   IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL));
00404   o = G_OBJECT(device);
00405 
00406   // charging
00407   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00408                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
00409                    INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
00410                    INDICATOR_POWER_DEVICE_TIME, guint64(60*61),
00411                    NULL);
00412   check_strings (device, "(1:01)",
00413                          "Battery (1:01 to charge)",
00414                          "Battery (1 hour 1 minute to charge (50%))");
00415 
00416   // discharging, < 12 hours left
00417   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00418                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00419                    INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
00420                    INDICATOR_POWER_DEVICE_TIME, guint64(60*61),
00421                    NULL);
00422   check_strings (device, "1:01",
00423                          "Battery (1:01 left)",
00424                          "Battery (1 hour 1 minute left (50%))");
00425 
00426   // discharging, > 12 hours left
00427   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00428                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00429                    INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
00430                    INDICATOR_POWER_DEVICE_TIME, guint64(60*60*13),
00431                    NULL);
00432   check_strings (device, "13:00", "Battery", "Battery");
00433 
00434   // fully charged
00435   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00436                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED,
00437                    INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0,
00438                    INDICATOR_POWER_DEVICE_TIME, guint64(0),
00439                    NULL);
00440   check_strings (device, "", "Battery (charged)", "Battery (charged)");
00441 
00442   // percentage but no time estimate
00443   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00444                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00445                    INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
00446                    INDICATOR_POWER_DEVICE_TIME, guint64(0),
00447                    NULL);
00448   check_strings (device, "(50%)", "Battery (50%)", "Battery (50%)");
00449 
00450   // no percentage, no time estimate
00451   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
00452                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
00453                    INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0,
00454                    INDICATOR_POWER_DEVICE_TIME, guint64(0),
00455                    NULL);
00456   check_strings (device, "(not present)", "Battery (not present)", "Battery (not present)");
00457 
00458   // power line
00459   g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER,
00460                    INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN,
00461                    INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0,
00462                    INDICATOR_POWER_DEVICE_TIME, guint64(0),
00463                    NULL);
00464   check_strings (device, "AC Adapter", "AC Adapter", "AC Adapter");
00465 
00466   // cleanup
00467   g_object_unref(o);
00468   g_setenv ("LANG", real_lang, TRUE);
00469   g_free (real_lang);
00470 }