File: keyboard_controller_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (489 lines) | stat: -rw-r--r-- 19,027 bytes parent folder | download | duplicates (5)
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
// 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/keyboard/ui/keyboard_ui_controller.h"
#include "ash/keyboard/ui/resources/keyboard_resource_util.h"
#include "ash/public/cpp/keyboard/keyboard_switches.h"
#include "base/command_line.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_controller_client.h"
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_ui.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ime/dummy_text_input_client.h"
#include "ui/base/ime/init/input_method_factory.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/events/test/event_generator.h"

namespace {

const int kKeyboardHeightForTest = 100;

// TODO(shend): Remove this since all calls are synchronous now.
class KeyboardVisibleWaiter : public ChromeKeyboardControllerClient::Observer {
 public:
  explicit KeyboardVisibleWaiter(bool visible) : visible_(visible) {
    ChromeKeyboardControllerClient::Get()->AddObserver(this);
  }

  KeyboardVisibleWaiter(const KeyboardVisibleWaiter&) = delete;
  KeyboardVisibleWaiter& operator=(const KeyboardVisibleWaiter&) = delete;

  ~KeyboardVisibleWaiter() override {
    ChromeKeyboardControllerClient::Get()->RemoveObserver(this);
  }

  void Wait() {
    if (ChromeKeyboardControllerClient::Get()->is_keyboard_visible() ==
        visible_) {
      return;
    }
    run_loop_.Run();
  }

  // ChromeKeyboardControllerClient::Observer
  void OnKeyboardVisibilityChanged(bool visible) override {
    if (visible == visible_) {
      run_loop_.QuitWhenIdle();
    }
  }

 private:
  base::RunLoop run_loop_;
  const bool visible_;
};

class KeyboardLoadedWaiter : public ChromeKeyboardControllerClient::Observer {
 public:
  KeyboardLoadedWaiter() {
    ChromeKeyboardControllerClient::Get()->AddObserver(this);
  }

  KeyboardLoadedWaiter(const KeyboardLoadedWaiter&) = delete;
  KeyboardLoadedWaiter& operator=(const KeyboardLoadedWaiter&) = delete;

  ~KeyboardLoadedWaiter() override {
    ChromeKeyboardControllerClient::Get()->RemoveObserver(this);
  }

  void Wait() {
    if (ChromeKeyboardControllerClient::Get()->is_keyboard_loaded()) {
      return;
    }
    run_loop_.Run();
  }

  // ChromeKeyboardControllerClient::Observer
  void OnKeyboardLoaded() override { run_loop_.QuitWhenIdle(); }

 private:
  base::RunLoop run_loop_;
};

class KeyboardOccludedBoundsChangeWaiter
    : public ChromeKeyboardControllerClient::Observer {
 public:
  KeyboardOccludedBoundsChangeWaiter() {
    ChromeKeyboardControllerClient::Get()->AddObserver(this);
  }

  KeyboardOccludedBoundsChangeWaiter(
      const KeyboardOccludedBoundsChangeWaiter&) = delete;
  KeyboardOccludedBoundsChangeWaiter& operator=(
      const KeyboardOccludedBoundsChangeWaiter&) = delete;

  ~KeyboardOccludedBoundsChangeWaiter() override {
    ChromeKeyboardControllerClient::Get()->RemoveObserver(this);
  }

  void Wait() { run_loop_.Run(); }

  // ChromeKeyboardControllerClient::Observer
  void OnKeyboardOccludedBoundsChanged(const gfx::Rect& bounds) override {
    run_loop_.QuitWhenIdle();
  }

 private:
  base::RunLoop run_loop_;
};

ui::InputMethod* GetInputMethod() {
  aura::Window* root_window = ChromeKeyboardControllerClient::Get()
                                  ->GetKeyboardWindow()
                                  ->GetRootWindow();
  return root_window ? root_window->GetHost()->GetInputMethod() : nullptr;
}

}  // namespace

