1 // Copyright (c) 2013 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/values.h"
8 #include "content/browser/media/webrtc_internals.h"
9 #include "content/browser/media/webrtc_internals_ui_observer.h"
10 #include "content/public/test/test_browser_thread.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 static const std::string kContraints
= "c";
18 static const std::string kRtcConfiguration
= "r";
19 static const std::string kUrl
= "u";
21 class MockWebRTCInternalsProxy
: public WebRTCInternalsUIObserver
{
23 void OnUpdate(const std::string
& command
, const base::Value
* value
) override
{
26 value_
.reset(value
->DeepCopy());
29 std::string
command() {
33 base::Value
* value() {
39 scoped_ptr
<base::Value
> value_
;
42 class WebRTCInternalsTest
: public testing::Test
{
44 WebRTCInternalsTest() : io_thread_(BrowserThread::UI
, &io_loop_
) {
45 WebRTCInternals::GetInstance()->ResetForTesting();
49 void VerifyString(const base::DictionaryValue
* dict
,
50 const std::string
& key
,
51 const std::string
& expected
) {
53 EXPECT_TRUE(dict
->GetString(key
, &actual
));
54 EXPECT_EQ(expected
, actual
);
57 void VerifyInt(const base::DictionaryValue
* dict
,
58 const std::string
& key
,
61 EXPECT_TRUE(dict
->GetInteger(key
, &actual
));
62 EXPECT_EQ(expected
, actual
);
65 void VerifyList(const base::DictionaryValue
* dict
,
66 const std::string
& key
,
67 const base::ListValue
& expected
) {
68 const base::ListValue
* actual
= NULL
;
69 EXPECT_TRUE(dict
->GetList(key
, &actual
));
70 EXPECT_TRUE(expected
.Equals(actual
));
73 void VerifyGetUserMediaData(base::Value
* actual_data
,
76 const std::string
& origin
,
77 const std::string
& audio
,
78 const std::string
& video
) {
79 base::DictionaryValue
* dict
= NULL
;
80 EXPECT_TRUE(actual_data
->GetAsDictionary(&dict
));
82 VerifyInt(dict
, "rid", rid
);
83 VerifyInt(dict
, "pid", pid
);
84 VerifyString(dict
, "origin", origin
);
85 VerifyString(dict
, "audio", audio
);
86 VerifyString(dict
, "video", video
);
89 base::MessageLoop io_loop_
;
90 TestBrowserThread io_thread_
;
95 TEST_F(WebRTCInternalsTest
, AddRemoveObserver
) {
96 scoped_ptr
<MockWebRTCInternalsProxy
> observer(
97 new MockWebRTCInternalsProxy());
98 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
99 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
100 WebRTCInternals::GetInstance()->OnAddPeerConnection(
101 0, 3, 4, kUrl
, kRtcConfiguration
, kContraints
);
102 EXPECT_EQ("", observer
->command());
104 WebRTCInternals::GetInstance()->OnRemovePeerConnection(3, 4);
107 TEST_F(WebRTCInternalsTest
, SendAddPeerConnectionUpdate
) {
108 scoped_ptr
<MockWebRTCInternalsProxy
> observer(
109 new MockWebRTCInternalsProxy());
110 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
111 WebRTCInternals::GetInstance()->OnAddPeerConnection(
112 0, 1, 2, kUrl
, kRtcConfiguration
, kContraints
);
113 EXPECT_EQ("addPeerConnection", observer
->command());
115 base::DictionaryValue
* dict
= NULL
;
116 EXPECT_TRUE(observer
->value()->GetAsDictionary(&dict
));
118 VerifyInt(dict
, "pid", 1);
119 VerifyInt(dict
, "lid", 2);
120 VerifyString(dict
, "url", kUrl
);
121 VerifyString(dict
, "rtcConfiguration", kRtcConfiguration
);
122 VerifyString(dict
, "constraints", kContraints
);
124 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
125 WebRTCInternals::GetInstance()->OnRemovePeerConnection(1, 2);
128 TEST_F(WebRTCInternalsTest
, SendRemovePeerConnectionUpdate
) {
129 scoped_ptr
<MockWebRTCInternalsProxy
> observer(
130 new MockWebRTCInternalsProxy());
131 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
132 WebRTCInternals::GetInstance()->OnAddPeerConnection(
133 0, 1, 2, kUrl
, kRtcConfiguration
, kContraints
);
134 WebRTCInternals::GetInstance()->OnRemovePeerConnection(1, 2);
135 EXPECT_EQ("removePeerConnection", observer
->command());
137 base::DictionaryValue
* dict
= NULL
;
138 EXPECT_TRUE(observer
->value()->GetAsDictionary(&dict
));
140 VerifyInt(dict
, "pid", 1);
141 VerifyInt(dict
, "lid", 2);
143 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
146 TEST_F(WebRTCInternalsTest
, SendUpdatePeerConnectionUpdate
) {
147 scoped_ptr
<MockWebRTCInternalsProxy
> observer(
148 new MockWebRTCInternalsProxy());
149 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
150 WebRTCInternals::GetInstance()->OnAddPeerConnection(
151 0, 1, 2, kUrl
, kRtcConfiguration
, kContraints
);
153 const std::string update_type
= "fakeType";
154 const std::string update_value
= "fakeValue";
155 WebRTCInternals::GetInstance()->OnUpdatePeerConnection(
156 1, 2, update_type
, update_value
);
158 EXPECT_EQ("updatePeerConnection", observer
->command());
160 base::DictionaryValue
* dict
= NULL
;
161 EXPECT_TRUE(observer
->value()->GetAsDictionary(&dict
));
163 VerifyInt(dict
, "pid", 1);
164 VerifyInt(dict
, "lid", 2);
165 VerifyString(dict
, "type", update_type
);
166 VerifyString(dict
, "value", update_value
);
169 EXPECT_TRUE(dict
->GetString("time", &time
));
170 EXPECT_FALSE(time
.empty());
172 WebRTCInternals::GetInstance()->OnRemovePeerConnection(1, 2);
173 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
176 TEST_F(WebRTCInternalsTest
, AddGetUserMedia
) {
177 scoped_ptr
<MockWebRTCInternalsProxy
> observer(new MockWebRTCInternalsProxy());
179 // Add one observer before "getUserMedia".
180 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
184 const std::string audio_constraint
= "aaa";
185 const std::string video_constraint
= "vvv";
186 WebRTCInternals::GetInstance()->OnGetUserMedia(
187 rid
, pid
, kUrl
, true, true, audio_constraint
, video_constraint
);
189 EXPECT_EQ("addGetUserMedia", observer
->command());
190 VerifyGetUserMediaData(
191 observer
->value(), rid
, pid
, kUrl
, audio_constraint
, video_constraint
);
193 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
196 TEST_F(WebRTCInternalsTest
, SendAllUpdateWithGetUserMedia
) {
199 const std::string audio_constraint
= "aaa";
200 const std::string video_constraint
= "vvv";
201 WebRTCInternals::GetInstance()->OnGetUserMedia(
202 rid
, pid
, kUrl
, true, true, audio_constraint
, video_constraint
);
204 scoped_ptr
<MockWebRTCInternalsProxy
> observer(new MockWebRTCInternalsProxy());
205 // Add one observer after "getUserMedia".
206 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
207 WebRTCInternals::GetInstance()->UpdateObserver(observer
.get());
209 EXPECT_EQ("addGetUserMedia", observer
->command());
210 VerifyGetUserMediaData(
211 observer
->value(), rid
, pid
, kUrl
, audio_constraint
, video_constraint
);
213 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
216 TEST_F(WebRTCInternalsTest
, SendAllUpdatesWithPeerConnectionUpdate
) {
217 const int rid
= 0, pid
= 1, lid
= 2;
218 const std::string update_type
= "fakeType";
219 const std::string update_value
= "fakeValue";
221 WebRTCInternals::GetInstance()->OnAddPeerConnection(
222 rid
, pid
, lid
, kUrl
, kRtcConfiguration
, kContraints
);
223 WebRTCInternals::GetInstance()->OnUpdatePeerConnection(
224 pid
, lid
, update_type
, update_value
);
226 scoped_ptr
<MockWebRTCInternalsProxy
> observer(new MockWebRTCInternalsProxy());
227 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
229 WebRTCInternals::GetInstance()->UpdateObserver(observer
.get());
231 EXPECT_EQ("updateAllPeerConnections", observer
->command());
233 base::ListValue
* list
= NULL
;
234 EXPECT_TRUE(observer
->value()->GetAsList(&list
));
235 EXPECT_EQ(1U, list
->GetSize());
237 base::DictionaryValue
* dict
= NULL
;
238 EXPECT_TRUE((*list
->begin())->GetAsDictionary(&dict
));
240 VerifyInt(dict
, "rid", rid
);
241 VerifyInt(dict
, "pid", pid
);
242 VerifyInt(dict
, "lid", lid
);
243 VerifyString(dict
, "url", kUrl
);
244 VerifyString(dict
, "rtcConfiguration", kRtcConfiguration
);
245 VerifyString(dict
, "constraints", kContraints
);
247 base::ListValue
* log
= NULL
;
248 EXPECT_TRUE(dict
->GetList("log", &log
));
249 EXPECT_EQ(1U, log
->GetSize());
251 EXPECT_TRUE((*log
->begin())->GetAsDictionary(&dict
));
252 VerifyString(dict
, "type", update_type
);
253 VerifyString(dict
, "value", update_value
);
255 EXPECT_TRUE(dict
->GetString("time", &time
));
256 EXPECT_FALSE(time
.empty());
259 TEST_F(WebRTCInternalsTest
, OnAddStats
) {
260 const int rid
= 0, pid
= 1, lid
= 2;
261 scoped_ptr
<MockWebRTCInternalsProxy
> observer(new MockWebRTCInternalsProxy());
262 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
263 WebRTCInternals::GetInstance()->OnAddPeerConnection(
264 rid
, pid
, lid
, kUrl
, kRtcConfiguration
, kContraints
);
266 base::ListValue list
;
267 list
.AppendString("xxx");
268 list
.AppendString("yyy");
269 WebRTCInternals::GetInstance()->OnAddStats(pid
, lid
, list
);
271 EXPECT_EQ("addStats", observer
->command());
272 base::DictionaryValue
* dict
= NULL
;
273 EXPECT_TRUE(observer
->value()->GetAsDictionary(&dict
));
275 VerifyInt(dict
, "pid", pid
);
276 VerifyInt(dict
, "lid", lid
);
277 VerifyList(dict
, "reports", list
);
280 TEST_F(WebRTCInternalsTest
, AecRecordingFileSelectionCanceled
) {
281 scoped_ptr
<MockWebRTCInternalsProxy
> observer(new MockWebRTCInternalsProxy());
282 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
283 WebRTCInternals::GetInstance()->FileSelectionCanceled(NULL
);
284 EXPECT_EQ("aecRecordingFileSelectionCancelled", observer
->command());
285 EXPECT_EQ(NULL
, observer
->value());
288 } // namespace content