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 kServers
= "s";
19 static const std::string kUrl
= "u";
21 class MockWebRTCInternalsProxy
: public WebRTCInternalsUIObserver
{
23 virtual void OnUpdate(const std::string
& command
,
24 const base::Value
* value
) OVERRIDE
{
27 value_
.reset(value
->DeepCopy());
30 std::string
command() {
34 base::Value
* value() {
40 scoped_ptr
<base::Value
> value_
;
43 class WebRTCInternalsTest
: public testing::Test
{
45 WebRTCInternalsTest() : io_thread_(BrowserThread::UI
, &io_loop_
) {
46 WebRTCInternals::GetInstance()->ResetForTesting();
50 void VerifyString(const base::DictionaryValue
* dict
,
51 const std::string
& key
,
52 const std::string
& expected
) {
54 EXPECT_TRUE(dict
->GetString(key
, &actual
));
55 EXPECT_EQ(expected
, actual
);
58 void VerifyInt(const base::DictionaryValue
* dict
,
59 const std::string
& key
,
62 EXPECT_TRUE(dict
->GetInteger(key
, &actual
));
63 EXPECT_EQ(expected
, actual
);
66 void VerifyList(const base::DictionaryValue
* dict
,
67 const std::string
& key
,
68 const base::ListValue
& expected
) {
69 const base::ListValue
* actual
= NULL
;
70 EXPECT_TRUE(dict
->GetList(key
, &actual
));
71 EXPECT_TRUE(expected
.Equals(actual
));
74 void VerifyGetUserMediaData(base::Value
* actual_data
,
77 const std::string
& origin
,
78 const std::string
& audio
,
79 const std::string
& video
) {
80 base::DictionaryValue
* dict
= NULL
;
81 EXPECT_TRUE(actual_data
->GetAsDictionary(&dict
));
83 VerifyInt(dict
, "rid", rid
);
84 VerifyInt(dict
, "pid", pid
);
85 VerifyString(dict
, "origin", origin
);
86 VerifyString(dict
, "audio", audio
);
87 VerifyString(dict
, "video", video
);
90 base::MessageLoop io_loop_
;
91 TestBrowserThread io_thread_
;
96 TEST_F(WebRTCInternalsTest
, AddRemoveObserver
) {
97 scoped_ptr
<MockWebRTCInternalsProxy
> observer(
98 new MockWebRTCInternalsProxy());
99 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
100 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
101 WebRTCInternals::GetInstance()->OnAddPeerConnection(
102 0, 3, 4, kUrl
, kServers
, kContraints
);
103 EXPECT_EQ("", observer
->command());
105 WebRTCInternals::GetInstance()->OnRemovePeerConnection(3, 4);
108 TEST_F(WebRTCInternalsTest
, SendAddPeerConnectionUpdate
) {
109 scoped_ptr
<MockWebRTCInternalsProxy
> observer(
110 new MockWebRTCInternalsProxy());
111 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
112 WebRTCInternals::GetInstance()->OnAddPeerConnection(
113 0, 1, 2, kUrl
, kServers
, kContraints
);
114 EXPECT_EQ("addPeerConnection", observer
->command());
116 base::DictionaryValue
* dict
= NULL
;
117 EXPECT_TRUE(observer
->value()->GetAsDictionary(&dict
));
119 VerifyInt(dict
, "pid", 1);
120 VerifyInt(dict
, "lid", 2);
121 VerifyString(dict
, "url", kUrl
);
122 VerifyString(dict
, "servers", kServers
);
123 VerifyString(dict
, "constraints", kContraints
);
125 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
126 WebRTCInternals::GetInstance()->OnRemovePeerConnection(1, 2);
129 TEST_F(WebRTCInternalsTest
, SendRemovePeerConnectionUpdate
) {
130 scoped_ptr
<MockWebRTCInternalsProxy
> observer(
131 new MockWebRTCInternalsProxy());
132 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
133 WebRTCInternals::GetInstance()->OnAddPeerConnection(
134 0, 1, 2, kUrl
, kServers
, kContraints
);
135 WebRTCInternals::GetInstance()->OnRemovePeerConnection(1, 2);
136 EXPECT_EQ("removePeerConnection", observer
->command());
138 base::DictionaryValue
* dict
= NULL
;
139 EXPECT_TRUE(observer
->value()->GetAsDictionary(&dict
));
141 VerifyInt(dict
, "pid", 1);
142 VerifyInt(dict
, "lid", 2);
144 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
147 TEST_F(WebRTCInternalsTest
, SendUpdatePeerConnectionUpdate
) {
148 scoped_ptr
<MockWebRTCInternalsProxy
> observer(
149 new MockWebRTCInternalsProxy());
150 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
151 WebRTCInternals::GetInstance()->OnAddPeerConnection(
152 0, 1, 2, kUrl
, kServers
, kContraints
);
154 const std::string update_type
= "fakeType";
155 const std::string update_value
= "fakeValue";
156 WebRTCInternals::GetInstance()->OnUpdatePeerConnection(
157 1, 2, update_type
, update_value
);
159 EXPECT_EQ("updatePeerConnection", observer
->command());
161 base::DictionaryValue
* dict
= NULL
;
162 EXPECT_TRUE(observer
->value()->GetAsDictionary(&dict
));
164 VerifyInt(dict
, "pid", 1);
165 VerifyInt(dict
, "lid", 2);
166 VerifyString(dict
, "type", update_type
);
167 VerifyString(dict
, "value", update_value
);
169 WebRTCInternals::GetInstance()->OnRemovePeerConnection(1, 2);
170 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
173 TEST_F(WebRTCInternalsTest
, AddGetUserMedia
) {
174 scoped_ptr
<MockWebRTCInternalsProxy
> observer(new MockWebRTCInternalsProxy());
176 // Add one observer before "getUserMedia".
177 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
181 const std::string audio_constraint
= "aaa";
182 const std::string video_constraint
= "vvv";
183 WebRTCInternals::GetInstance()->OnGetUserMedia(
184 rid
, pid
, kUrl
, true, true, audio_constraint
, video_constraint
);
186 EXPECT_EQ("addGetUserMedia", observer
->command());
187 VerifyGetUserMediaData(
188 observer
->value(), rid
, pid
, kUrl
, audio_constraint
, video_constraint
);
190 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
193 TEST_F(WebRTCInternalsTest
, SendAllUpdateWithGetUserMedia
) {
196 const std::string audio_constraint
= "aaa";
197 const std::string video_constraint
= "vvv";
198 WebRTCInternals::GetInstance()->OnGetUserMedia(
199 rid
, pid
, kUrl
, true, true, audio_constraint
, video_constraint
);
201 scoped_ptr
<MockWebRTCInternalsProxy
> observer(new MockWebRTCInternalsProxy());
202 // Add one observer after "getUserMedia".
203 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
204 WebRTCInternals::GetInstance()->UpdateObserver(observer
.get());
206 EXPECT_EQ("addGetUserMedia", observer
->command());
207 VerifyGetUserMediaData(
208 observer
->value(), rid
, pid
, kUrl
, audio_constraint
, video_constraint
);
210 WebRTCInternals::GetInstance()->RemoveObserver(observer
.get());
213 TEST_F(WebRTCInternalsTest
, SendAllUpdatesWithPeerConnectionUpdate
) {
214 const int rid
= 0, pid
= 1, lid
= 2;
215 const std::string update_type
= "fakeType";
216 const std::string update_value
= "fakeValue";
218 WebRTCInternals::GetInstance()->OnAddPeerConnection(
219 rid
, pid
, lid
, kUrl
, kServers
, kContraints
);
220 WebRTCInternals::GetInstance()->OnUpdatePeerConnection(
221 pid
, lid
, update_type
, update_value
);
223 scoped_ptr
<MockWebRTCInternalsProxy
> observer(new MockWebRTCInternalsProxy());
224 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
226 WebRTCInternals::GetInstance()->UpdateObserver(observer
.get());
228 EXPECT_EQ("updateAllPeerConnections", observer
->command());
230 base::ListValue
* list
= NULL
;
231 EXPECT_TRUE(observer
->value()->GetAsList(&list
));
232 EXPECT_EQ(1U, list
->GetSize());
234 base::DictionaryValue
* dict
= NULL
;
235 EXPECT_TRUE((*list
->begin())->GetAsDictionary(&dict
));
237 VerifyInt(dict
, "rid", rid
);
238 VerifyInt(dict
, "pid", pid
);
239 VerifyInt(dict
, "lid", lid
);
240 VerifyString(dict
, "url", kUrl
);
241 VerifyString(dict
, "servers", kServers
);
242 VerifyString(dict
, "constraints", kContraints
);
244 base::ListValue
* log
= NULL
;
245 EXPECT_TRUE(dict
->GetList("log", &log
));
246 EXPECT_EQ(1U, log
->GetSize());
248 EXPECT_TRUE((*log
->begin())->GetAsDictionary(&dict
));
249 VerifyString(dict
, "type", update_type
);
250 VerifyString(dict
, "value", update_value
);
253 TEST_F(WebRTCInternalsTest
, OnAddStats
) {
254 const int rid
= 0, pid
= 1, lid
= 2;
255 scoped_ptr
<MockWebRTCInternalsProxy
> observer(new MockWebRTCInternalsProxy());
256 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
257 WebRTCInternals::GetInstance()->OnAddPeerConnection(
258 rid
, pid
, lid
, kUrl
, kServers
, kContraints
);
260 base::ListValue list
;
261 list
.AppendString("xxx");
262 list
.AppendString("yyy");
263 WebRTCInternals::GetInstance()->OnAddStats(pid
, lid
, list
);
265 EXPECT_EQ("addStats", observer
->command());
266 base::DictionaryValue
* dict
= NULL
;
267 EXPECT_TRUE(observer
->value()->GetAsDictionary(&dict
));
269 VerifyInt(dict
, "pid", pid
);
270 VerifyInt(dict
, "lid", lid
);
271 VerifyList(dict
, "reports", list
);
274 TEST_F(WebRTCInternalsTest
, AecRecordingFileSelectionCanceled
) {
275 scoped_ptr
<MockWebRTCInternalsProxy
> observer(new MockWebRTCInternalsProxy());
276 WebRTCInternals::GetInstance()->AddObserver(observer
.get());
277 WebRTCInternals::GetInstance()->FileSelectionCanceled(NULL
);
278 EXPECT_EQ("aecRecordingFileSelectionCancelled", observer
->command());
279 EXPECT_EQ(NULL
, observer
->value());
282 } // namespace content