~stgraber/indicator-datetime/fix-deps

« back to all changes in this revision

Viewing changes to tests/test-snap.cpp

Honor the 'other vibrations' setting to enable/disable vibrations when alarm notifications are shown.
Approved by: Rodney Dawes

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
#include <glib.h>
31
31
 
 
32
#include <unistd.h> // getuid()
 
33
#include <sys/types.h> // getuid()
 
34
 
32
35
using namespace unity::indicator::datetime;
33
36
 
34
37
#include "glib-fixture.h"
83
86
 
84
87
  static constexpr char const * HINT_TIMEOUT {"x-canonical-snap-decisions-timeout"};
85
88
 
 
89
  static constexpr char const * AS_BUSNAME            {"org.freedesktop.Accounts"};
 
90
  static constexpr char const * AS_INTERFACE          {"com.ubuntu.touch.AccountsService.Sound"};
 
91
  static constexpr char const * PROP_OTHER_VIBRATIONS {"OtherVibrate"};
 
92
  static constexpr char const * PROP_SILENT_MODE      {"SilentMode"};
 
93
 
86
94
  Appointment appt;
87
95
  GDBusConnection * system_bus = nullptr;
88
96
  GDBusConnection * session_bus = nullptr;
89
97
  DbusTestService * service = nullptr;
 
98
  DbusTestDbusMock * as_mock = nullptr;
90
99
  DbusTestDbusMock * notify_mock = nullptr;
91
100
  DbusTestDbusMock * powerd_mock = nullptr;
92
101
  DbusTestDbusMock * screen_mock = nullptr;
93
102
  DbusTestDbusMock * haptic_mock = nullptr;
 
103
  DbusTestDbusMockObject * as_obj = nullptr;
94
104
  DbusTestDbusMockObject * notify_obj = nullptr;
95
105
  DbusTestDbusMockObject * powerd_obj = nullptr;
96
106
  DbusTestDbusMockObject * screen_obj = nullptr;
116
126
    service = dbus_test_service_new(nullptr);
117
127
 
118
128
    ///
 
129
    ///  Add the AccountsService mock
 
130
    ///
 
131
 
 
132
    as_mock = dbus_test_dbus_mock_new(AS_BUSNAME);
 
133
    auto as_path = g_strdup_printf("/org/freedesktop/Accounts/User%lu", (gulong)getuid());
 
134
    as_obj = dbus_test_dbus_mock_get_object(as_mock,
 
135
                                            as_path,
 
136
                                            AS_INTERFACE,
 
137
                                            &error);
 
138
    g_free(as_path);
 
139
    g_assert_no_error(error);
 
140
 
 
141
    // PROP_SILENT_MODE
 
142
    dbus_test_dbus_mock_object_add_property(as_mock,
 
143
                                            as_obj,
 
144
                                            PROP_SILENT_MODE,
 
145
                                            G_VARIANT_TYPE_BOOLEAN,
 
146
                                            g_variant_new_boolean(false),
 
147
                                            &error);
 
148
    g_assert_no_error(error);
 
149
 
 
150
    // PROP_OTHER_VIBRATIONS
 
151
    dbus_test_dbus_mock_object_add_property(as_mock,
 
152
                                            as_obj,
 
153
                                            PROP_OTHER_VIBRATIONS,
 
154
                                            G_VARIANT_TYPE_BOOLEAN,
 
155
                                            g_variant_new_boolean(true),
 
156
                                            &error);
 
157
    g_assert_no_error(error);
 
158
    dbus_test_service_add_task(service, DBUS_TEST_TASK(as_mock));
 
159
 
 
160
    ///
119
161
    ///  Add the Notifications mock
120
162
    ///
121
163
 
283
325
    g_clear_object(&screen_mock);
284
326
    g_clear_object(&powerd_mock);
285
327
    g_clear_object(&notify_mock);
 
328
    g_clear_object(&as_mock);
286
329
    g_clear_object(&service);
287
330
    g_object_unref(session_bus);
288
331
    g_object_unref(system_bus);
