File: target_view_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (229 lines) | stat: -rw-r--r-- 8,900 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
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/input_overlay/ui/target_view.h"

#include "ash/shell.h"
#include "chrome/browser/ash/arc/input_overlay/actions/action.h"
#include "chrome/browser/ash/arc/input_overlay/constants.h"
#include "chrome/browser/ash/arc/input_overlay/test/overlay_view_test_base.h"
#include "chrome/browser/ash/arc/input_overlay/touch_injector.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"

namespace arc::input_overlay {

class TargetViewTest : public OverlayViewTestBase {
 public:
  TargetViewTest() = default;
  ~TargetViewTest() override = default;

  gfx::Point GetTargetCenter(TargetView* target) {
    DCHECK(target);
    return target->center_;
  }

  int GetTargetPadding(TargetView* target) {
    DCHECK(target);
    return target->GetPadding();
  }

  // Convert the point in `TargetView` coordinates to screen coordinates.
  gfx::Point GetPointInScreenFromTargetView(const gfx::Point& point) const {
    DCHECK(touch_injector_);

    gfx::Rect bounds = touch_injector_->content_bounds();
    gfx::Point point_in_screen = point;
    point_in_screen.Offset(bounds.origin().x(), bounds.origin().y());
    return point_in_screen;
  }