class KeyboardControllerWebContentTest : public InProcessBrowserTest {
 public:
  KeyboardControllerWebContentTest() = default;

  KeyboardControllerWebContentTest(const KeyboardControllerWebContentTest&) =
      delete;
  KeyboardControllerWebContentTest& operator=(
      const KeyboardControllerWebContentTest&) = delete;

  ~KeyboardControllerWebContentTest() override = default;

  void SetUp() override { InProcessBrowserTest::SetUp(); }

  void TearDown() override { InProcessBrowserTest::TearDown(); }

  // Ensure that the virtual keyboard is enabled.
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(keyboard::switches::kEnableVirtualKeyboard);
  }

 protected:
  void FocusEditableNodeAndShowKeyboard(const gfx::Rect& init_bounds) {
    client =
        std::make_unique<ui::DummyTextInputClient>(ui::TEXT_INPUT_TYPE_TEXT);
    ui::InputMethod* input_method = GetInputMethod();
    ASSERT_TRUE(input_method);
    input_method->SetFocusedTextInputClient(client.get());
    input_method->SetVirtualKeyboardVisibilityIfEnabled(true);
    // Mock window.resizeTo that is expected to be called after navigate to a
    // new virtual keyboard.
    auto* keyboard_controller = ChromeKeyboardControllerClient::Get();
    keyboard_controller->GetKeyboardWindow()->SetBounds(init_bounds);
  }

  void FocusNonEditableNode() {
    client =
        std::make_unique<ui::DummyTextInputClient>(ui::TEXT_INPUT_TYPE_NONE);
    GetInputMethod()->SetFocusedTextInputClient(client.get());
  }

  void MockEnableIMEInDifferentExtension(const std::string& url,
                                         const gfx::Rect& init_bounds) {
    DCHECK(!url.empty());
    auto* keyboard_controller = ChromeKeyboardControllerClient::Get();
    keyboard_controller->set_virtual_keyboard_url_for_test(GURL(url));
    keyboard_controller->ReloadKeyboardIfNeeded();
    // Mock window.resizeTo that is expected to be called after navigate to a
    // new virtual keyboard.
    keyboard_controller->GetKeyboardWindow()->SetBounds(init_bounds);
  }

 private:
  std::unique_ptr<ui::DummyTextInputClient> client;
  ui::ScopedTestInputMethodFactory scoped_test_input_method_factory_;
};

// Test for crbug.com/404340. After enabling an IME in a different extension,
// its virtual keyboard should not become visible if previous one is not.
IN_PROC_BROWSER_TEST_F(KeyboardControllerWebContentTest,
                       EnableIMEInDifferentExtension) {
  KeyboardLoadedWaiter().Wait();

  gfx::Rect test_bounds(0, 0, 0, kKeyboardHeightForTest);
  FocusEditableNodeAndShowKeyboard(test_bounds);
  KeyboardVisibleWaiter(true).Wait();

  FocusNonEditableNode();
  KeyboardVisibleWaiter(false).Wait();

  MockEnableIMEInDifferentExtension("chrome-extension://domain-1", test_bounds);
  // Keyboard should not become visible if previous keyboard is not.
  EXPECT_FALSE(ChromeKeyboardControllerClient::Get()->is_keyboard_visible());

  FocusEditableNodeAndShowKeyboard(test_bounds);
  // Keyboard should become visible after focus on an editable node.
  KeyboardVisibleWaiter(true).Wait();

  // Simulate hide keyboard by pressing hide key on the virtual keyboard.
  ChromeKeyboardControllerClient::Get()->HideKeyboard(ash::HideReason::kUser);
  KeyboardVisibleWaiter(false).Wait();

  MockEnableIMEInDifferentExtension("chrome-extension://domain-2", test_bounds);
  // Keyboard should not become visible if previous keyboard is not, even if it
  // is currently focused on an editable node.
  EXPECT_FALSE(ChromeKeyboardControllerClient::Get()->is_keyboard_visible());
}