480
523
****
481
524
***/
482
525
 
483
 
TEST_F(SnapFixture, HapticModes)
 
526
TEST_F(SnapFixture,Vibrate)
484
527
{
485
528
  auto settings = std::make_shared<Settings>();
486
529
  auto ne = std::make_shared<unity::indicator::notifications::Engine>(APP_NAME);
487
530
  auto func = [this](const Appointment&, const Alarm&){g_idle_add(quit_idle, loop);};
488
531
  GError * error = nullptr;
489
532
 
490
 
  // invoke a snap decision while haptic feedback is set to "pulse",
491
 
  // confirm that VibratePattern got called
492
 
  settings->alarm_haptic.set("pulse");
493
 
  auto snap = new Snap (ne, settings);
494
 
  (*snap)(appt, appt.alarms.front(), func, func);
495
 
  wait_msec(100);
496
 
  EXPECT_TRUE (dbus_test_dbus_mock_object_check_method_call (haptic_mock,
497
 
                                                             haptic_obj,
498
 
                                                             HAPTIC_METHOD_VIBRATE_PATTERN,
499
 
                                                             nullptr,
500
 
                                                             &error));
501
 
  delete snap;
502
 
 
503
 
  // invoke a snap decision while haptic feedback is set to "none",
504
 
  // confirm that VibratePattern =didn't= get called
505
 
  wait_msec(100);
506
 
  dbus_test_dbus_mock_object_clear_method_calls (haptic_mock, haptic_obj, &error);
507
 
  settings->alarm_haptic.set("none");
508
 
  snap = new Snap (ne, settings);
509
 
  (*snap)(appt, appt.alarms.front(), func, func);
510
 
  wait_msec(100);
511
 
  EXPECT_FALSE (dbus_test_dbus_mock_object_check_method_call (haptic_mock,
512
 
                                                              haptic_obj,
513
 
                                                              HAPTIC_METHOD_VIBRATE_PATTERN,
514
 
                                                              nullptr,
515
 
                                                              &error));
516
 
  delete snap;
517
 
 
518
 
  g_assert_no_error (error);
 
533
  struct {
 
534
      bool other_vibrations; // the com.ubuntu.touch.AccountsService.Sound "other vibrations" setting
 
535
      const char* haptic_mode;  // supported values: "none", "pulse"
 
536
      bool expected_vibrate_called; // do we expect the phone to vibrate?
 
537
  } test_cases[] = {
 
538
      { false, "none",  false },
 
539
      { true,  "none",  false },
 
540
      { false, "pulse", false },
 
541
      { true,  "pulse", true  }
 
542
  };
 
543
 
 
544
  auto snap = std::make_shared<Snap>(ne, settings);
 
545
 
 
546
  for(const auto& test_case : test_cases)
 
547
  {
 
548
    // clear out any previous iterations' noise
 
549
    dbus_test_dbus_mock_object_clear_method_calls(haptic_mock, haptic_obj, &error);
 
550
 
 
551
    // set the properties to match the test case
 
552
    settings->alarm_haptic.set(test_case.haptic_mode);
 
553
    dbus_test_dbus_mock_object_update_property(as_mock,
 
554
                                               as_obj,
 
555
                                               PROP_OTHER_VIBRATIONS,
 
556
                                               g_variant_new_boolean(test_case.other_vibrations),
 
557
                                               &error);
 
558
    g_assert_no_error(error);
 
559
    wait_msec(100);
 
560
 
 
561
    // run the test
 
562
    (*snap)(appt, appt.alarms.front(), func, func);
 
563
    wait_msec(100);
 
564
    const bool vibrate_called = dbus_test_dbus_mock_object_check_method_call(haptic_mock,
 
565
                                                                             haptic_obj,
 
566
                                                                             HAPTIC_METHOD_VIBRATE_PATTERN,
 
567
                                                                             nullptr,
 
568
                                                                             &error);
 
569
    g_assert_no_error(error);
 
570
    EXPECT_EQ(test_case.expected_vibrate_called, vibrate_called);
 
571
  }
519
572
}