Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / media / media_internals_unittest.cc
blobad43e3393e63fca938f2bb65e50616202b12049b
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) || defined(OS_LINUX) || \
113 defined(OS_ANDROID)
114 TEST_F(MediaInternalsVideoCaptureDeviceTest,
115 AllCaptureApiTypesHaveProperStringRepresentation) {
116 typedef media::VideoCaptureDevice::Name VideoCaptureDeviceName;
117 typedef std::map<VideoCaptureDeviceName::CaptureApiType, std::string>
118 CaptureApiTypeStringMap;
119 CaptureApiTypeStringMap m;
120 #if defined(OS_LINUX)
121 m[VideoCaptureDeviceName::V4L2_SINGLE_PLANE] = "V4L2 SPLANE";
122 m[VideoCaptureDeviceName::V4L2_MULTI_PLANE] = "V4L2 MPLANE";
123 #elif defined(OS_WIN)
124 m[VideoCaptureDeviceName::MEDIA_FOUNDATION] = "Media Foundation";
125 m[VideoCaptureDeviceName::DIRECT_SHOW] = "Direct Show";
126 #elif defined(OS_MACOSX)
127 m[VideoCaptureDeviceName::AVFOUNDATION] = "AV Foundation";
128 m[VideoCaptureDeviceName::QTKIT] = "QTKit";
129 m[VideoCaptureDeviceName::DECKLINK] = "DeckLink";
130 #elif defined(OS_ANDROID)
131 m[VideoCaptureDeviceName::API1] = "Camera API1";
132 m[VideoCaptureDeviceName::API2_LEGACY] = "Camera API2 Legacy";
133 m[VideoCaptureDeviceName::API2_FULL] = "Camera API2 Full";
134 m[VideoCaptureDeviceName::API2_LIMITED] = "Camera API2 Limited";
135 m[VideoCaptureDeviceName::TANGO] = "Tango API";
136 #endif
137 EXPECT_EQ(media::VideoCaptureDevice::Name::API_TYPE_UNKNOWN, m.size());
138 for (CaptureApiTypeStringMap::iterator it = m.begin(); it != m.end(); ++it) {
139 const VideoCaptureDeviceName device_name("dummy", "dummy", it->first);
140 EXPECT_EQ(it->second, device_name.GetCaptureApiTypeString());
143 #endif
145 TEST_F(MediaInternalsVideoCaptureDeviceTest,
146 VideoCaptureFormatStringIsInExpectedFormat) {
147 // Since media internals will send video capture capabilities to JavaScript in
148 // an expected format and there are no public methods for accessing the
149 // resolutions, frame rates or pixel formats, this test checks that the format
150 // has not changed. If the test fails because of the changed format, it should
151 // be updated at the same time as the media internals JS files.
152 const float kFrameRate = 30.0f;
153 const gfx::Size kFrameSize(1280, 720);
154 const media::VideoPixelFormat kPixelFormat = media::PIXEL_FORMAT_I420;
155 const media::VideoCaptureFormat capture_format(
156 kFrameSize, kFrameRate, kPixelFormat);
157 const std::string expected_string =
158 base::StringPrintf("resolution: %s, fps: %.3f, pixel format: %s",
159 kFrameSize.ToString().c_str(),
160 kFrameRate,
161 media::VideoCaptureFormat::PixelFormatToString(
162 kPixelFormat).c_str());
163 EXPECT_EQ(expected_string, capture_format.ToString());
166 TEST_F(MediaInternalsVideoCaptureDeviceTest,
167 NotifyVideoCaptureDeviceCapabilitiesEnumerated) {
168 const int kWidth = 1280;
169 const int kHeight = 720;
170 const float kFrameRate = 30.0f;
171 const media::VideoPixelFormat kPixelFormat = media::PIXEL_FORMAT_I420;
172 const media::VideoCaptureFormat format_hd({kWidth, kHeight},
173 kFrameRate, kPixelFormat);
174 media::VideoCaptureFormats formats{};
175 formats.push_back(format_hd);
176 const media::VideoCaptureDeviceInfo device_info(
177 #if defined(OS_MACOSX)
178 media::VideoCaptureDevice::Name("dummy", "dummy",
179 media::VideoCaptureDevice::Name::QTKIT),
180 #elif defined(OS_WIN)
181 media::VideoCaptureDevice::Name("dummy", "dummy",
182 media::VideoCaptureDevice::Name::DIRECT_SHOW),
183 #elif defined(OS_LINUX)
184 media::VideoCaptureDevice::Name(
185 "dummy", "/dev/dummy",
186 media::VideoCaptureDevice::Name::V4L2_SINGLE_PLANE),
187 #elif defined(OS_ANDROID)
188 media::VideoCaptureDevice::Name("dummy", "dummy",
189 media::VideoCaptureDevice::Name::API2_LEGACY),
190 #else
191 media::VideoCaptureDevice::Name("dummy", "dummy"),
192 #endif
193 formats);
194 media::VideoCaptureDeviceInfos device_infos{};
195 device_infos.push_back(device_info);
197 // When updating video capture capabilities, the update will serialize
198 // a JSON array of objects to string. So here, the |UpdateCallbackImpl| will
199 // deserialize the first object in the array. This means we have to have
200 // exactly one device_info in the |device_infos|.
201 media_internals_->UpdateVideoCaptureDeviceCapabilities(device_infos);
203 #if defined(OS_LINUX)
204 ExpectString("id", "/dev/dummy");
205 #else
206 ExpectString("id", "dummy");
207 #endif
208 ExpectString("name", "dummy");
209 base::ListValue expected_list;
210 expected_list.AppendString(format_hd.ToString());
211 ExpectListOfStrings("formats", expected_list);
212 #if defined(OS_LINUX)
213 ExpectString("captureApi", "V4L2 SPLANE");
214 #elif defined(OS_WIN)
215 ExpectString("captureApi", "Direct Show");
216 #elif defined(OS_MACOSX)
217 ExpectString("captureApi", "QTKit");
218 #elif defined(OS_ANDROID)
219 ExpectString("captureApi", "Camera API2 Legacy");
220 #endif
223 class MediaInternalsAudioLogTest
224 : public MediaInternalsTestBase,
225 public testing::TestWithParam<media::AudioLogFactory::AudioComponent> {
226 public:
227 MediaInternalsAudioLogTest() :
228 update_cb_(base::Bind(&MediaInternalsAudioLogTest::UpdateCallbackImpl,
229 base::Unretained(this))),
230 test_params_(media::AudioParameters::AUDIO_PCM_LINEAR,
231 media::CHANNEL_LAYOUT_MONO,
232 48000,
234 128,
235 media::AudioParameters::ECHO_CANCELLER |
236 media::AudioParameters::DUCKING),
237 test_component_(GetParam()),
238 audio_log_(media_internals_->CreateAudioLog(test_component_)) {
239 media_internals_->AddUpdateCallback(update_cb_);
242 virtual ~MediaInternalsAudioLogTest() {
243 media_internals_->RemoveUpdateCallback(update_cb_);
246 protected:
247 MediaInternals::UpdateCallback update_cb_;
248 const media::AudioParameters test_params_;
249 const media::AudioLogFactory::AudioComponent test_component_;
250 scoped_ptr<media::AudioLog> audio_log_;
253 TEST_P(MediaInternalsAudioLogTest, AudioLogCreateStartStopErrorClose) {
254 audio_log_->OnCreated(kTestComponentID, test_params_, kTestDeviceID);
255 base::RunLoop().RunUntilIdle();
257 ExpectString("channel_layout",
258 media::ChannelLayoutToString(test_params_.channel_layout()));
259 ExpectInt("sample_rate", test_params_.sample_rate());
260 ExpectInt("frames_per_buffer", test_params_.frames_per_buffer());
261 ExpectInt("channels", test_params_.channels());
262 ExpectString("effects", "ECHO_CANCELLER | DUCKING");
263 ExpectString("device_id", kTestDeviceID);
264 ExpectInt("component_id", kTestComponentID);
265 ExpectInt("component_type", test_component_);
266 ExpectStatus("created");
268 // Verify OnStarted().
269 audio_log_->OnStarted(kTestComponentID);
270 base::RunLoop().RunUntilIdle();
271 ExpectStatus("started");
273 // Verify OnStopped().
274 audio_log_->OnStopped(kTestComponentID);
275 base::RunLoop().RunUntilIdle();
276 ExpectStatus("stopped");
278 // Verify OnError().
279 const char kErrorKey[] = "error_occurred";
280 std::string no_value;
281 ASSERT_FALSE(update_data_.GetString(kErrorKey, &no_value));
282 audio_log_->OnError(kTestComponentID);
283 base::RunLoop().RunUntilIdle();
284 ExpectString(kErrorKey, "true");
286 // Verify OnClosed().
287 audio_log_->OnClosed(kTestComponentID);
288 base::RunLoop().RunUntilIdle();
289 ExpectStatus("closed");
292 TEST_P(MediaInternalsAudioLogTest, AudioLogCreateClose) {
293 audio_log_->OnCreated(kTestComponentID, test_params_, kTestDeviceID);
294 base::RunLoop().RunUntilIdle();
295 ExpectStatus("created");
297 audio_log_->OnClosed(kTestComponentID);
298 base::RunLoop().RunUntilIdle();
299 ExpectStatus("closed");
302 INSTANTIATE_TEST_CASE_P(
303 MediaInternalsAudioLogTest, MediaInternalsAudioLogTest, testing::Values(
304 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER,
305 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER,
306 media::AudioLogFactory::AUDIO_OUTPUT_STREAM));
308 } // namespace content