// This test requires using the Ash keyboard window for EventGenerator to work.
// TODO(stevenjb/shend): Investigate/fix.
IN_PROC_BROWSER_TEST_F(KeyboardControllerWebContentTest,
                       CanDragFloatingKeyboardWithMouse) {
  ChromeKeyboardControllerClient::Get()->SetContainerType(
      keyboard::ContainerType::kFloating, gfx::Rect(0, 0, 400, 200),
      base::DoNothing());

  auto* controller = keyboard::KeyboardUIController::Get();
  controller->ShowKeyboard(false);
  KeyboardVisibleWaiter(true).Wait();

  aura::Window* keyboard_window = controller->GetKeyboardWindow();
  keyboard_window->SetBounds(gfx::Rect(0, 0, 100, 100));
  EXPECT_EQ(gfx::Point(0, 0), keyboard_window->bounds().origin());

  controller->SetDraggableArea(keyboard_window->bounds());

  // Drag the top left corner of the keyboard to move it.
  ui::test::EventGenerator event_generator(keyboard_window->GetRootWindow());
  event_generator.MoveMouseTo(gfx::Point(0, 0));
  event_generator.PressLeftButton();
  event_generator.MoveMouseTo(gfx::Point(50, 50));
  event_generator.ReleaseLeftButton();
  event_generator.MoveMouseTo(gfx::Point(100, 100));

  EXPECT_EQ(gfx::Point(50, 50), keyboard_window->bounds().origin());
}

class KeyboardControllerAppWindowTest
    : public extensions::PlatformAppBrowserTest {
 public:
  KeyboardControllerAppWindowTest() = default;

  KeyboardControllerAppWindowTest(const KeyboardControllerAppWindowTest&) =
      delete;
  KeyboardControllerAppWindowTest& operator=(
      const KeyboardControllerAppWindowTest&) = delete;

  ~KeyboardControllerAppWindowTest() override = default;

  // Ensure that the virtual keyboard is enabled.
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(keyboard::switches::kEnableVirtualKeyboard);
  }

  scoped_refptr<const extensions::Extension> CreateDummyExtension() {
    auto extension =
        extensions::ExtensionBuilder()
            .SetManifest(base::Value::Dict()
                             .Set("name", "test extension")
                             .Set("version", "1")
                             .Set("manifest_version", 2)
                             .Set("background",
                                  base::Value::Dict().Set(
                                      "scripts", base::Value::List().Append(
                                                     "background.js"))))
            .Build();
    extension_registrar()->AddExtension(extension);
    return extension;
  }
};

IN_PROC_BROWSER_TEST_F(KeyboardControllerAppWindowTest,
                       ShowingKeyboardChangesViewport) {
  auto* client = ChromeKeyboardControllerClient::Get();

  auto extension = CreateDummyExtension();
  extensions::AppWindow::CreateParams params;
  params.frame = extensions::AppWindow::FRAME_NONE;
  params.state = ui::mojom::WindowShowState::kMaximized;
  extensions::AppWindow* app_window =
      CreateAppWindowFromParams(browser()->profile(), extension.get(), params);

  // Wait until the keyboard is shown.
  KeyboardLoadedWaiter().Wait();
  client->ShowKeyboard();
  KeyboardVisibleWaiter(true).Wait();

  const int new_viewport_height = app_window->web_contents()
                                      ->GetRenderWidgetHostView()
                                      ->GetVisibleViewportSize()
                                      .height();
  const int screen_height = display::Screen::GetScreen()
                                ->GetPrimaryDisplay()
                                .GetSizeInPixel()
                                .height();
  EXPECT_EQ(new_viewport_height,
            screen_height - client->GetKeyboardWindow()->bounds().height());
}

