File: audio_lifetime_enforcer_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (229 lines) | stat: -rw-r--r-- 7,897 bytes parent folder | download | duplicates (2)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/api/offscreen/audio_lifetime_enforcer.h"

#include <optional>

#include "base/run_loop.h"
#include "base/test/bind.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "components/version_info/channel.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/offscreen_document_host.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/extension.h"
#include "extensions/test/test_extension_dir.h"

namespace extensions {

namespace {

// A helper class to wait until a given WebContents is audible or inaudible.
class AudioWaiter : public content::WebContentsObserver {
 public:
  explicit AudioWaiter(content::WebContents* contents)
      : content::WebContentsObserver(contents) {}

  void WaitForAudible() {
    DCHECK(!expected_state_);

    if (web_contents()->IsCurrentlyAudible())
      return;

    expected_state_ = true;
    run_loop_.Run();
  }

  void WaitForInaudible() {
    DCHECK(!expected_state_);

    if (!web_contents()->IsCurrentlyAudible())
      return;

    expected_state_ = false;
    run_loop_.Run();
  }

 private:
  void OnAudioStateChanged(bool audible) override {
    if (!expected_state_.has_value()) {
      // We aren't waiting for an event yet, so don't quit the run loop.
      // Otherwise, we'd quit it now, and it would immediately quit when we
      // later try to wait for it in WaitFor(In)Audible().
      return;
    }

    // Otherwise, the expected state should be equal to the new state (since
    // there are only two possible states, and if it were already in the
    // appropriate state, we wouldn't have waited).
    EXPECT_EQ(expected_state_, audible);
    run_loop_.QuitWhenIdle();
  }

  base::RunLoop run_loop_;

  // The eventual desired state.
  std::optional<bool> expected_state_;
};

}  // namespace

class AudioLifetimeEnforcerBrowserTest : public ExtensionApiTest {
 public:
  AudioLifetimeEnforcerBrowserTest() = default;
  ~AudioLifetimeEnforcerBrowserTest() override = default;

  // Creates a new OffscreenDocumentHost and waits for it to load.
  std::unique_ptr<OffscreenDocumentHost> CreateOffscreenDocument(
      const Extension& extension,
      const GURL& url) {
    scoped_refptr<content::SiteInstance> site_instance =
        ProcessManager::Get(profile())->GetSiteInstanceForURL(url);

    content::TestNavigationObserver navigation_observer(url);
    navigation_observer.StartWatchingNewWebContents();
    auto offscreen_document = std::make_unique<OffscreenDocumentHost>(
        extension, site_instance.get(), profile(), url);
    offscreen_document->CreateRendererSoon();
    navigation_observer.Wait();
    EXPECT_TRUE(navigation_observer.last_navigation_succeeded());

    return offscreen_document;
  }

  scoped_refptr<const Extension> LoadOffscreenDocumentExtension() {
    static constexpr char kManifest[] =
        R"({
             "name": "Offscreen Document Test",
             "manifest_version": 3,
             "version": "0.1",
             "permissions": ["offscreen"]
           })";
    test_dir_.WriteManifest(kManifest);
    test_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), "// Blank.");
    test_dir_.WriteFile(FILE_PATH_LITERAL("offscreen.html"),
                        "<html>offscreen</html>");

    return LoadExtension(test_dir_.UnpackedPath());
  }

 private:
  TestExtensionDir test_dir_;
};

