Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/media/media_internals.h" | 5 #include "content/browser/media/media_internals.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "media/audio/audio_parameters.h" | 13 #include "media/audio/audio_parameters.h" |
| 14 #include "media/base/channel_layout.h" | 14 #include "media/base/channel_layout.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 const int kTestComponentID = 0; | 18 const int kTestComponentID = 0; |
| 19 const char kTestDeviceID[] = "test-device-id"; | 19 const char kTestDeviceID[] = "test-device-id"; |
| 20 } // namespace | |
| 21 | 20 |
| 22 namespace content { | 21 // This class encapsulates a MediaInternals reference. It also has some useful |
| 23 | 22 // methods to receive a callback, deserialize its associated data and expect |
| 24 class MediaInternalsTest | 23 // integer/string values. |
| 25 : public testing::TestWithParam<media::AudioLogFactory::AudioComponent> { | 24 class MediaInternalsTestBase { |
| 26 public: | 25 public: |
| 27 MediaInternalsTest() | 26 MediaInternalsTestBase() |
| 28 : media_internals_(MediaInternals::GetInstance()), | 27 : media_internals_(content::MediaInternals::GetInstance()) { |
| 29 update_cb_(base::Bind(&MediaInternalsTest::UpdateCallbackImpl, | |
| 30 base::Unretained(this))), | |
| 31 test_params_(media::AudioParameters::AUDIO_PCM_LINEAR, | |
| 32 media::CHANNEL_LAYOUT_MONO, | |
| 33 48000, | |
| 34 16, | |
| 35 128, | |
| 36 media::AudioParameters::ECHO_CANCELLER | | |
| 37 media::AudioParameters::DUCKING), | |
| 38 test_component_(GetParam()), | |
| 39 audio_log_(media_internals_->CreateAudioLog(test_component_)) { | |
| 40 media_internals_->AddUpdateCallback(update_cb_); | |
| 41 } | 28 } |
| 42 | 29 virtual ~MediaInternalsTestBase() {} |
| 43 virtual ~MediaInternalsTest() { | |
| 44 media_internals_->RemoveUpdateCallback(update_cb_); | |
| 45 } | |
| 46 | 30 |
| 47 protected: | 31 protected: |
| 48 // Extracts and deserializes the JSON update data; merges into |update_data_|. | 32 // Extracts and deserializes the JSON update data; merges into |update_data_|. |
| 49 void UpdateCallbackImpl(const base::string16& update) { | 33 void UpdateCallbackImpl(const base::string16& update) { |
| 50 // Each update string looks like "<JavaScript Function Name>({<JSON>});", to | 34 // Each update string looks like "<JavaScript Function Name>({<JSON>});", to |
| 51 // use the JSON reader we need to strip out the JavaScript code. | 35 // use the JSON reader we need to strip out the JavaScript code. |
| 52 std::string utf8_update = base::UTF16ToUTF8(update); | 36 std::string utf8_update = base::UTF16ToUTF8(update); |
| 53 const std::string::size_type first_brace = utf8_update.find('{'); | 37 const std::string::size_type first_brace = utf8_update.find('{'); |
| 54 const std::string::size_type last_brace = utf8_update.rfind('}'); | 38 const std::string::size_type last_brace = utf8_update.rfind('}'); |
| 55 scoped_ptr<base::Value> output_value(base::JSONReader::Read( | 39 scoped_ptr<base::Value> output_value(base::JSONReader::Read( |
| 56 utf8_update.substr(first_brace, last_brace - first_brace + 1))); | 40 utf8_update.substr(first_brace, last_brace - first_brace + 1))); |
| 57 CHECK(output_value); | 41 CHECK(output_value); |
| 58 | 42 |
| 59 base::DictionaryValue* output_dict = NULL; | 43 base::DictionaryValue* output_dict = NULL; |
| 60 CHECK(output_value->GetAsDictionary(&output_dict)); | 44 CHECK(output_value->GetAsDictionary(&output_dict)); |
| 61 update_data_.MergeDictionary(output_dict); | 45 update_data_.MergeDictionary(output_dict); |
| 62 } | 46 } |
| 63 | 47 |
| 64 void ExpectInt(const std::string& key, int expected_value) { | 48 void ExpectInt(const std::string& key, int expected_value) const { |
| 65 int actual_value = 0; | 49 int actual_value = 0; |
| 66 ASSERT_TRUE(update_data_.GetInteger(key, &actual_value)); | 50 ASSERT_TRUE(update_data_.GetInteger(key, &actual_value)); |
| 67 EXPECT_EQ(expected_value, actual_value); | 51 EXPECT_EQ(expected_value, actual_value); |
| 68 } | 52 } |
| 69 | 53 |
| 70 void ExpectString(const std::string& key, const std::string& expected_value) { | 54 void ExpectString(const std::string& key, |
| 55 const std::string& expected_value) const { | |
| 71 std::string actual_value; | 56 std::string actual_value; |
| 72 ASSERT_TRUE(update_data_.GetString(key, &actual_value)); | 57 ASSERT_TRUE(update_data_.GetString(key, &actual_value)); |
| 73 EXPECT_EQ(expected_value, actual_value); | 58 EXPECT_EQ(expected_value, actual_value); |
| 74 } | 59 } |
| 75 | 60 |
| 76 void ExpectStatus(const std::string& expected_value) { | 61 void ExpectStatus(const std::string& expected_value) const { |
| 77 ExpectString("status", expected_value); | 62 ExpectString("status", expected_value); |
| 78 } | 63 } |
| 79 | 64 |
| 80 TestBrowserThreadBundle thread_bundle_; | 65 const content::TestBrowserThreadBundle thread_bundle_; |
| 81 MediaInternals* const media_internals_; | 66 base::DictionaryValue update_data_; |
| 67 content::MediaInternals* const media_internals_; | |
| 68 }; | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 namespace content { | |
| 73 | |
| 74 class MediaInternalsVideoCaptureDeviceTest : public testing::Test, | |
| 75 public MediaInternalsTestBase {}; | |
| 76 | |
| 77 TEST_F(MediaInternalsVideoCaptureDeviceTest, | |
| 78 NotifyVideoCaptureDeviceCapabilitiesEnumerated) { | |
| 79 const int kWidth = 1280; | |
| 80 const int kHeight = 720; | |
| 81 const float kFrameRate = 30.0f; | |
| 82 const media::VideoPixelFormat kPixelFormat = media::PIXEL_FORMAT_I420; | |
| 83 const media::VideoCaptureFormat format_hd({kWidth, kHeight}, | |
| 84 kFrameRate, kPixelFormat); | |
| 85 media::VideoCaptureFormats formats{}; | |
| 86 formats.push_back(format_hd); | |
| 87 const media::VideoCaptureDeviceInfo device_info( | |
| 88 media::VideoCaptureDevice::Name("dummy", "dummy" | |
| 89 #if defined(OS_MACOSX) | |
| 90 , media::VideoCaptureDevice::Name::QTKIT | |
| 91 #elif defined(OS_WIN) | |
| 92 , media::VideoCaptureDevice::Name::DIRECT_SHOW | |
| 93 #endif | |
| 94 ), | |
| 95 formats); | |
| 96 media::VideoCaptureDeviceInfos device_infos{}; | |
| 97 device_infos.push_back(device_info); | |
| 98 | |
| 99 // TODO(mcasas): Listen for the serialised version of |device_infos| and | |
| 100 // check its content using ExpectInt(), ExpectString(), after RunUntilIdle(). | |
| 101 media_internals_->UpdateVideoCaptureDeviceCapabilities(device_infos); | |
| 102 base::RunLoop().RunUntilIdle(); | |
| 103 } | |
| 104 | |
| 105 class MediaInternalsAudioLogTest | |
| 106 : public MediaInternalsTestBase, | |
| 107 public testing::TestWithParam<media::AudioLogFactory::AudioComponent> { | |
| 108 public: | |
| 109 MediaInternalsAudioLogTest() : | |
| 110 update_cb_(base::Bind(&MediaInternalsAudioLogTest::UpdateCallbackImpl, | |
|
wolenetz
2014/10/10 18:16:17
aside: Use ..TestBase in bind? On second thought,
mcasas
2014/10/11 21:12:37
Acknowledged.
| |
| 111 base::Unretained(this))), | |
| 112 test_params_(media::AudioParameters::AUDIO_PCM_LINEAR, | |
| 113 media::CHANNEL_LAYOUT_MONO, | |
| 114 48000, | |
| 115 16, | |
| 116 128, | |
| 117 media::AudioParameters::ECHO_CANCELLER | | |
| 118 media::AudioParameters::DUCKING), | |
| 119 test_component_(GetParam()), | |
| 120 audio_log_(media_internals_->CreateAudioLog(test_component_)) { | |
| 121 media_internals_->AddUpdateCallback(update_cb_); | |
| 122 } | |
| 123 | |
| 124 virtual ~MediaInternalsAudioLogTest() { | |
| 125 media_internals_->RemoveUpdateCallback(update_cb_); | |
| 126 } | |
| 127 | |
| 128 protected: | |
| 82 MediaInternals::UpdateCallback update_cb_; | 129 MediaInternals::UpdateCallback update_cb_; |
| 83 base::DictionaryValue update_data_; | |
| 84 const media::AudioParameters test_params_; | 130 const media::AudioParameters test_params_; |
| 85 const media::AudioLogFactory::AudioComponent test_component_; | 131 const media::AudioLogFactory::AudioComponent test_component_; |
| 86 scoped_ptr<media::AudioLog> audio_log_; | 132 scoped_ptr<media::AudioLog> audio_log_; |
| 87 }; | 133 }; |
| 88 | 134 |
| 89 TEST_P(MediaInternalsTest, AudioLogCreateStartStopErrorClose) { | 135 TEST_P(MediaInternalsAudioLogTest, AudioLogCreateStartStopErrorClose) { |
| 90 audio_log_->OnCreated( | 136 audio_log_->OnCreated(kTestComponentID, test_params_, kTestDeviceID); |
| 91 kTestComponentID, test_params_, kTestDeviceID); | |
| 92 base::RunLoop().RunUntilIdle(); | 137 base::RunLoop().RunUntilIdle(); |
| 93 | 138 |
| 94 ExpectString("channel_layout", | 139 ExpectString("channel_layout", |
| 95 media::ChannelLayoutToString(test_params_.channel_layout())); | 140 media::ChannelLayoutToString(test_params_.channel_layout())); |
| 96 ExpectInt("sample_rate", test_params_.sample_rate()); | 141 ExpectInt("sample_rate", test_params_.sample_rate()); |
| 97 ExpectInt("frames_per_buffer", test_params_.frames_per_buffer()); | 142 ExpectInt("frames_per_buffer", test_params_.frames_per_buffer()); |
| 98 ExpectInt("channels", test_params_.channels()); | 143 ExpectInt("channels", test_params_.channels()); |
| 99 ExpectString("effects", "ECHO_CANCELLER | DUCKING"); | 144 ExpectString("effects", "ECHO_CANCELLER | DUCKING"); |
| 100 ExpectString("device_id", kTestDeviceID); | 145 ExpectString("device_id", kTestDeviceID); |
| 101 ExpectInt("component_id", kTestComponentID); | 146 ExpectInt("component_id", kTestComponentID); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 119 audio_log_->OnError(kTestComponentID); | 164 audio_log_->OnError(kTestComponentID); |
| 120 base::RunLoop().RunUntilIdle(); | 165 base::RunLoop().RunUntilIdle(); |
| 121 ExpectString(kErrorKey, "true"); | 166 ExpectString(kErrorKey, "true"); |
| 122 | 167 |
| 123 // Verify OnClosed(). | 168 // Verify OnClosed(). |
| 124 audio_log_->OnClosed(kTestComponentID); | 169 audio_log_->OnClosed(kTestComponentID); |
| 125 base::RunLoop().RunUntilIdle(); | 170 base::RunLoop().RunUntilIdle(); |
| 126 ExpectStatus("closed"); | 171 ExpectStatus("closed"); |
| 127 } | 172 } |
| 128 | 173 |
| 129 TEST_P(MediaInternalsTest, AudioLogCreateClose) { | 174 TEST_P(MediaInternalsAudioLogTest, AudioLogCreateClose) { |
| 130 audio_log_->OnCreated( | 175 audio_log_->OnCreated(kTestComponentID, test_params_, kTestDeviceID); |
| 131 kTestComponentID, test_params_, kTestDeviceID); | |
| 132 base::RunLoop().RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
| 133 ExpectStatus("created"); | 177 ExpectStatus("created"); |
| 134 | 178 |
| 135 audio_log_->OnClosed(kTestComponentID); | 179 audio_log_->OnClosed(kTestComponentID); |
| 136 base::RunLoop().RunUntilIdle(); | 180 base::RunLoop().RunUntilIdle(); |
| 137 ExpectStatus("closed"); | 181 ExpectStatus("closed"); |
| 138 } | 182 } |
| 139 | 183 |
| 140 INSTANTIATE_TEST_CASE_P( | 184 INSTANTIATE_TEST_CASE_P( |
| 141 MediaInternalsTest, MediaInternalsTest, testing::Values( | 185 MediaInternalsAudioLogTest, MediaInternalsAudioLogTest, testing::Values( |
| 142 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER, | 186 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER, |
| 143 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER, | 187 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER, |
| 144 media::AudioLogFactory::AUDIO_OUTPUT_STREAM)); | 188 media::AudioLogFactory::AUDIO_OUTPUT_STREAM)); |
| 145 | 189 |
| 146 } // namespace content | 190 } // namespace content |
| OLD | NEW |