File: virtual_keyboard_unittest.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 (124 lines) | stat: -rw-r--r-- 4,897 bytes parent folder | download | duplicates (6)
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
// Copyright 2018 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/keyboard_util.h"
#include "ash/keyboard/ui/test/keyboard_test_util.h"
#include "ash/public/cpp/keyboard/keyboard_switches.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/functional/callback_helpers.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/events/test/event_generator.h"

namespace ash {

class VirtualKeyboardTest : public AshTestBase {
 public:
  VirtualKeyboardTest() = default;

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

  ~VirtualKeyboardTest() override = default;

  void SetUp() override {
    AshTestBase::SetUp();
    SetVirtualKeyboardEnabled(true);
  }

  void TearDown() override {
    SetVirtualKeyboardEnabled(false);
    AshTestBase::TearDown();
  }
};

TEST_F(VirtualKeyboardTest, EventsAreHandledBasedOnHitTestBounds) {
  aura::Window* root_window = Shell::GetPrimaryRootWindow();

  // Create a test window in the background with the same size as the screen.
  aura::test::EventCountDelegate delegate;
  std::unique_ptr<aura::Window> background_window(
      CreateTestWindowInShellWithDelegate(&delegate, 0, root_window->bounds()));

  auto* keyboard_controller = keyboard::KeyboardUIController::Get();
  keyboard_controller->ShowKeyboard(false);
  ASSERT_TRUE(keyboard::test::WaitUntilShown());

  // Add two hit test bounds (coordinates relative to keyboard window).
  // Both are 10x10 squares, but placed in different locations.
  std::vector<gfx::Rect> hit_test_bounds;
  hit_test_bounds.emplace_back(0, 0, 10, 10);
  hit_test_bounds.emplace_back(20, 20, 10, 10);
  keyboard_controller->SetHitTestBounds(hit_test_bounds);

  // Click at various places within the keyboard window and check whether the
  // event passes through the keyboard window to the background window.
  ui::test::EventGenerator generator(root_window);
  const gfx::Point origin =
      keyboard_controller->GetVisualBoundsInScreen().origin();

  // (0, 0) is inside the first hit rect, so the event is handled by the window
  // and is not received by the background window.
  generator.MoveMouseTo(origin);
  generator.ClickLeftButton();
  EXPECT_EQ("0 0", delegate.GetMouseButtonCountsAndReset());

  // (25, 25) is inside the second hit rect, so the background window does not
  // receive the event.
  generator.MoveMouseTo(origin + gfx::Vector2d(25, 25));
  generator.ClickLeftButton();
  EXPECT_EQ("0 0", delegate.GetMouseButtonCountsAndReset());

  // (5, 25) is not inside any hit rect, so the background window receives the
  // event.
  generator.MoveMouseTo(origin + gfx::Vector2d(5, 25));
  generator.ClickLeftButton();
  EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());

  // (25, 5) is not inside any hit rect, so the background window receives the
  // event.
  generator.MoveMouseTo(origin + gfx::Vector2d(25, 5));
  generator.ClickLeftButton();
  EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());
}

TEST_F(VirtualKeyboardTest, HitTestBoundsAreResetWhenContainerTypeChanges) {
  aura::Window* root_window = Shell::GetPrimaryRootWindow();

  // Create a test window in the background with the same size as the screen.
  aura::test::EventCountDelegate delegate;
  std::unique_ptr<aura::Window> background_window(
      CreateTestWindowInShellWithDelegate(&delegate, 0, root_window->bounds()));

  auto* keyboard_controller = keyboard::KeyboardUIController::Get();
  keyboard_controller->ShowKeyboard(false);
  ASSERT_TRUE(keyboard::test::WaitUntilShown());

  // Set empty hit test bounds, so all events pass through to the background.
  keyboard_controller->SetHitTestBounds(std::vector<gfx::Rect>());

  ui::test::EventGenerator generator(root_window);
  aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();

  // (0, 0) passes through and is received by background window.
  generator.MoveMouseTo(keyboard_window->bounds().origin());
  generator.ClickLeftButton();
  EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());

  // Change the container behavior, which should reset the hit test bounds to
  // the whole keyboard window.
  keyboard_controller->HideKeyboardExplicitlyBySystem();
  keyboard_controller->SetContainerType(keyboard::ContainerType::kFloating,
                                        gfx::Rect(0, 0, 400, 200),
                                        base::DoNothing());
  keyboard_controller->ShowKeyboard(false);

  // (0, 0) should no longer pass through the keyboard window.
  generator.MoveMouseTo(keyboard_window->bounds().origin());
  generator.ClickLeftButton();
  EXPECT_EQ("0 0", delegate.GetMouseButtonCountsAndReset());
}

}  // namespace ash