~afrantzis/unity-system-compositor/no-external-spinner-zombie-processes-sigchld

« back to all changes in this revision

Viewing changes to tests/integration-tests/test_unity_screen_service.cpp

Introduce UnityScreenService and tests.

Approved by PS Jenkins bot, Kevin DuBois.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 
17
 */
 
18
 
 
19
#include "src/unity_screen_service.h"
 
20
#include "src/dbus_connection_handle.h"
 
21
#include "src/dbus_message_handle.h"
 
22
#include "src/screen.h"
 
23
#include "src/power_state_change_reason.h"
 
24
#include "src/unity_screen_service_introspection.h"
 
25
#include "wait_condition.h"
 
26
#include "dbus_bus.h"
 
27
#include "dbus_client.h"
 
28
 
 
29
#include <gtest/gtest.h>
 
30
#include <gmock/gmock.h>
 
31
 
 
32
#include <stdexcept>
 
33
#include <memory>
 
34
 
 
35
namespace ut = usc::test;
 
36
 
 
37
namespace
 
38
{
 
39
 
 
40
struct MockScreen : usc::Screen
 
41
{
 
42
    MOCK_METHOD1(enable_inactivity_timers, void(bool enable));
 
43
    MOCK_METHOD1(toggle_screen_power_mode, void(PowerStateChangeReason reason));
 
44
    MOCK_METHOD0(keep_display_on_temporarily, void());
 
45
 
 
46
    MOCK_METHOD2(set_screen_power_mode, void(MirPowerMode mode, PowerStateChangeReason reason));
 
47
    MOCK_METHOD1(keep_display_on, void(bool on));
 
48
    MOCK_METHOD1(set_brightness, void(int brightness));
 
49
    MOCK_METHOD1(enable_auto_brightness, void(bool enable));
 
50
    MOCK_METHOD2(set_inactivity_timeouts, void(int power_off_timeout, int dimmer_timeout));
 
51
 
 
52
    MOCK_METHOD1(set_touch_visualization_enabled, void(bool enabled));
 
53
 
 
54
    void register_power_state_change_handler(
 
55
        usc::PowerStateChangeHandler const& handler)
 
56
    {
 
57
        power_state_change_handler = handler;
 
58
    }
 
59
 
 
60
    usc::PowerStateChangeHandler power_state_change_handler;
 
61
};
 
62
 
 
63
class UnityScreenDBusClient : public ut::DBusClient
 
64
{
 
65
public:
 
66
    UnityScreenDBusClient(std::string const& address)
 
67
        : ut::DBusClient{
 
68
            address,
 
69
            "com.canonical.Unity.Screen",
 
70
            "/com/canonical/Unity/Screen"}
 
71
    {
 
72
        connection.add_match(
 
73
            "type='signal',"
 
74
            "interface='com.canonical.Unity.Screen',"
 
75
            "member='DisplayPowerStateChange'");
 
76
    }
 
77
 
 
78
    ut::DBusAsyncReplyString request_introspection()
 
79
    {
 
80
        return invoke_with_reply<ut::DBusAsyncReplyString>(
 
81
            "org.freedesktop.DBus.Introspectable", "Introspect",
 
82
            DBUS_TYPE_INVALID);
 
83
    }
 
84
 
 
85
    ut::DBusAsyncReplyVoid request_set_user_brightness(int32_t brightness)
 
86
    {
 
87
        return invoke_with_reply<ut::DBusAsyncReplyVoid>(
 
88
            unity_screen_interface, "setUserBrightness",
 
89
            DBUS_TYPE_INT32, &brightness,
 
90
            DBUS_TYPE_INVALID);
 
91
    }
 
92
 
 
93
    ut::DBusAsyncReplyVoid request_user_auto_brightness_enable(bool enabled)
 
94
    {
 
95
        dbus_bool_t const e = enabled ? TRUE : FALSE;
 
96
        return invoke_with_reply<ut::DBusAsyncReplyVoid>(
 
97
            unity_screen_interface, "userAutobrightnessEnable",
 
98
            DBUS_TYPE_BOOLEAN, &e,
 
99
            DBUS_TYPE_INVALID);
 
100
    }
 
101
 
 
102
    ut::DBusAsyncReplyVoid request_set_inactivity_timeouts(
 
103
        int32_t poweroff_timeout, int32_t dimmer_timeout)
 
104
    {
 
105
        return invoke_with_reply<ut::DBusAsyncReplyVoid>(
 
106
            unity_screen_interface, "setInactivityTimeouts",
 
107
            DBUS_TYPE_INT32, &poweroff_timeout,
 
108
            DBUS_TYPE_INT32, &dimmer_timeout,
 
109
            DBUS_TYPE_INVALID);
 
110
    }
 
111
 
 
112
    ut::DBusAsyncReplyVoid request_set_touch_visualization_enabled(bool enabled)
 
113
    {
 
114
        dbus_bool_t const e = enabled ? TRUE : FALSE;
 
115
        return invoke_with_reply<ut::DBusAsyncReplyVoid>(
 
116
            unity_screen_interface, "setTouchVisualizationEnabled",
 
117
            DBUS_TYPE_BOOLEAN, &e,
 
118
            DBUS_TYPE_INVALID);
 
119
    }
 
120
 
 
121
    ut::DBusAsyncReplyBool request_set_screen_power_mode(
 
122
        std::string const& mode, int reason)
 
123
    {
 
124
        auto mode_cstr = mode.c_str();
 
125
        return invoke_with_reply<ut::DBusAsyncReplyBool>(
 
126
            unity_screen_interface, "setScreenPowerMode",
 
127
            DBUS_TYPE_STRING, &mode_cstr,
 
128
            DBUS_TYPE_INT32, &reason,
 
129
            DBUS_TYPE_INVALID);
 
130
    }
 
131
 
 
132
    ut::DBusAsyncReplyInt request_keep_display_on()
 
133
    {
 
134
        return invoke_with_reply<ut::DBusAsyncReplyInt>(
 
135
            unity_screen_interface, "keepDisplayOn",
 
136
            DBUS_TYPE_INVALID);
 
137
    }
 
138
 
 
139
    ut::DBusAsyncReplyVoid request_remove_display_on_request(int id)
 
140
    {
 
141
        return invoke_with_reply<ut::DBusAsyncReplyVoid>(
 
142
            unity_screen_interface, "removeDisplayOnRequest",
 
143
            DBUS_TYPE_INT32, &id,
 
144
            DBUS_TYPE_INVALID);
 
145
    }
 
146
 
 
147
    ut::DBusAsyncReply request_invalid_method()
 
148
    {
 
149
        return invoke_with_reply<ut::DBusAsyncReply>(
 
150
            unity_screen_interface, "invalidMethod",
 
151
            DBUS_TYPE_INVALID);
 
152
    }
 
153
 
 
154
    ut::DBusAsyncReply request_method_with_invalid_arguments()
 
155
    {
 
156
        char const* const str = "abcd";
 
157
        return invoke_with_reply<ut::DBusAsyncReply>(
 
158
            unity_screen_interface, "setUserBrightness",
 
159
            DBUS_TYPE_STRING, &str,
 
160
            DBUS_TYPE_INVALID);
 
161
    }
 
162
 
 
163
    usc::DBusMessageHandle listen_for_power_state_change_signal()
 
164
    {
 
165
        while (true)
 
166
        {
 
167
            dbus_connection_read_write(connection, 1);
 
168
            auto msg = usc::DBusMessageHandle{dbus_connection_pop_message(connection)};
 
169
 
 
170
            if (msg && dbus_message_is_signal(msg, "com.canonical.Unity.Screen", "DisplayPowerStateChange"))
 
171
            {
 
172
                return msg;
 
173
            }
 
174
        }
 
175
    }
 
176
 
 
177
    char const* const unity_screen_interface = "com.canonical.Unity.Screen";
 
178
};
 
179
 
 
180
struct AUnityScreenService : testing::Test
 
181
{
 
182
    std::chrono::seconds const default_timeout{3};
 
183
    ut::DBusBus bus;
 
184
 
 
185
    std::shared_ptr<MockScreen> const mock_screen =
 
186
        std::make_shared<testing::NiceMock<MockScreen>>();
 
187
    UnityScreenDBusClient client{bus.address()};
 
188
    usc::UnityScreenService service{bus.address(), mock_screen};
 
189
};
 
190
 
 
191
}
 
