aboutsummaryrefslogtreecommitdiff
path: root/src/testing.c
blob: 0d002a15b5f15ba2d81ff587f9b9844c350eb5e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

#include "dbus-shared.h"
#include "device-provider-mock.h"
#include "device-provider-upower.h"
#include "dbus-testing.h"
#include "service.h"
#include "testing.h"

#include <glib-object.h>

/**
***  GObject Properties
**/

enum
{
  PROP_0,
  PROP_SERVICE,
  LAST_PROP
};

static GParamSpec * properties[LAST_PROP];

/**
***
**/

typedef struct
{
  GDBusConnection * bus;
  DbusTesting * skeleton;
  IndicatorPowerService * service;
  IndicatorPowerDevice * battery_mock;
  gpointer provider_mock;
  gpointer provider_upower;
}
IndicatorPowerTestingPrivate;

typedef IndicatorPowerTestingPrivate priv_t;

G_DEFINE_TYPE_WITH_PRIVATE(IndicatorPowerTesting,
                           indicator_power_testing,
                           G_TYPE_OBJECT)

#define get_priv(o) ((priv_t*)indicator_power_testing_get_instance_private(o))

/***
****
***/

static void
update_device_provider (IndicatorPowerTesting * self)
{
  priv_t * const p = get_priv(self);
  IndicatorPowerDeviceProvider * device_provider;

  device_provider = dbus_testing_get_mock_battery_enabled(p->skeleton)
                  ? p->provider_mock
                  : p->provider_upower;
  indicator_power_service_set_device_provider(p->service, device_provider);
}

static void
set_bus(IndicatorPowerTesting * self, GDBusConnection * bus)
{
  priv_t * p;
  GDBusInterfaceSkeleton * skel;

  g_return_if_fail(INDICATOR_IS_POWER_TESTING(self));
  g_return_if_fail((bus == NULL) || G_IS_DBUS_CONNECTION(bus));

  p = get_priv (self);

  if (p->bus == bus)
    return;

  skel = G_DBUS_INTERFACE_SKELETON(p->skeleton);

  if (p->bus != NULL)
    {
      if (skel != NULL)
        g_dbus_interface_skeleton_unexport (skel);

      g_clear_object (&p->bus);
    }

  if (bus != NULL)
    {
      GError * error;

      p->bus = g_object_ref (bus);

      error = NULL;
      if (!g_dbus_interface_skeleton_export(skel,
                                            bus,
                                            BUS_PATH"/Testing",
                                            &error))
        {
          g_warning ("Unable to export Testing properties: %s", error->message);
          g_error_free (error);
        }
    }
}

/***
****
***/

static void
on_mock_battery_enabled_changed(DbusTesting           * skeleton  G_GNUC_UNUSED,
                                GParamSpec            * pspec     G_GNUC_UNUSED,
                                IndicatorPowerTesting * self)
{
  update_device_provider (self);
}

static void
on_mock_battery_level_changed(DbusTesting           * skeleton,
                              GParamSpec            * pspec     G_GNUC_UNUSED,
                              IndicatorPowerTesting * self)
{
  g_object_set(get_priv(self)->battery_mock,
               INDICATOR_POWER_DEVICE_PERCENTAGE, (gdouble)dbus_testing_get_mock_battery_level(skeleton),
               NULL);
}

static void
on_mock_battery_state_changed(DbusTesting           * skeleton,
                              GParamSpec            * pspec    G_GNUC_UNUSED,
                              IndicatorPowerTesting * self)
{
  const gchar* state_str = dbus_testing_get_mock_battery_state(skeleton);
  UpDeviceState state;

  if (!g_strcmp0(state_str, "charging"))
    {
      state = UP_DEVICE_STATE_CHARGING;
    }
  else if (!g_strcmp0(state_str, "discharging"))
    {
      state = UP_DEVICE_STATE_DISCHARGING;
    }
  else
    {
      g_warning("%s unsupported state: '%s'", G_STRLOC, state_str);
      state = UP_DEVICE_STATE_UNKNOWN;
    }

  g_object_set(get_priv(self)->battery_mock,
               INDICATOR_POWER_DEVICE_STATE, (gint)state,
               NULL);
}

static void
on_mock_battery_minutes_left_changed(DbusTesting           * skeleton,
                                     GParamSpec            * pspec    G_GNUC_UNUSED,
                                     IndicatorPowerTesting * self)
{
  g_object_set(get_priv(self)->battery_mock,
               INDICATOR_POWER_DEVICE_TIME, (guint64)dbus_testing_get_mock_battery_minutes_left(skeleton),
               NULL);
}

static void
on_bus_changed(IndicatorPowerService * service,
               GParamSpec            * spec     G_GNUC_UNUSED,
               IndicatorPowerTesting * self)
{
  GObject * bus = NULL;
  g_object_get(service, "bus", &bus, NULL);
  set_bus(self, G_DBUS_CONNECTION(bus));
  g_clear_object(&bus);
}

