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 mime_type(base::UTF8ToUTF16("video/vp8")); |
| 77 EXPECT_FALSE(media_recorder_handler_->canSupportMimeType(mime_type)); |
| 78 } |
| 79 |
| 80 // Checks that the initialization-destruction sequence works fine. |
| 81 TEST_F(MediaRecorderHandlerTest, InitializeStartStop) { |
| 82 const WebString mime_type(base::UTF8ToUTF16("video/vp8")); |
| 83 EXPECT_TRUE(media_recorder_handler_->initialize(this, |
| 84 registry_.test_stream(), |
| 85 mime_type)); |
| 86 EXPECT_FALSE(recording()); |
| 87 EXPECT_FALSE(hasVideoRecorders()); |
| 88 |
| 89 EXPECT_TRUE(media_recorder_handler_->start()); |
| 90 EXPECT_TRUE(recording()); |
| 91 EXPECT_TRUE(hasVideoRecorders()); |
| 92 |
| 93 media_recorder_handler_->stop(); |
| 94 EXPECT_FALSE(recording()); |
| 95 EXPECT_FALSE(hasVideoRecorders()); |
| 96 |
| 97 // Expect a last call on destruction. |
| 98 EXPECT_CALL(*this, writeData(_, _, true)).Times(1); |
| 99 media_recorder_handler_.reset(); |
| 100 } |
| 101 |
| 102 // Sends 2 frames and expect them as WebM contained encoded data in writeData(). |
| 103 TEST_F(MediaRecorderHandlerTest, EncodeVideoFrames) { |
| 104 const WebString mime_type(base::UTF8ToUTF16("video/vp8")); |
| 105 EXPECT_TRUE(media_recorder_handler_->initialize(this, registry_.test_stream(), |
| 106 mime_type)); |
| 107 EXPECT_TRUE(media_recorder_handler_->start()); |
| 108 |
| 109 InSequence s; |
| 110 const scoped_refptr<media::VideoFrame> video_frame = |
| 111 media::VideoFrame::CreateBlackFrame(gfx::Size(160, 80)); |
| 112 |
| 113 { |
| 114 base::RunLoop run_loop; |
| 115 base::Closure quit_closure = run_loop.QuitClosure(); |
| 116 // writeData() is pinged a number of times as the WebM header is written; |
| 117 // the last time it is called it has the encoded data. |
| 118 const size_t kEncodedDataSize = 52; |
| 119 EXPECT_CALL(*this, writeData(_, Lt(kEncodedDataSize), false)) |
| 120 .Times(AtLeast(1)); |
| 121 EXPECT_CALL(*this, writeData(_, kEncodedDataSize, false)) |
| 122 .Times(1) |
| 123 .WillOnce(RunClosure(quit_closure)); |
| 124 |
| 125 OnVideoFrameForTesting(video_frame); |
| 126 run_loop.Run(); |
| 127 } |
| 128 |
| 129 { |
| 130 base::RunLoop run_loop; |
| 131 base::Closure quit_closure = run_loop.QuitClosure(); |
| 132 // The second time around writeData() is called a number of times to write |
| 133 // the WebM frame header, and then is pinged with the encoded data. |
| 134 const size_t kSecondEncodedDataSize = 32; |
| 135 EXPECT_CALL(*this, writeData(_, Lt(kSecondEncodedDataSize), false)) |
| 136 .Times(AtLeast(1)); |
| 137 EXPECT_CALL(*this, writeData(_, kSecondEncodedDataSize, false)) |
| 138 .Times(1) |
| 139 .WillOnce(RunClosure(quit_closure)); |
| 140 |
| 141 OnVideoFrameForTesting(video_frame); |
| 142 run_loop.Run(); |
| 143 } |
| 144 |
| 145 media_recorder_handler_->stop(); |
| 146 |
| 147 // Expect a last call on destruction, with size 0 and |lastInSlice| true. |
| 148 EXPECT_CALL(*this, writeData(nullptr, 0, true)).Times(1); |
| 149 media_recorder_handler_.reset(); |
| 150 } |
| 151 |
| 152 } // namespace content |
OLD | NEW |