File: web_app_audio_focus_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; 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 (227 lines) | stat: -rw-r--r-- 8,566 bytes parent folder | download | duplicates (6)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/scoped_feature_list.h"
#include "base/types/expected.h"
#include "base/unguessable_token.h"
#include "chrome/browser/apps/link_capturing/link_capturing_feature_test_support.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/media_session/public/cpp/features.h"
#include "ui/base/page_transition_types.h"
#include "url/gurl.h"
#include "url/third_party/mozilla/url_parse.h"
#include "url/url_canon.h"

namespace web_app {

namespace {

const char kAudioFocusTestPageURL[] =
    "/extensions/audio_focus_web_app/main.html";

}  // namespace

// WebAppAudioFocusBrowserTest test that PWAs have separate audio
// focus from the rest of the browser.
class WebAppAudioFocusBrowserTest : public WebAppBrowserTestBase {
 public:
  WebAppAudioFocusBrowserTest() = default;
  ~WebAppAudioFocusBrowserTest() override = default;

  void SetUp() override {
    std::vector<base::test::FeatureRefAndParams> features;
#if !BUILDFLAG(IS_CHROMEOS)
    features = apps::test::GetFeaturesToEnableLinkCapturingUX(
        apps::test::LinkCapturingFeatureVersion::kV2DefaultOn);
#endif
    features.emplace_back(media_session::features::kMediaSessionService,
                          base::FieldTrialParams());
    features.emplace_back(media_session::features::kAudioFocusEnforcement,
                          base::FieldTrialParams());
    features.emplace_back(media_session::features::kAudioFocusSessionGrouping,
                          base::FieldTrialParams());

    scoped_feature_list_.InitWithFeaturesAndParameters(
        features, /*disabled_features=*/{});

    WebAppBrowserTestBase::SetUp();
  }

  bool IsPaused(content::WebContents* web_contents) {
    return content::EvalJs(web_contents, "isPaused()").ExtractBool();
  }

  bool WaitForPause(content::WebContents* web_contents) {
    return content::EvalJs(web_contents, "waitForPause()").ExtractBool();
  }

  bool StartPlaying(content::WebContents* web_contents) {
    return content::EvalJs(web_contents, "startPlaying()").ExtractBool();
  }

  content::WebContents* AddTestPageTabAtIndex(int index) {
    EXPECT_TRUE(AddTabAtIndex(
        index, embedded_test_server()->GetURL(kAudioFocusTestPageURL),
        ui::PAGE_TRANSITION_TYPED));
    content::WebContents* tab =
        browser()->tab_strip_model()->GetActiveWebContents();
    EXPECT_TRUE(content::WaitForLoadStop(tab));
    return tab;
  }

  const base::UnguessableToken& GetAudioFocusGroupId(
      content::WebContents* web_contents) {
    WebAppTabHelper* helper = WebAppTabHelper::FromWebContents(web_contents);
    return helper->GetAudioFocusGroupIdForTesting();
  }

