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

Side by Side Diff: chrome/browser/extensions/api/webrtc_audio_private/webrtc_audio_private_browsertest.cc

Issue 880393002: Fix webrtcAudioPrivate API to handle requests from <webview>s. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update test Created 5 years, 10 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/json/json_writer.h" 5 #include "base/json/json_writer.h"
6 #include "base/strings/string_util.h" 6 #include "base/strings/string_util.h"
7 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "base/threading/platform_thread.h" 10 #include "base/threading/platform_thread.h"
(...skipping 14 matching lines...) Expand all
25 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
26 #include "content/public/test/browser_test_utils.h" 26 #include "content/public/test/browser_test_utils.h"
27 #include "extensions/common/permissions/permission_set.h" 27 #include "extensions/common/permissions/permission_set.h"
28 #include "extensions/common/permissions/permissions_data.h" 28 #include "extensions/common/permissions/permissions_data.h"
29 #include "media/audio/audio_manager.h" 29 #include "media/audio/audio_manager.h"
30 #include "media/audio/audio_manager_base.h" 30 #include "media/audio/audio_manager_base.h"
31 #include "net/test/embedded_test_server/embedded_test_server.h" 31 #include "net/test/embedded_test_server/embedded_test_server.h"
32 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
33 33
34 using base::JSONWriter; 34 using base::JSONWriter;
35 using content::RenderViewHost; 35 using content::RenderProcessHost;
36 using content::WebContents; 36 using content::WebContents;
37 using media::AudioDeviceNames; 37 using media::AudioDeviceNames;
38 using media::AudioManager; 38 using media::AudioManager;
39 39
40 namespace extensions { 40 namespace extensions {
41 41
42 using extension_function_test_utils::RunFunctionAndReturnError; 42 using extension_function_test_utils::RunFunctionAndReturnError;
43 using extension_function_test_utils::RunFunctionAndReturnSingleResult; 43 using extension_function_test_utils::RunFunctionAndReturnSingleResult;
44 44
45 class AudioWaitingExtensionTest : public ExtensionApiTest { 45 class AudioWaitingExtensionTest : public ExtensionApiTest {
46 protected: 46 protected:
47 void WaitUntilAudioIsPlaying(WebContents* tab) { 47 void WaitUntilAudioIsPlaying(WebContents* tab) {
48 // Wait for audio to start playing. We gate this on there being one 48 // Wait for audio to start playing. We gate this on there being one
49 // or more AudioOutputController objects for our tab. 49 // or more AudioOutputController objects for our tab.
50 bool audio_playing = false; 50 bool audio_playing = false;
51 for (size_t remaining_tries = 50; remaining_tries > 0; --remaining_tries) { 51 for (size_t remaining_tries = 50; remaining_tries > 0; --remaining_tries) {
52 tab->GetRenderViewHost()->GetAudioOutputControllers( 52 tab->GetRenderProcessHost()->GetAudioOutputControllers(
53 base::Bind(OnAudioControllers, &audio_playing)); 53 base::Bind(OnAudioControllers, &audio_playing));
54 base::MessageLoop::current()->RunUntilIdle(); 54 base::MessageLoop::current()->RunUntilIdle();
55 if (audio_playing) 55 if (audio_playing)
56 break; 56 break;
57 57
58 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); 58 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
59 } 59 }
60 60
61 if (!audio_playing) 61 if (!audio_playing)
62 FAIL() << "Audio did not start playing within ~5 seconds."; 62 FAIL() << "Audio did not start playing within ~5 seconds.";
63 } 63 }
64 64
65 // Used by the test above to wait until audio is playing. 65 // Used by the test above to wait until audio is playing.
66 static void OnAudioControllers( 66 static void OnAudioControllers(
67 bool* audio_playing, 67 bool* audio_playing,
68 const RenderViewHost::AudioOutputControllerList& list) { 68 const RenderProcessHost::AudioOutputControllerList& list) {
69 if (!list.empty()) 69 if (!list.empty())
70 *audio_playing = true; 70 *audio_playing = true;
71 } 71 }
72 }; 72 };
73 73
74 class WebrtcAudioPrivateTest : public AudioWaitingExtensionTest { 74 class WebrtcAudioPrivateTest : public AudioWaitingExtensionTest {
75 public: 75 public:
76 WebrtcAudioPrivateTest() 76 WebrtcAudioPrivateTest()
77 : enumeration_event_(false, false) { 77 : enumeration_event_(false, false) {
78 } 78 }
79 79
80 void SetUpOnMainThread() override { 80 void SetUpOnMainThread() override {
81 AudioWaitingExtensionTest::SetUpOnMainThread(); 81 AudioWaitingExtensionTest::SetUpOnMainThread();
82 // Needs to happen after chrome's schemes are added. 82 // Needs to happen after chrome's schemes are added.
83 source_url_ = GURL("chrome-extension://fakeid012345678/fakepage.html"); 83 source_url_ = GURL("chrome-extension://fakeid012345678/fakepage.html");
84 } 84 }
85 85
86 protected: 86 protected:
87 void AppendTabIdToRequestInfo(base::ListValue* params, int tab_id) {
88 base::DictionaryValue* request_info = new base::DictionaryValue();
89 request_info->SetInteger("tabId", tab_id);
90 params->Append(request_info);
91 }
92
87 std::string InvokeGetActiveSink(int tab_id) { 93 std::string InvokeGetActiveSink(int tab_id) {
88 base::ListValue parameters; 94 base::ListValue parameters;
89 parameters.AppendInteger(tab_id); 95 AppendTabIdToRequestInfo(&parameters, tab_id);
90 std::string parameter_string; 96 std::string parameter_string;
91 JSONWriter::Write(&parameters, &parameter_string); 97 JSONWriter::Write(&parameters, &parameter_string);
92 98
93 scoped_refptr<WebrtcAudioPrivateGetActiveSinkFunction> function = 99 scoped_refptr<WebrtcAudioPrivateGetActiveSinkFunction> function =
94 new WebrtcAudioPrivateGetActiveSinkFunction(); 100 new WebrtcAudioPrivateGetActiveSinkFunction();
95 function->set_source_url(source_url_); 101 function->set_source_url(source_url_);
96 scoped_ptr<base::Value> result( 102 scoped_ptr<base::Value> result(
97 RunFunctionAndReturnSingleResult(function.get(), 103 RunFunctionAndReturnSingleResult(function.get(),
98 parameter_string, 104 parameter_string,
99 browser())); 105 browser()));
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 } 220 }
215 } 221 }
216 #endif // OS_MACOSX 222 #endif // OS_MACOSX
217 223
218 // This exercises the case where you have a tab with no active media 224 // This exercises the case where you have a tab with no active media
219 // stream and try to retrieve the currently active audio sink. 225 // stream and try to retrieve the currently active audio sink.
220 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetActiveSinkNoMediaStream) { 226 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetActiveSinkNoMediaStream) {
221 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); 227 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
222 int tab_id = ExtensionTabUtil::GetTabId(tab); 228 int tab_id = ExtensionTabUtil::GetTabId(tab);
223 base::ListValue parameters; 229 base::ListValue parameters;
224 parameters.AppendInteger(tab_id); 230 AppendTabIdToRequestInfo(&parameters, tab_id);
225 std::string parameter_string; 231 std::string parameter_string;
226 JSONWriter::Write(&parameters, &parameter_string); 232 JSONWriter::Write(&parameters, &parameter_string);
227 233
228 scoped_refptr<WebrtcAudioPrivateGetActiveSinkFunction> function = 234 scoped_refptr<WebrtcAudioPrivateGetActiveSinkFunction> function =
229 new WebrtcAudioPrivateGetActiveSinkFunction(); 235 new WebrtcAudioPrivateGetActiveSinkFunction();
230 function->set_source_url(source_url_); 236 function->set_source_url(source_url_);
231 scoped_ptr<base::Value> result( 237 scoped_ptr<base::Value> result(
232 RunFunctionAndReturnSingleResult(function.get(), 238 RunFunctionAndReturnSingleResult(function.get(),
233 parameter_string, 239 parameter_string,
234 browser())); 240 browser()));
235 241
236 std::string result_string; 242 std::string result_string;
237 JSONWriter::Write(result.get(), &result_string); 243 JSONWriter::Write(result.get(), &result_string);
238 EXPECT_EQ("\"\"", result_string); 244 EXPECT_EQ("\"\"", result_string);
239 } 245 }
240 246
241 // This exercises the case where you have a tab with no active media 247 // This exercises the case where you have a tab with no active media
242 // stream and try to set the audio sink. 248 // stream and try to set the audio sink.
243 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, SetActiveSinkNoMediaStream) { 249 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, SetActiveSinkNoMediaStream) {
244 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); 250 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
245 int tab_id = ExtensionTabUtil::GetTabId(tab); 251 int tab_id = ExtensionTabUtil::GetTabId(tab);
246 base::ListValue parameters; 252 base::ListValue parameters;
247 parameters.AppendInteger(tab_id); 253 AppendTabIdToRequestInfo(&parameters, tab_id);
248 parameters.AppendString("no such id"); 254 parameters.AppendString("no such id");
249 std::string parameter_string; 255 std::string parameter_string;
250 JSONWriter::Write(&parameters, &parameter_string); 256 JSONWriter::Write(&parameters, &parameter_string);
251 257
252 scoped_refptr<WebrtcAudioPrivateSetActiveSinkFunction> function = 258 scoped_refptr<WebrtcAudioPrivateSetActiveSinkFunction> function =
253 new WebrtcAudioPrivateSetActiveSinkFunction(); 259 new WebrtcAudioPrivateSetActiveSinkFunction();
254 function->set_source_url(source_url_); 260 function->set_source_url(source_url_);
255 std::string error(RunFunctionAndReturnError(function.get(), 261 std::string error(RunFunctionAndReturnError(function.get(),
256 parameter_string, 262 parameter_string,
257 browser())); 263 browser()));
258 EXPECT_EQ(base::StringPrintf("No active stream for tab with id: %d.", tab_id), 264 EXPECT_EQ(base::StringPrintf("No active stream for tabId %d", tab_id),
259 error); 265 error);
260 } 266 }
261 267
262 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetAndSetWithMediaStream) { 268 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetAndSetWithMediaStream) {
263 // First retrieve the list of all sinks, so that we can run a test 269 // First retrieve the list of all sinks, so that we can run a test
264 // where we set the active sink to each of the different available 270 // where we set the active sink to each of the different available
265 // sinks in turn. 271 // sinks in turn.
266 base::ListValue* sink_list = NULL; 272 base::ListValue* sink_list = NULL;
267 scoped_ptr<base::Value> result = InvokeGetSinks(&sink_list); 273 scoped_ptr<base::Value> result = InvokeGetSinks(&sink_list);
268 274
(...skipping 14 matching lines...) Expand all
283 EXPECT_NE("", current_device); 289 EXPECT_NE("", current_device);
284 290
285 // Set to each of the other devices in turn. 291 // Set to each of the other devices in turn.
286 for (size_t ix = 0; ix < sink_list->GetSize(); ++ix) { 292 for (size_t ix = 0; ix < sink_list->GetSize(); ++ix) {
287 base::DictionaryValue* dict = NULL; 293 base::DictionaryValue* dict = NULL;
288 sink_list->GetDictionary(ix, &dict); 294 sink_list->GetDictionary(ix, &dict);
289 std::string target_device; 295 std::string target_device;
290 dict->GetString("sinkId", &target_device); 296 dict->GetString("sinkId", &target_device);
291 297
292 base::ListValue parameters; 298 base::ListValue parameters;
293 parameters.AppendInteger(tab_id); 299 AppendTabIdToRequestInfo(&parameters, tab_id);
294 parameters.AppendString(target_device); 300 parameters.AppendString(target_device);
295 std::string parameter_string; 301 std::string parameter_string;
296 JSONWriter::Write(&parameters, &parameter_string); 302 JSONWriter::Write(&parameters, &parameter_string);
297 303
298 scoped_refptr<WebrtcAudioPrivateSetActiveSinkFunction> function = 304 scoped_refptr<WebrtcAudioPrivateSetActiveSinkFunction> function =
299 new WebrtcAudioPrivateSetActiveSinkFunction(); 305 new WebrtcAudioPrivateSetActiveSinkFunction();
300 function->set_source_url(source_url_); 306 function->set_source_url(source_url_);
301 scoped_ptr<base::Value> result(RunFunctionAndReturnSingleResult( 307 scoped_ptr<base::Value> result(RunFunctionAndReturnSingleResult(
302 function.get(), parameter_string, browser())); 308 function.get(), parameter_string, browser()));
303 // The function was successful if the above invocation doesn't 309 // The function was successful if the above invocation doesn't
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("failure")); 416 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("failure"));
411 base::string16 result = title_watcher.WaitAndGetTitle(); 417 base::string16 result = title_watcher.WaitAndGetTitle();
412 EXPECT_EQ(base::ASCIIToUTF16("success"), result); 418 EXPECT_EQ(base::ASCIIToUTF16("success"), result);
413 419
414 g_browser_process->webrtc_log_uploader()->OverrideUploadWithBufferForTesting( 420 g_browser_process->webrtc_log_uploader()->OverrideUploadWithBufferForTesting(
415 NULL); 421 NULL);
416 } 422 }
417 #endif // defined(GOOGLE_CHROME_BUILD) || defined(ENABLE_HANGOUT_SERVICES_EXTEN SION) 423 #endif // defined(GOOGLE_CHROME_BUILD) || defined(ENABLE_HANGOUT_SERVICES_EXTEN SION)
418 424
419 } // namespace extensions 425 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698