Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / video_destination_handler_unittest.cc
1 // Copyright (c) 2013 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 <string>
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/renderer/media/media_stream.h"
9 #include "content/renderer/media/mock_media_stream_dependency_factory.h"
10 #include "content/renderer/media/mock_media_stream_registry.h"
11 #include "content/renderer/media/video_destination_handler.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
15 #include "third_party/WebKit/public/platform/WebString.h"
16
17 using cricket::CapturedFrame;
18 using cricket::CaptureState;
19 using cricket::VideoCapturer;
20 using cricket::VideoFormat;
21 using cricket::VideoFormatPod;
22
23 namespace content {
24
25 static const std::string kTestStreamUrl = "stream_url";
26 static const std::string kUnknownStreamUrl = "unknown_stream_url";
27 static const VideoFormatPod kTestFormat = {
28   640, 360, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY
29 };
30
31 class PpFrameWriterTest
32     : public ::testing::Test,
33       public sigslot::has_slots<> {
34  public:
35   PpFrameWriterTest()
36       : last_capture_state_(cricket::CS_FAILED),
37         captured_frame_count_(0),
38         captured_frame_(NULL) {
39     writer_.SignalStateChange.connect(this, &PpFrameWriterTest::OnStateChange);
40     writer_.SignalFrameCaptured.connect(
41         this, &PpFrameWriterTest::OnFrameCaptured);
42   }
43
44   void OnStateChange(VideoCapturer* capturer, CaptureState state) {
45     last_capture_state_ = state;
46   }
47
48   void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame) {
49     ++captured_frame_count_;
50     captured_frame_ = const_cast<CapturedFrame*>(frame);
51   }
52
53  protected:
54   PpFrameWriter writer_;
55   CaptureState last_capture_state_;
56   int captured_frame_count_;
57   CapturedFrame* captured_frame_;
58 };
59
60 class VideoDestinationHandlerTest : public ::testing::Test {
61  public:
62   VideoDestinationHandlerTest() : registry_(&factory_) {
63     registry_.Init(kTestStreamUrl);
64   }
65
66  protected:
67   MockMediaStreamDependencyFactory factory_;
68   MockMediaStreamRegistry registry_;
69 };
70
71 TEST_F(PpFrameWriterTest, StartStop) {
72   EXPECT_FALSE(writer_.IsRunning());
73   EXPECT_EQ(cricket::CS_STARTING, writer_.Start(VideoFormat(kTestFormat)));
74   EXPECT_TRUE(writer_.IsRunning());
75   EXPECT_EQ(cricket::CS_FAILED, writer_.Start(VideoFormat(kTestFormat)));
76   writer_.Stop();
77   EXPECT_EQ(cricket::CS_STOPPED, last_capture_state_);
78 }
79
80 TEST_F(PpFrameWriterTest, GetPreferredFourccs) {
81   std::vector<uint32> fourccs;
82   EXPECT_TRUE(writer_.GetPreferredFourccs(&fourccs));
83   EXPECT_EQ(1u, fourccs.size());
84   EXPECT_EQ(cricket::FOURCC_BGRA, fourccs[0]);
85 }
86
87 TEST_F(PpFrameWriterTest, GetBestCaptureFormat) {
88   VideoFormat desired(kTestFormat);
89   VideoFormat best_format;
90   EXPECT_FALSE(writer_.GetBestCaptureFormat(desired, NULL));
91   EXPECT_TRUE(writer_.GetBestCaptureFormat(desired, &best_format));
92   EXPECT_EQ(cricket::FOURCC_BGRA, best_format.fourcc);
93
94   desired.fourcc = best_format.fourcc;
95   EXPECT_EQ(desired, best_format);
96 }
97
98 TEST_F(VideoDestinationHandlerTest, Open) {
99   FrameWriterInterface* frame_writer = NULL;
100   // Unknow url will return false.
101   EXPECT_FALSE(VideoDestinationHandler::Open(&factory_, &registry_,
102                                              kUnknownStreamUrl, &frame_writer));
103   EXPECT_TRUE(VideoDestinationHandler::Open(&factory_, &registry_,
104                                             kTestStreamUrl, &frame_writer));
105   EXPECT_TRUE(frame_writer);
106
107   // Verify the video track has been added.
108   const blink::WebMediaStream test_stream = registry_.test_stream();
109   blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
110   test_stream.videoTracks(video_tracks);
111   EXPECT_EQ(1u, video_tracks.size());
112
113   // Verify the native video track has been added.
114   MediaStream* native_stream = MediaStream::GetMediaStream(test_stream);
115   DCHECK(native_stream);
116   webrtc::MediaStreamInterface* webrtc_stream =
117       MediaStream::GetAdapter(test_stream);
118   DCHECK(webrtc_stream);
119   webrtc::VideoTrackVector webrtc_video_tracks =
120       webrtc_stream->GetVideoTracks();
121   EXPECT_EQ(1u, webrtc_video_tracks.size());
122
123   delete frame_writer;
124 }
125
126 }  // namespace content