File: omnibox_result_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 (318 lines) | stat: -rw-r--r-- 13,601 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
// Copyright 2017 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/ui/views/omnibox/omnibox_result_view.h"

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ui/omnibox/omnibox_theme.h"
#include "chrome/browser/ui/views/omnibox/omnibox_header_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_popup_view_views.h"
#include "chrome/browser/ui/views/omnibox/omnibox_row_view.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "components/omnibox/browser/omnibox_controller.h"
#include "components/omnibox/browser/test_omnibox_client.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/display/test/test_screen.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/event_utils.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/image/image.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/widget/widget.h"

#if defined(USE_AURA)
#include "ui/aura/env.h"
#endif

namespace {

// An arbitrary index for the result view under test. Used to test the selection
// state. There are 6 results total so the index should be in the range 0-5.
static constexpr size_t kTestResultViewIndex = 4;

class TestOmniboxPopupViewViews : public OmniboxPopupViewViews {
 public:
  explicit TestOmniboxPopupViewViews(OmniboxController* controller)
      : OmniboxPopupViewViews(/*omnibox_view=*/nullptr,
                              controller,
                              /*location_bar_view=*/nullptr),
        selection_(OmniboxPopupSelection(0, OmniboxPopupSelection::NORMAL)) {}

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

  void SetSelectedIndex(size_t index) override { selection_.line = index; }

  size_t GetSelectedIndex() const override { return selection_.line; }

  OmniboxPopupSelection GetSelection() const override { return selection_; }

 private:
  OmniboxPopupSelection selection_;
};

}  // namespace

class OmniboxResultViewTest : public ChromeViewsTestBase {
 public:
  void SetUp() override {
#if !defined(USE_AURA)
    test_screen_ = std::make_unique<display::test::TestScreen>();
    display::Screen::SetScreenInstance(test_screen_.get());
#endif
    ChromeViewsTestBase::SetUp();

    // Create a widget and assign bounds to support calls to HitTestPoint.
    widget_ =
        CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);

    omnibox_controller_ = std::make_unique<OmniboxController>(
        /*view=*/nullptr, std::make_unique<TestOmniboxClient>());
    popup_view_ =
        std::make_unique<TestOmniboxPopupViewViews>(omnibox_controller_.get());
    result_view_ =
        new OmniboxResultView(popup_view_.get(), kTestResultViewIndex);

    views::View* root_view = widget_->GetRootView();
    root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
    result_view_->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
    root_view->AddChildViewRaw(result_view_.get());

    // Start by not hovering over the result view.
    FakeMouseEvent(ui::EventType::kMouseMoved, 0, 200, 200);
  }

  void TearDown() override {
    widget_.reset();
    ChromeViewsTestBase::TearDown();
    display::Screen::SetScreenInstance(nullptr);
    test_screen_.reset();
  }

  // Also sets the fake screen's mouse cursor to 0, 0.
  ui::MouseEvent FakeMouseEvent(ui::EventType type, int flags) {
    return FakeMouseEvent(type, flags, 0, 0);
  }

  // Also sets the fake screen's mouse cursor to |x|, |y|.
  ui::MouseEvent FakeMouseEvent(ui::EventType type,
                                int flags,
                                float x,
                                float y) {
#if !defined(USE_AURA)
    test_screen_->set_cursor_screen_point(gfx::Point(x, y));
#else
    aura::Env::GetInstance()->SetLastMouseLocation(gfx::Point(x, y));
#endif
    return ui::MouseEvent(type, gfx::Point(x, y), gfx::Point(),
                          ui::EventTimeForNow(), flags, 0);
  }

  OmniboxEditModel* edit_model() { return omnibox_controller_->edit_model(); }
  OmniboxPopupViewViews* popup_view() { return popup_view_.get(); }
  OmniboxResultView* result_view() { return result_view_; }

 private:
  std::unique_ptr<OmniboxController> omnibox_controller_;
  std::unique_ptr<TestOmniboxPopupViewViews> popup_view_;
  raw_ptr<OmniboxResultView, DanglingUntriaged> result_view_;
  std::unique_ptr<views::Widget> widget_;

  std::unique_ptr<display::test::TestScreen> test_screen_;
};

