~3v1n0/unity/light-shortcuts

« back to all changes in this revision

Viewing changes to tests/test_gnome_session_manager.cpp

  • Committer: Marco Trevisan (Treviño)
  • Date: 2013-04-26 12:41:09 UTC
  • Revision ID: mail@3v1n0.net-20130426124109-t3b2shjah2omiqa2
Unity: Remove all the views, but the Shortcuts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
2
 
/*
3
 
 * Copyright (C) 2013 Canonical Ltd
4
 
 *
5
 
 * This program is free software: you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License version 3 as
7
 
 * published by the Free Software Foundation.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
 *
17
 
 * Authored by: Marco Trevisan (Treviño) <marco.trevisan@canonical.com>
18
 
 */
19
 
 
20
 
#include <gmock/gmock.h>
21
 
#include <UnityCore/GLibDBusProxy.h>
22
 
#include <UnityCore/GLibDBusServer.h>
23
 
#include <UnityCore/GnomeSessionManager.h>
24
 
#include <UnityCore/Variant.h>
25
 
#include "test_utils.h"
26
 
 
27
 
using namespace unity;
28
 
using namespace unity::glib;
29
 
 
30
 
namespace
31
 
{
32
 
namespace
33
 
{
34
 
const std::string TEST_SERVER_NAME = "com.canonical.Unity.Test.GnomeManager";
35
 
 
36
 
const std::string SHELL_INTERFACE = "org.gnome.SessionManager.EndSessionDialog";
37
 
const std::string SHELL_OBJECT_PATH = "/org/gnome/SessionManager/EndSessionDialog";
38
 
 
39
 
const std::string UPOWER_PATH = "/org/freedesktop/UPower";
40
 
const std::string LOGIND_PATH = "/org/freedesktop/login1";
41
 
const std::string CONSOLE_KIT_PATH = "/org/freedesktop/ConsoleKit/Manager";
42
 
const std::string SCREEN_SAVER_PATH = "/org/gnome/ScreenSaver";
43
 
const std::string SESSION_MANAGER_PATH = "/org/gnome/SessionManager";
44
 
 
45
 
const std::string SESSION_OPTIONS = "com.canonical.indicator.session";
46
 
const std::string SUPPRESS_DIALOGS_KEY = "suppress-logout-restart-shutdown";
47
 
}
48
 
 
49
 
namespace introspection
50
 
{
51
 
const std::string UPOWER =
52
 
R"(<node>
53
 
  <interface name="org.freedesktop.UPower">
54
 
    <method name="Suspend"/>
55
 
    <method name="Hibernate"/>
56
 
    <method name="HibernateAllowed">
57
 
      <arg type="b" name="hibernate_allowed" direction="out"/>
58
 
    </method>
59
 
    <method name="SuspendAllowed">
60
 
      <arg type="b" name="suspend_allowed" direction="out"/>
61
 
    </method>
62
 
  </interface>
63
 
</node>)";
64
 
 
65
 
const std::string LOGIND =
66
 
R"(<node>
67
 
  <interface name="org.freedesktop.login1.Manager">
68
 
    <method name="CanSuspend">
69
 
      <arg type="s" name="result" direction="out"/>
70
 
    </method>
71
 
    <method name="CanHibernate">
72
 
      <arg type="s" name="result" direction="out"/>
73
 
    </method>
74
 
    <method name="PowerOff">
75
 
      <arg type="b" name="interactive" direction="in"/>
76
 
    </method>
77
 
    <method name="Reboot">
78
 
      <arg type="b" name="interactive" direction="in"/>
79
 
    </method>
80
 
    <method name="Suspend">
81
 
      <arg type="b" name="interactive" direction="in"/>
82
 
    </method>
83
 
    <method name="Hibernate">
84
 
      <arg type="b" name="interactive" direction="in"/>
85
 
    </method>
86
 
    <method name="TerminateSession">
87
 
      <arg type="s" name="id" direction="in"/>
88
 
    </method>
89
 
  </interface>
90
 
</node>)";
91
 
 
92
 
const std::string CONSOLE_KIT =
93
 
R"(<node>
94
 
  <interface name="org.freedesktop.ConsoleKit.Manager">
95
 
    <method name="Stop"/>
96
 
    <method name="Restart"/>
97
 
    <method name="CloseSession">
98
 
      <arg type="s" name="cookie" direction="in"/>
99
 
    </method>
100
 
  </interface>
101
 
</node>)";
102
 
 
103
 
const std::string SCREEN_SAVER =
104
 
R"(<node>
105
 
  <interface name="org.gnome.ScreenSaver">
106
 
    <method name="Lock"/>
107
 
    <method name="SimulateUserActivity"/>
108
 
  </interface>
109
 
</node>)";
110
 
 
111
 
const std::string SESSION_MANAGER =
112
 
R"(<node>
113
 
  <interface name="org.gnome.SessionManager">
114
 
    <method name="Logout">
115
 
      <arg type="u" name="type" direction="in"/>
116
 
    </method>
117
 
    <method name="Reboot"/>
118
 
    <method name="Shutdown"/>
119
 
    <method name="CanShutdown">
