File: zoom_view_interactive_ui_tests.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (187 lines) | stat: -rw-r--r-- 7,726 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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_view.h"
#include "chrome/browser/ui/views/page_action/page_action_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_button.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "components/zoom/zoom_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/page_zoom.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/state_observer.h"
#include "ui/base/models/image_model.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"

namespace zoom {
namespace {

class ZoomViewInteractiveUiTest : public InteractiveBrowserTest {
 public:
  ZoomViewInteractiveUiTest() {
    scoped_feature_list_.InitAndEnableFeatureWithParameters(
        features::kPageActionsMigration,
        {{features::kPageActionsMigrationZoom.name, "true"}});
  }

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

  ~ZoomViewInteractiveUiTest() override = default;

 protected:
  int GetZoomPercent() {
    content::WebContents* web_contents =
        browser()->tab_strip_model()->GetActiveWebContents();

    return ZoomController::FromWebContents(web_contents)->GetZoomPercent();
  }

  void SetZoomLevel(content::PageZoom zoom_level) {
    chrome::Zoom(browser(), zoom_level);
  }

  auto DoZoomIn() {
    return Do([&]() { SetZoomLevel(content::PAGE_ZOOM_IN); });
  }

  auto DoZoomOut() {
    return Do([&]() { SetZoomLevel(content::PAGE_ZOOM_OUT); });
  }

  auto DoZoomReset() {
    return Do([&]() { SetZoomLevel(content::PAGE_ZOOM_RESET); });
  }

  auto WaitForZoomBubbleShow() { return WaitForZoomBubble(/*visible=*/true); }

  auto WaitForZoomBubbleHide() { return WaitForZoomBubble(/*visible=*/false); }

  MultiStep WaitForZoomBubble(bool visible) {
    DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(ui::test::PollingStateObserver<bool>,
                                        kZoomBubbleVisible);

    return Steps(PollState(kZoomBubbleVisible,
                           [&, visible]() {
                             bool is_visible =
                                 ZoomBubbleView::GetZoomBubble() != nullptr;
                             return is_visible == visible;
                           }),
                 WaitForState(kZoomBubbleVisible, true),
                 StopObservingState(kZoomBubbleVisible));
  }

  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(ZoomViewInteractiveUiTest, ZoomStateUpdates) {
  // We'll store the zoom-in image so we can compare it later to the zoom-out
  // image.
  ui::ImageModel zoom_in_image;
  RunTestSequence(
      WaitForZoomBubbleHide(), DoZoomIn(),
      WaitForShow(kActionItemZoomElementId),
      CheckViewProperty(kActionItemZoomElementId,
                        &page_actions::PageActionView::GetTooltipText,
                        u"Zoom: 110%"),
      CheckResult([&]() { return GetZoomPercent(); }, testing::Eq(110)),
      WithView(kActionItemZoomElementId,
               [&](page_actions::PageActionView* page_action_view) {
                 zoom_in_image =
                     page_action_view
                         ->GetImageModel(views::Button::STATE_NORMAL)
                         .value();
               }),
      WaitForZoomBubbleShow(), DoZoomReset(), WaitForZoomBubbleShow(),
      CheckResult([&]() { return GetZoomPercent(); }, testing::Eq(100)),
      DoZoomOut(), WaitForShow(kActionItemZoomElementId),
      CheckViewProperty(kActionItemZoomElementId,
                        &page_actions::PageActionView::GetTooltipText,
                        u"Zoom: 90%"),
      CheckResult([&]() { return GetZoomPercent(); }, testing::Eq(90)),
      CheckView(kActionItemZoomElementId,
                [&](page_actions::PageActionView* page_action_view) {
                  return page_action_view
                             ->GetImageModel(views::Button::STATE_NORMAL)
                             .value() != zoom_in_image;
                }),
      WaitForZoomBubbleShow());
}

IN_PROC_BROWSER_TEST_F(ZoomViewInteractiveUiTest,
                       ShowAndHideZoomBubbleByClickWithMouse) {
  RunTestSequence(WaitForZoomBubbleHide(), DoZoomIn(),
                  WaitForShow(kActionItemZoomElementId),
                  MoveMouseTo(kActionItemZoomElementId), ClickMouse(),
                  WaitForZoomBubbleShow(),
                  MoveMouseTo(kActionItemZoomElementId), ClickMouse(),
                  WaitForZoomBubbleHide(),
                  MoveMouseTo(kActionItemZoomElementId), ClickMouse(),
                  WaitForZoomBubbleShow());
}

IN_PROC_BROWSER_TEST_F(ZoomViewInteractiveUiTest,
                       ShowAndHideZoomBubbleByClickWithKeyboardPress) {
  RunTestSequence(
      WaitForZoomBubbleHide(), DoZoomIn(),
      WaitForShow(kActionItemZoomElementId),
      PressButton(kActionItemZoomElementId), WaitForZoomBubbleShow(),
      PressButton(kActionItemZoomElementId), WaitForZoomBubbleHide(),
      PressButton(kActionItemZoomElementId), WaitForZoomBubbleShow());
}

// Verifies that after a "Reset", and then after the closing of the
// bubble, the page-action icon disappears immediately.
IN_PROC_BROWSER_TEST_F(ZoomViewInteractiveUiTest,
                       IconHidesAfterResetAndBubbleClose) {
  RunTestSequence(
      WaitForZoomBubbleHide(), DoZoomIn(),
      WaitForShow(kActionItemZoomElementId),
      MoveMouseTo(kActionItemZoomElementId), ClickMouse(),
      WaitForZoomBubbleShow(), DoZoomReset(),
      CheckResult([&] { return GetZoomPercent(); }, testing::Eq(100)),
      WaitForZoomBubbleShow(), MoveMouseTo(kActionItemZoomElementId),
      ClickMouse(), WaitForZoomBubbleHide(),
      WaitForHide(kActionItemZoomElementId));
}

// Verifies that if the user resets to 100 % *without* opening the bubble
// first, the icon is hidden right after the reset, independent of the
// bubble’s existence.
IN_PROC_BROWSER_TEST_F(ZoomViewInteractiveUiTest,
                       IconHidesImmediatelyAfterProgrammaticReset) {
  RunTestSequence(WaitForZoomBubbleHide(), DoZoomIn(),
                  WaitForShow(kActionItemZoomElementId), DoZoomReset(),
                  WaitForHide(kActionItemZoomElementId),
                  WaitForZoomBubbleHide());
}

// Verifies that the icon remains hidden after an auto-closed bubble at
// default zoom.
IN_PROC_BROWSER_TEST_F(ZoomViewInteractiveUiTest,
                       IconStaysHiddenAfterAutoCloseAtDefaultZoom) {
  RunTestSequence(WaitForZoomBubbleHide(), DoZoomIn(),
                  MoveMouseTo(kActionItemZoomElementId), ClickMouse(),
                  WaitForZoomBubbleShow(), DoZoomReset(),
                  CheckResult([&] { return GetZoomPercent(); }, 100),
                  WaitForZoomBubbleHide(),
                  WaitForHide(kActionItemZoomElementId));
}

}  // namespace
}  // namespace zoom