OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/tuple.h" | |
6 #include "content/browser/media/android/media_session.h" | |
7 #include "content/browser/media/android/media_session_controller.h" | |
8 #include "content/browser/media/android/media_web_contents_observer_android.h" | |
9 #include "content/common/media/media_player_delegate_messages.h" | |
10 #include "content/test/test_render_view_host.h" | |
11 #include "content/test/test_web_contents.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace content { | |
15 | |
16 class MediaSessionControllerTest : public RenderViewHostImplTestHarness { | |
17 public: | |
18 void SetUp() override { | |
19 RenderViewHostImplTestHarness::SetUp(); | |
20 id_ = WebContentsObserver::MediaPlayerId(contents()->GetMainFrame(), 0); | |
21 observer_ = MediaWebContentsObserverAndroid::FromWebContents(contents()); | |
22 controller_ = CreateController(); | |
23 } | |
24 | |
25 void TearDown() override { | |
26 // Destruct the controller prior to any other teardown to avoid out of order | |
27 // destruction relative to the MediaSession instance. | |
28 controller_.reset(); | |
29 RenderViewHostImplTestHarness::TearDown(); | |
30 } | |
31 | |
32 protected: | |
33 scoped_ptr<MediaSessionController> CreateController() { | |
34 return scoped_ptr<MediaSessionController>( | |
35 new MediaSessionController(id_, observer_)); | |
36 } | |
37 | |
38 MediaSession* media_session() { return MediaSession::Get(contents()); } | |
39 | |
40 IPC::TestSink& test_sink() { return main_test_rfh()->GetProcess()->sink(); } | |
41 | |
42 void Suspend() { | |
43 controller_->OnSuspend(controller_->get_player_id_for_testing()); | |
44 } | |
45 | |
46 void Resume() { | |
47 controller_->OnResume(controller_->get_player_id_for_testing()); | |
48 } | |
49 | |
50 void SetVolumeMultiplier(double multiplier) { | |
51 controller_->OnSetVolumeMultiplier(controller_->get_player_id_for_testing(), | |
52 multiplier); | |
53 } | |
54 | |
55 // Returns a duration long enough for a media session instance to be created. | |
56 base::TimeDelta DurationJustRight() { | |
57 return base::TimeDelta::FromSeconds( | |
58 MediaSessionController::kMinimumDurationForContentSecs + 1); | |
59 } | |
60 | |
61 // Returns a duration too short for a media session instance. | |
62 base::TimeDelta DurationTooShort() { | |
63 return base::TimeDelta::FromSeconds( | |
64 MediaSessionController::kMinimumDurationForContentSecs); | |
65 } | |
66 | |
67 template <typename T> | |
68 bool ReceivedMessagePlayPause() { | |
69 const IPC::Message* msg = test_sink().GetUniqueMessageMatching(T::ID); | |
70 if (!msg) | |
71 return false; | |
72 | |
73 base::Tuple<int> result; | |
74 if (!T::Read(msg, &result)) | |
75 return false; | |
76 | |
77 EXPECT_EQ(id_.second, base::get<0>(result)); | |
78 test_sink().ClearMessages(); | |
79 return id_.second == base::get<0>(result); | |
80 } | |
81 | |
82 template <typename T> | |
83 bool ReceivedMessageVolumeMultiplierUpdate(double expected_multiplier) { | |
84 const IPC::Message* msg = test_sink().GetUniqueMessageMatching(T::ID); | |
85 if (!msg) | |
86 return false; | |
87 | |
88 base::Tuple<int, double> result; | |
89 if (!T::Read(msg, &result)) | |
90 return false; | |
91 | |
92 EXPECT_EQ(id_.second, base::get<0>(result)); | |
93 if (id_.second != base::get<0>(result)) | |
94 return false; | |
95 | |
96 EXPECT_EQ(expected_multiplier, base::get<1>(result)); | |
97 test_sink().ClearMessages(); | |
98 return expected_multiplier == base::get<1>(result); | |
99 } | |
100 | |
101 WebContentsObserver::MediaPlayerId id_; | |
102 MediaWebContentsObserverAndroid* observer_; | |
103 scoped_ptr<MediaSessionController> controller_; | |
104 }; | |
105 | |
106 TEST_F(MediaSessionControllerTest, NoAudioNoSession) { | |
107 ASSERT_TRUE(controller_->Initialize(false, false, DurationJustRight())); | |
108 EXPECT_TRUE(media_session()->IsSuspended()); | |
109 EXPECT_FALSE(media_session()->IsControllable()); | |
110 } | |
111 | |
112 TEST_F(MediaSessionControllerTest, IsRemoteNoSession) { | |
113 ASSERT_TRUE(controller_->Initialize(true, true, DurationJustRight())); | |
114 EXPECT_TRUE(media_session()->IsSuspended()); | |
115 EXPECT_FALSE(media_session()->IsControllable()); | |
116 } | |
117 | |
118 TEST_F(MediaSessionControllerTest, TooShortNoControllableSession) { | |
119 ASSERT_TRUE(controller_->Initialize(true, false, DurationTooShort())); | |
120 EXPECT_FALSE(media_session()->IsSuspended()); | |
121 EXPECT_FALSE(media_session()->IsControllable()); | |
122 } | |
123 | |
124 TEST_F(MediaSessionControllerTest, BasicControls) { | |
125 ASSERT_TRUE(controller_->Initialize(true, false, DurationJustRight())); | |
126 EXPECT_FALSE(media_session()->IsSuspended()); | |
127 EXPECT_TRUE(media_session()->IsControllable()); | |
128 | |
129 // Verify suspend notifies the renderer and maintains its session. | |
130 Suspend(); | |
131 EXPECT_TRUE(ReceivedMessagePlayPause<MediaPlayerDelegateMsg_Pause>()); | |
132 | |
133 // Likewise verify the resume behavior. | |
134 Resume(); | |
135 EXPECT_TRUE(ReceivedMessagePlayPause<MediaPlayerDelegateMsg_Play>()); | |
136 | |
137 // Verify destruction of the controller removes its session. | |
138 controller_.reset(); | |
139 EXPECT_TRUE(media_session()->IsSuspended()); | |
140 EXPECT_FALSE(media_session()->IsControllable()); | |
141 } | |
142 | |
143 TEST_F(MediaSessionControllerTest, VolumeMultiplier) { | |
144 ASSERT_TRUE(controller_->Initialize(true, false, DurationJustRight())); | |
145 EXPECT_FALSE(media_session()->IsSuspended()); | |
146 EXPECT_TRUE(media_session()->IsControllable()); | |
147 | |
148 // Upon creation of the MediaSession the default multiplier will be sent. | |
149 EXPECT_TRUE(ReceivedMessageVolumeMultiplierUpdate< | |
150 MediaPlayerDelegateMsg_UpdateVolumeMultiplier>(1.0)); | |
151 | |
152 // Verify a different volume multiplier is sent. | |
153 const double kTestMultiplier = 0.5; | |
154 SetVolumeMultiplier(kTestMultiplier); | |
155 EXPECT_TRUE(ReceivedMessageVolumeMultiplierUpdate< | |
156 MediaPlayerDelegateMsg_UpdateVolumeMultiplier>(kTestMultiplier)); | |
157 } | |
158 | |
159 TEST_F(MediaSessionControllerTest, ControllerSidePause) { | |
160 ASSERT_TRUE(controller_->Initialize(true, false, DurationJustRight())); | |
161 EXPECT_FALSE(media_session()->IsSuspended()); | |
162 EXPECT_TRUE(media_session()->IsControllable()); | |
163 | |
164 // Verify pause behavior. | |
165 controller_->OnPlaybackPaused(); | |
166 EXPECT_TRUE(media_session()->IsSuspended()); | |
167 EXPECT_TRUE(media_session()->IsControllable()); | |
168 | |
169 // Verify the next Initialize() call restores the session. | |
170 ASSERT_TRUE(controller_->Initialize(true, false, DurationJustRight())); | |
171 EXPECT_FALSE(media_session()->IsSuspended()); | |
172 EXPECT_TRUE(media_session()->IsControllable()); | |
173 } | |
174 | |
175 TEST_F(MediaSessionControllerTest, Reinitialize) { | |
176 ASSERT_TRUE(controller_->Initialize(false, false, DurationJustRight())); | |
177 EXPECT_TRUE(media_session()->IsSuspended()); | |
178 EXPECT_FALSE(media_session()->IsControllable()); | |
179 | |
180 // Create a transient type session. | |
181 ASSERT_TRUE(controller_->Initialize(true, false, DurationTooShort())); | |
182 EXPECT_FALSE(media_session()->IsSuspended()); | |
183 EXPECT_FALSE(media_session()->IsControllable()); | |
184 const int current_player_id = controller_->get_player_id_for_testing(); | |
185 | |
186 // Reinitialize the session as a content type. | |
187 ASSERT_TRUE(controller_->Initialize(true, false, DurationJustRight())); | |
188 EXPECT_FALSE(media_session()->IsSuspended()); | |
189 EXPECT_TRUE(media_session()->IsControllable()); | |
190 // Player id should not change when there's an active session. | |
191 EXPECT_EQ(current_player_id, controller_->get_player_id_for_testing()); | |
192 | |
193 // Verify suspend notifies the renderer and maintains its session. | |
194 Suspend(); | |
195 EXPECT_TRUE(ReceivedMessagePlayPause<MediaPlayerDelegateMsg_Pause>()); | |
196 | |
197 // Likewise verify the resume behavior. | |
198 Resume(); | |
199 EXPECT_TRUE(ReceivedMessagePlayPause<MediaPlayerDelegateMsg_Play>()); | |
200 | |
201 // Attempt to switch to no audio player, which should do nothing. | |
202 // TODO(dalecurtis): Delete this test once we're no longer using WMPA and | |
203 // the BrowserMediaPlayerManagers. Tracked by http://crbug.com/580626 | |
204 ASSERT_TRUE(controller_->Initialize(false, false, DurationJustRight())); | |
205 EXPECT_FALSE(media_session()->IsSuspended()); | |
206 EXPECT_TRUE(media_session()->IsControllable()); | |
207 EXPECT_EQ(current_player_id, controller_->get_player_id_for_testing()); | |
208 | |
209 // Switch to a remote player, which should release the session. | |
210 ASSERT_TRUE(controller_->Initialize(true, true, DurationJustRight())); | |
211 EXPECT_TRUE(media_session()->IsSuspended()); | |
212 EXPECT_FALSE(media_session()->IsControllable()); | |
213 EXPECT_EQ(current_player_id, controller_->get_player_id_for_testing()); | |
214 } | |
215 | |
216 } // namespace content | |
OLD | NEW |