TEST_F(OmniboxResultViewTest, MousePressedWithLeftButtonSelectsThisResult) {
  EXPECT_NE(OmniboxPartState::SELECTED, result_view()->GetThemeState());
  EXPECT_NE(popup_view()->GetSelectedIndex(), kTestResultViewIndex);

  // Right button press should not select.
  result_view()->OnMousePressed(
      FakeMouseEvent(ui::EventType::kMousePressed, ui::EF_RIGHT_MOUSE_BUTTON));
  EXPECT_NE(OmniboxPartState::SELECTED, result_view()->GetThemeState());
  EXPECT_NE(popup_view()->GetSelectedIndex(), kTestResultViewIndex);

  // Middle button press should not select.
  result_view()->OnMousePressed(
      FakeMouseEvent(ui::EventType::kMousePressed, ui::EF_MIDDLE_MOUSE_BUTTON));
  EXPECT_NE(OmniboxPartState::SELECTED, result_view()->GetThemeState());
  EXPECT_NE(popup_view()->GetSelectedIndex(), kTestResultViewIndex);

  // Multi-button press should not select.
  result_view()->OnMousePressed(
      FakeMouseEvent(ui::EventType::kMousePressed,
                     ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON));
  EXPECT_NE(OmniboxPartState::SELECTED, result_view()->GetThemeState());
  EXPECT_NE(popup_view()->GetSelectedIndex(), kTestResultViewIndex);

  // Left button press should select.
  result_view()->OnMousePressed(
      FakeMouseEvent(ui::EventType::kMousePressed, ui::EF_LEFT_MOUSE_BUTTON));
  EXPECT_EQ(OmniboxPartState::SELECTED, result_view()->GetThemeState());
  EXPECT_EQ(popup_view()->GetSelectedIndex(), kTestResultViewIndex);
}

TEST_F(OmniboxResultViewTest, MouseDragWithLeftButtonSelectsThisResult) {
  EXPECT_NE(OmniboxPartState::SELECTED, result_view()->GetThemeState());
  EXPECT_NE(popup_view()->GetSelectedIndex(), kTestResultViewIndex);

  // Right button drag should not select.
  result_view()->OnMouseDragged(
      FakeMouseEvent(ui::EventType::kMouseDragged, ui::EF_RIGHT_MOUSE_BUTTON));
  EXPECT_NE(OmniboxPartState::SELECTED, result_view()->GetThemeState());
  EXPECT_NE(popup_view()->GetSelectedIndex(), kTestResultViewIndex);

  // Middle button drag should not select.
  result_view()->OnMouseDragged(
      FakeMouseEvent(ui::EventType::kMouseDragged, ui::EF_MIDDLE_MOUSE_BUTTON));
  EXPECT_NE(OmniboxPartState::SELECTED, result_view()->GetThemeState());
  EXPECT_NE(popup_view()->GetSelectedIndex(), kTestResultViewIndex);

  // Multi-button drag should not select.
  result_view()->OnMouseDragged(
      FakeMouseEvent(ui::EventType::kMouseDragged,
                     ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON));
  EXPECT_NE(OmniboxPartState::SELECTED, result_view()->GetThemeState());
  EXPECT_NE(popup_view()->GetSelectedIndex(), kTestResultViewIndex);

  // Left button drag should select.
  result_view()->OnMouseDragged(
      FakeMouseEvent(ui::EventType::kMouseDragged, ui::EF_LEFT_MOUSE_BUTTON));
  EXPECT_EQ(OmniboxPartState::SELECTED, result_view()->GetThemeState());
  EXPECT_EQ(popup_view()->GetSelectedIndex(), kTestResultViewIndex);
}

TEST_F(OmniboxResultViewTest, MouseDragWithNonLeftButtonSetsHoveredState) {
  EXPECT_NE(OmniboxPartState::HOVERED, result_view()->GetThemeState());

  // Right button drag should put the view in the HOVERED state.
  result_view()->OnMouseDragged(FakeMouseEvent(
      ui::EventType::kMouseDragged, ui::EF_RIGHT_MOUSE_BUTTON, 50, 50));
  EXPECT_EQ(OmniboxPartState::HOVERED, result_view()->GetThemeState());

  // Left button drag should take the view out of the HOVERED state.
  result_view()->OnMouseDragged(FakeMouseEvent(
      ui::EventType::kMouseDragged, ui::EF_LEFT_MOUSE_BUTTON, 200, 200));
  EXPECT_NE(OmniboxPartState::HOVERED, result_view()->GetThemeState());
}

TEST_F(OmniboxResultViewTest, MouseDragOutOfViewCancelsHoverState) {
  EXPECT_NE(OmniboxPartState::HOVERED, result_view()->GetThemeState());

  // Right button drag in the view should put the view in the HOVERED state.
  result_view()->OnMouseDragged(FakeMouseEvent(
      ui::EventType::kMouseDragged, ui::EF_RIGHT_MOUSE_BUTTON, 50, 50));
  EXPECT_EQ(OmniboxPartState::HOVERED, result_view()->GetThemeState());

  // Right button drag outside of the view should revert the HOVERED state.
  result_view()->OnMouseDragged(FakeMouseEvent(
      ui::EventType::kMouseDragged, ui::EF_RIGHT_MOUSE_BUTTON, 200, 200));
  EXPECT_NE(OmniboxPartState::HOVERED, result_view()->GetThemeState());
}