/***
****  GObject virtual functions
***/

static void
my_get_property(GObject     * o,
                guint         property_id,
                GValue      * value,
                GParamSpec  * pspec)
{
  IndicatorPowerTesting * const self = INDICATOR_POWER_TESTING (o);
  priv_t * const p = get_priv(self);

  switch (property_id)
    {
      case PROP_SERVICE:
        g_value_set_object(value, p->service);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(o, property_id, pspec);
    }
}

static void
my_set_property(GObject       * o,
                guint           property_id,
                const GValue  * value,
                GParamSpec    * pspec)
{
  IndicatorPowerTesting * const self = INDICATOR_POWER_TESTING (o);
  priv_t * const p = get_priv(self);

  switch (property_id)
    {
      case PROP_SERVICE:
        g_assert(p->service == NULL); /* G_PARAM_CONSTRUCT_ONLY */
        p->service = g_value_dup_object(value);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(o, property_id, pspec);
    }
}

static void
my_dispose(GObject * o)
{
  IndicatorPowerTesting * const self = INDICATOR_POWER_TESTING(o);
  priv_t * const p = get_priv (self);

  set_bus(self, NULL);
  g_clear_object(&p->skeleton);
  g_clear_object(&p->provider_upower);
  g_clear_object(&p->provider_mock);
  g_clear_object(&p->battery_mock);
  g_clear_object(&p->service);

  G_OBJECT_CLASS (indicator_power_testing_parent_class)->dispose(o);
}

static void
my_finalize (GObject * o G_GNUC_UNUSED)
{
}

static void
my_constructed (GObject * o)
{
  IndicatorPowerTesting * const self = INDICATOR_POWER_TESTING(o);
  priv_t * const p = get_priv (self);

  g_assert(p->service != NULL); /* G_PARAM_CONSTRUCT_ONLY */
  g_signal_connect(p->service, "notify::bus", G_CALLBACK(on_bus_changed), o);
  on_bus_changed(p->service, NULL, self);
  update_device_provider(self);
}


/***
****  Instantiation
***/

static void
indicator_power_testing_init (IndicatorPowerTesting * self)
{
  priv_t * const p = get_priv (self);

  /* DBus Skeleton */

  p->skeleton = dbus_testing_skeleton_new();
  dbus_testing_set_mock_battery_level(p->skeleton, 50u);
  dbus_testing_set_mock_battery_state(p->skeleton, "discharging");
  dbus_testing_set_mock_battery_enabled(p->skeleton, FALSE);
  dbus_testing_set_mock_battery_minutes_left(p->skeleton, 30);

  g_signal_connect(p->skeleton, "notify::mock-battery-enabled",
                   G_CALLBACK(on_mock_battery_enabled_changed), self);
  g_signal_connect(p->skeleton, "notify::mock-battery-level",
                   G_CALLBACK(on_mock_battery_level_changed), self);
  g_signal_connect(p->skeleton, "notify::mock-battery-state",
                   G_CALLBACK(on_mock_battery_state_changed), self);
  g_signal_connect(p->skeleton, "notify::mock-battery-minutes-left",
                   G_CALLBACK(on_mock_battery_minutes_left_changed), self);

  /* Mock Battery */

  p->battery_mock = indicator_power_device_new("/some/path",
                                               UP_DEVICE_KIND_BATTERY,
                                               50.0,
                                               UP_DEVICE_STATE_DISCHARGING,
                                               60*30,
                                               TRUE);


  /* Mock Provider */

  p->provider_mock = indicator_power_device_provider_mock_new();

  indicator_power_device_provider_add_device(INDICATOR_POWER_DEVICE_PROVIDER_MOCK(p->provider_mock),
                                             p->battery_mock);

  /* UPower Provider */

  p->provider_upower = indicator_power_device_provider_upower_new();
}

static void
indicator_power_testing_class_init (IndicatorPowerTestingClass * klass)
{
  GObjectClass * object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = my_dispose;
  object_class->finalize = my_finalize;
  object_class->constructed = my_constructed;
  object_class->get_property = my_get_property;
  object_class->set_property = my_set_property;

  properties[PROP_SERVICE] = g_param_spec_object (
    "service",
    "Servie",
    "The Ayatana Indicator Power Service",
    G_TYPE_OBJECT,
    G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);

  g_object_class_install_properties (object_class, LAST_PROP, properties);
}

/***
****  Public API
***/

IndicatorPowerTesting *
indicator_power_testing_new (IndicatorPowerService * service)
{
  GObject * o = g_object_new (INDICATOR_TYPE_POWER_TESTING, "service", service, NULL);

  return INDICATOR_POWER_TESTING (o);
}