IN_PROC_BROWSER_TEST_F(KeyboardControllerAppWindowTest,
                       ShowingStickyKeyboardChangesViewport) {
  auto extension = CreateDummyExtension();
  extensions::AppWindow::CreateParams params;
  params.frame = extensions::AppWindow::FRAME_NONE;
  params.state = ui::mojom::WindowShowState::kMaximized;
  extensions::AppWindow* app_window =
      CreateAppWindowFromParams(browser()->profile(), extension.get(), params);

  // Wait until the keyboard is shown.
  KeyboardLoadedWaiter().Wait();
  keyboard::KeyboardUIController::Get()->ShowKeyboard(/*locked*/ true);
  KeyboardVisibleWaiter(true).Wait();

  const int new_viewport_height = app_window->web_contents()
                                      ->GetRenderWidgetHostView()
                                      ->GetVisibleViewportSize()
                                      .height();
  const int screen_height = display::Screen::GetScreen()
                                ->GetPrimaryDisplay()
                                .GetSizeInPixel()
                                .height();
  EXPECT_EQ(new_viewport_height,
            screen_height - ChromeKeyboardControllerClient::Get()
                                ->GetKeyboardWindow()
                                ->bounds()
                                .height());
}

// Tests that ime window won't overscroll. See crbug.com/529880.
IN_PROC_BROWSER_TEST_F(KeyboardControllerAppWindowTest,
                       DisableOverscrollForImeWindow) {
  auto extension = CreateDummyExtension();
  extensions::AppWindow::CreateParams non_ime_params;
  non_ime_params.frame = extensions::AppWindow::FRAME_NONE;
  extensions::AppWindow* non_ime_app_window = CreateAppWindowFromParams(
      browser()->profile(), extension.get(), non_ime_params);
  int non_ime_window_visible_height = non_ime_app_window->web_contents()
                                          ->GetRenderWidgetHostView()
                                          ->GetVisibleViewportSize()
                                          .height();

  extensions::AppWindow::CreateParams ime_params;
  ime_params.frame = extensions::AppWindow::FRAME_NONE;
  ime_params.is_ime_window = true;
  extensions::AppWindow* ime_app_window = CreateAppWindowFromParams(
      browser()->profile(), extension.get(), ime_params);
  int ime_window_visible_height = ime_app_window->web_contents()
                                      ->GetRenderWidgetHostView()
                                      ->GetVisibleViewportSize()
                                      .height();

  ASSERT_EQ(non_ime_window_visible_height, ime_window_visible_height);
  ASSERT_TRUE(ime_window_visible_height > 0);

  // Make sure the keyboard has loaded before showing.
  KeyboardLoadedWaiter().Wait();

  auto* controller = ChromeKeyboardControllerClient::Get();
  controller->ShowKeyboard();
  KeyboardVisibleWaiter(true).Wait();

  int screen_height = display::Screen::GetScreen()
                          ->GetPrimaryDisplay()
                          .GetSizeInPixel()
                          .height();
  int keyboard_height = screen_height - ime_window_visible_height + 1;
  ASSERT_GT(keyboard_height, 0);
  gfx::Rect test_bounds = controller->GetKeyboardWindow()->bounds();
  test_bounds.set_height(keyboard_height);
  {
    // Waiter needs to be created before SetBounds() is invoked so that it can
    // catch OnOccludedBoundsChanged event even before it starts waiting.
    KeyboardOccludedBoundsChangeWaiter waiter;
    controller->GetKeyboardWindow()->SetBounds(test_bounds);
    // Wait for the keyboard bounds change has been processed.
    waiter.Wait();
  }

  // Non ime window should have smaller visible view port due to overlap with
  // virtual keyboard.
  EXPECT_LT(non_ime_app_window->web_contents()
                ->GetRenderWidgetHostView()
                ->GetVisibleViewportSize()
                .height(),
            non_ime_window_visible_height);
  // Ime window should have not be affected by virtual keyboard.
  EXPECT_EQ(ime_app_window->web_contents()
                ->GetRenderWidgetHostView()
                ->GetVisibleViewportSize()
                .height(),
            ime_window_visible_height);
}

