File: interaction_test_util_mouse_interactive_uitest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (214 lines) | stat: -rw-r--r-- 10,210 bytes parent folder | download | duplicates (3)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/interaction/interaction_test_util_mouse.h"

#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/toolbar/app_menu_model.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/interaction/expect_call_in_scope.h"
#include "ui/base/interaction/interaction_sequence.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/test/ui_controls.h"
#include "ui/views/interaction/element_tracker_views.h"
#include "ui/views/view_utils.h"

class InteractionTestUtilMouseUiTest
    : public InProcessBrowserTest,
      public testing::WithParamInterface<bool> {
 public:
  InteractionTestUtilMouseUiTest() = default;
  ~InteractionTestUtilMouseUiTest() override = default;

  using Mouse = views::test::InteractionTestUtilMouse;

  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    mouse_ = std::make_unique<Mouse>(
        BrowserView::GetBrowserViewForBrowser(browser())->GetWidget());
    CHECK(mouse_->SetTouchMode(GetParam()));
  }

  void TearDownOnMainThread() override {
    mouse_.reset();
    InProcessBrowserTest::TearDownOnMainThread();
  }

 protected:
  std::unique_ptr<Mouse> mouse_;
};

#if BUILDFLAG(IS_CHROMEOS)
INSTANTIATE_TEST_SUITE_P(TouchMode,
                         InteractionTestUtilMouseUiTest,
                         testing::Bool());
#else
INSTANTIATE_TEST_SUITE_P(TouchMode,
                         InteractionTestUtilMouseUiTest,
                         testing::Values(false));
#endif

IN_PROC_BROWSER_TEST_P(InteractionTestUtilMouseUiTest, MoveAndClick) {
  UNCALLED_MOCK_CALLBACK(ui::InteractionSequence::AbortedCallback, aborted);
  UNCALLED_MOCK_CALLBACK(ui::InteractionSequence::CompletedCallback, completed);

  auto sequence =
      ui::InteractionSequence::Builder()
          .SetContext(browser()->window()->GetElementContext())
          .SetAbortedCallback(aborted.Get())
          .SetCompletedCallback(completed.Get())
          // Find the app menu button.
          .AddStep(ui::InteractionSequence::StepBuilder()
                       .SetElementID(kToolbarAppMenuButtonElementId)
                       .SetStartCallback(base::BindLambdaForTesting(
                           [this](ui::InteractionSequence* seq,
                                  ui::TrackedElement* el) {
                             auto* const view =
                                 el->AsA<views::TrackedElementViews>()->view();
                             const gfx::Point pos =
                                 view->GetBoundsInScreen().CenterPoint();
                             // Perform the following gesture:
                             // - move to the center point of the app menu
                             // button
                             // - click the left mouse button
                             if (!mouse_->PerformGestures(
                                     Mouse::GestureParams(
                                         view->GetWidget()->GetNativeWindow(),
                                         false),
                                     Mouse::MoveTo(pos),
                                     Mouse::Click(ui_controls::LEFT))) {
                               seq->FailForTesting();
                             }
                           })))
          // Verify that the click opened the app menu, which should contain a
          // known menu item.
          .AddStep(ui::InteractionSequence::StepBuilder().SetElementID(
              AppMenuModel::kMoreToolsMenuItem))
          .Build();

  EXPECT_CALL_IN_SCOPE(completed, Run, sequence->RunSynchronouslyForTesting());
}

