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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
ash / system / session / logout_confirmation_controller_unittest.cc [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/session/logout_confirmation_controller.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/session/session_controller_impl.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/test_window_builder.h"
#include "ash/wm/desks/desks_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/time/tick_clock.h"
#include "components/prefs/pref_service.h"
#include "components/session_manager/session_manager_types.h"
#include "components/user_manager/user_type.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/window.h"
#include "ui/views/widget/widget.h"
namespace ash {
constexpr char kUserEmail[] = "user1@test.com";
class LogoutConfirmationControllerTest : public testing::Test {
public:
LogoutConfirmationControllerTest(const LogoutConfirmationControllerTest&) =
delete;
LogoutConfirmationControllerTest& operator=(
const LogoutConfirmationControllerTest&) = delete;
protected:
LogoutConfirmationControllerTest();
~LogoutConfirmationControllerTest() override;
void LogOut(LogoutConfirmationController::Source source);
bool log_out_called_;
scoped_refptr<base::TestMockTimeTaskRunner> runner_;
base::SingleThreadTaskRunner::CurrentDefaultHandle
runner_current_default_handle_;
LogoutConfirmationController controller_;
};
LogoutConfirmationControllerTest::LogoutConfirmationControllerTest()
: log_out_called_(false),
runner_(new base::TestMockTimeTaskRunner),
runner_current_default_handle_(runner_) {
controller_.SetClockForTesting(runner_->GetMockTickClock());
controller_.SetLogoutCallbackForTesting(base::BindRepeating(
&LogoutConfirmationControllerTest::LogOut, base::Unretained(this)));
}
LogoutConfirmationControllerTest::~LogoutConfirmationControllerTest() = default;
void LogoutConfirmationControllerTest::LogOut(
LogoutConfirmationController::Source source) {
log_out_called_ = true;
}
// Verifies that the user is logged out immediately if logout confirmation with
// a zero-length countdown is requested.
TEST_F(LogoutConfirmationControllerTest, ZeroDuration) {
controller_.ConfirmLogout(
runner_->NowTicks(),
LogoutConfirmationController::Source::kShelfExitButton);
EXPECT_FALSE(log_out_called_);
runner_->FastForwardBy(base::TimeDelta());
EXPECT_TRUE(log_out_called_);
}
// Verifies that the user is logged out when the countdown expires.
TEST_F(LogoutConfirmationControllerTest, DurationExpired) {
controller_.ConfirmLogout(
runner_->NowTicks() + base::Seconds(10),
LogoutConfirmationController::Source::kShelfExitButton);
EXPECT_FALSE(log_out_called_);
runner_->FastForwardBy(base::Seconds(9));
EXPECT_FALSE(log_out_called_);
runner_->FastForwardBy(base::Seconds(2));
EXPECT_TRUE(log_out_called_);
}
// Verifies that when a second request to confirm logout is made and the second
// request's countdown ends before the original request's, the user is logged
// out when the new countdown expires.
TEST_F(LogoutConfirmationControllerTest, DurationShortened) {
controller_.ConfirmLogout(
runner_->NowTicks() + base::Seconds(30),
LogoutConfirmationController::Source::kShelfExitButton);
EXPECT_FALSE(log_out_called_);
runner_->FastForwardBy(base::Seconds(9));
EXPECT_FALSE(log_out_called_);
controller_.ConfirmLogout(
runner_->NowTicks() + base::Seconds(10),
LogoutConfirmationController::Source::kShelfExitButton);
runner_->FastForwardBy(base::Seconds(9));
EXPECT_FALSE(log_out_called_);
runner_->FastForwardBy(base::Seconds(2));
EXPECT_TRUE(log_out_called_);
}
// Verifies that when a second request to confirm logout is made and the second
// request's countdown ends after the original request's, the user is logged
// out when the original countdown expires.
TEST_F(LogoutConfirmationControllerTest, DurationExtended) {
controller_.ConfirmLogout(
runner_->NowTicks() + base::Seconds(10),
LogoutConfirmationController::Source::kShelfExitButton);
EXPECT_FALSE(log_out_called_);
runner_->FastForwardBy(base::Seconds(9));
EXPECT_FALSE(log_out_called_);
controller_.ConfirmLogout(
runner_->NowTicks() + base::Seconds(10),
LogoutConfirmationController::Source::kShelfExitButton);
runner_->FastForwardBy(base::Seconds(2));
EXPECT_TRUE(log_out_called_);
}
// Verifies that when the screen is locked while the countdown is running, the
// user is not logged out, even when the original countdown expires.
TEST_F(LogoutConfirmationControllerTest, Lock) {
controller_.ConfirmLogout(
runner_->NowTicks() + base::Seconds(10),
LogoutConfirmationController::Source::kShelfExitButton);
EXPECT_FALSE(log_out_called_);
controller_.OnLockStateChanged(true);
runner_->FastForwardUntilNoTasksRemain();
EXPECT_FALSE(log_out_called_);
}
// Verifies that when the user confirms the logout request, the user is logged
// out immediately.
TEST_F(LogoutConfirmationControllerTest, UserAccepted) {
controller_.ConfirmLogout(
runner_->NowTicks() + base::Seconds(10),
LogoutConfirmationController::Source::kShelfExitButton);
EXPECT_FALSE(log_out_called_);
controller_.OnLogoutConfirmed();
EXPECT_TRUE(log_out_called_);
}
// Verifies that when the user denies the logout request, the user is not logged
// out, even when the original countdown expires.
TEST_F(LogoutConfirmationControllerTest, UserDenied) {
controller_.ConfirmLogout(
runner_->NowTicks() + base::Seconds(10),
LogoutConfirmationController::Source::kShelfExitButton);
EXPECT_FALSE(log_out_called_);
controller_.OnDialogClosed();
runner_->FastForwardUntilNoTasksRemain();
EXPECT_FALSE(log_out_called_);
}
// Verifies that after the user has denied a logout request, a subsequent logout
// request is handled correctly and the user is logged out when the countdown
// expires.
TEST_F(LogoutConfirmationControllerTest, DurationExpiredAfterDeniedRequest) {
controller_.ConfirmLogout(
runner_->NowTicks() + base::Seconds(10),
LogoutConfirmationController::Source::kShelfExitButton);
EXPECT_FALSE(log_out_called_);
controller_.OnDialogClosed();
runner_->FastForwardUntilNoTasksRemain();
EXPECT_FALSE(log_out_called_);
controller_.ConfirmLogout(
runner_->NowTicks() + base::Seconds(10),
LogoutConfirmationController::Source::kShelfExitButton);
EXPECT_FALSE(log_out_called_);
runner_->FastForwardBy(base::Seconds(9));
EXPECT_FALSE(log_out_called_);
runner_->FastForwardBy(base::Seconds(2));
EXPECT_TRUE(log_out_called_);
}
class LastWindowClosedTest : public NoSessionAshTestBase {
public:
LastWindowClosedTest() = default;
LastWindowClosedTest(const LastWindowClosedTest&) = delete;
LastWindowClosedTest& operator=(const LastWindowClosedTest&) = delete;
~LastWindowClosedTest() override = default;
// Simulate a managed guest session (non-demo session) login.
void StartManagedGuestSession() {
TestSessionControllerClient* session = GetSessionControllerClient();
session->Reset();
session->AddUserSession(kUserEmail, user_manager::UserType::kPublicAccount);
session->SetSessionState(session_manager::SessionState::ACTIVE);
}
// Simulate a demo session signing in.
void StartDemoSession() {
GetSessionControllerClient()->SetIsDemoSession();
// Demo session is implemented as a managed guest session.
StartManagedGuestSession();
}
// PrefService for the managed guest session started by
// StartManagedGuestSession().
PrefService* pref_service() {
return Shell::Get()->session_controller()->GetUserPrefServiceForUser(
AccountId::FromUserEmail(kUserEmail));
}
};
TEST_F(LastWindowClosedTest, RegularSession) {
// Dialog is not visible at startup.
LogoutConfirmationController* controller =
Shell::Get()->logout_confirmation_controller();
EXPECT_FALSE(controller->dialog_for_testing());
// Dialog is not visible after login.
SimulateUserLogin(kDefaultUserEmail);
EXPECT_FALSE(controller->dialog_for_testing());
// Creating and closing a window does not show the dialog because this is not
// a managed guest session.
std::unique_ptr<aura::Window> window = CreateToplevelTestWindow();
EXPECT_FALSE(controller->dialog_for_testing());
window.reset();
EXPECT_FALSE(controller->dialog_for_testing());
}
TEST_F(LastWindowClosedTest, DemoSession) {
// Dialog is not visible at startup.
LogoutConfirmationController* controller =
Shell::Get()->logout_confirmation_controller();
EXPECT_FALSE(controller->dialog_for_testing());
// Dialog is not visible after demo session starts.
StartDemoSession();
EXPECT_FALSE(controller->dialog_for_testing());
// Creating and closing a window does not show the dialog.
std::unique_ptr<aura::Window> window = CreateToplevelTestWindow();
EXPECT_FALSE(controller->dialog_for_testing());
window.reset();
EXPECT_FALSE(controller->dialog_for_testing());
}
TEST_F(LastWindowClosedTest, ManagedGuestSession) {
LogoutConfirmationController* controller =
Shell::Get()->logout_confirmation_controller();
// Dialog is not visible after managed guest session login.
StartManagedGuestSession();
EXPECT_FALSE(controller->dialog_for_testing());
// Opening windows does not show the dialog.
std::unique_ptr<views::Widget> widget1 =
CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
std::unique_ptr<views::Widget> widget2 =
CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
EXPECT_FALSE(controller->dialog_for_testing());
// Closing the last window shows the dialog.
widget1.reset();
EXPECT_FALSE(controller->dialog_for_testing());
widget2.reset();
EXPECT_TRUE(controller->dialog_for_testing());
}
TEST_F(LastWindowClosedTest, SuggestLogoutAfterClosingLastWindowPolicy) {
LogoutConfirmationController* controller =
Shell::Get()->logout_confirmation_controller();
// Dialog is not visible after managed guest session login.
StartManagedGuestSession();
pref_service()->SetBoolean(prefs::kSuggestLogoutAfterClosingLastWindow,
false);
EXPECT_FALSE(controller->dialog_for_testing());
// Opening windows does not show the dialog.
std::unique_ptr<views::Widget> widget1 =
CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
std::unique_ptr<views::Widget> widget2 =
CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
EXPECT_FALSE(controller->dialog_for_testing());
// Closing the last window does not show the dialog because the
// kSuggestLogoutAfterClosingLastWindow is set to false.
widget1.reset();
EXPECT_FALSE(controller->dialog_for_testing());
widget2.reset();
EXPECT_FALSE(controller->dialog_for_testing());
}
// Test ARC++ window hierarchy where window minimize, restore and go in/out full
// screen causes a window removing deep inside the top window hierarchy. Actions
// above should no cause logout timer and only closing the last top window
// triggers the logout timer.
TEST_F(LastWindowClosedTest, ManagedGuestSessionComplexHierarchy) {
LogoutConfirmationController* controller =
Shell::Get()->logout_confirmation_controller();
StartManagedGuestSession();
EXPECT_FALSE(controller->dialog_for_testing());
std::unique_ptr<aura::Window> window = CreateToplevelTestWindow();
EXPECT_FALSE(controller->dialog_for_testing());
std::unique_ptr<aura::Window> window_child =
ChildTestWindowBuilder(window.get()).Build();
EXPECT_FALSE(controller->dialog_for_testing());
window_child.reset();
EXPECT_FALSE(controller->dialog_for_testing());
window.reset();
EXPECT_TRUE(controller->dialog_for_testing());
}
TEST_F(LastWindowClosedTest, AlwaysOnTop) {
LogoutConfirmationController* controller =
Shell::Get()->logout_confirmation_controller();
StartManagedGuestSession();
// The new widget starts in the default window container.
std::unique_ptr<views::Widget> widget =
CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
// Moving the widget to the always-on-top container does not trigger the
// dialog because the window didn't close.
widget->SetZOrderLevel(ui::ZOrderLevel::kFloatingWindow);
EXPECT_FALSE(controller->dialog_for_testing());
// Closing the window triggers the dialog.
widget.reset();
EXPECT_TRUE(controller->dialog_for_testing());
}
TEST_F(LastWindowClosedTest, MultipleContainers) {
LogoutConfirmationController* controller =
Shell::Get()->logout_confirmation_controller();
StartManagedGuestSession();
// Create two windows in different containers.
std::unique_ptr<views::Widget> normal_widget =
CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
std::unique_ptr<views::Widget> always_on_top_widget =
CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
always_on_top_widget->SetZOrderLevel(ui::ZOrderLevel::kFloatingWindow);
// Closing the last window shows the dialog.
always_on_top_widget.reset();
EXPECT_FALSE(controller->dialog_for_testing());
normal_widget.reset();
EXPECT_TRUE(controller->dialog_for_testing());
}
TEST_F(LastWindowClosedTest, MultipleDisplays) {
LogoutConfirmationController* controller =
Shell::Get()->logout_confirmation_controller();
StartManagedGuestSession();
// Create two displays, each with a window.
UpdateDisplay("1024x768,800x600");
std::unique_ptr<aura::Window> window1 =
ChildTestWindowBuilder(Shell::GetAllRootWindows()[0]->GetChildById(
desks_util::GetActiveDeskContainerId()))
.Build();
std::unique_ptr<aura::Window> window2 =
ChildTestWindowBuilder(Shell::GetAllRootWindows()[1]->GetChildById(
desks_util::GetActiveDeskContainerId()))
.Build();
// Closing the last window shows the dialog.
window1.reset();
EXPECT_FALSE(controller->dialog_for_testing());
window2.reset();
EXPECT_TRUE(controller->dialog_for_testing());
}
} // namespace ash