class KeyboardControllerStateTest : public InProcessBrowserTest {
 public:
  KeyboardControllerStateTest() = default;

  KeyboardControllerStateTest(const KeyboardControllerStateTest&) = delete;
  KeyboardControllerStateTest& operator=(const KeyboardControllerStateTest&) =
      delete;

  ~KeyboardControllerStateTest() override = default;

  // Ensure that the virtual keyboard is enabled.
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(keyboard::switches::kEnableVirtualKeyboard);
  }
};

IN_PROC_BROWSER_TEST_F(KeyboardControllerStateTest, OpenTwice) {
  auto* controller = ChromeKeyboardControllerClient::Get();

  EXPECT_FALSE(controller->is_keyboard_visible());

  // Call ShowKeyboard twice, the keyboard should become visible.
  controller->ShowKeyboard();
  controller->ShowKeyboard();
  KeyboardVisibleWaiter(true).Wait();
  EXPECT_TRUE(controller->is_keyboard_visible());

  // Ensure the keyboard remains visible. Note: we call RunUntilIdle to at least
  // ensure no other messages are pending instead of relying on a timeout that
  // will slow down tests and potentially be flakey.
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(controller->is_keyboard_visible());
}

IN_PROC_BROWSER_TEST_F(KeyboardControllerStateTest, OpenAndCloseAndOpen) {
  auto* controller = ChromeKeyboardControllerClient::Get();
  controller->ShowKeyboard();
  KeyboardVisibleWaiter(true).Wait();

  controller->HideKeyboard(ash::HideReason::kSystem);
  KeyboardVisibleWaiter(false).Wait();

  controller->ShowKeyboard();
  KeyboardVisibleWaiter(true).Wait();
}

// NOTE: The following tests test internal state of keyboard::KeyboardController
// and will not work in Multi Process Mash. TODO(stevenjb/shend): Determine
// whether this needs to be tested in a keyboard::KeyboardController unit test.

IN_PROC_BROWSER_TEST_F(KeyboardControllerStateTest, StateResolvesAfterPreload) {
  auto* controller = keyboard::KeyboardUIController::Get();
  EXPECT_EQ(controller->GetStateForTest(), keyboard::KeyboardUIState::kLoading);
  KeyboardLoadedWaiter().Wait();
  EXPECT_EQ(controller->GetStateForTest(), keyboard::KeyboardUIState::kHidden);
}

IN_PROC_BROWSER_TEST_F(KeyboardControllerStateTest,
                       OpenAndCloseAndOpenInternal) {
  auto* controller = keyboard::KeyboardUIController::Get();
  controller->ShowKeyboard(false);
  // Need to wait the extension to be loaded. Hence LOADING_EXTENSION.
  EXPECT_EQ(controller->GetStateForTest(), keyboard::KeyboardUIState::kLoading);
  KeyboardVisibleWaiter(true).Wait();

  controller->HideKeyboardExplicitlyBySystem();
  EXPECT_EQ(controller->GetStateForTest(), keyboard::KeyboardUIState::kHidden);

  controller->ShowKeyboard(false);
  // The extension already has been loaded. Hence SHOWING.
  EXPECT_EQ(controller->GetStateForTest(), keyboard::KeyboardUIState::kShown);
}

// See crbug.com/755354.
IN_PROC_BROWSER_TEST_F(KeyboardControllerStateTest,
                       DisablingKeyboardGoesToInitialState) {
  auto* controller = keyboard::KeyboardUIController::Get();

  EXPECT_EQ(controller->GetStateForTest(), keyboard::KeyboardUIState::kLoading);

  controller->Shutdown();
  EXPECT_EQ(controller->GetStateForTest(), keyboard::KeyboardUIState::kInitial);
}