192
 
 
193
TEST_F(AUnityScreenService, replies_to_introspection_request)
 
194
{
 
195
    using namespace testing;
 
196
 
 
197
    auto reply = client.request_introspection();
 
198
    EXPECT_THAT(reply.get(), Eq(unity_screen_service_introspection));
 
199
}
 
200
 
 
201
TEST_F(AUnityScreenService, forwards_set_user_brightness_request)
 
202
{
 
203
    int const brightness = 10;
 
204
 
 
205
    EXPECT_CALL(*mock_screen, set_brightness(brightness));
 
206
 
 
207
    client.request_set_user_brightness(brightness);
 
208
}
 
209
 
 
210
TEST_F(AUnityScreenService, forwards_user_auto_brightness_enable_request)
 
211
{
 
212
    bool const enable = true;
 
213
 
 
214
    EXPECT_CALL(*mock_screen, enable_auto_brightness(enable));
 
215
 
 
216
    client.request_user_auto_brightness_enable(enable);
 
217
}
 
218
 
 
219
TEST_F(AUnityScreenService, forwards_set_inactivity_timeouts_request)
 
220
{
 
221
    int const poweroff_timeout = 1000;
 
222
    int const dimmer_timeout = 500;
 
223
 
 
224
    EXPECT_CALL(*mock_screen, set_inactivity_timeouts(poweroff_timeout, dimmer_timeout));
 
225
 
 
226
    client.request_set_inactivity_timeouts(poweroff_timeout, dimmer_timeout);
 
227
}
 
