File: sound_content_setting_observer_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (293 lines) | stat: -rw-r--r-- 10,162 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
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
// 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/content_settings/sound_content_setting_observer.h"

#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/recently_audible_helper.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/ukm/content/source_url_recorder.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/test/web_contents_tester.h"
#include "media/base/media_switches.h"
#include "testing/gtest/include/gtest/gtest.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/tabs/tab_utils.h"
#endif

namespace {

constexpr char kURL1[] = "http://google.com/";
constexpr char kURL2[] = "http://youtube.com/";
constexpr char kSiteMutedEvent[] = "Media.SiteMuted";
constexpr char kSiteMutedReason[] = "MuteReason";
#if !BUILDFLAG(IS_ANDROID)
constexpr char kChromeURL[] = "chrome://dino";
constexpr char kExtensionId[] = "extensionid";
#endif

}  // anonymous namespace

class SoundContentSettingObserverTest : public ChromeRenderViewHostTestHarness {
 public:
  SoundContentSettingObserverTest() {
    scoped_feature_list_.InitWithFeatures({media::kEnableTabMuting}, {});
  }

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

  ~SoundContentSettingObserverTest() override = default;

  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();

    RecentlyAudibleHelper::CreateForWebContents(web_contents());
    SoundContentSettingObserver::CreateForWebContents(web_contents());
    ukm::InitializeSourceUrlRecorderForWebContents(web_contents());
    host_content_settings_map_ = HostContentSettingsMapFactory::GetForProfile(
        Profile::FromBrowserContext(web_contents()->GetBrowserContext()));
    test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();

    NavigateAndCommit(GURL(kURL1));
  }

 protected:
  void ChangeSoundContentSettingTo(ContentSetting setting) {
    GURL url = web_contents()->GetLastCommittedURL();
    host_content_settings_map_->SetContentSettingDefaultScope(
        url, url, ContentSettingsType::SOUND, setting);
  }

  void ChangeDefaultSoundContentSettingTo(ContentSetting setting) {
    host_content_settings_map_->SetDefaultContentSetting(
        ContentSettingsType::SOUND, setting);
  }

  void SimulateAudioStarting() {
    SoundContentSettingObserver::FromWebContents(web_contents())
        ->OnAudioStateChanged(true);
  }

  void SimulateAudioPlaying() {
    content::WebContentsTester::For(web_contents())
        ->SetIsCurrentlyAudible(true);
  }

  bool RecordedSiteMuted() {
    auto entries = test_ukm_recorder_->GetEntriesByName(kSiteMutedEvent);
    return !entries.empty();
  }

  void ExpectRecordedForReason(SoundContentSettingObserver::MuteReason reason) {
    auto entries = test_ukm_recorder_->GetEntriesByName(kSiteMutedEvent);
    EXPECT_EQ(1u, entries.size());
    for (const ukm::mojom::UkmEntry* const entry : entries) {
      test_ukm_recorder_->ExpectEntrySourceHasUrl(entry, GURL(kURL1));
      test_ukm_recorder_->ExpectEntryMetric(entry, kSiteMutedReason, reason);
    }
  }

// TabMutedReason does not exist on Android.
#if !BUILDFLAG(IS_ANDROID)
  void SetMuteStateForReason(bool state, TabMutedReason reason) {
    SetTabAudioMuted(web_contents(), state, reason, kExtensionId);
  }
#endif

 private:
  raw_ptr<HostContentSettingsMap, DanglingUntriaged> host_content_settings_map_;
  std::unique_ptr<ukm::TestUkmRecorder> test_ukm_recorder_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(SoundContentSettingObserverTest, AudioMutingUpdatesWithContentSetting) {
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  // Block site.
  ChangeSoundContentSettingTo(CONTENT_SETTING_BLOCK);
  EXPECT_TRUE(web_contents()->IsAudioMuted());

  // Allow site.
  ChangeSoundContentSettingTo(CONTENT_SETTING_ALLOW);
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  // Set site to default.
  ChangeSoundContentSettingTo(CONTENT_SETTING_DEFAULT);
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  // Block by default.
  ChangeDefaultSoundContentSettingTo(CONTENT_SETTING_BLOCK);
  EXPECT_TRUE(web_contents()->IsAudioMuted());

  // Should not affect site if explicitly allowed.
  ChangeSoundContentSettingTo(CONTENT_SETTING_ALLOW);
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  // Change site back to default.
  ChangeSoundContentSettingTo(CONTENT_SETTING_DEFAULT);
  EXPECT_TRUE(web_contents()->IsAudioMuted());

  // Allow by default.
  ChangeDefaultSoundContentSettingTo(CONTENT_SETTING_ALLOW);
  EXPECT_FALSE(web_contents()->IsAudioMuted());
}

TEST_F(SoundContentSettingObserverTest, AudioMutingUpdatesWithNavigation) {
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  // Block for kURL1.
  ChangeSoundContentSettingTo(CONTENT_SETTING_BLOCK);
  EXPECT_TRUE(web_contents()->IsAudioMuted());

  // Should not be muted for kURL2.
  NavigateAndCommit(GURL(kURL2));
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  // Should be muted for kURL1.
  NavigateAndCommit(GURL(kURL1));
  EXPECT_TRUE(web_contents()->IsAudioMuted());
}

