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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "MediaControlService.h"
#include "MediaController.h"
#include "mozilla/dom/MediaSessionBinding.h"
using namespace mozilla::dom;
#define CONTROLLER_ID 0
#define FAKE_CONTEXT_ID 0
#define FIRST_CONTROLLER_ID 0
TEST(MediaController, DefaultValueCheck)
{
RefPtr<MediaController> controller = new MediaController(CONTROLLER_ID);
ASSERT_TRUE(!controller->IsAnyMediaBeingControlled());
ASSERT_TRUE(controller->Id() == CONTROLLER_ID);
ASSERT_TRUE(controller->PlaybackState() == MediaSessionPlaybackState::None);
ASSERT_TRUE(!controller->IsAudible());
}
TEST(MediaController, IsAnyMediaBeingControlled)
{
RefPtr<MediaController> controller = new MediaController(CONTROLLER_ID);
ASSERT_TRUE(!controller->IsAnyMediaBeingControlled());
controller->NotifyMediaPlaybackChanged(FAKE_CONTEXT_ID,
MediaPlaybackState::eStarted);
ASSERT_TRUE(controller->IsAnyMediaBeingControlled());
controller->NotifyMediaPlaybackChanged(FAKE_CONTEXT_ID,
MediaPlaybackState::eStarted);
ASSERT_TRUE(controller->IsAnyMediaBeingControlled());
controller->NotifyMediaPlaybackChanged(FAKE_CONTEXT_ID,
MediaPlaybackState::eStopped);
ASSERT_TRUE(controller->IsAnyMediaBeingControlled());
controller->NotifyMediaPlaybackChanged(FAKE_CONTEXT_ID,
MediaPlaybackState::eStopped);
ASSERT_TRUE(!controller->IsAnyMediaBeingControlled());
}
class FakeControlledMedia final {
public:
explicit FakeControlledMedia(MediaController* aController)
: mController(aController) {
mController->NotifyMediaPlaybackChanged(FAKE_CONTEXT_ID,
MediaPlaybackState::eStarted);
}
void SetPlaying(MediaPlaybackState aState) {
if (mPlaybackState == aState) {
return;
}
mController->NotifyMediaPlaybackChanged(FAKE_CONTEXT_ID, aState);
mPlaybackState = aState;
}
void SetAudible(MediaAudibleState aState) {
if (mAudibleState == aState) {
return;
}
mController->NotifyMediaAudibleChanged(FAKE_CONTEXT_ID, aState);
mAudibleState = aState;
}
~FakeControlledMedia() {
if (mPlaybackState == MediaPlaybackState::ePlayed) {
mController->NotifyMediaPlaybackChanged(FAKE_CONTEXT_ID,
MediaPlaybackState::ePaused);
}
mController->NotifyMediaPlaybackChanged(FAKE_CONTEXT_ID,
MediaPlaybackState::eStopped);
}
private:
MediaPlaybackState mPlaybackState = MediaPlaybackState::eStopped;
MediaAudibleState mAudibleState = MediaAudibleState::eInaudible;
RefPtr<MediaController> mController;
};
TEST(MediaController, ActiveAndDeactiveController)
{
RefPtr<MediaControlService> service = MediaControlService::GetService();
ASSERT_TRUE(service->GetActiveControllersNum() == 0);
RefPtr<MediaController> controller = new MediaController(FIRST_CONTROLLER_ID);
// In order to check active control number after FakeControlledMedia
// destroyed.
{
FakeControlledMedia fakeMedia(controller);
fakeMedia.SetPlaying(MediaPlaybackState::ePlayed);
ASSERT_TRUE(service->GetActiveControllersNum() == 1);
fakeMedia.SetAudible(MediaAudibleState::eAudible);
ASSERT_TRUE(service->GetActiveControllersNum() == 1);
fakeMedia.SetAudible(MediaAudibleState::eInaudible);
ASSERT_TRUE(service->GetActiveControllersNum() == 1);
}
ASSERT_TRUE(service->GetActiveControllersNum() == 0);
}
TEST(MediaController, AudibleChanged)
{
RefPtr<MediaController> controller = new MediaController(CONTROLLER_ID);
FakeControlledMedia fakeMedia(controller);
fakeMedia.SetPlaying(MediaPlaybackState::ePlayed);
ASSERT_TRUE(!controller->IsAudible());
fakeMedia.SetAudible(MediaAudibleState::eAudible);
ASSERT_TRUE(controller->IsAudible());
fakeMedia.SetAudible(MediaAudibleState::eInaudible);
ASSERT_TRUE(!controller->IsAudible());
}
TEST(MediaController, PlayingStateChangeViaControlledMedia)
{
RefPtr<MediaController> controller = new MediaController(CONTROLLER_ID);
// In order to check playing state after FakeControlledMedia destroyed.
{
FakeControlledMedia foo(controller);
ASSERT_TRUE(controller->PlaybackState() == MediaSessionPlaybackState::None);
foo.SetPlaying(MediaPlaybackState::ePlayed);
ASSERT_TRUE(controller->PlaybackState() ==
MediaSessionPlaybackState::Playing);
foo.SetPlaying(MediaPlaybackState::ePaused);
ASSERT_TRUE(controller->PlaybackState() ==
MediaSessionPlaybackState::Paused);
foo.SetPlaying(MediaPlaybackState::ePlayed);
ASSERT_TRUE(controller->PlaybackState() ==
MediaSessionPlaybackState::Playing);
}
// FakeControlledMedia has been destroyed, no playing media exists.
ASSERT_TRUE(controller->PlaybackState() == MediaSessionPlaybackState::Paused);
}
TEST(MediaController, ControllerShouldRemainPlayingIfAnyPlayingMediaExists)
{
RefPtr<MediaController> controller = new MediaController(CONTROLLER_ID);
{
FakeControlledMedia foo(controller);
ASSERT_TRUE(controller->PlaybackState() == MediaSessionPlaybackState::None);
foo.SetPlaying(MediaPlaybackState::ePlayed);
ASSERT_TRUE(controller->PlaybackState() ==
MediaSessionPlaybackState::Playing);
// foo is playing, so controller is in `playing` state.
FakeControlledMedia bar(controller);
ASSERT_TRUE(controller->PlaybackState() ==
MediaSessionPlaybackState::Playing);
bar.SetPlaying(MediaPlaybackState::ePlayed);
ASSERT_TRUE(controller->PlaybackState() ==
MediaSessionPlaybackState::Playing);
// Although we paused bar, but foo is still playing, so the controller would
// still be in `playing`.
bar.SetPlaying(MediaPlaybackState::ePaused);
ASSERT_TRUE(controller->PlaybackState() ==
MediaSessionPlaybackState::Playing);
foo.SetPlaying(MediaPlaybackState::ePaused);
ASSERT_TRUE(controller->PlaybackState() ==
MediaSessionPlaybackState::Paused);
}
// both foo and bar have been destroyed, no playing media exists.
ASSERT_TRUE(controller->PlaybackState() == MediaSessionPlaybackState::Paused);
}
TEST(MediaController, PictureInPictureModeOrFullscreen)
{
RefPtr<MediaController> controller = new MediaController(CONTROLLER_ID);
ASSERT_TRUE(!controller->IsBeingUsedInPIPModeOrFullscreen());
controller->SetIsInPictureInPictureMode(FAKE_CONTEXT_ID, true);
ASSERT_TRUE(controller->IsBeingUsedInPIPModeOrFullscreen());
controller->SetIsInPictureInPictureMode(FAKE_CONTEXT_ID, false);
ASSERT_TRUE(!controller->IsBeingUsedInPIPModeOrFullscreen());
controller->NotifyMediaFullScreenState(FAKE_CONTEXT_ID, true);
ASSERT_TRUE(controller->IsBeingUsedInPIPModeOrFullscreen());
controller->NotifyMediaFullScreenState(FAKE_CONTEXT_ID, false);
ASSERT_TRUE(!controller->IsBeingUsedInPIPModeOrFullscreen());
}
|