OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/run_loop.h" | 5 #include "base/run_loop.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "content/child/child_process.h" | 7 #include "content/child/child_process.h" |
8 #include "content/renderer/media/media_recorder_handler.h" | 8 #include "content/renderer/media/media_recorder_handler.h" |
9 #include "content/renderer/media/mock_media_stream_registry.h" | 9 #include "content/renderer/media/mock_media_stream_registry.h" |
10 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/WebKit/public/platform/WebMediaRecorderHandlerClient.h" | 13 #include "third_party/WebKit/public/platform/WebMediaRecorderHandlerClient.h" |
14 #include "third_party/WebKit/public/platform/WebString.h" | 14 #include "third_party/WebKit/public/platform/WebString.h" |
15 #include "third_party/WebKit/public/web/WebHeap.h" | 15 #include "third_party/WebKit/public/web/WebHeap.h" |
16 | 16 |
17 using ::testing::_; | 17 using ::testing::_; |
18 using ::testing::AtLeast; | 18 using ::testing::AtLeast; |
19 using ::testing::InSequence; | 19 using ::testing::InSequence; |
20 using ::testing::Lt; | 20 using ::testing::Lt; |
21 using ::testing::Mock; | 21 using ::testing::Mock; |
| 22 using ::testing::TestWithParam; |
| 23 using ::testing::ValuesIn; |
22 | 24 |
23 using blink::WebString; | 25 using blink::WebString; |
24 | 26 |
25 namespace content { | 27 namespace content { |
26 | 28 |
27 ACTION_P(RunClosure, closure) { | 29 ACTION_P(RunClosure, closure) { |
28 closure.Run(); | 30 closure.Run(); |
29 } | 31 } |
30 | 32 |
31 static const std::string kTestStreamUrl = "stream_url"; | 33 static const std::string kTestStreamUrl = "stream_url"; |
32 static const std::string kTestVideoTrackId = "video_track_id"; | 34 static const std::string kTestVideoTrackId = "video_track_id"; |
33 | 35 |
34 class MediaRecorderHandlerTest | 36 struct MediaRecorderTestParams { |
35 : public testing::Test | 37 const char* const mime_type; |
36 , public blink::WebMediaRecorderHandlerClient { | 38 const size_t first_encoded_frame_size; |
| 39 const size_t second_encoded_frame_size; |
| 40 }; |
| 41 |
| 42 static const MediaRecorderTestParams kMediaRecorderTestParams[] = { |
| 43 {"video/vp8", 52, 32}, |
| 44 {"video/vp9", 33, 18}}; |
| 45 |
| 46 class MediaRecorderHandlerTest : public TestWithParam<MediaRecorderTestParams>, |
| 47 public blink::WebMediaRecorderHandlerClient { |
37 public: | 48 public: |
38 MediaRecorderHandlerTest() | 49 MediaRecorderHandlerTest() |
39 : media_recorder_handler_(new MediaRecorderHandler()) { | 50 : media_recorder_handler_(new MediaRecorderHandler()) { |
40 EXPECT_FALSE(media_recorder_handler_->recording_); | 51 EXPECT_FALSE(media_recorder_handler_->recording_); |
41 | 52 |
42 registry_.Init(kTestStreamUrl); | 53 registry_.Init(kTestStreamUrl); |
43 registry_.AddVideoTrack(kTestVideoTrackId); | 54 registry_.AddVideoTrack(kTestVideoTrackId); |
44 } | 55 } |
45 | 56 |
46 ~MediaRecorderHandlerTest() { | 57 ~MediaRecorderHandlerTest() { |
(...skipping 25 matching lines...) Expand all Loading... |
72 // The Class under test. Needs to be scoped_ptr to force its destruction. | 83 // The Class under test. Needs to be scoped_ptr to force its destruction. |
73 scoped_ptr<MediaRecorderHandler> media_recorder_handler_; | 84 scoped_ptr<MediaRecorderHandler> media_recorder_handler_; |
74 | 85 |
75 private: | 86 private: |
76 DISALLOW_COPY_AND_ASSIGN(MediaRecorderHandlerTest); | 87 DISALLOW_COPY_AND_ASSIGN(MediaRecorderHandlerTest); |
77 }; | 88 }; |
78 | 89 |
79 // Checks that canSupportMimeType() works as expected. | 90 // Checks that canSupportMimeType() works as expected. |
80 // TODO(mcasas): revisit this when canSupportMimeType() is fully implemented. | 91 // TODO(mcasas): revisit this when canSupportMimeType() is fully implemented. |
81 TEST_F(MediaRecorderHandlerTest, CanSupportMimeType) { | 92 TEST_F(MediaRecorderHandlerTest, CanSupportMimeType) { |
82 const WebString good_mime_type(base::UTF8ToUTF16("video/vp8")); | 93 const WebString good_mime_type_vp8(base::UTF8ToUTF16("video/vp8")); |
83 EXPECT_TRUE(media_recorder_handler_->canSupportMimeType(good_mime_type)); | 94 EXPECT_TRUE(media_recorder_handler_->canSupportMimeType(good_mime_type_vp8)); |
| 95 |
| 96 const WebString good_mime_type_vp9(base::UTF8ToUTF16("video/vp9")); |
| 97 EXPECT_TRUE(media_recorder_handler_->canSupportMimeType(good_mime_type_vp9)); |
84 | 98 |
85 const WebString bad_mime_type(base::UTF8ToUTF16("video/unsupportedcodec")); | 99 const WebString bad_mime_type(base::UTF8ToUTF16("video/unsupportedcodec")); |
86 EXPECT_FALSE(media_recorder_handler_->canSupportMimeType(bad_mime_type)); | 100 EXPECT_FALSE(media_recorder_handler_->canSupportMimeType(bad_mime_type)); |
87 | 101 |
88 const WebString empty_mime_type(base::UTF8ToUTF16("")); | 102 const WebString empty_mime_type(base::UTF8ToUTF16("")); |
89 EXPECT_TRUE(media_recorder_handler_->canSupportMimeType(empty_mime_type)); | 103 EXPECT_TRUE(media_recorder_handler_->canSupportMimeType(empty_mime_type)); |
90 } | 104 } |
91 | 105 |
92 // Checks that the initialization-destruction sequence works fine. | 106 // Checks that the initialization-destruction sequence works fine. |
93 TEST_F(MediaRecorderHandlerTest, InitializeStartStop) { | 107 TEST_P(MediaRecorderHandlerTest, InitializeStartStop) { |
94 const WebString mime_type(base::UTF8ToUTF16("")); | 108 const WebString mime_type(base::UTF8ToUTF16(GetParam().mime_type)); |
95 EXPECT_TRUE(media_recorder_handler_->initialize(this, | 109 EXPECT_TRUE(media_recorder_handler_->initialize(this, |
96 registry_.test_stream(), | 110 registry_.test_stream(), |
97 mime_type)); | 111 mime_type)); |
98 EXPECT_FALSE(recording()); | 112 EXPECT_FALSE(recording()); |
99 EXPECT_FALSE(hasVideoRecorders()); | 113 EXPECT_FALSE(hasVideoRecorders()); |
100 | 114 |
101 EXPECT_TRUE(media_recorder_handler_->start()); | 115 EXPECT_TRUE(media_recorder_handler_->start()); |
102 EXPECT_TRUE(recording()); | 116 EXPECT_TRUE(recording()); |
103 EXPECT_TRUE(hasVideoRecorders()); | 117 EXPECT_TRUE(hasVideoRecorders()); |
104 | 118 |
105 media_recorder_handler_->stop(); | 119 media_recorder_handler_->stop(); |
106 EXPECT_FALSE(recording()); | 120 EXPECT_FALSE(recording()); |
107 EXPECT_FALSE(hasVideoRecorders()); | 121 EXPECT_FALSE(hasVideoRecorders()); |
108 | 122 |
109 // Expect a last call on destruction. | 123 // Expect a last call on destruction. |
110 EXPECT_CALL(*this, writeData(_, _, true)).Times(1); | 124 EXPECT_CALL(*this, writeData(_, _, true)).Times(1); |
111 media_recorder_handler_.reset(); | 125 media_recorder_handler_.reset(); |
112 } | 126 } |
113 | 127 |
114 // Sends 2 frames and expect them as WebM contained encoded data in writeData(). | 128 // Sends 2 frames and expect them as WebM contained encoded data in writeData(). |
115 TEST_F(MediaRecorderHandlerTest, EncodeVideoFrames) { | 129 TEST_P(MediaRecorderHandlerTest, EncodeVideoFrames) { |
116 const WebString mime_type(base::UTF8ToUTF16("video/vp8")); | 130 const WebString mime_type(base::UTF8ToUTF16(GetParam().mime_type)); |
117 EXPECT_TRUE(media_recorder_handler_->initialize(this, registry_.test_stream(), | 131 EXPECT_TRUE(media_recorder_handler_->initialize(this, registry_.test_stream(), |
118 mime_type)); | 132 mime_type)); |
119 EXPECT_TRUE(media_recorder_handler_->start()); | 133 EXPECT_TRUE(media_recorder_handler_->start()); |
120 | 134 |
121 InSequence s; | 135 InSequence s; |
122 const scoped_refptr<media::VideoFrame> video_frame = | 136 const scoped_refptr<media::VideoFrame> video_frame = |
123 media::VideoFrame::CreateBlackFrame(gfx::Size(160, 80)); | 137 media::VideoFrame::CreateBlackFrame(gfx::Size(160, 80)); |
124 | 138 |
125 { | 139 { |
126 base::RunLoop run_loop; | 140 base::RunLoop run_loop; |
127 base::Closure quit_closure = run_loop.QuitClosure(); | 141 base::Closure quit_closure = run_loop.QuitClosure(); |
128 // writeData() is pinged a number of times as the WebM header is written; | 142 // writeData() is pinged a number of times as the WebM header is written; |
129 // the last time it is called it has the encoded data. | 143 // the last time it is called it has the encoded data. |
130 const size_t kEncodedDataSize = 52; | 144 const size_t encoded_data_size = GetParam().first_encoded_frame_size; |
131 EXPECT_CALL(*this, writeData(_, Lt(kEncodedDataSize), false)) | 145 EXPECT_CALL(*this, writeData(_, Lt(encoded_data_size), false)) |
132 .Times(AtLeast(1)); | 146 .Times(AtLeast(1)); |
133 EXPECT_CALL(*this, writeData(_, kEncodedDataSize, false)) | 147 EXPECT_CALL(*this, writeData(_, encoded_data_size, false)) |
134 .Times(1) | 148 .Times(1) |
135 .WillOnce(RunClosure(quit_closure)); | 149 .WillOnce(RunClosure(quit_closure)); |
136 | 150 |
137 OnVideoFrameForTesting(video_frame); | 151 OnVideoFrameForTesting(video_frame); |
138 run_loop.Run(); | 152 run_loop.Run(); |
139 } | 153 } |
140 | 154 |
141 { | 155 { |
142 base::RunLoop run_loop; | 156 base::RunLoop run_loop; |
143 base::Closure quit_closure = run_loop.QuitClosure(); | 157 base::Closure quit_closure = run_loop.QuitClosure(); |
144 // The second time around writeData() is called a number of times to write | 158 // The second time around writeData() is called a number of times to write |
145 // the WebM frame header, and then is pinged with the encoded data. | 159 // the WebM frame header, and then is pinged with the encoded data. |
146 const size_t kSecondEncodedDataSize = 32; | 160 const size_t encoded_data_size = GetParam().second_encoded_frame_size; |
147 EXPECT_CALL(*this, writeData(_, Lt(kSecondEncodedDataSize), false)) | 161 EXPECT_CALL(*this, writeData(_, Lt(encoded_data_size), false)) |
148 .Times(AtLeast(1)); | 162 .Times(AtLeast(1)); |
149 EXPECT_CALL(*this, writeData(_, kSecondEncodedDataSize, false)) | 163 EXPECT_CALL(*this, writeData(_, encoded_data_size, false)) |
150 .Times(1) | 164 .Times(1) |
151 .WillOnce(RunClosure(quit_closure)); | 165 .WillOnce(RunClosure(quit_closure)); |
152 | 166 |
153 OnVideoFrameForTesting(video_frame); | 167 OnVideoFrameForTesting(video_frame); |
154 run_loop.Run(); | 168 run_loop.Run(); |
155 } | 169 } |
156 | 170 |
157 media_recorder_handler_->stop(); | 171 media_recorder_handler_->stop(); |
158 | 172 |
159 // Expect a last call on destruction, with size 0 and |lastInSlice| true. | 173 // Expect a last call on destruction, with size 0 and |lastInSlice| true. |
160 EXPECT_CALL(*this, writeData(nullptr, 0, true)).Times(1); | 174 EXPECT_CALL(*this, writeData(nullptr, 0, true)).Times(1); |
161 media_recorder_handler_.reset(); | 175 media_recorder_handler_.reset(); |
162 } | 176 } |
163 | 177 |
| 178 INSTANTIATE_TEST_CASE_P(, |
| 179 MediaRecorderHandlerTest, |
| 180 ValuesIn(kMediaRecorderTestParams)); |
| 181 |
164 } // namespace content | 182 } // namespace content |
OLD | NEW |