IN_PROC_BROWSER_TEST_P(InteractionTestUtilMouseUiTest, GestureAborted) {
  UNCALLED_MOCK_CALLBACK(ui::InteractionSequence::AbortedCallback, aborted);
  UNCALLED_MOCK_CALLBACK(ui::InteractionSequence::CompletedCallback, completed);

  auto cancel =
      base::BindLambdaForTesting([this]() { mouse_->CancelAllGestures(); });

  auto sequence =
      ui::InteractionSequence::Builder()
          .SetContext(browser()->window()->GetElementContext())
          .SetAbortedCallback(aborted.Get())
          .SetCompletedCallback(completed.Get())
          // Find the app menu button.
          .AddStep(ui::InteractionSequence::StepBuilder()
                       .SetElementID(kToolbarAppMenuButtonElementId)
                       .SetStartCallback(base::BindLambdaForTesting(
                           [this, &cancel](ui::TrackedElement* el) {
                             auto* const view =
                                 el->AsA<views::TrackedElementViews>()->view();
                             const gfx::Point pos =
                                 view->GetBoundsInScreen().CenterPoint();
                             // Queue a cancellation. This should execute
                             // sometime after the mouse move is sent.
                             base::SingleThreadTaskRunner::GetCurrentDefault()
                                 ->PostTask(FROM_HERE, cancel);
                             // Perform the following gesture:
                             // - move to the center point of the app menu
                             // button
                             // - click the left mouse button
                             EXPECT_FALSE(mouse_->PerformGestures(
                                 Mouse::GestureParams(
                                     view->GetWidget()->GetNativeWindow(),
                                     false),
                                 Mouse::MoveTo(pos),
                                 Mouse::Click(ui_controls::LEFT)));
                           })))
          .Build();

  EXPECT_CALL_IN_SCOPE(completed, Run, sequence->RunSynchronouslyForTesting());
}

IN_PROC_BROWSER_TEST_P(InteractionTestUtilMouseUiTest, Drag) {
  UNCALLED_MOCK_CALLBACK(ui::InteractionSequence::AbortedCallback, aborted);
  UNCALLED_MOCK_CALLBACK(ui::InteractionSequence::CompletedCallback, completed);

  const GURL first_url =
      browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL();
  const GURL kSecondUrl("chrome://version");
  ASSERT_TRUE(AddTabAtIndex(-1, kSecondUrl, ui::PAGE_TRANSITION_LINK));

  auto sequence =
      ui::InteractionSequence::Builder()
          .SetContext(browser()->window()->GetElementContext())
          .SetAbortedCallback(aborted.Get())
          .SetCompletedCallback(completed.Get())
          // Find the tab strip.
          .AddStep(
              ui::InteractionSequence::StepBuilder()
                  .SetElementID(kTabStripElementId)
                  .SetStartCallback(base::BindLambdaForTesting(
                      [&](ui::InteractionSequence* seq,
                          ui::TrackedElement* el) {
                        auto* const tab_strip = views::AsViewClass<TabStrip>(
                            el->AsA<views::TrackedElementViews>()->view());
                        // The second tab might still be animating in, which
                        // could cause weirdness if we try to drag.
                        tab_strip->StopAnimating(/* layout =*/true);

                        const gfx::Point start = tab_strip->tab_at(0)
                                                     ->GetBoundsInScreen()
                                                     .CenterPoint();
                        const gfx::Point end = tab_strip->tab_at(1)
                                                   ->GetBoundsInScreen()
                                                   .CenterPoint();
                        // Drag the first tab into the second spot.
                        if (!mouse_->PerformGestures(
                                Mouse::GestureParams(
                                    tab_strip->GetWidget()->GetNativeWindow(),
                                    false),
                                Mouse::MoveTo(start),
                                Mouse::DragAndRelease(end))) {
                          seq->FailForTesting();
                        }
                      })))
          // When the gesture is complete, check that the gesture succeeded.
          .AddStep(
              ui::InteractionSequence::StepBuilder()
                  .SetElementID(kTabStripElementId)
                  .SetStartCallback(base::BindLambdaForTesting(
                      [&](ui::InteractionSequence* seq,
                          ui::TrackedElement* el) {
                        // Stop any remaining animations, and verify that the
                        // tab was moved.
                        auto* const tab_strip = views::AsViewClass<TabStrip>(
                            el->AsA<views::TrackedElementViews>()->view());
                        tab_strip->StopAnimating(/* layout =*/true);

                        EXPECT_EQ(kSecondUrl, browser()
                                                  ->tab_strip_model()
                                                  ->GetWebContentsAt(0)
                                                  ->GetURL());
                        EXPECT_EQ(first_url, browser()
                                                 ->tab_strip_model()
                                                 ->GetWebContentsAt(1)
                                                 ->GetURL());
                        // Clean up any drag gestures that have not yet properly
                        // cleaned up (this is a platform implementation issue
                        // for drag event handling).
                        mouse_->CancelAllGestures();
                      })))
          .Build();

  EXPECT_CALL_IN_SCOPE(completed, Run, sequence->RunSynchronouslyForTesting());
}