File: tab_alert_controller_unittest.cc

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (247 lines) | stat: -rw-r--r-- 10,733 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
// 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/tabs/alert/tab_alert_controller.h"

#include <memory>
#include <optional>

#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/ui/browser_window/test/mock_browser_window_interface.h"
#include "chrome/browser/ui/tabs/alert/tab_alert.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/vr/vr_tab_helper.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/unowned_user_data/unowned_user_data_host.h"

namespace tabs {

class FakeBrowserWindowInterface : public MockBrowserWindowInterface {
 public:
  ~FakeBrowserWindowInterface() override = default;
  explicit FakeBrowserWindowInterface(Profile* profile) : profile_(profile) {}
  Profile* GetProfile() override { return profile_; }

 private:
  raw_ptr<Profile> profile_ = nullptr;
};

class MockTabAlertControllerSubscriber {
 public:
  MockTabAlertControllerSubscriber() = default;

  MOCK_METHOD1(OnPrioritizedAlertStateChanged,
               void(std::optional<TabAlert> new_alert));
};

class TabAlertControllerTest : public testing::Test {
 public:
  void SetUp() override {
    testing_profile_manager_ = std::make_unique<TestingProfileManager>(
        TestingBrowserProcess::GetGlobal());
    ASSERT_TRUE(testing_profile_manager_->SetUp());
    TestingBrowserProcess::GetGlobal()->CreateGlobalFeaturesForTesting();
    profile_ = testing_profile_manager_->CreateTestingProfile("profile");
    browser_window_interface_ =
        std::make_unique<FakeBrowserWindowInterface>(profile_);
    tab_strip_model_delegate_ = std::make_unique<TestTabStripModelDelegate>();
    tab_strip_model_delegate_->SetBrowserWindowInterface(
        browser_window_interface_.get());
    tab_strip_model_ = std::make_unique<TabStripModel>(
        tab_strip_model_delegate_.get(), profile_);
    EXPECT_CALL(*browser_window_interface_, GetTabStripModel())
        .WillRepeatedly(testing::Return(tab_strip_model_.get()));
    std::unique_ptr<content::WebContents> web_contents =
        content::WebContentsTester::CreateTestWebContents(profile_, nullptr);
    tab_model_ = std::make_unique<TabModel>(std::move(web_contents),
                                            tab_strip_model_.get());
  }

  void TearDown() override {
    // Explicitly reset the pointers to prevent them from causing the
    // BrowserTaskEnvironment to time out on destruction.
    tab_model_.reset();
    tab_strip_model_.reset();
    tab_strip_model_delegate_.reset();
    browser_window_interface_.reset();
    profile_ = nullptr;
    TestingBrowserProcess::GetGlobal()->GetFeatures()->Shutdown();
    testing_profile_manager_.reset();
  }

  TabAlertController* tab_alert_controller() {
    return tabs::TabAlertController::From(tab_model_.get());
  }

  TabInterface* tab_interface() { return tab_model_.get(); }

  void SimulateAudioState(bool is_playing_audio) {
    content::WebContentsTester::For(tab_model_->GetContents())
        ->SetIsCurrentlyAudible(is_playing_audio);
  }

