File: page_action_observer_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (247 lines) | stat: -rw-r--r-- 9,518 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
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
// 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 "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"

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(testing::ReturnRef(kTestTooltip));
    ON_CALL(model_factory_.Get(kTestPageActionId), GetText())
        .WillByDefault(testing::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(testing::Return(false));
  observer().RegisterAsPageActionObserver(controller());

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

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

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

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

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

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

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

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

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

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

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

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

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

  ON_CALL(model, GetVisible()).WillByDefault(testing::Return(false));
  // Further change notifications shouldn't trigger the event anymore.
  EXPECT_CALL(observer(), OnPageActionChipHidden(testing::_)).Times(1);
  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(testing::Return(true));
  observer().RegisterAsPageActionObserver(controller());

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

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

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

}  // namespace
}  // namespace page_actions