File: page_action_observer_unittest.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (312 lines) | stat: -rw-r--r-- 12,000 bytes parent folder | download | duplicates (4)
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
// 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 "chrome/browser/ui/views/page_action/page_action_observer.h"

#include <optional>
#include <string>

#include "chrome/browser/ui/views/page_action/page_action_controller.h"
#include "chrome/browser/ui/views/page_action/page_action_model.h"
#include "chrome/browser/ui/views/page_action/page_action_model_observer.h"
#include "chrome/browser/ui/views/page_action/test_support/fake_tab_interface.h"
#include "chrome/browser/ui/views/page_action/test_support/mock_page_action_model.h"
#include "chrome/browser/ui/views/page_action/test_support/noop_page_action_metrics_recorder.h"
#include "chrome/browser/ui/views/page_action/test_support/test_page_action_properties_provider.h"
#include "chrome/test/base/testing_profile.h"
#include "components/tabs/public/mock_tab_interface.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::Return;
using testing::ReturnRef;

namespace page_actions {
namespace {

static const std::u16string kTestLabel = u"Test Label";
static const std::u16string kTestTooltip = u"Test Tooltip";

static constexpr actions::ActionId kTestPageActionId = 0;
static const PageActionPropertiesMap kTestProperties = PageActionPropertiesMap{
    {
        kTestPageActionId,
        PageActionProperties{
            .histogram_name = "Test",
        },
    },
};

PageActionState ExpectedShowingState() {
  PageActionState expected_state;
  expected_state.action_id = kTestPageActionId;
  expected_state.showing = true;
  expected_state.tooltip = std::make_optional(kTestTooltip);
  return expected_state;
}

PageActionState ExpectedHiddenState() {
  PageActionState expected_state;
  expected_state.action_id = kTestPageActionId;
  return expected_state;
}

PageActionState ExpectedShowingChipState() {
  PageActionState expected_state;
  expected_state.action_id = kTestPageActionId;
  expected_state.showing = true;
  expected_state.chip_showing = true;
  expected_state.label = std::make_optional(kTestLabel);
  expected_state.tooltip = std::make_optional(kTestTooltip);
  return expected_state;
}

class MockPageActionObserver : public PageActionObserver {
 public:
  MockPageActionObserver() : PageActionObserver(kTestPageActionId) {}
  ~MockPageActionObserver() override = default;

  MOCK_METHOD(void,
              OnPageActionIconShown,
              (const PageActionState&),
              (override));
  MOCK_METHOD(void,
              OnPageActionIconHidden,
              (const PageActionState&),
              (override));
  MOCK_METHOD(void,
              OnPageActionChipShown,
              (const PageActionState&),
              (override));
  MOCK_METHOD(void,
              OnPageActionChipHidden,
              (const PageActionState&),
              (override));
};

class NotifierPageActionModel : public MockPageActionModel {
 public:
  NotifierPageActionModel() = default;
  ~NotifierPageActionModel() override {
    observer_list_.Notify(
        &PageActionModelObserver::OnPageActionModelWillBeDeleted, *this);
  }

  void AddObserver(PageActionModelObserver* observer) override {
    observer_list_.AddObserver(observer);
  }

  void RemoveObserver(PageActionModelObserver* observer) override {
    observer_list_.RemoveObserver(observer);
  }

  void NotifyChanged() {
    observer_list_.Notify(&PageActionModelObserver::OnPageActionModelChanged,
                          *this);
  }

 private:
  base::ObserverList<PageActionModelObserver> observer_list_;
};

using MockPageActionModelFactory =
    FakePageActionModelFactory<NotifierPageActionModel>;

class PageActionObserverTest : public ::testing::Test {
 public:
  PageActionObserverTest() : tab_(nullptr) {}

  void SetUp() override {
    controller_ = std::make_unique<PageActionControllerImpl>(
        nullptr, &model_factory_, &metrics_factory_);
    controller_->Initialize(tab_, {kTestPageActionId},
                            TestPageActionPropertiesProvider(kTestProperties));
    ON_CALL(model_factory_.Get(kTestPageActionId), GetTooltipText())
        .WillByDefault(ReturnRef(kTestTooltip));
    ON_CALL(model_factory_.Get(kTestPageActionId), GetText())
        .WillByDefault(ReturnRef(kTestLabel));
  }