120
 
      <arg type="b" name="can_shutdown" direction="out"/>
121
 
    </method>
122
 
  </interface>
123
 
</node>
124
 
)";
125
 
}
126
 
 
127
 
struct MockGnomeSessionManager : session::GnomeManager {
128
 
  MockGnomeSessionManager()
129
 
    : GnomeManager(GnomeManager::TestMode())
130
 
  {}
131
 
};
132
 
 
133
 
struct TestGnomeSessionManager : testing::Test
134
 
{
135
 
  static void SetUpTestCase()
136
 
  {
137
 
    g_setenv("GSETTINGS_BACKEND", "memory", TRUE);
138
 
 
139
 
    can_shutdown_ = (g_random_int() % 2) ? true : false;
140
 
    can_suspend_ = (g_random_int() % 2) ? true : false;
141
 
    can_hibernate_ = (g_random_int() % 2) ? true : false;
142
 
 
143
 
    bool shutdown_called = false;
144
 
    bool hibernate_called = false;
145
 
    bool suspend_called = false;
146
 
 
147
 
    upower_ = std::make_shared<DBusServer>();
148
 
    upower_->AddObjects(introspection::UPOWER, UPOWER_PATH);
149
 
    upower_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
150
 
      if (method == "SuspendAllowed")
151
 
      {
152
 
        suspend_called = true;
153
 
        return g_variant_new("(b)", can_suspend_ ? TRUE : FALSE);
154
 
      }
155
 
      else if (method == "HibernateAllowed")
156
 
      {
157
 
        hibernate_called = true;
158
 
        return g_variant_new("(b)", can_hibernate_ ? TRUE : FALSE);
159
 
      }
160
 
 
161
 
      return nullptr;
162
 
    });
163
 
 
164
 
    logind_ = std::make_shared<DBusServer>();
165
 
    logind_->AddObjects(introspection::LOGIND, LOGIND_PATH);
166
 
    logind_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
167
 
      if (method == "CanSuspend")
168
 
      {
169
 
        suspend_called = true;
170
 
        return g_variant_new("(s)", can_suspend_ ? "yes" : "no");
171
 
      }
172
 
      else if (method == "CanHibernate")
173
 
      {
174
 
        hibernate_called = true;
175
 
        return g_variant_new("(s)", can_hibernate_ ? "yes" : "no");
176
 
      }
177
 
 
178
 
      return nullptr;
179
 
    });
180
 
 
181
 
    console_kit_ = std::make_shared<DBusServer>();
182
 
    console_kit_->AddObjects(introspection::CONSOLE_KIT, CONSOLE_KIT_PATH);
183
 
 
184
 
    screen_saver_ = std::make_shared<DBusServer>();
185
 
    screen_saver_->AddObjects(introspection::SCREEN_SAVER, SCREEN_SAVER_PATH);
186
 
 
187
 
    session_manager_ = std::make_shared<DBusServer>();
188
 
    session_manager_->AddObjects(introspection::SESSION_MANAGER, SESSION_MANAGER_PATH);
189
 
    session_manager_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
190
 
      if (method == "CanShutdown")
191
 
      {
192
 
        shutdown_called = true;
193
 
        return g_variant_new("(b)", can_shutdown_ ? TRUE : FALSE);
194
 
      }
195
 
 
196
 
      return nullptr;
197
 
    });
198
 
 
199
 
    manager = std::make_shared<MockGnomeSessionManager>();
200
 
    shell_proxy_ = std::make_shared<DBusProxy>(TEST_SERVER_NAME, SHELL_OBJECT_PATH, SHELL_INTERFACE);
201
 
 
202
 
    // We need to wait until the session manager has setup its internal values.
203
 
    Utils::WaitUntilMSec(hibernate_called);
204
 
    Utils::WaitUntilMSec(suspend_called);
205
 
    Utils::WaitUntilMSec(shutdown_called);
206
 
    EXPECT_TRUE(hibernate_called);
207
 
    EXPECT_TRUE(suspend_called);
208
 
    EXPECT_TRUE(shutdown_called);
209
 
    Utils::WaitForTimeoutMSec(100);
210
 
  }
211
 
 
212
 
  void SetUp()
213
 
  {
214
 
    ASSERT_NE(manager, nullptr);
215
 
    Utils::WaitUntilMSec([] { return upower_->IsConnected(); });
216
 
    Utils::WaitUntilMSec([] { return logind_->IsConnected(); });
217
 
    Utils::WaitUntilMSec([] { return console_kit_->IsConnected(); });
218
 
    Utils::WaitUntilMSec([] { return screen_saver_->IsConnected(); });
219
 
    Utils::WaitUntilMSec([] { return session_manager_->IsConnected(); });
220
 
    Utils::WaitUntilMSec([] { return shell_proxy_->IsConnected();});
221
 
    ASSERT_TRUE(shell_proxy_->IsConnected());
222
 
    EnableInteractiveShutdown(true);
223
 
 
224
 
    // reset default logind methods, to avoid tests clobbering each other
225
 
    logind_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
226
 
      if (method == "CanSuspend")
227
 
        return g_variant_new("(s)", can_suspend_ ? "yes" : "no");
228
 
      else if (method == "CanHibernate")
229
 
        return g_variant_new("(s)", can_hibernate_ ? "yes" : "no");
230
 
      return nullptr;
231
 
    });