// TabMutedReason does not exist on Android.
#if !BUILDFLAG(IS_ANDROID)
TEST_F(SoundContentSettingObserverTest, DontMuteWhenUnmutedByExtension) {
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  // Mute kURL1 via content setting.
  ChangeSoundContentSettingTo(CONTENT_SETTING_BLOCK);
  EXPECT_TRUE(web_contents()->IsAudioMuted());

  // Unmute by extension.
  SetMuteStateForReason(false, TabMutedReason::EXTENSION);
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  // Navigating to a new URL and back to kURL1 should not mute the tab unmuted
  // by an extension.
  NavigateAndCommit(GURL(kURL2));
  EXPECT_FALSE(web_contents()->IsAudioMuted());
  NavigateAndCommit(GURL(kURL1));
  EXPECT_FALSE(web_contents()->IsAudioMuted());
}

TEST_F(SoundContentSettingObserverTest, DontUnmuteWhenMutedByExtension) {
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  SetMuteStateForReason(true, TabMutedReason::EXTENSION);
  EXPECT_TRUE(web_contents()->IsAudioMuted());

  // Navigating to a new URL should not unmute the tab muted by an extension.
  NavigateAndCommit(GURL(kURL2));
  EXPECT_TRUE(web_contents()->IsAudioMuted());
}

TEST_F(SoundContentSettingObserverTest, DontUnmuteWhenMutedByAudioIndicator) {
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  SetMuteStateForReason(true, TabMutedReason::AUDIO_INDICATOR);
  EXPECT_TRUE(web_contents()->IsAudioMuted());

  // Navigating to a new URL should not unmute the tab muted by audio indicator.
  NavigateAndCommit(GURL(kURL2));
  EXPECT_TRUE(web_contents()->IsAudioMuted());
}

TEST_F(SoundContentSettingObserverTest, DontUnmuteChromeTabWhenMuted) {
  NavigateAndCommit(GURL(kChromeURL));
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  SetMuteStateForReason(true, TabMutedReason::CONTENT_SETTING_CHROME);
  EXPECT_TRUE(web_contents()->IsAudioMuted());

  NavigateAndCommit(GURL(kChromeURL));
  EXPECT_TRUE(web_contents()->IsAudioMuted());
}

TEST_F(SoundContentSettingObserverTest,
       UnmuteChromeTabWhenNavigatingToNonChromeUrl) {
  NavigateAndCommit(GURL(kChromeURL));
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  SetMuteStateForReason(true, TabMutedReason::CONTENT_SETTING_CHROME);
  EXPECT_TRUE(web_contents()->IsAudioMuted());

  NavigateAndCommit(GURL(kURL1));
  EXPECT_FALSE(web_contents()->IsAudioMuted());
}

TEST_F(SoundContentSettingObserverTest,
       UnmuteNonChromeTabWhenNavigatingToChromeUrl) {
  NavigateAndCommit(GURL(kURL1));
  EXPECT_FALSE(web_contents()->IsAudioMuted());

  ChangeSoundContentSettingTo(CONTENT_SETTING_BLOCK);
  EXPECT_TRUE(web_contents()->IsAudioMuted());

  NavigateAndCommit(GURL(kChromeURL));
  EXPECT_FALSE(web_contents()->IsAudioMuted());
}
#endif  // !BUILDFLAG(IS_ANDROID)

TEST_F(SoundContentSettingObserverTest,
       UnmutedAudioPlayingDoesNotRecordSiteMuted) {
  // Play audio while sound content setting is allowed.
  ChangeSoundContentSettingTo(CONTENT_SETTING_ALLOW);
  SimulateAudioStarting();
  EXPECT_FALSE(RecordedSiteMuted());
}

TEST_F(SoundContentSettingObserverTest, MutedAudioBlockedBySiteException) {
  // Play audio while sound content setting is blocked.
  ChangeSoundContentSettingTo(CONTENT_SETTING_BLOCK);
  SimulateAudioStarting();
  EXPECT_TRUE(RecordedSiteMuted());
  ExpectRecordedForReason(
      SoundContentSettingObserver::MuteReason::kSiteException);
}

TEST_F(SoundContentSettingObserverTest,
       MutingAudioWhileSoundIsPlayingBlocksSound) {
  // Unmuted audio starts playing.
  SimulateAudioPlaying();
  // Sound is not blocked.
  EXPECT_FALSE(RecordedSiteMuted());
  // User mutes the site.
  ChangeSoundContentSettingTo(CONTENT_SETTING_BLOCK);
  // Sound is blocked.
  EXPECT_TRUE(RecordedSiteMuted());
  ExpectRecordedForReason(
      SoundContentSettingObserver::MuteReason::kSiteException);
}

TEST_F(SoundContentSettingObserverTest, MuteByDefaultRecordsCorrectly) {
  // Blocking audio via default content setting records properly.
  ChangeDefaultSoundContentSettingTo(CONTENT_SETTING_BLOCK);
  SimulateAudioStarting();
  EXPECT_TRUE(RecordedSiteMuted());
  ExpectRecordedForReason(
      SoundContentSettingObserver::MuteReason::kMuteByDefault);
}

TEST_F(SoundContentSettingObserverTest,
       MuteByDefaultAndExceptionRecordsException) {
  // Block audio via default and exception.
  ChangeSoundContentSettingTo(CONTENT_SETTING_BLOCK);
  ChangeDefaultSoundContentSettingTo(CONTENT_SETTING_BLOCK);
  SimulateAudioStarting();
  EXPECT_TRUE(RecordedSiteMuted());
  ExpectRecordedForReason(
      SoundContentSettingObserver::MuteReason::kSiteException);
}