  // Verifies whether `touch_injector` has `expect_size` actions and the last
  // action has `expect_position`.
  void VerifyLastActionPosition(size_t expect_size,
                                const gfx::Point& expect_position) {
    DCHECK(touch_injector_);

    // Verify action view size.
    EXPECT_GE(expect_size, 1u);
    EXPECT_EQ(expect_size, GetActionViewSize());

    // Verify action size.
    const auto& actions = touch_injector_->actions();
    EXPECT_EQ(expect_size, actions.size());

    // Verify last action position.
    const auto* new_action = actions[expect_size - 1].get();
    const auto& positions = new_action->touch_down_positions();
    EXPECT_EQ(1u, positions.size());
    EXPECT_EQ(gfx::PointF(expect_position), positions[0]);
  }
};

TEST_F(TargetViewTest, TestInitialCursorLocation) {
  PressAddButton();
  auto* target_view = GetTargetView();
  EXPECT_TRUE(target_view);
  EXPECT_EQ(target_view->GetBoundsInScreen().CenterPoint(),
            aura::Env::GetInstance()->last_mouse_location());
}

TEST_F(TargetViewTest, TestMouseSupport) {
  // Enter into the button placement mode and check mouse hover move and click.
  PressAddButton();
  auto* target_view = GetTargetView();
  EXPECT_TRUE(target_view);

  auto* event_generator = GetEventGenerator();
  auto global_center = target_view->GetBoundsInScreen().CenterPoint();
  event_generator->MoveMouseTo(global_center);
  auto local_center = GetTargetCenter(target_view);
  EXPECT_EQ(global_center, GetPointInScreenFromTargetView(local_center));

  // Move mouse by `dx` and `dy`.
  const int dx = 5;
  const int dy = 10;
  event_generator->MoveMouseBy(/*x=*/dx, /*y=*/dy);
  global_center.Offset(/*delta_x=*/dx, /*delta_y=*/dy);
  local_center.Offset(/*delta_x=*/dx, /*delta_y=*/dy);
  EXPECT_EQ(local_center, GetTargetCenter(target_view));
  EXPECT_EQ(global_center, GetPointInScreenFromTargetView(local_center));

  const size_t action_view_size = GetActionViewSize();
  // Mouse click to drop down the action.
  event_generator->ClickLeftButton();
  // Check if the action is dropped on the expected position.
  VerifyLastActionPosition(action_view_size + 1,
                           GetPointInScreenFromTargetView(local_center));
}

TEST_F(TargetViewTest, TestCenterClamp) {
  // Enter into the button placement mode and check when mouse hover moved
  // to outside of the window.
  PressAddButton();
  auto* target_view = GetTargetView();
  EXPECT_TRUE(target_view);

  auto* event_generator = GetEventGenerator();
  event_generator->MoveMouseTo(target_view->GetBoundsInScreen().CenterPoint());
  // Simulate moving mouse to the outside of the window on the left.
  for (int i = 0; i < target_view->size().width(); i++) {
    event_generator->MoveMouseBy(/*x=*/-1, /*y=*/0);
  }
  // Stay in the button placement mode and the target center should still stay
  // inside and show the complete circle.
  target_view = GetTargetView();
  EXPECT_TRUE(target_view);
  EXPECT_EQ(GetTargetCenter(target_view).x(), GetTargetPadding(target_view));
}

TEST_F(TargetViewTest, TestKeyboardSupport) {
  // Enter into the button placement mode.
  PressAddButton();
  EXPECT_TRUE(GetTargetView());

  // Press a random key and it is still in the button placement mode.
  auto* event_generator = GetEventGenerator();
  event_generator->PressAndReleaseKey(ui::VKEY_A, ui::EF_NONE);
  auto* target_view = GetTargetView();
  EXPECT_TRUE(target_view);

  // Press the key `left` twice to update the target center.
  auto local_center = GetTargetCenter(target_view);
  local_center.Offset(-2 * kArrowKeyMoveDistance, 0);
  event_generator->PressKey(ui::VKEY_LEFT, ui::EF_NONE);
  event_generator->PressKey(ui::VKEY_LEFT, ui::EF_NONE);
  event_generator->ReleaseKey(ui::VKEY_LEFT, ui::EF_NONE);
  target_view = GetTargetView();
  EXPECT_TRUE(target_view);
  EXPECT_EQ(local_center, GetTargetCenter(target_view));

  // Press the key `enter` to place down the action.
  size_t action_view_size = GetActionViewSize();
  event_generator->PressAndReleaseKey(ui::VKEY_RETURN, ui::EF_NONE);
  EXPECT_FALSE(GetTargetView());
  VerifyLastActionPosition(action_view_size + 1,
                           GetPointInScreenFromTargetView(local_center));
  PressDoneButtonOnButtonOptionsMenu();

  // Enter into the button placement mode again and check whether the key `esc`
  // exits the button placement mode without adding anything.
  PressAddButton();
  EXPECT_TRUE(GetTargetView());
  event_generator->PressAndReleaseKey(ui::VKEY_ESCAPE, ui::EF_NONE);
  EXPECT_FALSE(GetTargetView());
  EXPECT_EQ(action_view_size + 1, GetActionViewSize());
}

TEST_F(TargetViewTest, TestGestureSupport) {
  // Enter into the button placement mode and test the gesture tap on the
  // center.
  PressAddButton();
  auto* target_view = GetTargetView();
  EXPECT_TRUE(target_view);
  const size_t action_view_size = GetActionViewSize();
  auto global_center = target_view->GetBoundsInScreen().CenterPoint();
  GestureTapOn(target_view);
  EXPECT_FALSE(GetTargetView());
  // Check if the action is dropped on the expect position.
  VerifyLastActionPosition(action_view_size + 1, global_center);
  PressDoneButtonOnButtonOptionsMenu();

  // Enter into the button placement mode and test the gesture scroll.
  PressAddButton();
  target_view = GetTargetView();
  EXPECT_TRUE(target_view);
  auto* event_generator = GetEventGenerator();
  event_generator->PressTouch(global_center);
  auto local_center = GetTargetCenter(target_view);
  EXPECT_EQ(global_center, GetPointInScreenFromTargetView(local_center));

  const int dx = 3;
  const int dy = 4;
  global_center.Offset(/*delta_x=*/dx, /*delta_y=*/dy);
  local_center.Offset(/*delta_x=*/dx, /*delta_y=*/dy);
  event_generator->MoveTouch(global_center);
  global_center.Offset(/*delta_x=*/dx, /*delta_y=*/dy);
  local_center.Offset(/*delta_x=*/dx, /*delta_y=*/dy);
  // Touch move more than once to trigger scroll gesture.
  event_generator->MoveTouch(global_center);
  EXPECT_TRUE(GetTargetView());
  EXPECT_EQ(local_center, GetTargetCenter(target_view));
  EXPECT_EQ(global_center, GetPointInScreenFromTargetView(local_center));

  event_generator->ReleaseTouch();
  // Check if the action is dropped on the expected position.
  VerifyLastActionPosition(action_view_size + 2,
                           GetPointInScreenFromTargetView(local_center));
}

TEST_F(TargetViewTest, TestMultiDisplay) {
  UpdateDisplay("1000x900,1000x900");
  aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows();
  display::Display display1 = display::Screen::GetScreen()->GetDisplayMatching(
      root_windows[1]->GetBoundsInScreen());

  // Move the window from the primary display to `display1`.
  auto* arc_window = widget_->GetNativeWindow();
  ASSERT_TRUE(arc_window);
  auto screen_bounds = arc_window->bounds();
  const auto& display_bounds = display1.bounds();
  screen_bounds.Offset(display_bounds.x(), display_bounds.y());
  arc_window->SetBoundsInScreen(screen_bounds, display1);
  // Update `controller_` since it is updated from switching display.
  controller_ = GetDisplayOverlayController();

  // Show `target_view` and it should show inside of the window bounds.
  EnableEditMode();
  PressAddButton();
  auto* target_view = GetTargetView();
  EXPECT_TRUE(target_view);
  EXPECT_TRUE(screen_bounds.Contains(target_view->GetBoundsInScreen()));
}

}  // namespace arc::input_overlay