OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/run_loop.h" |
| 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "content/child/child_process.h" |
| 8 #include "content/renderer/media/media_recorder_handler.h" |
| 9 #include "content/renderer/media/mock_media_stream_registry.h" |
| 10 #include "media/base/video_frame.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/public/platform/WebMediaRecorderHandlerClient.h" |
| 14 #include "third_party/WebKit/public/platform/WebString.h" |
| 15 |
| 16 using ::testing::_; |
| 17 using ::testing::AtLeast; |
| 18 using ::testing::InSequence; |
| 19 using ::testing::Lt; |
| 20 using ::testing::Mock; |
| 21 |
| 22 using blink::WebString; |
| 23 |
| 24 namespace content { |
| 25 |
| 26 ACTION_P(RunClosure, closure) { |
| 27 closure.Run(); |
| 28 } |
| 29 |
| 30 static const std::string kTestStreamUrl = "stream_url"; |
| 31 static const std::string kTestVideoTrackId = "video_track_id"; |
| 32 |
| 33 class MediaRecorderHandlerTest |
| 34 : public testing::Test |
| 35 , public blink::WebMediaRecorderHandlerClient { |
| 36 public: |
| 37 MediaRecorderHandlerTest() |
| 38 : media_recorder_handler_(new MediaRecorderHandler()) { |
| 39 EXPECT_FALSE(media_recorder_handler_->recording_); |
| 40 |
| 41 registry_.Init(kTestStreamUrl); |
| 42 registry_.AddVideoTrack(kTestVideoTrackId); |
| 43 } |
| 44 |
| 45 MOCK_METHOD3(writeData, void(const char*, size_t, bool)); |
| 46 MOCK_METHOD1(failOutOfMemory, void(const WebString& message)); |
| 47 MOCK_METHOD1(failIllegalStreamModification, void(const WebString& message)); |
| 48 MOCK_METHOD1(failOtherRecordingError, void(const WebString& message)); |
| 49 |
| 50 bool recording() const { return media_recorder_handler_->recording_; } |
| 51 bool hasVideoRecorders() const { |
| 52 return !media_recorder_handler_->video_recorders_.empty(); |
| 53 } |
| 54 |
| 55 void OnVideoFrameForTesting(const scoped_refptr<media::VideoFrame>& frame) { |
| 56 media_recorder_handler_->OnVideoFrameForTesting(frame, |
| 57 base::TimeTicks::Now()); |
| 58 } |
| 59 |
| 60 // The Class under test. Needs to be scoped_ptr to force its destruction. |
| 61 scoped_ptr<MediaRecorderHandler> media_recorder_handler_; |
| 62 |
| 63 // A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks |
| 64 // and Sources in |registry_| into believing they are on the right threads. |
| 65 const base::MessageLoopForUI message_loop_; |
| 66 const ChildProcess child_process_; |
| 67 MockMediaStreamRegistry registry_; |
| 68 |
| 69 private: |
| 70 DISALLOW_COPY_AND_ASSIGN(MediaRecorderHandlerTest); |
| 71 }; |
| 72 |
| 73 // Checks that canSupportMimeType() works as expected. |
| 74 // TODO(mcasas): revisit this when canSupportMimeType() is fully implemented. |
| 75 TEST_F(MediaRecorderHandlerTest, CanSupportMimeType) { |
| 76 const WebString good_mime_type(base::UTF8ToUTF16("video/vp8")); |
| 77 EXPECT_TRUE(media_recorder_handler_->canSupportMimeType(good_mime_type)); |
| 78 |
| 79 const WebString bad_mime_type(base::UTF8ToUTF16("video/unsupportedcodec")); |
| 80 EXPECT_FALSE(media_recorder_handler_->canSupportMimeType(bad_mime_type)); |
| 81 } |
| 82 |
| 83 // Checks that the initialization-destruction sequence works fine. |
| 84 TEST_F(MediaRecorderHandlerTest, InitializeStartStop) { |
| 85 const WebString mime_type(base::UTF8ToUTF16("video/vp8")); |
| 86 EXPECT_TRUE(media_recorder_handler_->initialize(this, |
| 87 registry_.test_stream(), |
| 88 mime_type)); |
| 89 EXPECT_FALSE(recording()); |
| 90 EXPECT_FALSE(hasVideoRecorders()); |
| 91 |
| 92 EXPECT_TRUE(media_recorder_handler_->start()); |
| 93 EXPECT_TRUE(recording()); |
| 94 EXPECT_TRUE(hasVideoRecorders()); |
| 95 |
| 96 media_recorder_handler_->stop(); |
| 97 EXPECT_FALSE(recording()); |
| 98 EXPECT_FALSE(hasVideoRecorders()); |
| 99 |
| 100 // Expect a last call on destruction. |
| 101 EXPECT_CALL(*this, writeData(_, _, true)).Times(1); |
| 102 media_recorder_handler_.reset(); |
| 103 } |
| 104 |
| 105 // Sends 2 frames and expect them as WebM contained encoded data in writeData(). |
| 106 TEST_F(MediaRecorderHandlerTest, EncodeVideoFrames) { |
| 107 const WebString mime_type(base::UTF8ToUTF16("video/vp8")); |
| 108 EXPECT_TRUE(media_recorder_handler_->initialize(this, registry_.test_stream(), |
| 109 mime_type)); |
| 110 EXPECT_TRUE(media_recorder_handler_->start()); |
| 111 |
| 112 InSequence s; |
| 113 const scoped_refptr<media::VideoFrame> video_frame = |
| 114 media::VideoFrame::CreateBlackFrame(gfx::Size(160, 80)); |
| 115 |
| 116 { |
| 117 base::RunLoop run_loop; |
| 118 base::Closure quit_closure = run_loop.QuitClosure(); |
| 119 // writeData() is pinged a number of times as the WebM header is written; |
| 120 // the last time it is called it has the encoded data. |
| 121 const size_t kEncodedDataSize = 52; |
| 122 EXPECT_CALL(*this, writeData(_, Lt(kEncodedDataSize), false)) |
| 123 .Times(AtLeast(1)); |
| 124 EXPECT_CALL(*this, writeData(_, kEncodedDataSize, false)) |
| 125 .Times(1) |
| 126 .WillOnce(RunClosure(quit_closure)); |
| 127 |
| 128 OnVideoFrameForTesting(video_frame); |
| 129 run_loop.Run(); |
| 130 } |
| 131 |
| 132 { |
| 133 base::RunLoop run_loop; |
| 134 base::Closure quit_closure = run_loop.QuitClosure(); |
| 135 // The second time around writeData() is called a number of times to write |
| 136 // the WebM frame header, and then is pinged with the encoded data. |
| 137 const size_t kSecondEncodedDataSize = 32; |
| 138 EXPECT_CALL(*this, writeData(_, Lt(kSecondEncodedDataSize), false)) |
| 139 .Times(AtLeast(1)); |
| 140 EXPECT_CALL(*this, writeData(_, kSecondEncodedDataSize, false)) |
| 141 .Times(1) |
| 142 .WillOnce(RunClosure(quit_closure)); |
| 143 |
| 144 OnVideoFrameForTesting(video_frame); |
| 145 run_loop.Run(); |
| 146 } |
| 147 |
| 148 media_recorder_handler_->stop(); |
| 149 |
| 150 // Expect a last call on destruction, with size 0 and |lastInSlice| true. |
| 151 EXPECT_CALL(*this, writeData(nullptr, 0, true)).Times(1); |
| 152 media_recorder_handler_.reset(); |
| 153 } |
| 154 |
| 155 } // namespace content |
OLD | NEW |