// Tests that an offscreen document is considered active while playing audio and
// notifies of inactivity when audio stops.
IN_PROC_BROWSER_TEST_F(AudioLifetimeEnforcerBrowserTest,
                       DocumentActiveWhilePlayingAudio) {
  scoped_refptr<const Extension> extension = LoadOffscreenDocumentExtension();
  ASSERT_TRUE(extension);

  const GURL offscreen_url = extension->GetResourceURL("offscreen.html");
  std::unique_ptr<OffscreenDocumentHost> offscreen_document =
      CreateOffscreenDocument(*extension, offscreen_url);

  content::WebContents* contents = offscreen_document->host_contents();

  bool terminate_called = false;
  int notify_inactive_calls = 0;
  auto on_notify_inactive = [&notify_inactive_calls]() {
    ++notify_inactive_calls;
  };
  auto on_terminate_called = [&terminate_called]() { terminate_called = true; };
  AudioLifetimeEnforcer audio_enforcer(
      offscreen_document.get(), base::BindLambdaForTesting(on_terminate_called),
      base::BindLambdaForTesting(on_notify_inactive));

  // On creation, the document is considered active (we give it some leeway
  // because the audio needs to load).
  EXPECT_TRUE(audio_enforcer.IsActive());
  EXPECT_EQ(0, notify_inactive_calls);

  // Load an audio tag and play it.
  static constexpr char kPlayAudio[] =
      R"(const audioTag = document.createElement('audio');
         audioTag.src = '_test_resources/long_audio.ogg';
         document.body.appendChild(audioTag);
         audioTag.play();
         'done';)";

  {
    AudioWaiter audio_waiter(contents);
    EXPECT_EQ("done", content::EvalJs(contents, kPlayAudio));
    audio_waiter.WaitForAudible();
  }

  // The document should be considered active.
  EXPECT_EQ(0, notify_inactive_calls);
  EXPECT_FALSE(terminate_called);
  EXPECT_TRUE(audio_enforcer.IsActive());

  // Next, prepare to stop the audio.
  static constexpr char kStopAudio[] =
      R"(document.body.getElementsByTagName('audio')[0].pause();
         'done';)";

  // Override the timeout. We can't do this at the top of the test because
  // otherwise, the document would immediately be considered inactive.
  auto timeout_override =
      AudioLifetimeEnforcer::SetTimeoutForTesting(base::Seconds(0));

  {
    // Stop the audio.
    AudioWaiter audio_waiter(contents);
    EXPECT_TRUE(content::ExecJs(contents, kStopAudio));
    audio_waiter.WaitForInaudible();
  }

  // The document should no longer be active.
  EXPECT_EQ(1, notify_inactive_calls);
  EXPECT_FALSE(audio_enforcer.IsActive());
  EXPECT_FALSE(terminate_called);
}

// Tests that an offscreen document is considered inactive if it never plays
// audio.
IN_PROC_BROWSER_TEST_F(AudioLifetimeEnforcerBrowserTest,
                       DocumentInactiveIfNeverPlayedAudio) {
  // Override the timeout to be immediate.
  auto timeout_override =
      AudioLifetimeEnforcer::SetTimeoutForTesting(base::Seconds(0));

  scoped_refptr<const Extension> extension = LoadOffscreenDocumentExtension();
  ASSERT_TRUE(extension);

  const GURL offscreen_url = extension->GetResourceURL("offscreen.html");
  std::unique_ptr<OffscreenDocumentHost> offscreen_document =
      CreateOffscreenDocument(*extension, offscreen_url);

  bool terminate_called = false;
  int notify_inactive_calls = 0;
  auto on_notify_inactive = [&notify_inactive_calls]() {
    ++notify_inactive_calls;
  };
  auto on_terminate_called = [&terminate_called]() { terminate_called = true; };
  AudioLifetimeEnforcer audio_enforcer(
      offscreen_document.get(), base::BindLambdaForTesting(on_terminate_called),
      base::BindLambdaForTesting(on_notify_inactive));

  // On creation, the document is considered active...
  EXPECT_EQ(0, notify_inactive_calls);
  EXPECT_FALSE(terminate_called);
  EXPECT_TRUE(audio_enforcer.IsActive());

  // ... but if it doesn't play audio after a set amount of time (here, 0
  // seconds), it is inactive (and thus, terminated).
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1, notify_inactive_calls);
  EXPECT_FALSE(terminate_called);
  EXPECT_FALSE(audio_enforcer.IsActive());
}

}  // namespace extensions