  MockPageActionObserver& observer() { return observer_; }
  PageActionController& controller() { return *controller_; }
  MockPageActionModelFactory& factory() { return model_factory_; }

 private:
  MockPageActionObserver observer_;
  FakeTabInterface tab_;
  MockPageActionModelFactory model_factory_;
  NoopPageActionMetricsRecorderFactory metrics_factory_;
  std::unique_ptr<PageActionControllerImpl> controller_;
};

TEST_F(PageActionObserverTest, OnPageActionIconShown) {
  NotifierPageActionModel& model = factory().Get(kTestPageActionId);
  ON_CALL(model, GetVisible()).WillByDefault(Return(false));
  observer().RegisterAsPageActionObserver(controller());

  ON_CALL(model, GetVisible()).WillByDefault(Return(true));
  EXPECT_CALL(observer(), OnPageActionIconShown(ExpectedShowingState()))
      .Times(1);
  model.NotifyChanged();

  // Further change notifications shouldn't trigger the event anymore.
  EXPECT_CALL(observer(), OnPageActionIconShown(_)).Times(0);
  model.NotifyChanged();
}

TEST_F(PageActionObserverTest, OnPageActionIconHidden) {
  NotifierPageActionModel& model = factory().Get(kTestPageActionId);
  ON_CALL(model, GetVisible()).WillByDefault(Return(true));
  observer().RegisterAsPageActionObserver(controller());

  ON_CALL(model, GetVisible()).WillByDefault(Return(false));
  EXPECT_CALL(observer(), OnPageActionIconHidden(ExpectedHiddenState()))
      .Times(1);
  model.NotifyChanged();

  // Further change notifications shouldn't trigger the event anymore.
  EXPECT_CALL(observer(), OnPageActionIconHidden(_)).Times(0);
  model.NotifyChanged();
}

TEST_F(PageActionObserverTest, OnPageActionChipShown) {
  NotifierPageActionModel& model = factory().Get(kTestPageActionId);
  ON_CALL(model, GetVisible()).WillByDefault(Return(true));
  ON_CALL(model, ShouldShowSuggestionChip()).WillByDefault(Return(false));
  ON_CALL(model, IsChipShowing()).WillByDefault(Return(false));
  observer().RegisterAsPageActionObserver(controller());

  ON_CALL(model, ShouldShowSuggestionChip()).WillByDefault(Return(true));
  ON_CALL(model, IsChipShowing()).WillByDefault(Return(true));
  EXPECT_CALL(observer(), OnPageActionChipShown(ExpectedShowingChipState()))
      .Times(1);
  EXPECT_CALL(observer(), OnPageActionChipHidden(_)).Times(0);
  model.NotifyChanged();

  // Further change notifications shouldn't trigger the event anymore.
  EXPECT_CALL(observer(), OnPageActionChipShown(_)).Times(0);
  EXPECT_CALL(observer(), OnPageActionChipHidden(_)).Times(0);
  model.NotifyChanged();
}

TEST_F(PageActionObserverTest, OnPageActionChipHidden) {
  NotifierPageActionModel& model = factory().Get(kTestPageActionId);
  ON_CALL(model, GetVisible()).WillByDefault(Return(true));
  ON_CALL(model, ShouldShowSuggestionChip()).WillByDefault(Return(true));
  ON_CALL(model, IsChipShowing()).WillByDefault(Return(true));
  observer().RegisterAsPageActionObserver(controller());

  ON_CALL(model, ShouldShowSuggestionChip()).WillByDefault(Return(false));
  ON_CALL(model, IsChipShowing()).WillByDefault(Return(false));
  EXPECT_CALL(observer(), OnPageActionChipHidden(ExpectedShowingState()))
      .Times(1);
  EXPECT_CALL(observer(), OnPageActionChipShown(_)).Times(0);
  model.NotifyChanged();

  // Further change notifications shouldn't trigger the event anymore.
  EXPECT_CALL(observer(), OnPageActionChipShown(_)).Times(0);
  EXPECT_CALL(observer(), OnPageActionChipHidden(_)).Times(0);
  model.NotifyChanged();
}

TEST_F(PageActionObserverTest,
       OnPageActionChipVisibilityConsidersIconVisibility) {
  NotifierPageActionModel& model = factory().Get(kTestPageActionId);
  ON_CALL(model, GetVisible()).WillByDefault(Return(false));
  ON_CALL(model, ShouldShowSuggestionChip()).WillByDefault(Return(true));
  ON_CALL(model, IsChipShowing()).WillByDefault(Return(true));
  observer().RegisterAsPageActionObserver(controller());

  ON_CALL(model, GetVisible()).WillByDefault(Return(true));
  EXPECT_CALL(observer(), OnPageActionChipShown(_)).Times(1);
  model.NotifyChanged();

  ON_CALL(model, GetVisible()).WillByDefault(Return(false));
  // Further change notifications shouldn't trigger the event anymore.
  EXPECT_CALL(observer(), OnPageActionChipHidden(_)).Times(1);
  model.NotifyChanged();
}

// When the icon is visible and the suggestion-chip request is true, flipping
// ShouldShowSuggestionChip() from false → true should be reported as “chip
// shown”.
TEST_F(PageActionObserverTest, ChipExpandedTrueTriggersChipShown) {
  NotifierPageActionModel& model = factory().Get(kTestPageActionId);

  // Icon visible, chip requested, but not yet expanded.
  ON_CALL(model, GetVisible()).WillByDefault(Return(true));
  ON_CALL(model, ShouldShowSuggestionChip()).WillByDefault(Return(true));
  ON_CALL(model, IsChipShowing()).WillByDefault(Return(false));

  observer().RegisterAsPageActionObserver(controller());

  // Flip to expanded.
  ON_CALL(model, ShouldShowSuggestionChip()).WillByDefault(Return(true));
  ON_CALL(model, IsChipShowing()).WillByDefault(Return(true));
  EXPECT_CALL(observer(), OnPageActionChipShown(ExpectedShowingChipState()))
      .Times(1);
  EXPECT_CALL(observer(), OnPageActionChipHidden(_)).Times(0);

  model.NotifyChanged();

  // Further notifications with no state change → no callbacks.
  EXPECT_CALL(observer(), OnPageActionChipShown(_)).Times(0);
  EXPECT_CALL(observer(), OnPageActionChipHidden(_)).Times(0);
  model.NotifyChanged();
}

// When the chip is currently expanded, collapsing it (true → false) – while the
// request to show a chip remains – must be reported as “chip hidden”.
TEST_F(PageActionObserverTest, ChipExpandedFalseTriggersChipHidden) {
  NotifierPageActionModel& model = factory().Get(kTestPageActionId);

  // Icon + expanded chip showing.
  ON_CALL(model, GetVisible()).WillByDefault(Return(true));
  ON_CALL(model, ShouldShowSuggestionChip()).WillByDefault(Return(true));
  ON_CALL(model, IsChipShowing()).WillByDefault(Return(true));

  observer().RegisterAsPageActionObserver(controller());

  // Collapse the chip.
  ON_CALL(model, ShouldShowSuggestionChip()).WillByDefault(Return(false));
  ON_CALL(model, IsChipShowing()).WillByDefault(Return(false));
  EXPECT_CALL(observer(), OnPageActionChipHidden(ExpectedShowingState()))
      .Times(1);
  EXPECT_CALL(observer(), OnPageActionChipShown(_)).Times(0);

  model.NotifyChanged();

  // No more callbacks on further unchanged notifications.
  EXPECT_CALL(observer(), OnPageActionChipShown(_)).Times(0);
  EXPECT_CALL(observer(), OnPageActionChipHidden(_)).Times(0);
  model.NotifyChanged();
}

// Ensures that the initial state of a page action model is applied when
// registering as the observer. Further change notifications shouldn't
// have an effect.
TEST_F(PageActionObserverTest, PageActionInitialStateShown) {
  NotifierPageActionModel& model = factory().Get(kTestPageActionId);
  ON_CALL(model, GetVisible()).WillByDefault(Return(true));
  observer().RegisterAsPageActionObserver(controller());

  // Further change notifications shouldn't trigger the event anymore.
  EXPECT_CALL(observer(), OnPageActionIconShown(_)).Times(0);
  model.NotifyChanged();
}

TEST_F(PageActionObserverTest, PageActionInitialStateHidden) {
  NotifierPageActionModel& model = factory().Get(kTestPageActionId);
  ON_CALL(model, GetVisible()).WillByDefault(Return(false));
  observer().RegisterAsPageActionObserver(controller());

  // Further change notifications shouldn't trigger the event anymore.
  EXPECT_CALL(observer(), OnPageActionIconShown(_)).Times(0);
  model.NotifyChanged();
}

}  // namespace
}  // namespace page_actions