232
 
  }
233
 
 
234
 
  void TearDown()
235
 
  {
236
 
    manager->logout_requested.clear();
237
 
    manager->reboot_requested.clear();
238
 
    manager->shutdown_requested.clear();
239
 
    manager->cancel_requested.clear();
240
 
    shell_proxy_->DisconnectSignal();
241
 
 
242
 
    // By calling this we reset the internal pending action status
243
 
    bool cancelled = false;
244
 
    shell_proxy_->Connect("Canceled", [&cancelled] (GVariant*) { cancelled = true; });
245
 
    manager->CancelAction();
246
 
    Utils::WaitUntilMSec(cancelled);
247
 
    shell_proxy_->DisconnectSignal("Canceled");
248
 
  }
249
 
 
250
 
  static void TearDownTestCase()
251
 
  {
252
 
    g_setenv("GSETTINGS_BACKEND", "", TRUE);
253
 
 
254
 
    bool cancelled = false;
255
 
    bool closed = false;
256
 
    shell_proxy_->Connect("Canceled", [&cancelled] (GVariant*) { cancelled = true; });
257
 
    shell_proxy_->Connect("Closed", [&closed] (GVariant*) { closed = true; });
258
 
 
259
 
    manager.reset();
260
 
 
261
 
    Utils::WaitUntilMSec(cancelled);
262
 
    Utils::WaitUntilMSec(closed);
263
 
    EXPECT_TRUE(cancelled);
264
 
    EXPECT_TRUE(closed);
265
 
 
266
 
    shell_proxy_.reset();
267
 
    upower_.reset();
268
 
    logind_.reset();
269
 
    console_kit_.reset();
270
 
    screen_saver_.reset();
271
 
    session_manager_.reset();
272
 
  }
273
 
 
274
 
  bool SettingsAvailable()
275
 
  {
276
 
    const gchar* const* schemas = g_settings_list_schemas();
277
 
 
278
 
    for (unsigned i = 0; schemas[i]; ++i)
279
 
    {
280
 
      if (g_strcmp0(schemas[i], SESSION_OPTIONS.c_str()) == 0)
281
 
        return true;
282
 
    }
283
 
 
284
 
    return false;
285
 
  }
286
 
 
287
 
  void EnableInteractiveShutdown(bool enable)
288
 
  {
289
 
    ASSERT_TRUE(SettingsAvailable());
290
 
    glib::Object<GSettings> setting(g_settings_new(SESSION_OPTIONS.c_str()));
291
 
    g_settings_set_boolean(setting, SUPPRESS_DIALOGS_KEY.c_str(), enable ? FALSE : TRUE);
292
 
  }
293
 
 
294
 
  enum class Action : unsigned
295
 
  {
296
 
    LOGOUT = 0,
297
 
    SHUTDOWN,
298
 
    REBOOT
299
 
  };
300
 
 
301
 
  void ShellOpenAction(Action action)
302
 
  {
303
 
    shell_proxy_->Call("Open", g_variant_new("(uuuao)", action, 0, 0, nullptr));
304
 
  }
305
 
 
306
 
  void ShellOpenActionWithInhibitor(Action action)
307
 
  {
308
 
    GVariantBuilder builder;
309
 
    g_variant_builder_init(&builder, G_VARIANT_TYPE ("ao"));
310
 
    g_variant_builder_add(&builder, "o", "/any/inhibitor/object");
311
 
    shell_proxy_->Call("Open", g_variant_new("(uuuao)", action, 0, 0, &builder));
312
 
  }
313
 
 
314
 
  static bool can_shutdown_;
315
 
  static bool can_suspend_;
316
 
  static bool can_hibernate_;
317
 
 
318
 
  static session::Manager::Ptr manager;
319
 
  static DBusServer::Ptr upower_;
320
 
  static DBusServer::Ptr console_kit_;
321
 
  static DBusServer::Ptr logind_;
322
 
  static DBusServer::Ptr screen_saver_;
323
 
  static DBusServer::Ptr session_manager_;
324
 
  static DBusProxy::Ptr shell_proxy_;
325
 
};
326
 
 
327
 
session::Manager::Ptr TestGnomeSessionManager::manager;
328
 
DBusServer::Ptr TestGnomeSessionManager::upower_;
329
 
DBusServer::Ptr TestGnomeSessionManager::console_kit_;
330
 
DBusServer::Ptr TestGnomeSessionManager::logind_;
331
 
DBusServer::Ptr TestGnomeSessionManager::screen_saver_;
332
 
DBusServer::Ptr TestGnomeSessionManager::session_manager_;
333
 
DBusProxy::Ptr TestGnomeSessionManager::shell_proxy_;
334
 
bool TestGnomeSessionManager::can_shutdown_;
335
 
bool TestGnomeSessionManager::can_suspend_;
336
 
bool TestGnomeSessionManager::can_hibernate_;
337
 
 
338
 
TEST_F(TestGnomeSessionManager, CanShutdown)
339
 
