| 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::Mock; | 
|  | 20 using ::testing::SaveArg; | 
|  | 21 | 
|  | 22 namespace content { | 
|  | 23 | 
|  | 24 ACTION_P(RunClosure, closure) { | 
|  | 25   closure.Run(); | 
|  | 26 } | 
|  | 27 | 
|  | 28 static const std::string kTestStreamUrl = "stream_url"; | 
|  | 29 static const std::string kTestVideoTrackId = "video_track_id"; | 
|  | 30 | 
|  | 31 // MediaRecorderHandler needs a meaningful WebMediaStream with at least a | 
|  | 32 // WebMediaStreamTrack - MediaStreamVideoTrack couple. We also need a | 
|  | 33 // WebMediaStreamSource - MockMediaStreamVideoSource (connected via | 
|  | 34 // ExtraData()), so VideoFrames can be injected in the pipeline using | 
|  | 35 // DeliverVideoFrame(). | 
|  | 36 class MediaRecorderHandlerTest | 
|  | 37     : public testing::Test | 
|  | 38     , public blink::WebMediaRecorderHandlerClient { | 
|  | 39  public: | 
|  | 40   MediaRecorderHandlerTest() { | 
|  | 41     EXPECT_FALSE(media_recorder_handler_.recording_); | 
|  | 42 | 
|  | 43     registry_.Init(kTestStreamUrl); | 
|  | 44     registry_.AddVideoTrack(kTestVideoTrackId); | 
|  | 45   } | 
|  | 46 | 
|  | 47   MOCK_METHOD3(writeData, void(const char*, size_t, bool)); | 
|  | 48 | 
|  | 49   bool recording() const { return media_recorder_handler_.recording_; } | 
|  | 50   bool hasVideoRecorders() const { | 
|  | 51     return !media_recorder_handler_.video_recorders_.empty(); | 
|  | 52   } | 
|  | 53 | 
|  | 54   void OnVideoFrameForTesting(const scoped_refptr<media::VideoFrame>& frame) { | 
|  | 55     media_recorder_handler_.OnVideoFrameForTesting(frame, | 
|  | 56                                                    base::TimeTicks::Now()); | 
|  | 57   } | 
|  | 58 | 
|  | 59   MediaRecorderHandler media_recorder_handler_; | 
|  | 60 | 
|  | 61   // A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks | 
|  | 62   // and Sources in |registry_| into believing they are on the right threads. | 
|  | 63   const base::MessageLoopForUI message_loop_; | 
|  | 64   ChildProcess child_process_; | 
|  | 65 | 
|  | 66   MockMediaStreamRegistry registry_; | 
|  | 67 | 
|  | 68  private: | 
|  | 69   DISALLOW_COPY_AND_ASSIGN(MediaRecorderHandlerTest); | 
|  | 70 }; | 
|  | 71 | 
|  | 72 TEST_F(MediaRecorderHandlerTest, CanSupportMimeType) { | 
|  | 73   const blink::WebString mime_type(base::UTF8ToUTF16("video/vp8")); | 
|  | 74   EXPECT_FALSE(media_recorder_handler_.canSupportMimeType(mime_type)); | 
|  | 75 } | 
|  | 76 | 
|  | 77 TEST_F(MediaRecorderHandlerTest, InitializeStartStop) { | 
|  | 78   const blink::WebString mime_type(base::UTF8ToUTF16("video/vp8")); | 
|  | 79   EXPECT_TRUE(media_recorder_handler_.initialize(this, | 
|  | 80                                                  registry_.test_stream(), | 
|  | 81                                                  mime_type)); | 
|  | 82   EXPECT_FALSE(recording()); | 
|  | 83   EXPECT_FALSE(hasVideoRecorders()); | 
|  | 84 | 
|  | 85   EXPECT_TRUE(media_recorder_handler_.start()); | 
|  | 86   EXPECT_TRUE(recording()); | 
|  | 87   EXPECT_TRUE(hasVideoRecorders()); | 
|  | 88 | 
|  | 89   media_recorder_handler_.stop(); | 
|  | 90   EXPECT_FALSE(recording()); | 
|  | 91   EXPECT_FALSE(hasVideoRecorders()); | 
|  | 92 | 
|  | 93   // Expect a last call on destruction. | 
|  | 94   EXPECT_CALL(*this, writeData(_, _, true)).Times(1); | 
|  | 95 } | 
|  | 96 | 
|  | 97 TEST_F(MediaRecorderHandlerTest, EncodeVideoFrames) { | 
|  | 98   const blink::WebString mime_type(base::UTF8ToUTF16("video/vp8")); | 
|  | 99   EXPECT_TRUE(media_recorder_handler_.initialize(this, | 
|  | 100                                                  registry_.test_stream(), | 
|  | 101                                                  mime_type)); | 
|  | 102   EXPECT_FALSE(recording()); | 
|  | 103   EXPECT_FALSE(hasVideoRecorders()); | 
|  | 104 | 
|  | 105   EXPECT_TRUE(media_recorder_handler_.start()); | 
|  | 106 | 
|  | 107   InSequence s; | 
|  | 108   size_t packetised_length = 0; | 
|  | 109   // writeData() is pinged a number of times as the WebM header is written; the | 
|  | 110   // last time it is called it has the encoded data. | 
|  | 111   EXPECT_CALL(*this, writeData(_, _, false)) | 
|  | 112       .Times(AtLeast(1)) | 
|  | 113       .WillRepeatedly(SaveArg<1>(&packetised_length)); | 
|  | 114   // Expect a last call on destruction. | 
|  | 115   EXPECT_CALL(*this, writeData(_, 0, true)).Times(1); | 
|  | 116 | 
|  | 117   const gfx::Size frame_size(160, 80); | 
|  | 118   const scoped_refptr<media::VideoFrame> video_frame = | 
|  | 119       media::VideoFrame::CreateBlackFrame(frame_size); | 
|  | 120   OnVideoFrameForTesting(video_frame); | 
|  | 121 | 
|  | 122   child_process_.io_message_loop()->RunUntilIdle(); | 
|  | 123   base::RunLoop().RunUntilIdle(); | 
|  | 124 | 
|  | 125   const size_t kFirstEncodedDataSize = 52; | 
|  | 126   EXPECT_EQ(packetised_length, kFirstEncodedDataSize); | 
|  | 127 | 
|  | 128   media_recorder_handler_.stop(); | 
|  | 129 } | 
|  | 130 | 
|  | 131 }  // namespace content | 
| OLD | NEW | 
|---|