  // Simulates a page calling window.open on an URL and waits for the
  // navigation.
  content::WebContents* OpenWindow(content::WebContents* contents,
                                   bool aux,
                                   const GURL& url) {
    content::WebContentsAddedObserver tab_added_observer;
    EXPECT_TRUE(
        content::ExecJs(contents, "window.open('" + url.spec() + "', '', '" +
                                      (aux ? "opener" : "noopener") + "')"));
    content::WebContents* new_contents = tab_added_observer.GetWebContents();
    EXPECT_TRUE(new_contents);
    WaitForLoadStop(new_contents);

    EXPECT_EQ(url, contents->GetController().GetLastCommittedEntry()->GetURL());
    EXPECT_EQ(
        content::PAGE_TYPE_NORMAL,
        new_contents->GetController().GetLastCommittedEntry()->GetPageType());
    if (aux) {
      EXPECT_EQ(contents->GetPrimaryMainFrame()->GetSiteInstance(),
                new_contents->GetPrimaryMainFrame()->GetSiteInstance());
    }
    return new_contents;
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(WebAppAudioFocusBrowserTest, AppHasDifferentAudioFocus) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL app_url = embedded_test_server()->GetURL(kAudioFocusTestPageURL);

  webapps::AppId app_id = InstallPWA(app_url);

  // Launch browser with media page.
  content::WebContents* tab1 = AddTestPageTabAtIndex(0);

  // Start the test page playing.
  EXPECT_TRUE(StartPlaying(tab1));

  // Launch a second tab in the browser.
  content::WebContents* tab2 = AddTestPageTabAtIndex(0);

  // Start the test page playing and check that both tabs now have focus.
  EXPECT_TRUE(StartPlaying(tab2));
  EXPECT_FALSE(IsPaused(tab1));

  // Check that the two tabs have no group id.
  EXPECT_EQ(base::UnguessableToken::Null(), GetAudioFocusGroupId(tab1));
  EXPECT_EQ(base::UnguessableToken::Null(), GetAudioFocusGroupId(tab2));

  // Launch the PWA.
  Browser* app_browser = LaunchWebAppBrowserAndWait(app_id);
  content::WebContents* web_contents =
      app_browser->tab_strip_model()->GetActiveWebContents();
  EXPECT_TRUE(content::WaitForLoadStop(web_contents));

  // Start the PWA playing and check that it has a group id.
  EXPECT_TRUE(StartPlaying(web_contents));
  const base::UnguessableToken& group_id = GetAudioFocusGroupId(web_contents);
  EXPECT_NE(base::UnguessableToken::Null(), group_id);

  // Check that the hosted app took audio focus from the browser tabs.
  EXPECT_TRUE(WaitForPause(tab1));
  EXPECT_TRUE(WaitForPause(tab2));

// TODO(https://crbug.com/376922620): Enable capturing v2 ChromeOS support.
#if !BUILDFLAG(IS_CHROMEOS)
  // Open captured window, which should share the same group id.
  {
    content::WebContents* new_contents =
        OpenWindow(web_contents, /*aux=*/false, app_url);
    EXPECT_EQ(group_id, GetAudioFocusGroupId(new_contents));
  }
  // Open an auxiliary window, which should also open in an app window and share
  // the group id.
  {
    content::WebContents* new_contents =
        OpenWindow(web_contents, /*aux=*/true, app_url);
    EXPECT_EQ(group_id, GetAudioFocusGroupId(new_contents));
  }

  ASSERT_EQ(apps::test::DisableLinkCapturingByUser(profile(), app_id),
            base::ok());
#endif

  // Without capturing, new window will open in the browser so it should have no
  // group id.
  {
    content::WebContents* new_contents =
        OpenWindow(web_contents, /*aux=*/false, app_url);
    EXPECT_EQ(base::UnguessableToken::Null(),
              GetAudioFocusGroupId(new_contents));
  }

  // Navigate inside the PWA and make sure we keep the same group id.
  {
    GURL::Replacements replacements;
    replacements.SetQueryStr("t=1");
    GURL new_url =
        web_contents->GetLastCommittedURL().ReplaceComponents(replacements);
    ASSERT_TRUE(NavigateInRenderer(web_contents, new_url));
    EXPECT_EQ(group_id, GetAudioFocusGroupId(web_contents));
  }

  // Launch a second window for the PWA. It should have the same group id.
  {
    Browser* second_app_browser = LaunchWebAppBrowserAndWait(app_id);

    content::WebContents* new_contents =
        second_app_browser->tab_strip_model()->GetActiveWebContents();
    EXPECT_TRUE(content::WaitForLoadStop(new_contents));

    EXPECT_EQ(group_id, GetAudioFocusGroupId(new_contents));
  }

  // Navigate away and check that the group id is still the same because we are
  // part of the same window.
  // TODO(crbug.com/40180004): Understand why this returns false.
  ASSERT_FALSE(
      NavigateInRenderer(web_contents, GURL("https://www.example.com")));
  EXPECT_EQ(group_id, GetAudioFocusGroupId(web_contents));
}

IN_PROC_BROWSER_TEST_F(WebAppAudioFocusBrowserTest, WebAppHasSameAudioFocus) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL app_url = embedded_test_server()->GetURL(kAudioFocusTestPageURL);

  webapps::AppId app_id = InstallPWA(app_url);

  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), app_url));
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  EXPECT_TRUE(content::WaitForLoadStop(web_contents));

  EXPECT_EQ(base::UnguessableToken::Null(), GetAudioFocusGroupId(web_contents));
}

}  // namespace web_app