{
340
 
  EXPECT_EQ(manager->CanShutdown(), can_shutdown_);
341
 
}
342
 
 
343
 
TEST_F(TestGnomeSessionManager, CanHibernate)
344
 
{
345
 
  EXPECT_EQ(manager->CanHibernate(), can_hibernate_);
346
 
}
347
 
 
348
 
TEST_F(TestGnomeSessionManager, CanSuspend)
349
 
{
350
 
  EXPECT_EQ(manager->CanSuspend(), can_suspend_);
351
 
}
352
 
 
353
 
TEST_F(TestGnomeSessionManager, RealName)
354
 
{
355
 
  const char* name = g_get_real_name();
356
 
 
357
 
  if (!name || g_strcmp0(name, "Unknown") == 0)
358
 
    EXPECT_TRUE(manager->RealName().empty());
359
 
  else
360
 
    EXPECT_EQ(manager->RealName(), name);
361
 
}
362
 
 
363
 
TEST_F(TestGnomeSessionManager, UserName)
364
 
{
365
 
  EXPECT_EQ(manager->UserName(), g_get_user_name());
366
 
}
367
 
 
368
 
TEST_F(TestGnomeSessionManager, LockScreen)
369
 
{
370
 
  bool lock_called = false;
371
 
  bool simulate_activity_called = false;
372
 
 
373
 
  screen_saver_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
374
 
    if (method == "Lock")
375
 
    {
376
 
      lock_called = true;
377
 
      EXPECT_FALSE(simulate_activity_called);
378
 
    }
379
 
    else if (method == "SimulateUserActivity")
380
 
    {
381
 
      simulate_activity_called = true;
382
 
      EXPECT_TRUE(lock_called);
383
 
    }
384
 
 
385
 
    return nullptr;
386
 
  });
387
 
 
388
 
  manager->LockScreen();
389
 
 
390
 
  Utils::WaitUntilMSec(lock_called);
391
 
  EXPECT_TRUE(lock_called);
392
 
 
393
 
  Utils::WaitUntilMSec(simulate_activity_called);
394
 
  EXPECT_TRUE(simulate_activity_called);
395
 
}
396
 
 
397
 
TEST_F(TestGnomeSessionManager, Logout)
398
 
{
399
 
  bool logout_called = false;
400
 
 
401
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant* par) -> GVariant* {
402
 
    if (method == "Logout")
403
 
    {
404
 
      logout_called = true;
405
 
      EXPECT_EQ(Variant(par).GetUInt(), 1);
406
 
    }
407
 
 
408
 
    return nullptr;
409
 
  });
410
 
 
411
 
  manager->Logout();
412
 
 
413
 
  Utils::WaitUntilMSec(logout_called);
414
 
  EXPECT_TRUE(logout_called);
415
 
}
416
 
 
417
 
TEST_F(TestGnomeSessionManager, LogoutFallbackLogind)
418
 
{
419
 
  // This makes the standard call to return an error.
420
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
421
 
  g_setenv("XDG_SESSION_ID", "logind-id0", TRUE);
422
 
  g_unsetenv("XDG_SESSION_COOKIE");
423
 
 
424
 
  bool logout_called = false;
425
 
 
426
 
  logind_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant* par) -> GVariant* {
427
 
    if (method == "TerminateSession")
428
 
    {
429
 
      logout_called = true;
430
 
      EXPECT_EQ(Variant(par).GetString(), "logind-id0");
431
 
    }
432
 
 
433
 
    return nullptr;
434
 
  });
435
 
 
436
 
  manager->Logout();
437
 
 
438
 
  Utils::WaitUntilMSec(logout_called);
439
 
  EXPECT_TRUE(logout_called);
440
 
}
441
 
 
442
 
TEST_F(TestGnomeSessionManager, LogoutFallbackConsolekit)
443
 
{
444
 
  // This makes the standard call to return an error.
445
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
446
 
  // disable logind
447
 
  logind_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
448
 
 
449
 
  g_setenv("XDG_SESSION_COOKIE", "ck-session-cookie", TRUE);
450
 
  g_setenv("XDG_SESSION_ID", "logind-id0", TRUE);
451
 
 
452
 
  bool logout_called = false;
453
 
 
454
 
  console_kit_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant* par) -> GVariant* {
455
 
    if (method == "CloseSession")
456
 
    {
457
 
      logout_called = true;
458
 
      EXPECT_EQ(Variant(par).GetString(), "ck-session-cookie");
459
 
    }
460
 
 
461
 
    return nullptr;
462
 
  });
463
 
 
464
 
  manager->Logout();
465
 
 
466
 
  Utils::WaitUntilMSec(logout_called);
467
 
  EXPECT_TRUE(logout_called);
468
 
}
469
 
 
470
 
TEST_F(TestGnomeSessionManager, LogoutFallbackConsolekitOnNoID)
471
 
{
472
 
  // This makes the standard call to return an error.
473
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
474
 
  g_setenv("XDG_SESSION_COOKIE", "ck-session-cookie", TRUE);
475
 
  g_unsetenv("XDG_SESSION_ID");
476
 
 
477
 
  bool logout_called = false;
478
 
 
479
 
  console_kit_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant* par) -> GVariant* {
480
 
    if (method == "CloseSession")
481
 
    {
482
 
      logout_called = true;
483
 
      EXPECT_EQ(Variant(par).GetString(), "ck-session-cookie");
484
 
    }
485
 
 
486
 
    return nullptr;
487
 
  });