228
 
 
229
TEST_F(AUnityScreenService, forwards_set_touch_visualization_enabled_request)
 
230
{
 
231
    bool const enabled = true;
 
232
 
 
233
    EXPECT_CALL(*mock_screen, set_touch_visualization_enabled(enabled));
 
234
 
 
235
    client.request_set_touch_visualization_enabled(enabled);
 
236
}
 
237
 
 
238
TEST_F(AUnityScreenService, forwards_set_screen_power_mode_request)
 
239
{
 
240
    auto const mode = MirPowerMode::mir_power_mode_standby;
 
241
    std::string const mode_str = "standby";
 
242
    auto const reason = PowerStateChangeReason::proximity;
 
243
    auto const reason_int = static_cast<int>(reason);
 
244
 
 
245
    EXPECT_CALL(*mock_screen, set_screen_power_mode(mode, reason));
 
246
 
 
247
    client.request_set_screen_power_mode(mode_str, reason_int);
 
248
}
 
249
 
 
250
TEST_F(AUnityScreenService, replies_to_set_screen_power_mode_request)
 
251
{
 
252
    using namespace testing;
 
253
 
 
254
    std::string const mode_str = "standby";
 
255
    auto const reason_int = static_cast<int>(PowerStateChangeReason::proximity);
 
256
 
 
257
    auto reply = client.request_set_screen_power_mode(mode_str, reason_int);
 
258
 
 
259
    EXPECT_THAT(reply.get(), Eq(true));
 
260
}
 
261
 
 
262
TEST_F(AUnityScreenService, forwards_keep_display_on_request)
 
263
{
 
264
    EXPECT_CALL(*mock_screen, keep_display_on(true));
 
265
 
 
266
    client.request_keep_display_on();
 
267
}
 
268
 
 
269
TEST_F(AUnityScreenService, replies_with_different_ids_to_keep_display_on_requests)
 
270
{
 
271
    using namespace testing;
 
272
 
 
273
    auto reply1 = client.request_keep_display_on();
 
274
    auto reply2 = client.request_keep_display_on();
 
275
 
 
276
    auto const id1 = reply1.get();
 
277
    auto const id2 = reply2.get();
 
278
 
 
279
    EXPECT_THAT(id1, Ne(id2));
 
280
}
 
281
 
 
282
TEST_F(AUnityScreenService, disables_keep_display_on_when_single_request_is_removed)
 
283
{
 
284
    using namespace testing;
 
285
 
 
286
    InSequence s;
 
287
    EXPECT_CALL(*mock_screen, keep_display_on(true));
 
288
    EXPECT_CALL(*mock_screen, keep_display_on(false));
 
289
 
 
290
    auto reply1 = client.request_keep_display_on();
 
291
    client.request_remove_display_on_request(reply1.get());
 
292
}
 
293
 
 
294
TEST_F(AUnityScreenService, disables_keep_display_on_when_all_requests_are_removed)
 
295
{
 
296
    using namespace testing;
 
297
 
 
298
    EXPECT_CALL(*mock_screen, keep_display_on(true)).Times(3);
 
299
    EXPECT_CALL(*mock_screen, keep_display_on(false)).Times(0);
 
300
 
 
301
    auto reply1 = client.request_keep_display_on();
 
302
    auto reply2 = client.request_keep_display_on();
 
303
    auto reply3 = client.request_keep_display_on();
 
304
 
 
305
    client.request_remove_display_on_request(reply1.get());
 
306
    client.request_remove_display_on_request(reply2.get());
 
307
    auto id3 = reply3.get();
 
308
 
 
309
    // Display should still be kept on at this point
 
310
    Mock::VerifyAndClearExpectations(mock_screen.get());
 
311
 
 
312
    // keep_display_on should be disable only when the last request is removed
 
313
    EXPECT_CALL(*mock_screen, keep_display_on(false));
 
314
 
 
315
    client.request_remove_display_on_request(id3);
 
316
}
 
