Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 #include "chrome/common/features.h" | 28 #include "chrome/common/features.h" |
| 29 #include "chrome/test/base/in_process_browser_test.h" | 29 #include "chrome/test/base/in_process_browser_test.h" |
| 30 #include "chrome/test/base/ui_test_utils.h" | 30 #include "chrome/test/base/ui_test_utils.h" |
| 31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/media_device_id.h" | 32 #include "content/public/browser/media_device_id.h" |
| 33 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 34 #include "content/public/test/browser_test_utils.h" | 34 #include "content/public/test/browser_test_utils.h" |
| 35 #include "extensions/common/permissions/permission_set.h" | 35 #include "extensions/common/permissions/permission_set.h" |
| 36 #include "extensions/common/permissions/permissions_data.h" | 36 #include "extensions/common/permissions/permissions_data.h" |
| 37 #include "media/audio/audio_device_description.h" | 37 #include "media/audio/audio_device_description.h" |
| 38 #include "media/audio/audio_manager.h" | 38 #include "media/audio/audio_system.h" |
| 39 #include "net/test/embedded_test_server/embedded_test_server.h" | 39 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 40 #include "testing/gtest/include/gtest/gtest.h" | 40 #include "testing/gtest/include/gtest/gtest.h" |
| 41 | 41 |
| 42 #if defined(OS_WIN) | 42 #if defined(OS_WIN) |
| 43 #include "base/win/windows_version.h" | 43 #include "base/win/windows_version.h" |
| 44 #endif | 44 #endif |
| 45 | 45 |
| 46 using base::JSONWriter; | 46 using base::JSONWriter; |
| 47 using content::RenderProcessHost; | 47 using content::RenderProcessHost; |
| 48 using content::WebContents; | 48 using content::WebContents; |
| 49 using media::AudioDeviceDescriptions; | 49 using media::AudioDeviceDescriptions; |
| 50 using media::AudioManager; | |
| 51 | 50 |
| 52 namespace extensions { | 51 namespace extensions { |
| 53 | 52 |
| 54 using extension_function_test_utils::RunFunctionAndReturnError; | 53 using extension_function_test_utils::RunFunctionAndReturnError; |
| 55 using extension_function_test_utils::RunFunctionAndReturnSingleResult; | 54 using extension_function_test_utils::RunFunctionAndReturnSingleResult; |
| 56 | 55 |
| 56 namespace { | |
|
Devlin
2017/04/06 01:42:33
nit: \n
o1ka
2017/04/06 11:01:22
Done.
| |
| 57 // Synchronously (from the calling thread's point of view) runs the | |
| 58 // given enumeration function on the device thread. On return, | |
| 59 // |device_descriptions| has been filled with the device descriptions | |
| 60 // resulting from that call. | |
| 61 void GetAudioDeviceDescriptions(bool for_input, | |
| 62 AudioDeviceDescriptions* device_descriptions) { | |
| 63 base::RunLoop run_loop; | |
| 64 media::AudioSystem::Get()->GetDeviceDescriptions( | |
| 65 base::Bind( | |
| 66 [](base::Closure finished_callback, AudioDeviceDescriptions* result, | |
|
Devlin
2017/04/06 01:42:33
nit: const base::Closure&
o1ka
2017/04/06 11:01:22
It takes the ownership. base::Closure is movable a
| |
| 67 AudioDeviceDescriptions received) { | |
| 68 *result = std::move(received); | |
| 69 finished_callback.Run(); | |
| 70 }, | |
| 71 base ::Passed(run_loop.QuitClosure()), device_descriptions), | |
|
Devlin
2017/04/06 01:42:33
eliminate rogue space between 'base' and '::'
o1ka
2017/04/06 11:01:22
Done.
| |
| 72 for_input); | |
| 73 run_loop.Run(); | |
| 74 } | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 57 class AudioWaitingExtensionTest : public ExtensionApiTest { | 78 class AudioWaitingExtensionTest : public ExtensionApiTest { |
| 58 protected: | 79 protected: |
| 59 void WaitUntilAudioIsPlaying(WebContents* tab) { | 80 void WaitUntilAudioIsPlaying(WebContents* tab) { |
| 60 // Wait for audio to start playing. | 81 // Wait for audio to start playing. |
| 61 bool audio_playing = false; | 82 bool audio_playing = false; |
| 62 for (size_t remaining_tries = 50; remaining_tries > 0; --remaining_tries) { | 83 for (size_t remaining_tries = 50; remaining_tries > 0; --remaining_tries) { |
| 63 audio_playing = tab->WasRecentlyAudible(); | 84 audio_playing = tab->WasRecentlyAudible(); |
| 64 base::RunLoop().RunUntilIdle(); | 85 base::RunLoop().RunUntilIdle(); |
| 65 if (audio_playing) | 86 if (audio_playing) |
| 66 break; | 87 break; |
| 67 | |
| 68 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); | 88 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
| 69 } | 89 } |
| 70 | |
| 71 if (!audio_playing) | 90 if (!audio_playing) |
| 72 FAIL() << "Audio did not start playing within ~5 seconds."; | 91 FAIL() << "Audio did not start playing within ~5 seconds."; |
| 73 } | 92 } |
| 74 }; | 93 }; |
| 75 | 94 |
| 76 class WebrtcAudioPrivateTest : public AudioWaitingExtensionTest { | 95 class WebrtcAudioPrivateTest : public AudioWaitingExtensionTest { |
| 77 public: | 96 public: |
| 78 WebrtcAudioPrivateTest() | 97 WebrtcAudioPrivateTest() {} |
| 79 : enumeration_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 80 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | |
| 81 | 98 |
| 82 void SetUpOnMainThread() override { | 99 void SetUpOnMainThread() override { |
| 83 AudioWaitingExtensionTest::SetUpOnMainThread(); | 100 AudioWaitingExtensionTest::SetUpOnMainThread(); |
| 84 // Needs to happen after chrome's schemes are added. | 101 // Needs to happen after chrome's schemes are added. |
| 85 source_url_ = GURL("chrome-extension://fakeid012345678/fakepage.html"); | 102 source_url_ = GURL("chrome-extension://fakeid012345678/fakepage.html"); |
| 86 } | 103 } |
| 87 | 104 |
| 88 protected: | 105 protected: |
| 89 void AppendTabIdToRequestInfo(base::ListValue* params, int tab_id) { | 106 void AppendTabIdToRequestInfo(base::ListValue* params, int tab_id) { |
| 90 std::unique_ptr<base::DictionaryValue> request_info( | 107 std::unique_ptr<base::DictionaryValue> request_info( |
| 91 new base::DictionaryValue()); | 108 new base::DictionaryValue()); |
| 92 request_info->SetInteger("tabId", tab_id); | 109 request_info->SetInteger("tabId", tab_id); |
| 93 params->Append(std::move(request_info)); | 110 params->Append(std::move(request_info)); |
| 94 } | 111 } |
| 95 | 112 |
| 96 std::unique_ptr<base::Value> InvokeGetSinks(base::ListValue** sink_list) { | 113 std::unique_ptr<base::Value> InvokeGetSinks(base::ListValue** sink_list) { |
| 97 scoped_refptr<WebrtcAudioPrivateGetSinksFunction> function = | 114 scoped_refptr<WebrtcAudioPrivateGetSinksFunction> function = |
| 98 new WebrtcAudioPrivateGetSinksFunction(); | 115 new WebrtcAudioPrivateGetSinksFunction(); |
| 99 function->set_source_url(source_url_); | 116 function->set_source_url(source_url_); |
| 100 | 117 |
| 101 std::unique_ptr<base::Value> result( | 118 std::unique_ptr<base::Value> result( |
| 102 RunFunctionAndReturnSingleResult(function.get(), "[]", browser())); | 119 RunFunctionAndReturnSingleResult(function.get(), "[]", browser())); |
| 103 result->GetAsList(sink_list); | 120 result->GetAsList(sink_list); |
| 104 return result; | 121 return result; |
| 105 } | 122 } |
| 106 | 123 |
| 107 // Synchronously (from the calling thread's point of view) runs the | |
| 108 // given enumeration function on the device thread. On return, | |
| 109 // |device_descriptions| has been filled with the device descriptions | |
| 110 // resulting from that call. | |
| 111 void GetAudioDeviceDescriptions( | |
| 112 void (AudioManager::*EnumerationFunc)(AudioDeviceDescriptions*), | |
| 113 AudioDeviceDescriptions* device_descriptions) { | |
| 114 AudioManager* audio_manager = AudioManager::Get(); | |
| 115 | |
| 116 if (!audio_manager->GetTaskRunner()->BelongsToCurrentThread()) { | |
| 117 audio_manager->GetTaskRunner()->PostTask( | |
| 118 FROM_HERE, | |
| 119 base::Bind(&WebrtcAudioPrivateTest::GetAudioDeviceDescriptions, | |
| 120 base::Unretained(this), EnumerationFunc, | |
| 121 device_descriptions)); | |
| 122 enumeration_event_.Wait(); | |
| 123 } else { | |
| 124 (audio_manager->*EnumerationFunc)(device_descriptions); | |
| 125 enumeration_event_.Signal(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 // Synchronously (from the calling thread's point of view) retrieve the | |
| 130 // device id in the |origin| on the IO thread. On return, | |
| 131 // |id_in_origin| contains the id |raw_device_id| is known by in | |
| 132 // the origin. | |
| 133 void GetIDInOrigin(content::ResourceContext* resource_context, | |
| 134 GURL origin, | |
| 135 const std::string& raw_device_id, | |
| 136 std::string* id_in_origin) { | |
| 137 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | |
| 138 content::BrowserThread::PostTask( | |
| 139 content::BrowserThread::UI, FROM_HERE, | |
| 140 base::Bind(&WebrtcAudioPrivateTest::GetIDInOrigin, | |
| 141 base::Unretained(this), | |
| 142 resource_context, origin, raw_device_id, id_in_origin)); | |
| 143 enumeration_event_.Wait(); | |
| 144 } else { | |
| 145 *id_in_origin = content::GetHMACForMediaDeviceID( | |
| 146 resource_context->GetMediaDeviceIDSalt(), url::Origin(origin), | |
| 147 raw_device_id); | |
| 148 enumeration_event_.Signal(); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 // Event used to signal completion of enumeration. | |
| 153 base::WaitableEvent enumeration_event_; | |
| 154 | |
| 155 GURL source_url_; | 124 GURL source_url_; |
| 156 }; | 125 }; |
| 157 | 126 |
| 158 #if !defined(OS_MACOSX) | 127 #if !defined(OS_MACOSX) |
| 159 // http://crbug.com/334579 | 128 // http://crbug.com/334579 |
| 160 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetSinks) { | 129 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetSinks) { |
| 161 AudioDeviceDescriptions devices; | 130 AudioDeviceDescriptions devices; |
| 162 GetAudioDeviceDescriptions(&AudioManager::GetAudioOutputDeviceDescriptions, | 131 GetAudioDeviceDescriptions(false, &devices); |
| 163 &devices); | |
| 164 | 132 |
| 165 base::ListValue* sink_list = NULL; | 133 base::ListValue* sink_list = NULL; |
| 166 std::unique_ptr<base::Value> result = InvokeGetSinks(&sink_list); | 134 std::unique_ptr<base::Value> result = InvokeGetSinks(&sink_list); |
| 167 | 135 |
| 168 std::string result_string; | 136 std::string result_string; |
| 169 JSONWriter::Write(*result, &result_string); | 137 JSONWriter::Write(*result, &result_string); |
| 170 VLOG(2) << result_string; | 138 VLOG(2) << result_string; |
| 171 | 139 |
| 172 EXPECT_EQ(devices.size(), sink_list->GetSize()); | 140 EXPECT_EQ(devices.size(), sink_list->GetSize()); |
| 173 | 141 |
| 174 // Iterate through both lists in lockstep and compare. The order | 142 // Iterate through both lists in lockstep and compare. The order |
| 175 // should be identical. | 143 // should be identical. |
| 176 size_t ix = 0; | 144 size_t ix = 0; |
| 177 AudioDeviceDescriptions::const_iterator it = devices.begin(); | 145 AudioDeviceDescriptions::const_iterator it = devices.begin(); |
| 178 for (; ix < sink_list->GetSize() && it != devices.end(); | 146 for (; ix < sink_list->GetSize() && it != devices.end(); |
| 179 ++ix, ++it) { | 147 ++ix, ++it) { |
| 180 base::DictionaryValue* dict = NULL; | 148 base::DictionaryValue* dict = NULL; |
| 181 sink_list->GetDictionary(ix, &dict); | 149 sink_list->GetDictionary(ix, &dict); |
| 182 std::string sink_id; | 150 std::string sink_id; |
| 183 dict->GetString("sinkId", &sink_id); | 151 dict->GetString("sinkId", &sink_id); |
| 184 | 152 |
| 185 std::string expected_id; | 153 std::string expected_id = |
| 186 if (media::AudioDeviceDescription::IsDefaultDevice(it->unique_id)) { | 154 media::AudioDeviceDescription::IsDefaultDevice(it->unique_id) |
| 187 expected_id = media::AudioDeviceDescription::kDefaultDeviceId; | 155 ? media::AudioDeviceDescription::kDefaultDeviceId |
| 188 } else { | 156 : content::GetHMACForMediaDeviceID( |
| 189 GetIDInOrigin(profile()->GetResourceContext(), | 157 profile()->GetResourceContext()->GetMediaDeviceIDSalt(), |
| 190 source_url_.GetOrigin(), | 158 url::Origin(source_url_.GetOrigin()), it->unique_id); |
| 191 it->unique_id, | |
| 192 &expected_id); | |
| 193 } | |
| 194 | 159 |
| 195 EXPECT_EQ(expected_id, sink_id); | 160 EXPECT_EQ(expected_id, sink_id); |
| 196 std::string sink_label; | 161 std::string sink_label; |
| 197 dict->GetString("sinkLabel", &sink_label); | 162 dict->GetString("sinkLabel", &sink_label); |
| 198 EXPECT_EQ(it->device_name, sink_label); | 163 EXPECT_EQ(it->device_name, sink_label); |
| 199 | 164 |
| 200 // TODO(joi): Verify the contents of these once we start actually | 165 // TODO(joi): Verify the contents of these once we start actually |
| 201 // filling them in. | 166 // filling them in. |
| 202 EXPECT_TRUE(dict->HasKey("isDefault")); | 167 EXPECT_TRUE(dict->HasKey("isDefault")); |
| 203 EXPECT_TRUE(dict->HasKey("isReady")); | 168 EXPECT_TRUE(dict->HasKey("isReady")); |
| 204 EXPECT_TRUE(dict->HasKey("sampleRate")); | 169 EXPECT_TRUE(dict->HasKey("sampleRate")); |
| 205 } | 170 } |
| 206 } | 171 } |
| 207 #endif // OS_MACOSX | 172 #endif // OS_MACOSX |
| 208 | 173 |
| 209 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetAssociatedSink) { | 174 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetAssociatedSink) { |
| 210 // Get the list of input devices. We can cheat in the unit test and | 175 // Get the list of input devices. We can cheat in the unit test and |
| 211 // run this on the main thread since nobody else will be running at | 176 // run this on the main thread since nobody else will be running at |
| 212 // the same time. | 177 // the same time. |
| 213 AudioDeviceDescriptions devices; | 178 AudioDeviceDescriptions devices; |
| 214 GetAudioDeviceDescriptions(&AudioManager::GetAudioInputDeviceDescriptions, | 179 GetAudioDeviceDescriptions(true, &devices); |
| 215 &devices); | |
| 216 | 180 |
| 217 // Try to get an associated sink for each source. | 181 // Try to get an associated sink for each source. |
| 218 for (const auto& device : devices) { | 182 for (const auto& device : devices) { |
| 219 scoped_refptr<WebrtcAudioPrivateGetAssociatedSinkFunction> function = | 183 scoped_refptr<WebrtcAudioPrivateGetAssociatedSinkFunction> function = |
| 220 new WebrtcAudioPrivateGetAssociatedSinkFunction(); | 184 new WebrtcAudioPrivateGetAssociatedSinkFunction(); |
| 221 function->set_source_url(source_url_); | 185 function->set_source_url(source_url_); |
| 222 | 186 |
| 223 std::string raw_device_id = device.unique_id; | 187 std::string raw_device_id = device.unique_id; |
| 224 VLOG(2) << "Trying to find associated sink for device " << raw_device_id; | 188 VLOG(2) << "Trying to find associated sink for device " << raw_device_id; |
| 225 std::string source_id_in_origin; | |
| 226 GURL origin(GURL("http://www.google.com/").GetOrigin()); | 189 GURL origin(GURL("http://www.google.com/").GetOrigin()); |
| 227 GetIDInOrigin(profile()->GetResourceContext(), | 190 std::string source_id_in_origin = content::GetHMACForMediaDeviceID( |
| 228 origin, | 191 profile()->GetResourceContext()->GetMediaDeviceIDSalt(), |
| 229 raw_device_id, | 192 url::Origin(origin), raw_device_id); |
| 230 &source_id_in_origin); | |
| 231 | 193 |
| 232 base::ListValue parameters; | 194 base::ListValue parameters; |
| 233 parameters.AppendString(origin.spec()); | 195 parameters.AppendString(origin.spec()); |
| 234 parameters.AppendString(source_id_in_origin); | 196 parameters.AppendString(source_id_in_origin); |
| 235 std::string parameter_string; | 197 std::string parameter_string; |
| 236 JSONWriter::Write(parameters, ¶meter_string); | 198 JSONWriter::Write(parameters, ¶meter_string); |
| 237 | 199 |
| 238 std::unique_ptr<base::Value> result(RunFunctionAndReturnSingleResult( | 200 std::unique_ptr<base::Value> result(RunFunctionAndReturnSingleResult( |
| 239 function.get(), parameter_string, browser())); | 201 function.get(), parameter_string, browser())); |
| 240 std::string result_string; | 202 std::string result_string; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("failure")); | 265 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("failure")); |
| 304 base::string16 result = title_watcher.WaitAndGetTitle(); | 266 base::string16 result = title_watcher.WaitAndGetTitle(); |
| 305 EXPECT_EQ(base::ASCIIToUTF16("success"), result); | 267 EXPECT_EQ(base::ASCIIToUTF16("success"), result); |
| 306 | 268 |
| 307 g_browser_process->webrtc_log_uploader()->OverrideUploadWithBufferForTesting( | 269 g_browser_process->webrtc_log_uploader()->OverrideUploadWithBufferForTesting( |
| 308 NULL); | 270 NULL); |
| 309 } | 271 } |
| 310 #endif // BUILDFLAG(ENABLE_HANGOUT_SERVICES_EXTENSION) | 272 #endif // BUILDFLAG(ENABLE_HANGOUT_SERVICES_EXTENSION) |
| 311 | 273 |
| 312 } // namespace extensions | 274 } // namespace extensions |
| OLD | NEW |