Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / media / media_internals_unittest.cc
blobda8e2c51eb3aca11010d6e2192d55bb6bd388e7a
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
3 // found in the LICENSE file.
5 #include "content/browser/media/media_internals.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/json/json_reader.h"
10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "media/audio/audio_parameters.h"
15 #include "media/base/channel_layout.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/gfx/geometry/size.h"
19 namespace {
20 const int kTestComponentID = 0;
21 const char kTestDeviceID[] = "test-device-id";
23 // This class encapsulates a MediaInternals reference. It also has some useful
24 // methods to receive a callback, deserialize its associated data and expect
25 // integer/string values.
26 class MediaInternalsTestBase {
27 public:
28 MediaInternalsTestBase()
29 : media_internals_(content::MediaInternals::GetInstance()) {
31 virtual ~MediaInternalsTestBase() {}
33 protected:
34 // Extracts and deserializes the JSON update data; merges into |update_data_|.
35 void UpdateCallbackImpl(const base::string16& update) {
36 // Each update string looks like "<JavaScript Function Name>({<JSON>});"
37 // or for video capabilities: "<JavaScript Function Name>([{<JSON>}]);".
38 // In the second case we will be able to extract the dictionary if it is the
39 // only member of the list.
40 // To use the JSON reader we need to strip out the JS function name and ().
41 std::string utf8_update = base::UTF16ToUTF8(update);
42 const std::string::size_type first_brace = utf8_update.find('{');
43 const std::string::size_type last_brace = utf8_update.rfind('}');
44 scoped_ptr<base::Value> output_value(base::JSONReader::Read(
45 utf8_update.substr(first_brace, last_brace - first_brace + 1)));
46 CHECK(output_value);
48 base::DictionaryValue* output_dict = NULL;
49 CHECK(output_value->GetAsDictionary(&output_dict));
50 update_data_.MergeDictionary(output_dict);
53 void ExpectInt(const std::string& key, int expected_value) const {
54 int actual_value = 0;
55 ASSERT_TRUE(update_data_.GetInteger(key, &actual_value));
56 EXPECT_EQ(expected_value, actual_value);
59 void ExpectString(const std::string& key,
60 const std::string& expected_value) const {
61 std::string actual_value;
62 ASSERT_TRUE(update_data_.GetString(key, &actual_value));
63 EXPECT_EQ(expected_value, actual_value);
66 void ExpectStatus(const std::string& expected_value) const {
67 ExpectString("status", expected_value);
70 void ExpectListOfStrings(const std::string& key,
71 const base::ListValue& expected_list) const {
72 const base::ListValue* actual_list;
73 ASSERT_TRUE(update_data_.GetList(key, &actual_list));
74 const size_t expected_size = expected_list.GetSize();
75 const size_t actual_size = actual_list->GetSize();
76 ASSERT_EQ(expected_size, actual_size);
77 for (size_t i = 0; i < expected_size; ++i) {
78 std::string expected_value, actual_value;
79 ASSERT_TRUE(expected_list.GetString(i, &expected_value));
80 ASSERT_TRUE(actual_list->GetString(i, &actual_value));
81 EXPECT_EQ(expected_value, actual_value);
85 const content::TestBrowserThreadBundle thread_bundle_;
86 base::DictionaryValue update_data_;
87 content::MediaInternals* const media_internals_;
90 } // namespace
92 namespace content {
94 class MediaInternalsVideoCaptureDeviceTest : public testing::Test,
95 public MediaInternalsTestBase {
96 public:
97 MediaInternalsVideoCaptureDeviceTest()
98 : update_cb_(base::Bind(
99 &MediaInternalsVideoCaptureDeviceTest::UpdateCallbackImpl,
100 base::Unretained(this))) {
101 media_internals_->AddUpdateCallback(update_cb_);
104 ~MediaInternalsVideoCaptureDeviceTest() override {
105 media_internals_->RemoveUpdateCallback(update_cb_);
108 protected:
109 MediaInternals::UpdateCallback update_cb_;
112 #if defined(OS_WIN) || defined(OS_MACOSX)
113 TEST_F(MediaInternalsVideoCaptureDeviceTest,
114 AllCaptureApiTypesHaveProperStringRepresentation) {
115 typedef media::VideoCaptureDevice::Name VideoCaptureDeviceName;
116 typedef std::map<VideoCaptureDeviceName::CaptureApiType, std::string>
117 CaptureApiTypeStringMap;
118 CaptureApiTypeStringMap m;
119 #if defined(OS_WIN)
120 m[VideoCaptureDeviceName::MEDIA_FOUNDATION] = "Media Foundation";
121 m[VideoCaptureDeviceName::DIRECT_SHOW] = "Direct Show";
122 m[VideoCaptureDeviceName::DIRECT_SHOW_WDM_CROSSBAR] =
123 "Direct Show WDM Crossbar";
124 #elif defined(OS_MACOSX)
125 m[VideoCaptureDeviceName::AVFOUNDATION] = "AV Foundation";
126 m[VideoCaptureDeviceName::QTKIT] = "QTKit";
127 m[VideoCaptureDeviceName::DECKLINK] = "DeckLink";
128 #endif
129 EXPECT_EQ(media::VideoCaptureDevice::Name::API_TYPE_UNKNOWN, m.size());
130 for (CaptureApiTypeStringMap::iterator it = m.begin(); it != m.end(); ++it) {
131 const VideoCaptureDeviceName device_name("dummy", "dummy", it->first);
132 EXPECT_EQ(it->second, device_name.GetCaptureApiTypeString());
135 #endif
137 TEST_F(MediaInternalsVideoCaptureDeviceTest,
138 VideoCaptureFormatStringIsInExpectedFormat) {
139 // Since media internals will send video capture capabilities to JavaScript in
140 // an expected format and there are no public methods for accessing the
141 // resolutions, frame rates or pixel formats, this test checks that the format
142 // has not changed. If the test fails because of the changed format, it should
143 // be updated at the same time as the media internals JS files.
144 const float kFrameRate = 30.0f;
145 const gfx::Size kFrameSize(1280, 720);
146 const media::VideoPixelFormat kPixelFormat = media::PIXEL_FORMAT_I420;
147 const media::VideoCaptureFormat capture_format(
148 kFrameSize, kFrameRate, kPixelFormat);
149 const std::string expected_string =
150 base::StringPrintf("resolution: %s, fps: %f, pixel format: %s",
151 kFrameSize.ToString().c_str(),
152 kFrameRate,
153 media::VideoCaptureFormat::PixelFormatToString(
154 kPixelFormat).c_str());
155 EXPECT_EQ(expected_string, capture_format.ToString());
158 TEST_F(MediaInternalsVideoCaptureDeviceTest,
159 NotifyVideoCaptureDeviceCapabilitiesEnumerated) {
160 const int kWidth = 1280;
161 const int kHeight = 720;
162 const float kFrameRate = 30.0f;
163 const media::VideoPixelFormat kPixelFormat = media::PIXEL_FORMAT_I420;
164 const media::VideoCaptureFormat format_hd({kWidth, kHeight},
165 kFrameRate, kPixelFormat);
166 media::VideoCaptureFormats formats{};
167 formats.push_back(format_hd);
168 const media::VideoCaptureDeviceInfo device_info(
169 #if defined(OS_MACOSX)
170 media::VideoCaptureDevice::Name("dummy", "dummy",
171 media::VideoCaptureDevice::Name::QTKIT),
172 #elif defined(OS_WIN)
173 media::VideoCaptureDevice::Name("dummy", "dummy",
174 media::VideoCaptureDevice::Name::DIRECT_SHOW),
175 #elif defined(OS_LINUX) || defined(OS_CHROMEOS)
176 media::VideoCaptureDevice::Name("dummy", "/dev/dummy"),
177 #else
178 media::VideoCaptureDevice::Name("dummy", "dummy"),
179 #endif
180 formats);
181 media::VideoCaptureDeviceInfos device_infos{};
182 device_infos.push_back(device_info);
184 // When updating video capture capabilities, the update will serialize
185 // a JSON array of objects to string. So here, the |UpdateCallbackImpl| will
186 // deserialize the first object in the array. This means we have to have
187 // exactly one device_info in the |device_infos|.
188 media_internals_->UpdateVideoCaptureDeviceCapabilities(device_infos);
190 #if defined(OS_LINUX) || defined(OS_CHROMEOS)
191 ExpectString("id", "/dev/dummy");
192 #else
193 ExpectString("id", "dummy");
194 #endif
195 ExpectString("name", "dummy");
196 base::ListValue expected_list;
197 expected_list.AppendString(format_hd.ToString());
198 ExpectListOfStrings("formats", expected_list);
199 #if defined(OS_MACOSX)
200 ExpectString("captureApi", "QTKit");
201 #elif defined(OS_WIN)
202 ExpectString("captureApi", "Direct Show");
203 #endif
206 class MediaInternalsAudioLogTest
207 : public MediaInternalsTestBase,
208 public testing::TestWithParam<media::AudioLogFactory::AudioComponent> {
209 public:
210 MediaInternalsAudioLogTest() :
211 update_cb_(base::Bind(&MediaInternalsAudioLogTest::UpdateCallbackImpl,
212 base::Unretained(this))),
213 test_params_(media::AudioParameters::AUDIO_PCM_LINEAR,
214 media::CHANNEL_LAYOUT_MONO,
215 48000,
217 128,
218 media::AudioParameters::ECHO_CANCELLER |
219 media::AudioParameters::DUCKING),
220 test_component_(GetParam()),
221 audio_log_(media_internals_->CreateAudioLog(test_component_)) {
222 media_internals_->AddUpdateCallback(update_cb_);
225 virtual ~MediaInternalsAudioLogTest() {
226 media_internals_->RemoveUpdateCallback(update_cb_);
229 protected:
230 MediaInternals::UpdateCallback update_cb_;
231 const media::AudioParameters test_params_;
232 const media::AudioLogFactory::AudioComponent test_component_;
233 scoped_ptr<media::AudioLog> audio_log_;
236 TEST_P(MediaInternalsAudioLogTest, AudioLogCreateStartStopErrorClose) {
237 audio_log_->OnCreated(kTestComponentID, test_params_, kTestDeviceID);
238 base::RunLoop().RunUntilIdle();
240 ExpectString("channel_layout",
241 media::ChannelLayoutToString(test_params_.channel_layout()));
242 ExpectInt("sample_rate", test_params_.sample_rate());
243 ExpectInt("frames_per_buffer", test_params_.frames_per_buffer());
244 ExpectInt("channels", test_params_.channels());
245 ExpectString("effects", "ECHO_CANCELLER | DUCKING");
246 ExpectString("device_id", kTestDeviceID);
247 ExpectInt("component_id", kTestComponentID);
248 ExpectInt("component_type", test_component_);
249 ExpectStatus("created");
251 // Verify OnStarted().
252 audio_log_->OnStarted(kTestComponentID);
253 base::RunLoop().RunUntilIdle();
254 ExpectStatus("started");
256 // Verify OnStopped().
257 audio_log_->OnStopped(kTestComponentID);
258 base::RunLoop().RunUntilIdle();
259 ExpectStatus("stopped");
261 // Verify OnError().
262 const char kErrorKey[] = "error_occurred";
263 std::string no_value;
264 ASSERT_FALSE(update_data_.GetString(kErrorKey, &no_value));
265 audio_log_->OnError(kTestComponentID);
266 base::RunLoop().RunUntilIdle();
267 ExpectString(kErrorKey, "true");
269 // Verify OnClosed().
270 audio_log_->OnClosed(kTestComponentID);
271 base::RunLoop().RunUntilIdle();
272 ExpectStatus("closed");
275 TEST_P(MediaInternalsAudioLogTest, AudioLogCreateClose) {
276 audio_log_->OnCreated(kTestComponentID, test_params_, kTestDeviceID);
277 base::RunLoop().RunUntilIdle();
278 ExpectStatus("created");
280 audio_log_->OnClosed(kTestComponentID);
281 base::RunLoop().RunUntilIdle();
282 ExpectStatus("closed");
285 INSTANTIATE_TEST_CASE_P(
286 MediaInternalsAudioLogTest, MediaInternalsAudioLogTest, testing::Values(
287 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER,
288 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER,
289 media::AudioLogFactory::AUDIO_OUTPUT_STREAM));
291 } // namespace content