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" |
(...skipping 13 matching lines...) Expand all Loading... |
24 class MediaInternalsTestBase { | 24 class MediaInternalsTestBase { |
25 public: | 25 public: |
26 MediaInternalsTestBase() | 26 MediaInternalsTestBase() |
27 : media_internals_(content::MediaInternals::GetInstance()) { | 27 : media_internals_(content::MediaInternals::GetInstance()) { |
28 } | 28 } |
29 virtual ~MediaInternalsTestBase() {} | 29 virtual ~MediaInternalsTestBase() {} |
30 | 30 |
31 protected: | 31 protected: |
32 // Extracts and deserializes the JSON update data; merges into |update_data_|. | 32 // Extracts and deserializes the JSON update data; merges into |update_data_|. |
33 void UpdateCallbackImpl(const base::string16& update) { | 33 void UpdateCallbackImpl(const base::string16& update) { |
34 // Each update string looks like "<JavaScript Function Name>({<JSON>});", to | 34 // Each update string looks like "<JavaScript Function Name>({<JSON>});" |
35 // use the JSON reader we need to strip out the JavaScript code. | 35 // or for video capabilities: "<JavaScript Function Name>([{<JSON>}]);". |
| 36 // In the second case we will be able to extract the dictionary if it is the |
| 37 // only member of the list. |
| 38 // To use the JSON reader we need to strip out the JS function name and (). |
36 std::string utf8_update = base::UTF16ToUTF8(update); | 39 std::string utf8_update = base::UTF16ToUTF8(update); |
37 const std::string::size_type first_brace = utf8_update.find('{'); | 40 const std::string::size_type first_brace = utf8_update.find('{'); |
38 const std::string::size_type last_brace = utf8_update.rfind('}'); | 41 const std::string::size_type last_brace = utf8_update.rfind('}'); |
39 scoped_ptr<base::Value> output_value(base::JSONReader::Read( | 42 scoped_ptr<base::Value> output_value(base::JSONReader::Read( |
40 utf8_update.substr(first_brace, last_brace - first_brace + 1))); | 43 utf8_update.substr(first_brace, last_brace - first_brace + 1))); |
41 CHECK(output_value); | 44 CHECK(output_value); |
42 | 45 |
43 base::DictionaryValue* output_dict = NULL; | 46 base::DictionaryValue* output_dict = NULL; |
44 CHECK(output_value->GetAsDictionary(&output_dict)); | 47 CHECK(output_value->GetAsDictionary(&output_dict)); |
45 update_data_.MergeDictionary(output_dict); | 48 update_data_.MergeDictionary(output_dict); |
46 } | 49 } |
47 | 50 |
48 void ExpectInt(const std::string& key, int expected_value) const { | 51 void ExpectInt(const std::string& key, int expected_value) const { |
49 int actual_value = 0; | 52 int actual_value = 0; |
50 ASSERT_TRUE(update_data_.GetInteger(key, &actual_value)); | 53 ASSERT_TRUE(update_data_.GetInteger(key, &actual_value)); |
51 EXPECT_EQ(expected_value, actual_value); | 54 EXPECT_EQ(expected_value, actual_value); |
52 } | 55 } |
53 | 56 |
54 void ExpectString(const std::string& key, | 57 void ExpectString(const std::string& key, |
55 const std::string& expected_value) const { | 58 const std::string& expected_value) const { |
56 std::string actual_value; | 59 std::string actual_value; |
57 ASSERT_TRUE(update_data_.GetString(key, &actual_value)); | 60 ASSERT_TRUE(update_data_.GetString(key, &actual_value)); |
58 EXPECT_EQ(expected_value, actual_value); | 61 EXPECT_EQ(expected_value, actual_value); |
59 } | 62 } |
60 | 63 |
61 void ExpectStatus(const std::string& expected_value) const { | 64 void ExpectStatus(const std::string& expected_value) const { |
62 ExpectString("status", expected_value); | 65 ExpectString("status", expected_value); |
63 } | 66 } |
64 | 67 |
| 68 void ExpectListOfStrings(const std::string& key, |
| 69 const base::ListValue& expected_list) const { |
| 70 const base::ListValue* actual_list; |
| 71 ASSERT_TRUE(update_data_.GetList(key, &actual_list)); |
| 72 const size_t expected_size = expected_list.GetSize(); |
| 73 const size_t actual_size = actual_list->GetSize(); |
| 74 ASSERT_EQ(expected_size, actual_size); |
| 75 for (size_t i = 0; i < expected_size; ++i) { |
| 76 std::string expected_value, actual_value; |
| 77 ASSERT_TRUE(expected_list.GetString(i, &expected_value)); |
| 78 ASSERT_TRUE(actual_list->GetString(i, &actual_value)); |
| 79 EXPECT_EQ(expected_value, actual_value); |
| 80 } |
| 81 } |
| 82 |
65 const content::TestBrowserThreadBundle thread_bundle_; | 83 const content::TestBrowserThreadBundle thread_bundle_; |
66 base::DictionaryValue update_data_; | 84 base::DictionaryValue update_data_; |
67 content::MediaInternals* const media_internals_; | 85 content::MediaInternals* const media_internals_; |
68 }; | 86 }; |
69 | 87 |
70 } // namespace | 88 } // namespace |
71 | 89 |
72 namespace content { | 90 namespace content { |
73 | 91 |
74 class MediaInternalsVideoCaptureDeviceTest : public testing::Test, | 92 class MediaInternalsVideoCaptureDeviceTest : public testing::Test, |
75 public MediaInternalsTestBase {}; | 93 public MediaInternalsTestBase { |
| 94 public: |
| 95 MediaInternalsVideoCaptureDeviceTest() |
| 96 : update_cb_(base::Bind( |
| 97 &MediaInternalsVideoCaptureDeviceTest::UpdateCallbackImpl, |
| 98 base::Unretained(this))) { |
| 99 media_internals_->AddUpdateCallback(update_cb_); |
| 100 } |
| 101 |
| 102 virtual ~MediaInternalsVideoCaptureDeviceTest() { |
| 103 media_internals_->RemoveUpdateCallback(update_cb_); |
| 104 } |
| 105 |
| 106 protected: |
| 107 MediaInternals::UpdateCallback update_cb_; |
| 108 }; |
76 | 109 |
77 TEST_F(MediaInternalsVideoCaptureDeviceTest, | 110 TEST_F(MediaInternalsVideoCaptureDeviceTest, |
78 NotifyVideoCaptureDeviceCapabilitiesEnumerated) { | 111 NotifyVideoCaptureDeviceCapabilitiesEnumerated) { |
79 const int kWidth = 1280; | 112 const int kWidth = 1280; |
80 const int kHeight = 720; | 113 const int kHeight = 720; |
81 const float kFrameRate = 30.0f; | 114 const float kFrameRate = 30.0f; |
82 const media::VideoPixelFormat kPixelFormat = media::PIXEL_FORMAT_I420; | 115 const media::VideoPixelFormat kPixelFormat = media::PIXEL_FORMAT_I420; |
83 const media::VideoCaptureFormat format_hd({kWidth, kHeight}, | 116 const media::VideoCaptureFormat format_hd({kWidth, kHeight}, |
84 kFrameRate, kPixelFormat); | 117 kFrameRate, kPixelFormat); |
85 media::VideoCaptureFormats formats{}; | 118 media::VideoCaptureFormats formats{}; |
86 formats.push_back(format_hd); | 119 formats.push_back(format_hd); |
87 const media::VideoCaptureDeviceInfo device_info( | 120 const media::VideoCaptureDeviceInfo device_info( |
88 #if defined(OS_MACOSX) | 121 #if defined(OS_MACOSX) |
89 media::VideoCaptureDevice::Name("dummy", "dummy", | 122 media::VideoCaptureDevice::Name("dummy", "dummy", |
90 media::VideoCaptureDevice::Name::QTKIT), | 123 media::VideoCaptureDevice::Name::QTKIT), |
91 #elif defined(OS_WIN) | 124 #elif defined(OS_WIN) |
92 media::VideoCaptureDevice::Name("dummy", "dummy", | 125 media::VideoCaptureDevice::Name("dummy", "dummy", |
93 media::VideoCaptureDevice::Name::DIRECT_SHOW), | 126 media::VideoCaptureDevice::Name::DIRECT_SHOW), |
94 #elif defined(OS_LINUX) || defined(OS_CHROMEOS) | 127 #elif defined(OS_LINUX) || defined(OS_CHROMEOS) |
95 media::VideoCaptureDevice::Name("dummy", "/dev/dummy"), | 128 media::VideoCaptureDevice::Name("dummy", "/dev/dummy"), |
96 #else | 129 #else |
97 media::VideoCaptureDevice::Name("dummy", "dummy"), | 130 media::VideoCaptureDevice::Name("dummy", "dummy"), |
98 #endif | 131 #endif |
99 formats); | 132 formats); |
100 media::VideoCaptureDeviceInfos device_infos{}; | 133 media::VideoCaptureDeviceInfos device_infos{}; |
101 device_infos.push_back(device_info); | 134 device_infos.push_back(device_info); |
102 | 135 |
103 // TODO(mcasas): Listen for the serialised version of |device_infos| and | 136 // When updating video capture capabilities, the update will serialize |
104 // check its content using ExpectInt(), ExpectString(), after RunUntilIdle(). | 137 // a JSON array of objects to string. So here, the |UpdateCallbackImpl| will |
| 138 // deserialize the first object in the array. This means we have to have |
| 139 // exactly one device_info in the |device_infos|. |
105 media_internals_->UpdateVideoCaptureDeviceCapabilities(device_infos); | 140 media_internals_->UpdateVideoCaptureDeviceCapabilities(device_infos); |
106 base::RunLoop().RunUntilIdle(); | 141 |
| 142 #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 143 ExpectString("id", "/dev/dummy"); |
| 144 #else |
| 145 ExpectString("id", "dummy"); |
| 146 #endif |
| 147 ExpectString("name", "dummy"); |
| 148 base::ListValue expected_list; |
| 149 expected_list.AppendString(format_hd.ToString()); |
| 150 ExpectListOfStrings("formats", expected_list); |
| 151 #if defined(OS_MACOSX) |
| 152 ExpectInt("captureApi", media::VideoCaptureDevice::Name::QTKIT); |
| 153 #elif defined(OS_WIN) |
| 154 ExpectInt("captureApi", media::VideoCaptureDevice::Name::DIRECT_SHOW); |
| 155 #endif |
107 } | 156 } |
108 | 157 |
109 class MediaInternalsAudioLogTest | 158 class MediaInternalsAudioLogTest |
110 : public MediaInternalsTestBase, | 159 : public MediaInternalsTestBase, |
111 public testing::TestWithParam<media::AudioLogFactory::AudioComponent> { | 160 public testing::TestWithParam<media::AudioLogFactory::AudioComponent> { |
112 public: | 161 public: |
113 MediaInternalsAudioLogTest() : | 162 MediaInternalsAudioLogTest() : |
114 update_cb_(base::Bind(&MediaInternalsAudioLogTest::UpdateCallbackImpl, | 163 update_cb_(base::Bind(&MediaInternalsAudioLogTest::UpdateCallbackImpl, |
115 base::Unretained(this))), | 164 base::Unretained(this))), |
116 test_params_(media::AudioParameters::AUDIO_PCM_LINEAR, | 165 test_params_(media::AudioParameters::AUDIO_PCM_LINEAR, |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 ExpectStatus("closed"); | 234 ExpectStatus("closed"); |
186 } | 235 } |
187 | 236 |
188 INSTANTIATE_TEST_CASE_P( | 237 INSTANTIATE_TEST_CASE_P( |
189 MediaInternalsAudioLogTest, MediaInternalsAudioLogTest, testing::Values( | 238 MediaInternalsAudioLogTest, MediaInternalsAudioLogTest, testing::Values( |
190 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER, | 239 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER, |
191 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER, | 240 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER, |
192 media::AudioLogFactory::AUDIO_OUTPUT_STREAM)); | 241 media::AudioLogFactory::AUDIO_OUTPUT_STREAM)); |
193 | 242 |
194 } // namespace content | 243 } // namespace content |
OLD | NEW |