317
 
 
318
TEST_F(AUnityScreenService, disables_keep_display_on_when_single_client_disconnects)
 
319
{
 
320
    ut::WaitCondition request_processed;
 
321
 
 
322
    EXPECT_CALL(*mock_screen, keep_display_on(true)).Times(3);
 
323
    EXPECT_CALL(*mock_screen, keep_display_on(false))
 
324
        .WillOnce(WakeUp(&request_processed));
 
325
 
 
326
    client.request_keep_display_on();
 
327
    client.request_keep_display_on();
 
328
    client.request_keep_display_on();
 
329
 
 
330
    client.disconnect();
 
331
 
 
332
    request_processed.wait_for(default_timeout);
 
333
    EXPECT_TRUE(request_processed.woken());
 
334
}
 
335
 
 
336
TEST_F(AUnityScreenService, disables_keep_display_on_when_all_clients_disconnect_or_remove_requests)
 
337
{
 
338
    using namespace testing;
 
339
 
 
340
    UnityScreenDBusClient other_client{bus.address()};
 
341
 
 
342
    EXPECT_CALL(*mock_screen, keep_display_on(true)).Times(4);
 
343
    EXPECT_CALL(*mock_screen, keep_display_on(false)).Times(0);
 
344
 
 
345
    auto reply1 = client.request_keep_display_on();
 
346
    auto reply2 = client.request_keep_display_on();
 
347
    other_client.request_keep_display_on();
 
348
    other_client.request_keep_display_on();
 
349
 
 
350
    other_client.disconnect();
 
351
    client.request_remove_display_on_request(reply1.get());
 
352
    auto id2 = reply2.get();
 
353
 
 
354
    // Display should still be kept on at this point
 
355
    Mock::VerifyAndClearExpectations(mock_screen.get());
 
356
 
 
357
    // keep_display_on should be disabled only when the last request is removed
 
358
    ut::WaitCondition request_processed;
 
359
    EXPECT_CALL(*mock_screen, keep_display_on(false))
 
360
        .WillOnce(WakeUp(&request_processed));
 
361
 
 
362
    client.request_remove_display_on_request(id2);
 
363
 
 
364
    request_processed.wait_for(default_timeout);
 
365
    EXPECT_TRUE(request_processed.woken());
 
366
}
 
367
 
 
368
TEST_F(AUnityScreenService, emits_power_state_change_signal)
 
369
{
 
370
    using namespace testing;
 
371
 
 
372
    auto async_message = std::async(std::launch::async,
 
373
        [&] { return client.listen_for_power_state_change_signal(); });
 
374
 
 
375
    mock_screen->power_state_change_handler(
 
376
        MirPowerMode::mir_power_mode_off, PowerStateChangeReason::power_key);
 
377
 
 
378
    auto message = async_message.get();
 
379
 
 
380
    int32_t state{-1};
 
381
    int32_t reason{-1};
 
382
    dbus_message_get_args(message, nullptr,
 
383
        DBUS_TYPE_INT32, &state,
 
384
        DBUS_TYPE_INT32, &reason,
 
385
        DBUS_TYPE_INVALID);
 
386
 
 
387
    int32_t const off_state{0};
 
388
 
 
389
    EXPECT_THAT(state, Eq(off_state));
 
390
    EXPECT_THAT(reason, Eq(static_cast<int32_t>(PowerStateChangeReason::power_key)));
 
391
}
 
392
 
 
393
TEST_F(AUnityScreenService, returns_error_reply_for_unsupported_method)
 
394
{
 
395
    using namespace testing;
 
396
 
 
397
    auto reply = client.request_invalid_method();
 
398
    auto reply_msg = reply.get();
 
399
 
 
400
    EXPECT_THAT(dbus_message_get_type(reply_msg), Eq(DBUS_MESSAGE_TYPE_ERROR));
 
401
    EXPECT_THAT(dbus_message_get_error_name(reply_msg), StrEq(DBUS_ERROR_FAILED));
 
402
}
 
403
 
 
404
TEST_F(AUnityScreenService, returns_error_reply_for_method_with_invalid_arguments)
 
405
{
 
406
    using namespace testing;
 
407
 
 
408
    auto reply = client.request_method_with_invalid_arguments();
 
409
    auto reply_msg = reply.get();
 
410
 
 
411
    EXPECT_THAT(dbus_message_get_type(reply_msg), Eq(DBUS_MESSAGE_TYPE_ERROR));
 
412
    EXPECT_THAT(dbus_message_get_error_name(reply_msg), StrEq(DBUS_ERROR_FAILED));
 
413
}