488
 
 
489
 
  manager->Logout();
490
 
 
491
 
  Utils::WaitUntilMSec(logout_called);
492
 
  EXPECT_TRUE(logout_called);
493
 
}
494
 
 
495
 
TEST_F(TestGnomeSessionManager, Reboot)
496
 
{
497
 
  bool reboot_called = false;
498
 
 
499
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
500
 
    if (method == "Reboot")
501
 
      reboot_called = true;
502
 
 
503
 
    return nullptr;
504
 
  });
505
 
 
506
 
  manager->Reboot();
507
 
 
508
 
  Utils::WaitUntilMSec(reboot_called);
509
 
  EXPECT_TRUE(reboot_called);
510
 
}
511
 
 
512
 
TEST_F(TestGnomeSessionManager, RebootFallbackLogind)
513
 
{
514
 
  // This makes the standard call to return an error.
515
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
516
 
 
517
 
  bool reboot_called = false;
518
 
 
519
 
  logind_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
520
 
    if (method == "Reboot")
521
 
      reboot_called = true;
522
 
 
523
 
    return nullptr;
524
 
  });
525
 
 
526
 
  manager->Reboot();
527
 
 
528
 
  Utils::WaitUntilMSec(reboot_called);
529
 
  EXPECT_TRUE(reboot_called);
530
 
}
531
 
 
532
 
TEST_F(TestGnomeSessionManager, RebootFallbackConsoleKit)
533
 
{
534
 
  // This makes the standard call to return an error.
535
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
536
 
  // disable logind
537
 
  logind_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
538
 
 
539
 
  bool reboot_called = false;
540
 
 
541
 
  console_kit_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
542
 
    if (method == "Restart")
543
 
      reboot_called = true;
544
 
 
545
 
    return nullptr;
546
 
  });
547
 
 
548
 
  manager->Reboot();
549
 
 
550
 
  Utils::WaitUntilMSec(reboot_called);
551
 
  EXPECT_TRUE(reboot_called);
552
 
}
553
 
 
554
 
TEST_F(TestGnomeSessionManager, Shutdown)
555
 
{
556
 
  bool shutdown_called = false;
557
 
 
558
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
559
 
    if (method == "Shutdown")
560
 
      shutdown_called = true;
561
 
 
562
 
    return nullptr;
563
 
  });
564
 
 
565
 
  manager->Shutdown();
566
 
 
567
 
  Utils::WaitUntilMSec(shutdown_called);
568
 
  EXPECT_TRUE(shutdown_called);
569
 
}
570
 
 
571
 
TEST_F(TestGnomeSessionManager, ShutdownFallbackLogind)
572
 
{
573
 
  // This makes the standard call to return an error.
574
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
575
 
 
576
 
  bool shutdown_called = false;
577
 
 
578
 
  logind_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
579
 
    if (method == "PowerOff")
580
 
      shutdown_called = true;
581
 
 
582
 
    return nullptr;
583
 
  });
584
 
 
585
 
  manager->Shutdown();
586
 
 
587
 
  Utils::WaitUntilMSec(shutdown_called);
588
 
  EXPECT_TRUE(shutdown_called);
589
 
}
590
 
 
591
 
TEST_F(TestGnomeSessionManager, ShutdownFallbackConsoleKit)
592
 
{
593
 
  // This makes the standard call to return an error.
594
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
595
 
  // disable logind
596
 
  logind_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
597
 
 
598
 
  bool shutdown_called = false;
599
 
 
600
 
  console_kit_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
601
 
    if (method == "Stop")
602
 
      shutdown_called = true;
603
 
 
604
 
    return nullptr;
605
 
  });
606
 
 
607
 
  manager->Shutdown();
608
 
 
609
 
  Utils::WaitUntilMSec(shutdown_called);
610
 
  EXPECT_TRUE(shutdown_called);
611
 
}
612
 
 
613
 
TEST_F(TestGnomeSessionManager, SuspendUPower)
614
 
{
615
 
  bool suspend_called = false;
616
 
 
617
 
  // disable logind
618
 
  logind_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
619
 
 
620
 
  upower_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
621
 
    if (method == "Suspend")
622
 
      suspend_called = true;
623
 
 
624
 
    return nullptr;
625
 
  });
626
 
 
627
 
  manager->Suspend();
628
 
 
629
 
  Utils::WaitUntilMSec(suspend_called);
630
 
  EXPECT_TRUE(suspend_called);
631
 
}
632
 
 
633
 
TEST_F(TestGnomeSessionManager, SuspendLogind)
634
 
{
635
 
  bool suspend_called = false;
636
 
 
637
 
  logind_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
638
 
    if (method == "Suspend")
639
 
      suspend_called = true;
640
 
 
641
 
    return nullptr;
642
 
  });
643
 
 
644
 
  manager->Suspend();