TEST_F(OmniboxResultViewTest, MouseEnterAndExitSetsHoveredState) {
  EXPECT_NE(OmniboxPartState::HOVERED, result_view()->GetThemeState());

  // The mouse entering the view should put the view in the HOVERED state.
  result_view()->OnMouseMoved(
      FakeMouseEvent(ui::EventType::kMouseMoved, 0, 50, 50));
  EXPECT_EQ(OmniboxPartState::HOVERED, result_view()->GetThemeState());

  // Continuing to move over the view should not change the state.
  result_view()->OnMouseMoved(
      FakeMouseEvent(ui::EventType::kMouseMoved, 0, 50, 50));
  EXPECT_EQ(OmniboxPartState::HOVERED, result_view()->GetThemeState());

  // But exiting should revert the HOVERED state.
  result_view()->OnMouseExited(
      FakeMouseEvent(ui::EventType::kMouseMoved, 0, 200, 200));
  EXPECT_NE(OmniboxPartState::HOVERED, result_view()->GetThemeState());
}

TEST_F(OmniboxResultViewTest, MouseEnterAndExitSetsHoveredAccessibleState) {
  ui::AXNodeData node_data;
  result_view()->GetViewAccessibility().GetAccessibleNodeData(&node_data);
  EXPECT_FALSE(result_view()->GetViewAccessibility().GetIsHovered());
  EXPECT_FALSE(node_data.HasState(ax::mojom::State::kHovered));

  // The mouse entering the view should put the view in the HOVERED state.
  result_view()->OnMouseEntered(
      FakeMouseEvent(ui::EventType::kMouseMoved, 0, 50, 50));
  node_data = ui::AXNodeData();
  result_view()->GetViewAccessibility().GetAccessibleNodeData(&node_data);
  EXPECT_TRUE(result_view()->GetViewAccessibility().GetIsHovered());
  EXPECT_TRUE(node_data.HasState(ax::mojom::State::kHovered));

  // Continuing to move over the view should not change the state.
  result_view()->OnMouseMoved(
      FakeMouseEvent(ui::EventType::kMouseMoved, 0, 50, 50));
  node_data = ui::AXNodeData();
  result_view()->GetViewAccessibility().GetAccessibleNodeData(&node_data);
  EXPECT_TRUE(result_view()->GetViewAccessibility().GetIsHovered());
  EXPECT_TRUE(node_data.HasState(ax::mojom::State::kHovered));

  // But exiting should revert the HOVERED state.
  result_view()->OnMouseExited(
      FakeMouseEvent(ui::EventType::kMouseMoved, 0, 200, 200));
  node_data = ui::AXNodeData();
  result_view()->GetViewAccessibility().GetAccessibleNodeData(&node_data);
  EXPECT_FALSE(result_view()->GetViewAccessibility().GetIsHovered());
  EXPECT_FALSE(node_data.HasState(ax::mojom::State::kHovered));
}

TEST_F(OmniboxResultViewTest, AccessibleProperties) {
  // Check accessibility of result.
  std::u16string match_url = u"https://google.com";
  AutocompleteMatch match(nullptr, 500, false,
                          AutocompleteMatchType::HISTORY_TITLE);
  match.contents = match_url;
  match.contents_class.emplace_back(0, ACMatchClassification::URL);
  match.destination_url = GURL(match_url);
  match.description = u"Google";
  match.allowed_to_be_default_match = true;
  result_view()->SetMatch(match);
  ui::AXNodeData result_node_data;
  result_view()->GetViewAccessibility().GetAccessibleNodeData(
      &result_node_data);
  EXPECT_EQ(result_node_data.role, ax::mojom::Role::kListBoxOption);
  EXPECT_EQ(
      result_node_data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet),
      int{kTestResultViewIndex} + 1);

  int result_size = static_cast<int>(
      popup_view()->controller()->autocomplete_controller()->result().size());
  EXPECT_EQ(result_size, result_node_data.GetIntAttribute(
                             ax::mojom::IntAttribute::kSetSize));

  // Check accessibility of list box.
  ui::AXNodeData popup_node_data;
  popup_view()->GetViewAccessibility().GetAccessibleNodeData(&popup_node_data);
  EXPECT_EQ(popup_node_data.role, ax::mojom::Role::kListBox);
  EXPECT_FALSE(popup_node_data.HasState(ax::mojom::State::kExpanded));
  EXPECT_TRUE(popup_node_data.HasState(ax::mojom::State::kCollapsed));
  EXPECT_TRUE(popup_node_data.HasState(ax::mojom::State::kInvisible));
  EXPECT_FALSE(
      popup_node_data.HasIntAttribute(ax::mojom::IntAttribute::kPopupForId));
}

TEST_F(OmniboxResultViewTest, StarterPackMatch) {
  AutocompleteMatch match(nullptr, 1350, false,
                          AutocompleteMatchType::STARTER_PACK);
  result_view()->SetMatch(match);
  // No assertions necessary; just exercising code paths for starter pack match.
}

TEST_F(OmniboxResultViewTest, FeaturedEnterpriseSearchMatch) {
  AutocompleteMatch match(nullptr, 1350, false,
                          AutocompleteMatchType::FEATURED_ENTERPRISE_SEARCH);
  result_view()->SetMatch(match);
  // No assertions necessary; just exercising code paths for featured Enterprise
  // search match.
}