  content::BrowserTaskEnvironment* task_environment() {
    return &task_environment_;
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
  content::BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  content::RenderViewHostTestEnabler test_enabler_;
  std::unique_ptr<TestingProfileManager> testing_profile_manager_;
  raw_ptr<Profile> profile_ = nullptr;
  std::unique_ptr<FakeBrowserWindowInterface> browser_window_interface_;
  std::unique_ptr<TestTabStripModelDelegate> tab_strip_model_delegate_;
  std::unique_ptr<TabStripModel> tab_strip_model_;
  std::unique_ptr<TabModel> tab_model_;
};

TEST_F(TabAlertControllerTest, NotifiedOnAlertShouldShowChanged) {
  auto mock_subscriber = std::make_unique<MockTabAlertControllerSubscriber>();
  auto subscription =
      tab_alert_controller()->AddAlertToShowChangedCallback(base::BindRepeating(
          &MockTabAlertControllerSubscriber::OnPrioritizedAlertStateChanged,
          base::Unretained(mock_subscriber.get())));

  // Activating an alert should notify observers since it will be the only
  // tab alert active.
  EXPECT_CALL(*mock_subscriber,
              OnPrioritizedAlertStateChanged(
                  std::make_optional(TabAlert::AUDIO_PLAYING)));
  SimulateAudioState(true);
  ::testing::Mock::VerifyAndClearExpectations(mock_subscriber.get());

  // Simulate a higher priority alert being activated.
  EXPECT_CALL(*mock_subscriber, OnPrioritizedAlertStateChanged(
                                    std::make_optional(TabAlert::PIP_PLAYING)));
  tab_alert_controller()->MediaPictureInPictureChanged(true);
  ::testing::Mock::VerifyAndClearExpectations(mock_subscriber.get());
  EXPECT_EQ(tab_alert_controller()->GetAlertToShow(), TabAlert::PIP_PLAYING);

  // Removing a lower priority tab alert shouldn't notify observers since the
  // prioritized alert wouldn't change.
  EXPECT_CALL(*mock_subscriber,
              OnPrioritizedAlertStateChanged(std::optional<TabAlert>()))
      .Times(0);
  SimulateAudioState(false);
  task_environment()->FastForwardBy(base::Seconds(2));
  ::testing::Mock::VerifyAndClearExpectations(mock_subscriber.get());

  // Remove the last active tab alert.
  EXPECT_CALL(*mock_subscriber, OnPrioritizedAlertStateChanged(testing::_));
  tab_alert_controller()->MediaPictureInPictureChanged(false);
  testing::Mock::VerifyAndClearExpectations(mock_subscriber.get());
  EXPECT_EQ(tab_alert_controller()->GetAlertToShow(), std::nullopt);
}

TEST_F(TabAlertControllerTest, GetAllAlert) {
  SimulateAudioState(true);
  tab_alert_controller()->OnCapabilityTypesChanged(
      content::WebContentsCapabilityType::kBluetoothConnected, true);
  tab_alert_controller()->MediaPictureInPictureChanged(true);
  tab_alert_controller()->DidUpdateAudioMutingState(true);

  std::optional<TabAlert> prioritized_alert =
      tab_alert_controller()->GetAlertToShow();
  ASSERT_TRUE(prioritized_alert.has_value());
  EXPECT_EQ(prioritized_alert.value(), TabAlert::BLUETOOTH_CONNECTED);
  EXPECT_EQ(tab_alert_controller()->GetAllActiveAlerts().size(), 4U);

  // Verify that the active alerts list is in sorted order
  std::vector<TabAlert> active_alerts =
      tab_alert_controller()->GetAllActiveAlerts();
  EXPECT_EQ(active_alerts[0], TabAlert::BLUETOOTH_CONNECTED);
  EXPECT_EQ(active_alerts[1], TabAlert::PIP_PLAYING);
  EXPECT_EQ(active_alerts[2], TabAlert::AUDIO_MUTING);
  EXPECT_EQ(active_alerts[3], TabAlert::AUDIO_PLAYING);
}

TEST_F(TabAlertControllerTest, AlertIsActive) {
  SimulateAudioState(true);
  tab_alert_controller()->OnCapabilityTypesChanged(
      content::WebContentsCapabilityType::kBluetoothConnected, true);
  tab_alert_controller()->MediaPictureInPictureChanged(true);

  EXPECT_TRUE(tab_alert_controller()->IsAlertActive(TabAlert::AUDIO_PLAYING));
  EXPECT_TRUE(
      tab_alert_controller()->IsAlertActive(TabAlert::BLUETOOTH_CONNECTED));
  EXPECT_TRUE(tab_alert_controller()->IsAlertActive(TabAlert::PIP_PLAYING));

  // When the non-prioritized alert is no longer active, the alert controller
  // should be updated to reflect that.
  tab_alert_controller()->MediaPictureInPictureChanged(false);
  EXPECT_FALSE(tab_alert_controller()->IsAlertActive(TabAlert::PIP_PLAYING));
}

TEST_F(TabAlertControllerTest, VrStateUpdatesAlertController) {
  EXPECT_FALSE(tab_alert_controller()->GetAlertToShow().has_value());
  vr::VrTabHelper* const vr_tab_helper =
      vr::VrTabHelper::FromWebContents(tab_interface()->GetContents());
  vr_tab_helper->SetIsContentDisplayedInHeadset(true);
  EXPECT_TRUE(tab_alert_controller()->GetAlertToShow().has_value());
  EXPECT_EQ(tab_alert_controller()->GetAlertToShow().value(),
            TabAlert::VR_PRESENTING_IN_HEADSET);
  vr_tab_helper->SetIsContentDisplayedInHeadset(false);
  EXPECT_FALSE(tab_alert_controller()->GetAlertToShow().has_value());
}

TEST_F(TabAlertControllerTest, AudioStateUpdatesAlertController) {
  EXPECT_FALSE(tab_alert_controller()->GetAlertToShow().has_value());
  SimulateAudioState(true);
  EXPECT_TRUE(tab_alert_controller()->GetAlertToShow().has_value());
  EXPECT_EQ(tab_alert_controller()->GetAlertToShow().value(),
            TabAlert::AUDIO_PLAYING);

  // The audio playing alert should still be active even though the audio has
  // stopped to prevent the audio state from toggling too frequently on pause.
  SimulateAudioState(false);
  EXPECT_TRUE(tab_alert_controller()->GetAlertToShow().has_value());
  EXPECT_EQ(tab_alert_controller()->GetAlertToShow().value(),
            TabAlert::AUDIO_PLAYING);

  // The tab alert should go away after 2 seconds of consistently not playing
  // audio.
  task_environment()->FastForwardBy(base::Seconds(2));
  EXPECT_FALSE(tab_alert_controller()->GetAlertToShow().has_value());
}

TEST_F(TabAlertControllerTest, MutedStateReliesOnRecentlyAudible) {
  EXPECT_FALSE(tab_alert_controller()->GetAlertToShow().has_value());
  tab_interface()->GetContents()->SetAudioMuted(true);
  // Even though the tab is muted, since it wasn't recently audible, the muted
  // tab alert shouldn't be active.
  EXPECT_FALSE(tab_alert_controller()->GetAlertToShow().has_value());

  // Simulating the tab to be audible should trigger the muted alert to be
  // active since the tab was already muted.
  SimulateAudioState(true);
  EXPECT_EQ(tab_alert_controller()->GetAlertToShow(), TabAlert::AUDIO_MUTING);

  // Turning off the audio state shouldn't immediately deactivate the muted
  // alert since the tab is still recently audible.
  SimulateAudioState(false);
  EXPECT_EQ(tab_alert_controller()->GetAlertToShow(), TabAlert::AUDIO_MUTING);

  // After waiting until the tab is no longer recently audible, the muted alert
  // state should go away.
  task_environment()->FastForwardBy(base::Seconds(2));
  EXPECT_FALSE(tab_alert_controller()->GetAlertToShow().has_value());
}
}  // namespace tabs