645
 
 
646
 
  Utils::WaitUntilMSec(suspend_called);
647
 
  EXPECT_TRUE(suspend_called);
648
 
}
649
 
 
650
 
TEST_F(TestGnomeSessionManager, HibernateUPower)
651
 
{
652
 
  bool hibernate_called = false;
653
 
 
654
 
  // disable logind
655
 
  logind_->GetObjects().front()->SetMethodsCallsHandler(nullptr);
656
 
 
657
 
  upower_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
658
 
    if (method == "Hibernate")
659
 
      hibernate_called = true;
660
 
 
661
 
    return nullptr;
662
 
  });
663
 
 
664
 
  manager->Hibernate();
665
 
 
666
 
  Utils::WaitUntilMSec(hibernate_called);
667
 
  EXPECT_TRUE(hibernate_called);
668
 
}
669
 
 
670
 
TEST_F(TestGnomeSessionManager, HibernateLogind)
671
 
{
672
 
  bool hibernate_called = false;
673
 
 
674
 
  logind_->GetObjects().front()->SetMethodsCallsHandler([&] (std::string const& method, GVariant*) -> GVariant* {
675
 
    if (method == "Hibernate")
676
 
      hibernate_called = true;
677
 
 
678
 
    return nullptr;
679
 
  });
680
 
 
681
 
  manager->Hibernate();
682
 
 
683
 
  Utils::WaitUntilMSec(hibernate_called);
684
 
  EXPECT_TRUE(hibernate_called);
685
 
}
686
 
 
687
 
TEST_F(TestGnomeSessionManager, CancelAction)
688
 
{
689
 
  bool cancelled = false;
690
 
 
691
 
  shell_proxy_->Connect("Canceled", [&cancelled] (GVariant*) { cancelled = true; });
692
 
  manager->CancelAction();
693
 
 
694
 
  Utils::WaitUntilMSec(cancelled);
695
 
  EXPECT_TRUE(cancelled);
696
 
}
697
 
 
698
 
TEST_F(TestGnomeSessionManager, LogoutRequested)
699
 
{
700
 
  bool logout_requested = false;
701
 
  bool cancelled = false;
702
 
 
703
 
  manager->logout_requested.connect([&logout_requested] (bool inhibitors) {
704
 
    logout_requested = true;
705
 
    EXPECT_FALSE(inhibitors);
706
 
  });
707
 
 
708
 
  shell_proxy_->Connect("Canceled", [&cancelled] (GVariant*) { cancelled = true; });
709
 
 
710
 
  ShellOpenAction(Action::LOGOUT);
711
 
 
712
 
  Utils::WaitUntilMSec(logout_requested);
713
 
  EXPECT_TRUE(logout_requested);
714
 
 
715
 
  Utils::WaitUntilMSec(cancelled);
716
 
  EXPECT_TRUE(cancelled);
717
 
}
718
 
 
719
 
struct InteractiveMode : TestGnomeSessionManager, testing::WithParamInterface<bool> {};
720
 
INSTANTIATE_TEST_CASE_P(TestGnomeSessionManager, InteractiveMode, testing::Bool());
721
 
 
722
 
TEST_P(/*TestGnomeSessionManager*/InteractiveMode, LogoutRequestedInhibitors)
723
 
{
724
 
  EnableInteractiveShutdown(GetParam());
725
 
 
726
 
  bool logout_requested = false;
727
 
  bool cancelled = false;
728
 
 
729
 
  manager->logout_requested.connect([&logout_requested] (bool inhibitors) {
730
 
    logout_requested = true;
731
 
    EXPECT_TRUE(inhibitors);
732
 
  });
733
 
 
734
 
  shell_proxy_->Connect("Canceled", [&cancelled] (GVariant*) { cancelled = true; });
735
 
 
736
 
  ShellOpenActionWithInhibitor(Action::LOGOUT);
737
 
 
738
 
  Utils::WaitUntilMSec(logout_requested);
739
 
  EXPECT_TRUE(logout_requested);
740
 
 
741
 
  Utils::WaitForTimeoutMSec(10);
742
 
  EXPECT_FALSE(cancelled);
743
 
}
744
 
 
745
 
TEST_F(TestGnomeSessionManager, ImmediateLogout)
746
 
{
747
 
  EnableInteractiveShutdown(false);
748
 
  bool logout_requested = false;
749
 
  bool confirmed = false;
750
 
  bool closed = false;
751
 
 
752
 
  manager->logout_requested.connect([&logout_requested] (bool inhibitors) {
753
 
    logout_requested = true;
754
 
    EXPECT_FALSE(inhibitors);
755
 
  });
756
 
 
757
 
  shell_proxy_->Connect("ConfirmedLogout", [&confirmed] (GVariant*) { confirmed = true; });
758
 
  shell_proxy_->Connect("Closed", [&closed] (GVariant*) { closed = true; });
759
 
 
760
 
  ShellOpenAction(Action::LOGOUT);
761
 
 
762
 
  Utils::WaitForTimeoutMSec(100);
763
 
  EXPECT_FALSE(logout_requested);
764
 
 
765
 
  Utils::WaitUntilMSec(confirmed);
766
 
  EXPECT_TRUE(confirmed);
767
 
 
768
 
  Utils::WaitUntilMSec(closed);
769
 
  EXPECT_TRUE(closed);
770
 
}
771
 
 
772
 
