forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathash_keyboard_controller_unittest.cc
476 lines (384 loc) · 16.9 KB
/
ash_keyboard_controller_unittest.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
// Copyright 2018 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/keyboard/ash_keyboard_controller.h"
#include <memory>
#include <vector>
#include "ash/public/cpp/test/test_keyboard_controller_observer.h"
#include "ash/public/interfaces/keyboard_controller.mojom.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_helper.h"
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/test/scoped_task_environment.h"
#include "mojo/public/cpp/bindings/associated_binding.h"
#include "services/service_manager/public/cpp/connector.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/window.h"
#include "ui/keyboard/container_behavior.h"
#include "ui/keyboard/keyboard_controller.h"
using keyboard::mojom::KeyboardConfig;
using keyboard::mojom::KeyboardConfigPtr;
using keyboard::mojom::KeyboardEnableFlag;
namespace ash {
namespace {
class TestClient {
public:
explicit TestClient(service_manager::Connector* connector) {
connector->BindInterface("test", &keyboard_controller_ptr_);
}
~TestClient() = default;
bool IsKeyboardEnabled() {
keyboard_controller_ptr_->IsKeyboardEnabled(base::BindOnce(
&TestClient::OnIsKeyboardEnabled, base::Unretained(this)));
keyboard_controller_ptr_.FlushForTesting();
return is_enabled_;
}
void GetKeyboardConfig() {
keyboard_controller_ptr_->GetKeyboardConfig(base::BindOnce(
&TestClient::OnGetKeyboardConfig, base::Unretained(this)));
keyboard_controller_ptr_.FlushForTesting();
}
void SetKeyboardConfig(KeyboardConfigPtr config) {
keyboard_controller_ptr_->SetKeyboardConfig(std::move(config));
keyboard_controller_ptr_.FlushForTesting();
}
void SetEnableFlag(KeyboardEnableFlag flag) {
keyboard_controller_ptr_->SetEnableFlag(flag);
keyboard_controller_ptr_.FlushForTesting();
}
void ClearEnableFlag(KeyboardEnableFlag flag) {
keyboard_controller_ptr_->ClearEnableFlag(flag);
keyboard_controller_ptr_.FlushForTesting();
}
std::vector<keyboard::mojom::KeyboardEnableFlag> GetEnableFlags() {
std::vector<keyboard::mojom::KeyboardEnableFlag> enable_flags;
base::RunLoop run_loop;
keyboard_controller_ptr_->GetEnableFlags(base::BindOnce(
[](std::vector<keyboard::mojom::KeyboardEnableFlag>* enable_flags,
base::OnceClosure callback,
const std::vector<keyboard::mojom::KeyboardEnableFlag>& flags) {
*enable_flags = flags;
std::move(callback).Run();
},
&enable_flags, run_loop.QuitClosure()));
run_loop.Run();
return enable_flags;
}
void RebuildKeyboardIfEnabled() {
keyboard_controller_ptr_->RebuildKeyboardIfEnabled();
keyboard_controller_ptr_.FlushForTesting();
}
bool IsKeyboardVisible() {
keyboard_controller_ptr_->IsKeyboardVisible(base::BindOnce(
&TestClient::OnIsKeyboardVisible, base::Unretained(this)));
keyboard_controller_ptr_.FlushForTesting();
return is_visible_;
}
void ShowKeyboard() {
keyboard_controller_ptr_->ShowKeyboard();
keyboard_controller_ptr_.FlushForTesting();
}
void HideKeyboard() {
keyboard_controller_ptr_->HideKeyboard(mojom::HideReason::kUser);
keyboard_controller_ptr_.FlushForTesting();
}
bool SetContainerType(keyboard::mojom::ContainerType container_type,
const base::Optional<gfx::Rect>& target_bounds) {
bool result;
base::RunLoop run_loop;
keyboard_controller_ptr_->SetContainerType(
container_type, target_bounds,
base::BindOnce(
[](bool* result_ptr, base::OnceClosure callback, bool result) {
*result_ptr = result;
std::move(callback).Run();
},
&result, run_loop.QuitClosure()));
run_loop.Run();
return result;
}
void SetKeyboardLocked(bool locked) {
keyboard_controller_ptr_->SetKeyboardLocked(locked);
keyboard_controller_ptr_.FlushForTesting();
}
void SetOccludedBounds(const std::vector<gfx::Rect>& bounds) {
keyboard_controller_ptr_->SetOccludedBounds(bounds);
keyboard_controller_ptr_.FlushForTesting();
}
void SetHitTestBounds(const std::vector<gfx::Rect>& bounds) {
keyboard_controller_ptr_->SetHitTestBounds(bounds);
keyboard_controller_ptr_.FlushForTesting();
}
void SetDraggableArea(const gfx::Rect& bounds) {
keyboard_controller_ptr_->SetDraggableArea(bounds);
keyboard_controller_ptr_.FlushForTesting();
}
void FlushMojoForTesting() { keyboard_controller_ptr_.FlushForTesting(); }
int got_keyboard_config_count() const { return got_keyboard_config_count_; }
const KeyboardConfig& keyboard_config() const { return keyboard_config_; }
private:
void OnIsKeyboardEnabled(bool enabled) { is_enabled_ = enabled; }
void OnIsKeyboardVisible(bool visible) { is_visible_ = visible; }
void OnGetKeyboardConfig(KeyboardConfigPtr config) {
++got_keyboard_config_count_;
keyboard_config_ = *config;
}
bool is_enabled_ = false;
bool is_visible_ = false;
int got_keyboard_config_count_ = 0;
KeyboardConfig keyboard_config_;
mojom::KeyboardControllerPtr keyboard_controller_ptr_;
};
class TestContainerBehavior : public keyboard::ContainerBehavior {
public:
TestContainerBehavior() : keyboard::ContainerBehavior(nullptr) {}
~TestContainerBehavior() override = default;
// keyboard::ContainerBehavior
void DoShowingAnimation(
aura::Window* window,
ui::ScopedLayerAnimationSettings* animation_settings) override {}
void DoHidingAnimation(
aura::Window* window,
wm::ScopedHidingAnimationSettings* animation_settings) override {}
void InitializeShowAnimationStartingState(aura::Window* container) override {}
gfx::Rect AdjustSetBoundsRequest(
const gfx::Rect& display_bounds,
const gfx::Rect& requested_bounds_in_screen_coords) override {
return gfx::Rect();
}
void SetCanonicalBounds(aura::Window* container,
const gfx::Rect& display_bounds) override {}
bool IsOverscrollAllowed() const override { return true; }
void SavePosition(const gfx::Rect& keyboard_bounds_in_screen,
const gfx::Size& screen_size) override {}
bool HandlePointerEvent(const ui::LocatedEvent& event,
const display::Display& current_display) override {
return false;
}
keyboard::mojom::ContainerType GetType() const override { return type_; }
bool TextBlurHidesKeyboard() const override { return false; }
void SetOccludedBounds(const gfx::Rect& bounds) override {
occluded_bounds_ = bounds;
}
gfx::Rect GetOccludedBounds(
const gfx::Rect& visual_bounds_in_window) const override {
return occluded_bounds_;
}
bool OccludedBoundsAffectWorkspaceLayout() const override { return false; }
void SetDraggableArea(const gfx::Rect& rect) override {
draggable_area_ = rect;
}
const gfx::Rect& occluded_bounds() const { return occluded_bounds_; }
const gfx::Rect& draggable_area() const { return draggable_area_; }
private:
keyboard::mojom::ContainerType type_ =
keyboard::mojom::ContainerType::kFullWidth;
gfx::Rect occluded_bounds_;
gfx::Rect draggable_area_;
};
class AshKeyboardControllerTest : public AshTestBase {
public:
AshKeyboardControllerTest() = default;
~AshKeyboardControllerTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
// Create a local service manager connector to handle requests to
// mojom::KeyboardController.
service_manager::mojom::ConnectorRequest request;
connector_ = service_manager::Connector::Create(&request);
service_manager::Connector::TestApi test_api(connector_.get());
test_api.OverrideBinderForTesting(
service_manager::ServiceFilter::ByName("test"),
mojom::KeyboardController::Name_,
base::BindRepeating(
&AshKeyboardControllerTest::AddKeyboardControllerBinding,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
test_client_ = std::make_unique<TestClient>(connector_.get());
// Set the initial observer config to the client (default) config.
test_observer()->set_config(test_client()->keyboard_config());
}
void TearDown() override {
test_client_.reset();
AshTestBase::TearDown();
}
void AddKeyboardControllerBinding(mojo::ScopedMessagePipeHandle handle) {
Shell::Get()->ash_keyboard_controller()->BindRequest(
mojom::KeyboardControllerRequest(std::move(handle)));
}
keyboard::KeyboardController* keyboard_controller() {
return Shell::Get()->ash_keyboard_controller()->keyboard_controller();
}
TestClient* test_client() { return test_client_.get(); }
TestKeyboardControllerObserver* test_observer() {
return ash_test_helper()->test_keyboard_controller_observer();
}
private:
std::unique_ptr<service_manager::Connector> connector_;
std::unique_ptr<TestClient> test_client_;
DISALLOW_COPY_AND_ASSIGN(AshKeyboardControllerTest);
};
} // namespace
TEST_F(AshKeyboardControllerTest, GetKeyboardConfig) {
test_client()->GetKeyboardConfig();
EXPECT_EQ(1, test_client()->got_keyboard_config_count());
}
TEST_F(AshKeyboardControllerTest, SetKeyboardConfig) {
// Enable the keyboard so that config changes trigger observer events.
test_client()->SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
test_client()->GetKeyboardConfig();
EXPECT_EQ(1, test_client()->got_keyboard_config_count());
KeyboardConfigPtr config =
KeyboardConfig::New(test_client()->keyboard_config());
// Set the observer config to the client (default) config.
test_observer()->set_config(*config);
// Change the keyboard config.
bool old_auto_complete = config->auto_complete;
config->auto_complete = !config->auto_complete;
test_client()->SetKeyboardConfig(std::move(config));
// Test that the config changes.
test_client()->GetKeyboardConfig();
EXPECT_NE(old_auto_complete, test_client()->keyboard_config().auto_complete);
// Test that the test observer received the change.
EXPECT_NE(old_auto_complete, test_observer()->config().auto_complete);
}
TEST_F(AshKeyboardControllerTest, EnableFlags) {
EXPECT_FALSE(test_client()->IsKeyboardEnabled());
// Enable the keyboard.
test_client()->SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
std::vector<keyboard::mojom::KeyboardEnableFlag> enable_flags =
test_client()->GetEnableFlags();
EXPECT_TRUE(
base::ContainsValue(enable_flags, KeyboardEnableFlag::kExtensionEnabled));
EXPECT_EQ(enable_flags, test_observer()->enable_flags());
EXPECT_TRUE(test_client()->IsKeyboardEnabled());
// Set the enable override to disable the keyboard.
test_client()->SetEnableFlag(KeyboardEnableFlag::kPolicyDisabled);
enable_flags = test_client()->GetEnableFlags();
EXPECT_TRUE(
base::ContainsValue(enable_flags, KeyboardEnableFlag::kExtensionEnabled));
EXPECT_TRUE(
base::ContainsValue(enable_flags, KeyboardEnableFlag::kPolicyDisabled));
EXPECT_EQ(enable_flags, test_observer()->enable_flags());
EXPECT_FALSE(test_client()->IsKeyboardEnabled());
// Clear the enable override; should enable the keyboard.
test_client()->ClearEnableFlag(KeyboardEnableFlag::kPolicyDisabled);
enable_flags = test_client()->GetEnableFlags();
EXPECT_TRUE(
base::ContainsValue(enable_flags, KeyboardEnableFlag::kExtensionEnabled));
EXPECT_FALSE(
base::ContainsValue(enable_flags, KeyboardEnableFlag::kPolicyDisabled));
EXPECT_EQ(enable_flags, test_observer()->enable_flags());
EXPECT_TRUE(test_client()->IsKeyboardEnabled());
}
TEST_F(AshKeyboardControllerTest, RebuildKeyboardIfEnabled) {
EXPECT_EQ(0, test_observer()->destroyed_count());
// Enable the keyboard.
test_client()->SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
EXPECT_EQ(0, test_observer()->destroyed_count());
// Enable the keyboard again; this should not reload the keyboard.
test_client()->SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
EXPECT_EQ(0, test_observer()->destroyed_count());
// Rebuild the keyboard. This should destroy the previous keyboard window.
test_client()->RebuildKeyboardIfEnabled();
EXPECT_EQ(1, test_observer()->destroyed_count());
// Disable the keyboard. The keyboard window should be destroyed.
test_client()->ClearEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
EXPECT_EQ(2, test_observer()->destroyed_count());
}
TEST_F(AshKeyboardControllerTest, ShowAndHideKeyboard) {
// Enable the keyboard. This will create the keyboard window but not show it.
test_client()->SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
ASSERT_TRUE(keyboard_controller()->GetKeyboardWindow());
EXPECT_FALSE(keyboard_controller()->GetKeyboardWindow()->IsVisible());
test_client()->ShowKeyboard();
EXPECT_TRUE(keyboard_controller()->GetKeyboardWindow()->IsVisible());
test_client()->HideKeyboard();
EXPECT_FALSE(keyboard_controller()->GetKeyboardWindow()->IsVisible());
// TODO(stevenjb): Also use TestKeyboardControllerObserver and
// IsKeyboardVisible to test visibility changes. https://crbug.com/849995.
}
TEST_F(AshKeyboardControllerTest, SetContainerType) {
// Enable the keyboard.
test_client()->SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
const auto default_behavior = keyboard::mojom::ContainerType::kFullWidth;
EXPECT_EQ(default_behavior, keyboard_controller()->GetActiveContainerType());
gfx::Rect target_bounds(0, 0, 10, 10);
// Set the container type to kFloating.
EXPECT_TRUE(test_client()->SetContainerType(
keyboard::mojom::ContainerType::kFloating, target_bounds));
EXPECT_EQ(keyboard::mojom::ContainerType::kFloating,
keyboard_controller()->GetActiveContainerType());
// Ensure that the window size is correct (position is determined by Ash).
EXPECT_EQ(
target_bounds.size(),
keyboard_controller()->GetKeyboardWindow()->GetTargetBounds().size());
// Set the container type to kFullscreen.
EXPECT_TRUE(test_client()->SetContainerType(
keyboard::mojom::ContainerType::kFullscreen, base::nullopt));
EXPECT_EQ(keyboard::mojom::ContainerType::kFullscreen,
keyboard_controller()->GetActiveContainerType());
// Setting the container type to the current type should fail.
EXPECT_FALSE(test_client()->SetContainerType(
keyboard::mojom::ContainerType::kFullscreen, base::nullopt));
EXPECT_EQ(keyboard::mojom::ContainerType::kFullscreen,
keyboard_controller()->GetActiveContainerType());
}
TEST_F(AshKeyboardControllerTest, SetKeyboardLocked) {
ASSERT_FALSE(keyboard_controller()->keyboard_locked());
test_client()->SetKeyboardLocked(true);
EXPECT_TRUE(keyboard_controller()->keyboard_locked());
test_client()->SetKeyboardLocked(false);
EXPECT_FALSE(keyboard_controller()->keyboard_locked());
}
TEST_F(AshKeyboardControllerTest, SetOccludedBounds) {
// Enable the keyboard.
test_client()->SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
// Override the container behavior.
auto scoped_behavior = std::make_unique<TestContainerBehavior>();
TestContainerBehavior* behavior = scoped_behavior.get();
keyboard_controller()->set_container_behavior_for_test(
std::move(scoped_behavior));
gfx::Rect bounds(10, 20, 30, 40);
test_client()->SetOccludedBounds({bounds});
EXPECT_EQ(bounds, behavior->occluded_bounds());
}
TEST_F(AshKeyboardControllerTest, SetHitTestBounds) {
// Enable the keyboard.
test_client()->SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
ASSERT_FALSE(keyboard_controller()->GetKeyboardWindow()->targeter());
// Setting the hit test bounds should set a WindowTargeter.
test_client()->SetHitTestBounds({gfx::Rect(10, 20, 30, 40)});
ASSERT_TRUE(keyboard_controller()->GetKeyboardWindow()->targeter());
}
TEST_F(AshKeyboardControllerTest, SetDraggableArea) {
// Enable the keyboard.
test_client()->SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
// Override the container behavior.
auto scoped_behavior = std::make_unique<TestContainerBehavior>();
TestContainerBehavior* behavior = scoped_behavior.get();
keyboard_controller()->set_container_behavior_for_test(
std::move(scoped_behavior));
gfx::Rect bounds(10, 20, 30, 40);
test_client()->SetDraggableArea(bounds);
EXPECT_EQ(bounds, behavior->draggable_area());
}
TEST_F(AshKeyboardControllerTest, ChangingSessionRebuildsKeyboard) {
// Enable the keyboard.
test_client()->SetEnableFlag(KeyboardEnableFlag::kExtensionEnabled);
// LOGGED_IN_NOT_ACTIVE session state needs to rebuild keyboard for supervised
// user profile.
Shell::Get()->ash_keyboard_controller()->OnSessionStateChanged(
session_manager::SessionState::LOGGED_IN_NOT_ACTIVE);
test_client()->FlushMojoForTesting();
EXPECT_EQ(1, test_observer()->destroyed_count());
// ACTIVE session state also needs to rebuild keyboard for guest user profile.
Shell::Get()->ash_keyboard_controller()->OnSessionStateChanged(
session_manager::SessionState::ACTIVE);
test_client()->FlushMojoForTesting();
EXPECT_EQ(2, test_observer()->destroyed_count());
}
} // namespace ash