aboutsummaryrefslogtreecommitdiff
path: root/tests/test-live-actions.cpp
blob: 403aeef4cde0c3033164a6fdca4ecec118a7c007 (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
/*
 * Copyright 2013 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 "state-mock.h"
#include "timedated-fixture.h"

#include <datetime/actions-live.h>

extern "C"
{
    #include <ayatana/common/utils.h>
}

using namespace ayatana::indicator::datetime;

class MockLiveActions: public LiveActions
{
public:

    explicit MockLiveActions(const std::shared_ptr<State>& state_in): LiveActions(state_in) {}
    ~MockLiveActions() {}
};

class TestLiveActionsFixture: public TimedatedFixture
{
private:

    using super = TimedatedFixture;

protected:

    std::shared_ptr<MockState> m_mock_state;
    std::shared_ptr<State> m_state;
    std::shared_ptr<MockLiveActions> m_live_actions;
    std::shared_ptr<Actions> m_actions;

    void SetUp() override
    {
        super::SetUp();

        // create the State and Actions
        m_mock_state.reset(new MockState);
        m_mock_state->settings.reset(new Settings);
        m_state = std::dynamic_pointer_cast<State>(m_mock_state);
        m_live_actions.reset(new MockLiveActions(m_state));
        m_actions = std::dynamic_pointer_cast<Actions>(m_live_actions);

        // start the timedate1 dbusmock
        start_timedate1("Etc/Utc");
    }

    void TearDown() override
    {
        m_actions.reset();
        m_live_actions.reset();
        m_state.reset();
        m_mock_state.reset();

        super::TearDown();
    }
};

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

TEST_F(TestLiveActionsFixture, HelloWorld)
{
    EXPECT_TRUE(true);
}

TEST_F(TestLiveActionsFixture, SetLocation)
{
    const std::string tzid = "America/Chicago";
    const std::string name = "Oklahoma City";
    const std::string expected = tzid + " " + name;

    EXPECT_NE(expected, m_state->settings->timezone_name.get());

    std::string new_name;
    m_state->settings->timezone_name.changed().connect(
        [&new_name](const std::string& n){new_name = n;}
    );

    m_actions->set_location(tzid, name);

    EXPECT_TRUE(wait_for([&new_name](){return !new_name.empty();}));
    EXPECT_EQ(expected, new_name);
    EXPECT_EQ(expected, m_state->settings->timezone_name.get());
    EXPECT_EQ(tzid, get_timedate1_timezone());
}

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

TEST_F(TestLiveActionsFixture, DesktopOpenAlarmApp)
{
    std::string sReturn = m_actions->open_alarm_app();
    const std::string expected = "evolution -c calendar";
    EXPECT_EQ(expected, sReturn);
}

TEST_F(TestLiveActionsFixture, DesktopOpenAppointment)
{
    Appointment a;
    a.uid = "some-uid";
    a.begin = DateTime::NowLocal();
    std::string sReturn = m_actions->open_appointment(a, a.begin);
    const std::string expected_substr = "evolution \"calendar:///?startdate=";
    EXPECT_NE(sReturn.find(expected_substr), std::string::npos);
}

TEST_F(TestLiveActionsFixture, DesktopOpenCalendarApp)
{
    std::string sReturn = m_actions->open_calendar_app(DateTime::NowLocal());
    const std::string expected_substr = "evolution \"calendar:///?startdate=";
    EXPECT_NE(sReturn.find(expected_substr), std::string::npos);
}

TEST_F(TestLiveActionsFixture, DesktopOpenSettingsApp)
{
    std::string sReturn = m_actions->open_settings_app();
    std::string expected_substr = "gnome-control-center datetime";

    if (ayatana_common_utils_is_unity())
    {
        expected_substr = "unity-control-center datetime";
    }
    else if (ayatana_common_utils_is_mate())
    {
        expected_substr = "mate-time-admin";
    }

    EXPECT_EQ(expected_substr, sReturn);
}

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

namespace
{
    const std::string clock_app_url = "alarm://";
}

TEST_F(TestLiveActionsFixture, PhoneOpenAlarmApp)
{
    setenv("XDG_CURRENT_DESKTOP", "Lomiri", 1);
    std::string sReturn = m_actions->open_alarm_app();
    EXPECT_EQ(clock_app_url, sReturn);
    unsetenv("XDG_CURRENT_DESKTOP");
}

TEST_F(TestLiveActionsFixture, PhoneOpenAppointment)
{
    setenv("XDG_CURRENT_DESKTOP", "Lomiri", 1);

    Appointment a;

    a.uid = "event-uid";
    a.source_uid = "source-uid";
    a.begin = DateTime::NowLocal();
    a.type = Appointment::EVENT;
    auto ocurrenceDate = DateTime::Local(2014, 1, 1, 0, 0, 0);
    std::string sReturn = m_actions->open_appointment(a, ocurrenceDate);
    const std::string appointment_app_url =  ocurrenceDate.to_timezone("UTC").format("calendar://startdate=%Y-%m-%dT%H:%M:%S+00:00");
    EXPECT_EQ(appointment_app_url, sReturn);

    a.type = Appointment::UBUNTU_ALARM;
    sReturn = m_actions->open_appointment(a, a.begin);
    EXPECT_EQ(clock_app_url, sReturn);
    unsetenv("XDG_CURRENT_DESKTOP");
}

TEST_F(TestLiveActionsFixture, PhoneOpenCalendarApp)
{
    setenv("XDG_CURRENT_DESKTOP", "Lomiri", 1);
    auto now = DateTime::NowLocal();
    std::string sReturn = m_actions->open_calendar_app(now);
    const std::string expected =  now.to_timezone("UTC").format("calendar://startdate=%Y-%m-%dT%H:%M:%S+00:00");
    EXPECT_EQ(expected, sReturn);
    unsetenv("XDG_CURRENT_DESKTOP");
}


TEST_F(TestLiveActionsFixture, PhoneOpenSettingsApp)
{
    setenv("XDG_CURRENT_DESKTOP", "Lomiri", 1);
    std::string sReturn = m_actions->open_settings_app();
    const std::string expected = "settings:///system/time-date";
    EXPECT_EQ(expected, sReturn);
    unsetenv("XDG_CURRENT_DESKTOP");
}

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

TEST_F(TestLiveActionsFixture, CalendarState)
{
    // init the clock
    auto now = DateTime::Local(2014, 1, 1, 0, 0, 0);
    m_mock_state->mock_clock->set_localtime (now);
    m_state->calendar_month->month().set(now);
    //m_state->planner->time.set(now);

    ///
    ///  Test the default calendar state.
    ///

    auto action_group = m_actions->action_group();
    auto calendar_state = g_action_group_get_action_state (action_group, "calendar");
    EXPECT_TRUE (calendar_state != nullptr);
    EXPECT_TRUE (g_variant_is_of_type (calendar_state, G_VARIANT_TYPE_DICTIONARY));

    // there's nothing in the planner yet, so appointment-days should be an empty array
    auto v = g_variant_lookup_value (calendar_state, "appointment-days", G_VARIANT_TYPE_ARRAY);
    EXPECT_TRUE (v != nullptr);
    EXPECT_EQ (0, g_variant_n_children (v));
    g_clear_pointer (&v, g_variant_unref);

    // calendar-day should be in sync with m_state->calendar_day
    v = g_variant_lookup_value (calendar_state, "calendar-day", G_VARIANT_TYPE_INT64);
    EXPECT_TRUE (v != nullptr);
    EXPECT_EQ (m_state->calendar_month->month().get().to_unix(), g_variant_get_int64(v));
    g_clear_pointer (&v, g_variant_unref);

    // show-week-numbers should be false because MockSettings defaults everything to 0
    v = g_variant_lookup_value (calendar_state, "show-week-numbers", G_VARIANT_TYPE_BOOLEAN);
    EXPECT_TRUE (v != nullptr);
    EXPECT_FALSE (g_variant_get_boolean (v));
    g_clear_pointer (&v, g_variant_unref);

    // cleanup this step
    g_clear_pointer (&calendar_state, g_variant_unref);


    ///
    ///  Now add appointments to the planner and confirm that the state keeps in sync
    ///

    auto tomorrow = now.add_days(1);
    auto tomorrow_begin = tomorrow.start_of_day();
    auto tomorrow_end = tomorrow.end_of_day();
    Appointment a1;
    a1.color = "green";
    a1.summary = "write unit tests";
    a1.uid = "D4B57D50247291478ED31DED17FF0A9838DED402";
    a1.begin = tomorrow_begin;
    a1.end = tomorrow_end;

    auto ubermorgen = now.add_days(2);
    auto ubermorgen_begin = ubermorgen.start_of_day();
    auto ubermorgen_end = ubermorgen.end_of_day();
    Appointment a2;
    a2.color = "orange";
    a2.summary = "code review";
    a2.uid = "2756ff7de3745bbffd65d2e4779c37c7ca60d843";
    a2.begin = ubermorgen_begin;
    a2.end = ubermorgen_end;

    m_state->calendar_month->appointments().set(std::vector<Appointment>({a1, a2}));

    ///
    ///  Now test the calendar state again.
    ///  The this_month field should now contain the appointments we just added.
    ///

    calendar_state = g_action_group_get_action_state (action_group, "calendar");
    v = g_variant_lookup_value (calendar_state, "appointment-days", G_VARIANT_TYPE_ARRAY);
    EXPECT_TRUE (v != nullptr);
    int i;
    g_variant_get_child (v, 0, "i", &i);
    EXPECT_EQ (a1.begin.day_of_month(), i);
    g_variant_get_child (v, 1, "i", &i);
    EXPECT_EQ (a2.begin.day_of_month(), i);
    g_clear_pointer(&v, g_variant_unref);
    g_clear_pointer(&calendar_state, g_variant_unref);

    ///
    ///  Confirm that the action state's dictionary
    ///  keeps in sync with settings.show_week_numbers
    ///

    auto b = m_state->settings->show_week_numbers.get();
    for (i=0; i<2; i++)
    {
        b = !b;
        m_state->settings->show_week_numbers.set(b);

        calendar_state = g_action_group_get_action_state (action_group, "calendar");
        v = g_variant_lookup_value (calendar_state, "show-week-numbers", G_VARIANT_TYPE_BOOLEAN);
        EXPECT_TRUE(v != nullptr);
        EXPECT_EQ(b, g_variant_get_boolean(v));

        g_clear_pointer(&v, g_variant_unref);
        g_clear_pointer(&calendar_state, g_variant_unref);
    }
}