TEST_F(TestGnomeSessionManager, SimulateRealLogout)
773
 
{
774
 
  bool confirmed = false;
775
 
  bool closed = false;
776
 
 
777
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler([this] (std::string const& method, GVariant*) -> GVariant* {
778
 
    if (method == "Logout")
779
 
      ShellOpenAction(Action::LOGOUT);
780
 
 
781
 
    return nullptr;
782
 
  });
783
 
 
784
 
  manager->Logout();
785
 
 
786
 
  shell_proxy_->Connect("ConfirmedLogout", [&confirmed] (GVariant*) { confirmed = true; });
787
 
  shell_proxy_->Connect("Closed", [&closed] (GVariant*) { closed = true; });
788
 
 
789
 
  Utils::WaitUntilMSec(confirmed);
790
 
  EXPECT_TRUE(confirmed);
791
 
 
792
 
  Utils::WaitUntilMSec(closed);
793
 
  EXPECT_TRUE(closed);
794
 
}
795
 
 
796
 
TEST_F(TestGnomeSessionManager, ShutdownRequested)
797
 
{
798
 
  bool shutdown_requested = false;
799
 
  bool cancelled = false;
800
 
 
801
 
  manager->shutdown_requested.connect([&shutdown_requested] (bool inhibitors) {
802
 
    shutdown_requested = true;
803
 
    EXPECT_FALSE(inhibitors);
804
 
  });
805
 
 
806
 
  shell_proxy_->Connect("Canceled", [&cancelled] (GVariant*) { cancelled = true; });
807
 
 
808
 
  ShellOpenAction(Action::SHUTDOWN);
809
 
 
810
 
  Utils::WaitUntilMSec(shutdown_requested);
811
 
  EXPECT_TRUE(shutdown_requested);
812
 
 
813
 
  Utils::WaitUntilMSec(cancelled);
814
 
  EXPECT_TRUE(cancelled);
815
 
}
816
 
 
817
 
TEST_P(/*TestGnomeSessionManager*/InteractiveMode, ShutdownRequestedInhibitors)
818
 
{
819
 
  bool shutdown_requested = false;
820
 
  bool cancelled = false;
821
 
 
822
 
  manager->shutdown_requested.connect([&shutdown_requested] (bool inhibitors) {
823
 
    shutdown_requested = true;
824
 
    EXPECT_TRUE(inhibitors);
825
 
  });
826
 
 
827
 
  shell_proxy_->Connect("Canceled", [&cancelled] (GVariant*) { cancelled = true; });
828
 
 
829
 
  ShellOpenActionWithInhibitor(Action::SHUTDOWN);
830
 
 
831
 
  Utils::WaitUntilMSec(shutdown_requested);
832
 
  EXPECT_TRUE(shutdown_requested);
833
 
 
834
 
  Utils::WaitForTimeoutMSec(10);
835
 
  EXPECT_FALSE(cancelled);
836
 
}
837
 
 
838
 
TEST_F(TestGnomeSessionManager, ImmediateShutdown)
839
 
{
840
 
  EnableInteractiveShutdown(false);
841
 
  bool shutdown_requested = false;
842
 
  bool confirmed = false;
843
 
  bool closed = false;
844
 
 
845
 
  manager->shutdown_requested.connect([&shutdown_requested] (bool inhibitors) {
846
 
    shutdown_requested = true;
847
 
    EXPECT_FALSE(inhibitors);
848
 
  });
849
 
 
850
 
  shell_proxy_->Connect("ConfirmedShutdown", [&confirmed] (GVariant*) { confirmed = true; });
851
 
  shell_proxy_->Connect("Closed", [&closed] (GVariant*) { closed = true; });
852
 
 
853
 
  ShellOpenAction(Action::SHUTDOWN);
854
 
 
855
 
  Utils::WaitForTimeoutMSec(100);
856
 
  EXPECT_FALSE(shutdown_requested);
857
 
 
858
 
  Utils::WaitUntilMSec(confirmed);
859
 
  EXPECT_TRUE(confirmed);
860
 
 
861
 
  Utils::WaitUntilMSec(closed);
862
 
  EXPECT_TRUE(closed);
863
 
}
864
 
 
865
 
TEST_F(TestGnomeSessionManager, SimulateRealShutdown)
866
 
{
867
 
  bool confirmed = false;
868
 
  bool closed = false;
869
 
 
870
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler([this] (std::string const& method, GVariant*) -> GVariant* {
871
 
    if (method == "Shutdown")
872
 
      ShellOpenAction(Action::SHUTDOWN);
873
 
 
874
 
    return nullptr;
875
 
  });
876
 
 
877
 
  manager->Shutdown();
878
 
 
879
 
  shell_proxy_->Connect("ConfirmedShutdown", [&confirmed] (GVariant*) { confirmed = true; });
880
 
  shell_proxy_->Connect("Closed", [&closed] (GVariant*) { closed = true; });
881
 
 
882
 
  Utils::WaitUntilMSec(confirmed);
883
 
  EXPECT_TRUE(confirmed);
884
 
 
885
 
  Utils::WaitUntilMSec(closed);
886
 
  EXPECT_TRUE(closed);
887
 
}
888
 
 
889
 
