Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: content/browser/media/session/media_session_service_impl_browsertest.cc

Issue 2736493002: Fix two notification update issues on page navigation (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/media/session/media_session_service_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/media/session/media_session_service_impl.h"
6
5 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/run_loop.h"
9 #include "content/browser/media/session/media_session_impl.h"
10 #include "content/browser/media/session/media_session_player_observer.h"
11 #include "content/public/browser/web_contents_observer.h"
12 #include "content/public/test/browser_test_utils.h"
6 #include "content/public/test/content_browser_test.h" 13 #include "content/public/test/content_browser_test.h"
7 #include "content/public/test/content_browser_test_utils.h" 14 #include "content/public/test/content_browser_test_utils.h"
8 #include "content/shell/browser/shell.h" 15 #include "content/shell/browser/shell.h"
16 #include "media/base/media_content_type.h"
17 #include "testing/gtest/include/gtest/gtest.h"
9 18
10 namespace content { 19 namespace content {
11 20
21 namespace {
22
23 class MockMediaSessionObserver : public MediaSessionObserver {
24 public:
25 explicit MockMediaSessionObserver(
26 MediaSession* session,
27 const base::Closure& closure_on_actions_change)
28 : MediaSessionObserver(session),
29 closure_on_actions_change_(closure_on_actions_change) {}
30
31 void MediaSessionActionsChanged(
32 const std::set<blink::mojom::MediaSessionAction>& actions) override {
33 // The actions might be empty when the service becomes routed for the first
34 // time.
35 if (actions.size() == 1)
36 closure_on_actions_change_.Run();
37 }
38
39 private:
40 base::Closure closure_on_actions_change_;
41 };
42
43 class MockWebContentsObserver : public WebContentsObserver {
44 public:
45 explicit MockWebContentsObserver(WebContents* contents,
46 const base::Closure& closure_on_navigate)
47 : WebContentsObserver(contents),
48 closure_on_navigate_(closure_on_navigate) {}
49
50 void DidFinishNavigation(NavigationHandle* navigation_handle) override {
51 closure_on_navigate_.Run();
52 }
53
54 private:
55 base::Closure closure_on_navigate_;
56 };
57
58 class MockMediaSessionPlayerObserver : public MediaSessionPlayerObserver {
59 public:
60 explicit MockMediaSessionPlayerObserver(RenderFrameHost* rfh)
61 : render_frame_host_(rfh) {}
62
63 ~MockMediaSessionPlayerObserver() override = default;
64
65 void OnSuspend(int player_id) override {}
66 void OnResume(int player_id) override {}
67 void OnSetVolumeMultiplier(int player_id, double volume_multiplier) override {
68 }
69
70 RenderFrameHost* GetRenderFrameHost() const override {
71 return render_frame_host_;
72 }
73
74 private:
75 RenderFrameHost* render_frame_host_;
76 };
77
78 void NavigateToURLAndWaitForFinish(Shell* window, const GURL& url) {
79 base::RunLoop run_loop;
80 MockWebContentsObserver observer(window->web_contents(),
81 run_loop.QuitClosure());
82
83 NavigateToURL(window, url);
84 run_loop.Run();
85 }
86
87 char kSetUpMediaSessionScript[] =
88 "navigator.mediaSession.playbackState = \"playing\";\n"
89 "navigator.mediaSession.metadata = new MediaMetadata({ title: \"foo\" });\n"
90 "navigator.mediaSession.setActionHandler(\"play\", _ => {});";
91
92 const int kPlayerId = 0;
93
94 } // anonymous namespace
95
12 class MediaSessionServiceImplBrowserTest : public ContentBrowserTest { 96 class MediaSessionServiceImplBrowserTest : public ContentBrowserTest {
13 protected: 97 protected:
14 void SetUpCommandLine(base::CommandLine* command_line) override { 98 void SetUpCommandLine(base::CommandLine* command_line) override {
15 ContentBrowserTest::SetUpCommandLine(command_line); 99 ContentBrowserTest::SetUpCommandLine(command_line);
16 command_line->AppendSwitchASCII("--enable-blink-features", "MediaSession"); 100 command_line->AppendSwitchASCII("--enable-blink-features", "MediaSession");
17 } 101 }
102
103 void EnsurePlayer() {
104 if (player_)
105 return;
106
107 player_.reset(new MockMediaSessionPlayerObserver(
108 shell()->web_contents()->GetMainFrame()));
109
110 MediaSessionImpl::Get(shell()->web_contents())
111 ->AddPlayer(player_.get(), kPlayerId,
112 media::MediaContentType::Persistent);
113 }
114
115 MediaSessionImpl* GetSession() {
116 return MediaSessionImpl::Get(shell()->web_contents());
117 }
118
119 MediaSessionServiceImpl* GetService() {
120 RenderFrameHost* main_frame = shell()->web_contents()->GetMainFrame();
121 if (GetSession()->services_.count(main_frame))
122 return GetSession()->services_[main_frame];
123
124 return nullptr;
125 }
126
127 bool ExecuteScriptToSetUpMediaSessionSync() {
128 // Using the actions change as the signal of completion.
129 base::RunLoop run_loop;
130 MockMediaSessionObserver observer(GetSession(), run_loop.QuitClosure());
131 bool result = ExecuteScript(shell(), kSetUpMediaSessionScript);
132 run_loop.Run();
133 return result;
134 }
135
136 private:
137 std::unique_ptr<MockMediaSessionPlayerObserver> player_;
18 }; 138 };
19 139
20 // Two windows from the same BrowserContext. 140 // Two windows from the same BrowserContext.
21 IN_PROC_BROWSER_TEST_F(MediaSessionServiceImplBrowserTest, 141 IN_PROC_BROWSER_TEST_F(MediaSessionServiceImplBrowserTest,
22 CrashMessageOnUnload) { 142 CrashMessageOnUnload) {
23 NavigateToURL(shell(), GetTestUrl("media/session", "embedder.html")); 143 NavigateToURL(shell(), GetTestUrl("media/session", "embedder.html"));
24 // Navigate to a chrome:// URL to avoid render process re-use. 144 // Navigate to a chrome:// URL to avoid render process re-use.
25 NavigateToURL(shell(), GURL("chrome://flags")); 145 NavigateToURL(shell(), GURL("chrome://flags"));
26 // Should not crash. 146 // Should not crash.
27 } 147 }
28 148
149 // Tests for checking if the media session service members are correctly reset
150 // when navigating. Due to the mojo services have different message queues, it's
151 // hard to wait for the messages to arrive. Temporarily, the tests are using
152 // observers to wait for the message to be processed on the MediaSessionObserver
153 // side.
154
155 IN_PROC_BROWSER_TEST_F(MediaSessionServiceImplBrowserTest,
156 ResetServiceWhenNavigatingAway) {
157 NavigateToURL(shell(), GetTestUrl(".", "title1.html"));
158 EnsurePlayer();
159
160 EXPECT_TRUE(ExecuteScriptToSetUpMediaSessionSync());
161
162 EXPECT_EQ(blink::mojom::MediaSessionPlaybackState::PLAYING,
163 GetService()->playback_state());
164 EXPECT_TRUE(GetService()->metadata().has_value());
165 EXPECT_EQ(1u, GetService()->actions().size());
166
167 // Start a non-same-page navigation and check the playback state, metadata,
168 // actions are reset.
169 NavigateToURLAndWaitForFinish(shell(), GetTestUrl(".", "title2.html"));
170
171 EXPECT_EQ(blink::mojom::MediaSessionPlaybackState::NONE,
172 GetService()->playback_state());
173 EXPECT_FALSE(GetService()->metadata().has_value());
174 EXPECT_EQ(0u, GetService()->actions().size());
175 }
176
177 IN_PROC_BROWSER_TEST_F(MediaSessionServiceImplBrowserTest,
178 DontResetServiceForSamePageNavigation) {
179 NavigateToURL(shell(), GetTestUrl(".", "title1.html"));
180 EnsurePlayer();
181
182 EXPECT_TRUE(ExecuteScriptToSetUpMediaSessionSync());
183
184 // Start a same-page navigation and check the playback state, metadata,
185 // actions are not reset.
186 GURL same_page_url = GetTestUrl(".", "title1.html");
187 same_page_url = GURL(same_page_url.spec() + "#some-anchor");
188 NavigateToURLAndWaitForFinish(shell(), same_page_url);
189
190 EXPECT_EQ(blink::mojom::MediaSessionPlaybackState::PLAYING,
191 GetService()->playback_state());
192 EXPECT_TRUE(GetService()->metadata().has_value());
193 EXPECT_EQ(1u, GetService()->actions().size());
194 }
195
29 } // namespace content 196 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media/session/media_session_service_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698