forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathash_test_base.cc
517 lines (441 loc) · 17.8 KB
/
ash_test_base.cc
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/test/ash_test_base.h"
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "ash/accessibility/ui/accessibility_panel_layout_manager.h"
#include "ash/ambient/test/ambient_ash_test_helper.h"
#include "ash/app_list/test/app_list_test_helper.h"
#include "ash/display/extended_mouse_warp_controller.h"
#include "ash/display/mouse_cursor_event_filter.h"
#include "ash/display/screen_orientation_controller_test_api.h"
#include "ash/display/unified_mouse_warp_controller.h"
#include "ash/display/window_tree_host_manager.h"
#include "ash/keyboard/keyboard_controller_impl.h"
#include "ash/public/cpp/ash_prefs.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/system/status_area_widget.h"
#include "ash/test/ash_test_helper.h"
#include "ash/test/test_widget_builder.h"
#include "ash/test/test_window_builder.h"
#include "ash/test_shell_delegate.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ash/wm/window_positioner.h"
#include "ash/wm/work_area_insets.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/user_names.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/window_parenting_client.h"
#include "ui/aura/env.h"
#include "ui/aura/test/aura_test_utils.h"
#include "ui/aura/test/event_generator_delegate_aura.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ime/init/input_method_initializer.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/display/types/display_constants.h"
#include "ui/events/devices/device_data_manager.h"
#include "ui/events/devices/touchscreen_device.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/wm/core/coordinate_conversion.h"
using session_manager::SessionState;
namespace ash {
namespace {
class AshEventGeneratorDelegate
: public aura::test::EventGeneratorDelegateAura {
public:
AshEventGeneratorDelegate() = default;
~AshEventGeneratorDelegate() override = default;
// aura::test::EventGeneratorDelegateAura overrides:
ui::EventTarget* GetTargetAt(const gfx::Point& point_in_screen) override {
display::Screen* screen = display::Screen::GetScreen();
display::Display display = screen->GetDisplayNearestPoint(point_in_screen);
return Shell::GetRootWindowForDisplayId(display.id())->GetHost()->window();
}
private:
DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
};
} // namespace
/////////////////////////////////////////////////////////////////////////////
AshTestBase::AshTestBase(
std::unique_ptr<base::test::TaskEnvironment> task_environment)
: task_environment_(std::move(task_environment)) {
RegisterLocalStatePrefs(local_state_.registry(), true);
}
AshTestBase::~AshTestBase() {
CHECK(setup_called_)
<< "You have overridden SetUp but never called AshTestBase::SetUp";
CHECK(teardown_called_)
<< "You have overridden TearDown but never called AshTestBase::TearDown";
}
void AshTestBase::SetUp() {
SetUp(nullptr);
}
void AshTestBase::SetUp(std::unique_ptr<TestShellDelegate> delegate) {
// At this point, the task APIs should already be provided by
// |task_environment_|.
CHECK(base::ThreadTaskRunnerHandle::IsSet());
CHECK(base::ThreadPoolInstance::Get());
setup_called_ = true;
AshTestHelper::InitParams params;
params.start_session = start_session_;
params.delegate = std::move(delegate);
params.local_state = local_state();
ash_test_helper_ = std::make_unique<AshTestHelper>();
ash_test_helper_->SetUp(std::move(params));
}
void AshTestBase::TearDown() {
teardown_called_ = true;
// Make sure that we can exit tablet mode before shutdown correctly.
Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
Shell::Get()->session_controller()->NotifyChromeTerminating();
// Flush the message loop to finish pending release tasks.
base::RunLoop().RunUntilIdle();
ash_test_helper_->TearDown();
event_generator_.reset();
// Some tests set an internal display id,
// reset it here, so other tests will continue in a clean environment.
display::Display::SetInternalDisplayId(display::kInvalidDisplayId);
// Tests can add devices, so reset the lists for future tests.
ui::DeviceDataManager::GetInstance()->ResetDeviceListsForTest();
}
// static
Shelf* AshTestBase::GetPrimaryShelf() {
return Shell::GetPrimaryRootWindowController()->shelf();
}
// static
UnifiedSystemTray* AshTestBase::GetPrimaryUnifiedSystemTray() {
return GetPrimaryShelf()->GetStatusAreaWidget()->unified_system_tray();
}
// static
WorkAreaInsets* AshTestBase::GetPrimaryWorkAreaInsets() {
return Shell::GetPrimaryRootWindowController()->work_area_insets();
}
ui::test::EventGenerator* AshTestBase::GetEventGenerator() {
if (!event_generator_) {
event_generator_ = std::make_unique<ui::test::EventGenerator>(
std::make_unique<AshEventGeneratorDelegate>());
}
return event_generator_.get();
}
// static
display::Display::Rotation AshTestBase::GetActiveDisplayRotation(int64_t id) {
return Shell::Get()
->display_manager()
->GetDisplayInfo(id)
.GetActiveRotation();
}
// static
display::Display::Rotation AshTestBase::GetCurrentInternalDisplayRotation() {
return GetActiveDisplayRotation(display::Display::InternalDisplayId());
}
void AshTestBase::UpdateDisplay(const std::string& display_specs) {
display::test::DisplayManagerTestApi(Shell::Get()->display_manager())
.UpdateDisplay(display_specs);
ScreenOrientationControllerTestApi(
Shell::Get()->screen_orientation_controller())
.UpdateNaturalOrientation();
}
aura::Window* AshTestBase::GetContext() {
return ash_test_helper_->GetContext();
}
// static
std::unique_ptr<views::Widget> AshTestBase::CreateTestWidget(
views::WidgetDelegate* delegate,
int container_id,
const gfx::Rect& bounds,
bool show) {
return TestWidgetBuilder()
.SetDelegate(delegate)
.SetBounds(bounds)
.SetParent(Shell::GetPrimaryRootWindow()->GetChildById(container_id))
.SetShow(show)
.BuildOwnsNativeWidget();
}
// static
std::unique_ptr<views::Widget> AshTestBase::CreateFramelessTestWidget() {
return TestWidgetBuilder()
.SetWidgetType(views::Widget::InitParams::TYPE_WINDOW_FRAMELESS)
.BuildOwnsNativeWidget();
}
std::unique_ptr<aura::Window> AshTestBase::CreateAppWindow(
const gfx::Rect& bounds_in_screen,
AppType app_type,
int shell_window_id) {
TestWidgetBuilder builder;
if (app_type != AppType::NON_APP) {
builder.SetWindowProperty(aura::client::kAppType,
static_cast<int>(app_type));
}
// |widget| is configured to be owned by the underlying window.
views::Widget* widget =
builder.SetTestWidgetDelegate()
.SetBounds(bounds_in_screen.IsEmpty() ? gfx::Rect(0, 0, 300, 300)
: bounds_in_screen)
.SetContext(Shell::GetPrimaryRootWindow())
.SetWindowId(shell_window_id)
.BuildOwnedByNativeWidget();
return base::WrapUnique(widget->GetNativeWindow());
}
std::unique_ptr<aura::Window> AshTestBase::CreateTestWindow(
const gfx::Rect& bounds_in_screen,
aura::client::WindowType type,
int shell_window_id) {
if (type != aura::client::WINDOW_TYPE_NORMAL) {
return base::WrapUnique(CreateTestWindowInShellWithDelegateAndType(
nullptr, type, shell_window_id, bounds_in_screen));
}
return CreateAppWindow(bounds_in_screen, AppType::NON_APP, shell_window_id);
}
std::unique_ptr<aura::Window> AshTestBase::CreateToplevelTestWindow(
const gfx::Rect& bounds_in_screen,
int shell_window_id) {
aura::test::TestWindowDelegate* delegate =
aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate();
return base::WrapUnique<aura::Window>(
CreateTestWindowInShellWithDelegateAndType(
delegate, aura::client::WINDOW_TYPE_NORMAL, shell_window_id,
bounds_in_screen));
}
aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
}
aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
const gfx::Rect& bounds) {
return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
}
aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
aura::WindowDelegate* delegate,
int id,
const gfx::Rect& bounds) {
return CreateTestWindowInShellWithDelegateAndType(
delegate, aura::client::WINDOW_TYPE_NORMAL, id, bounds);
}
aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
aura::WindowDelegate* delegate,
aura::client::WindowType type,
int id,
const gfx::Rect& bounds) {
return TestWindowBuilder()
.SetBounds(bounds)
.SetDelegate(delegate)
.SetWindowType(type)
.SetWindowId(id)
.AllowAllWindowStates()
.Build()
.release();
}
void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
aura::client::ParentWindowWithContext(window, Shell::GetPrimaryRootWindow(),
gfx::Rect());
}
void AshTestBase::SetUserPref(const std::string& user_email,
const std::string& path,
const base::Value& value) {
AccountId accountId = AccountId::FromUserEmail(user_email);
PrefService* prefs =
GetSessionControllerClient()->GetUserPrefService(accountId);
prefs->Set(path, value);
}
TestSessionControllerClient* AshTestBase::GetSessionControllerClient() {
return ash_test_helper_->test_session_controller_client();
}
TestSystemTrayClient* AshTestBase::GetSystemTrayClient() {
return ash_test_helper_->system_tray_client();
}
AppListTestHelper* AshTestBase::GetAppListTestHelper() {
return ash_test_helper_->app_list_test_helper();
}
AmbientAshTestHelper* AshTestBase::GetAmbientAshTestHelper() {
return ash_test_helper_->ambient_ash_test_helper();
}
void AshTestBase::CreateUserSessions(int n) {
GetSessionControllerClient()->CreatePredefinedUserSessions(n);
}
void AshTestBase::SimulateUserLogin(const std::string& user_email,
user_manager::UserType user_type) {
TestSessionControllerClient* session = GetSessionControllerClient();
session->AddUserSession(user_email, user_type);
session->SwitchActiveUser(AccountId::FromUserEmail(user_email));
session->SetSessionState(SessionState::ACTIVE);
}
void AshTestBase::SimulateNewUserFirstLogin(const std::string& user_email) {
TestSessionControllerClient* session = GetSessionControllerClient();
session->AddUserSession(user_email, user_manager::USER_TYPE_REGULAR,
true /* provide_pref_service */,
true /* is_new_profile */);
session->SwitchActiveUser(AccountId::FromUserEmail(user_email));
session->SetSessionState(session_manager::SessionState::ACTIVE);
}
void AshTestBase::SimulateGuestLogin() {
const std::string guest = user_manager::kGuestUserName;
TestSessionControllerClient* session = GetSessionControllerClient();
session->AddUserSession(guest, user_manager::USER_TYPE_GUEST);
session->SwitchActiveUser(AccountId::FromUserEmail(guest));
session->SetSessionState(SessionState::ACTIVE);
}
void AshTestBase::SimulateKioskMode(user_manager::UserType user_type) {
DCHECK(user_type == user_manager::USER_TYPE_ARC_KIOSK_APP ||
user_type == user_manager::USER_TYPE_KIOSK_APP);
const std::string user_email = "fake_kiosk@kioks-apps.device-local.localhost";
TestSessionControllerClient* session = GetSessionControllerClient();
session->SetIsRunningInAppMode(true);
session->AddUserSession(user_email, user_type);
session->SwitchActiveUser(AccountId::FromUserEmail(user_email));
session->SetSessionState(SessionState::ACTIVE);
}
void AshTestBase::SetAccessibilityPanelHeight(int panel_height) {
Shell::GetPrimaryRootWindowController()
->GetAccessibilityPanelLayoutManagerForTest()
->SetPanelBounds(gfx::Rect(0, 0, 0, panel_height),
AccessibilityPanelState::FULL_WIDTH);
}
void AshTestBase::ClearLogin() {
GetSessionControllerClient()->Reset();
}
void AshTestBase::SetCanLockScreen(bool can_lock) {
GetSessionControllerClient()->SetCanLockScreen(can_lock);
}
void AshTestBase::SetShouldLockScreenAutomatically(bool should_lock) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(should_lock);
}
void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
GetSessionControllerClient()->SetSessionState(
user_adding_screen_running ? SessionState::LOGIN_SECONDARY
: SessionState::ACTIVE);
}
void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
switch (block_reason) {
case BLOCKED_BY_LOCK_SCREEN:
CreateUserSessions(1);
GetSessionControllerClient()->LockScreen();
break;
case BLOCKED_BY_LOGIN_SCREEN:
ClearLogin();
break;
case BLOCKED_BY_USER_ADDING_SCREEN:
SetUserAddingScreenRunning(true);
break;
default:
NOTREACHED();
break;
}
}
void AshTestBase::UnblockUserSession() {
CreateUserSessions(1);
GetSessionControllerClient()->UnlockScreen();
}
void AshTestBase::SetVirtualKeyboardEnabled(bool enabled) {
// Note there are a lot of flags that can be set to control whether the
// keyboard is shown or not. You can see the logic in
// |KeyboardUIController::IsKeyboardEnableRequested|.
// The |kTouchEnabled| flag seems like a logical candidate to pick, but it
// does not work because the flag will automatically be toggled off once the
// |DeviceDataManager| detects there is a physical keyboard present. That's
// why I picked the |kPolicyEnabled| and |kPolicyDisabled| flags instead.
auto enable_flag = keyboard::KeyboardEnableFlag::kPolicyEnabled;
auto disable_flag = keyboard::KeyboardEnableFlag::kPolicyDisabled;
auto* keyboard_controller = Shell::Get()->keyboard_controller();
if (enabled) {
keyboard_controller->SetEnableFlag(enable_flag);
keyboard_controller->ClearEnableFlag(disable_flag);
} else {
keyboard_controller->ClearEnableFlag(enable_flag);
keyboard_controller->SetEnableFlag(disable_flag);
}
// Ensure that observer methods and mojo calls between KeyboardControllerImpl,
// keyboard::KeyboardUIController*, and AshKeyboardUI complete.
base::RunLoop().RunUntilIdle();
}
void AshTestBase::DisableIME() {
aura::test::DisableIME(Shell::GetPrimaryRootWindow()->GetHost());
}
display::DisplayManager* AshTestBase::display_manager() {
return Shell::Get()->display_manager();
}
chromeos::FakePowerManagerClient* AshTestBase::power_manager_client() const {
return chromeos::FakePowerManagerClient::Get();
}
bool AshTestBase::TestIfMouseWarpsAt(ui::test::EventGenerator* event_generator,
const gfx::Point& point_in_screen) {
DCHECK(!Shell::Get()->display_manager()->IsInUnifiedMode());
static_cast<ExtendedMouseWarpController*>(
Shell::Get()->mouse_cursor_filter()->mouse_warp_controller_for_test())
->allow_non_native_event_for_test();
display::Screen* screen = display::Screen::GetScreen();
display::Display original_display =
screen->GetDisplayNearestPoint(point_in_screen);
event_generator->MoveMouseTo(point_in_screen);
return original_display.id() !=
screen
->GetDisplayNearestPoint(
aura::Env::GetInstance()->last_mouse_location())
.id();
}
void AshTestBase::PressAndReleaseKey(ui::KeyboardCode key_code, int flags) {
GetEventGenerator()->PressAndReleaseKey(key_code, flags);
}
void AshTestBase::SimulateMouseClickAt(
ui::test::EventGenerator* event_generator,
const views::View* target_view) {
DCHECK(target_view);
event_generator->MoveMouseTo(target_view->GetBoundsInScreen().CenterPoint());
event_generator->ClickLeftButton();
}
bool AshTestBase::EnterOverview(OverviewEnterExitType type) {
return Shell::Get()->overview_controller()->StartOverview(
OverviewStartAction::kTests, type);
}
bool AshTestBase::ExitOverview(OverviewEnterExitType type) {
return Shell::Get()->overview_controller()->EndOverview(
OverviewEndAction::kTests, type);
}
void AshTestBase::SwapPrimaryDisplay() {
if (display::Screen::GetScreen()->GetNumDisplays() <= 1)
return;
Shell::Get()->window_tree_host_manager()->SetPrimaryDisplayId(
display::test::DisplayManagerTestApi(display_manager())
.GetSecondaryDisplay()
.id());
}
display::Display AshTestBase::GetPrimaryDisplay() const {
return display::Screen::GetScreen()->GetDisplayNearestWindow(
Shell::GetPrimaryRootWindow());
}
display::Display AshTestBase::GetSecondaryDisplay() const {
return ash_test_helper_->GetSecondaryDisplay();
}
// ============================================================================
// NoSessionAshTestBase:
NoSessionAshTestBase::NoSessionAshTestBase() {
set_start_session(false);
}
NoSessionAshTestBase::NoSessionAshTestBase(
base::test::TaskEnvironment::TimeSource time_source)
: AshTestBase(time_source) {
set_start_session(false);
}
NoSessionAshTestBase::~NoSessionAshTestBase() = default;
} // namespace ash