TEST_F(TestGnomeSessionManager, RebootRequested)
890
 
{
891
 
  bool reboot_requested = false;
892
 
  bool cancelled = false;
893
 
 
894
 
  manager->reboot_requested.connect([&reboot_requested] (bool inhibitors) {
895
 
    reboot_requested = true;
896
 
    EXPECT_FALSE(inhibitors);
897
 
  });
898
 
 
899
 
  shell_proxy_->Connect("Canceled", [&cancelled] (GVariant*) { cancelled = true; });
900
 
 
901
 
  ShellOpenAction(Action::REBOOT);
902
 
 
903
 
  Utils::WaitUntilMSec(reboot_requested);
904
 
  EXPECT_TRUE(reboot_requested);
905
 
 
906
 
  Utils::WaitUntilMSec(cancelled);
907
 
  EXPECT_TRUE(cancelled);
908
 
}
909
 
 
910
 
TEST_P(/*TestGnomeSessionManager*/InteractiveMode, RebootRequestedInhibitors)
911
 
{
912
 
  bool reboot_requested = false;
913
 
  bool cancelled = false;
914
 
 
915
 
  manager->reboot_requested.connect([&reboot_requested] (bool inhibitors) {
916
 
    reboot_requested = true;
917
 
    EXPECT_TRUE(inhibitors);
918
 
  });
919
 
 
920
 
  shell_proxy_->Connect("Canceled", [&cancelled] (GVariant*) { cancelled = true; });
921
 
 
922
 
  ShellOpenActionWithInhibitor(Action::REBOOT);
923
 
 
924
 
  Utils::WaitUntilMSec(reboot_requested);
925
 
  EXPECT_TRUE(reboot_requested);
926
 
 
927
 
  Utils::WaitForTimeoutMSec(10);
928
 
  EXPECT_FALSE(cancelled);
929
 
}
930
 
 
931
 
TEST_F(TestGnomeSessionManager, ImmediateReboot)
932
 
{
933
 
  EnableInteractiveShutdown(false);
934
 
  bool reboot_requested = false;
935
 
  bool confirmed = false;
936
 
  bool closed = false;
937
 
 
938
 
  manager->reboot_requested.connect([&reboot_requested] (bool inhibitors) {
939
 
    reboot_requested = true;
940
 
    EXPECT_FALSE(inhibitors);
941
 
  });
942
 
 
943
 
  shell_proxy_->Connect("ConfirmedReboot", [&confirmed] (GVariant*) { confirmed = true; });
944
 
  shell_proxy_->Connect("Closed", [&closed] (GVariant*) { closed = true; });
945
 
 
946
 
  ShellOpenAction(Action::REBOOT);
947
 
 
948
 
  Utils::WaitForTimeoutMSec(100);
949
 
  EXPECT_FALSE(reboot_requested);
950
 
 
951
 
  Utils::WaitUntilMSec(confirmed);
952
 
  EXPECT_TRUE(confirmed);
953
 
 
954
 
  Utils::WaitUntilMSec(closed);
955
 
  EXPECT_TRUE(closed);
956
 
}
957
 
 
958
 
TEST_F(TestGnomeSessionManager, SimulateRealReboot)
959
 
{
960
 
  bool confirmed = false;
961
 
  bool closed = false;
962
 
 
963
 
  session_manager_->GetObjects().front()->SetMethodsCallsHandler([this] (std::string const& method, GVariant*) -> GVariant* {
964
 
    if (method == "Reboot")
965
 
      ShellOpenAction(Action::REBOOT);
966
 
 
967
 
    return nullptr;
968
 
  });
969
 
 
970
 
  manager->Reboot();
971
 
 
972
 
  shell_proxy_->Connect("ConfirmedReboot", [&confirmed] (GVariant*) { confirmed = true; });
973
 
  shell_proxy_->Connect("Closed", [&closed] (GVariant*) { closed = true; });
974
 
 
975
 
  Utils::WaitUntilMSec(confirmed);
976
 
  EXPECT_TRUE(confirmed);
977
 
 
978
 
  Utils::WaitUntilMSec(closed);
979
 
  EXPECT_TRUE(closed);
980
 
}
981
 
 
982
 
TEST_F(TestGnomeSessionManager, CancelRequested)
983
 
{
984
 
  bool cancel_requested = false;
985
 
  bool closed = false;
986
 
 
987
 
  manager->cancel_requested.connect([&cancel_requested] { cancel_requested = true; });
988
 
  shell_proxy_->Connect("Closed", [&closed] (GVariant*) { closed = true; });
989
 
 
990
 
  shell_proxy_->Call("Close");
991
 
 
992
 
  Utils::WaitUntilMSec(cancel_requested);
993
 
  EXPECT_TRUE(cancel_requested);
994
 
 
995
 
  Utils::WaitUntilMSec(closed);
996
 
  EXPECT_TRUE(closed);
997
 
}
998
 
 
999
 
} // Namespace