OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2011 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <string> | |
12 | |
13 #include "webrtc/api/audiotrack.h" | |
14 #include "webrtc/api/mediastream.h" | |
15 #include "webrtc/api/test/fakevideotracksource.h" | |
16 #include "webrtc/api/videotrack.h" | |
17 #include "webrtc/base/gunit.h" | |
18 #include "webrtc/base/refcount.h" | |
19 #include "webrtc/test/gmock.h" | |
20 #include "webrtc/test/gtest.h" | |
21 | |
22 static const char kStreamLabel1[] = "local_stream_1"; | |
23 static const char kVideoTrackId[] = "dummy_video_cam_1"; | |
24 static const char kAudioTrackId[] = "dummy_microphone_1"; | |
25 | |
26 using rtc::scoped_refptr; | |
27 using ::testing::Exactly; | |
28 | |
29 namespace webrtc { | |
30 | |
31 // Helper class to test Observer. | |
32 class MockObserver : public ObserverInterface { | |
33 public: | |
34 explicit MockObserver(NotifierInterface* notifier) : notifier_(notifier) { | |
35 notifier_->RegisterObserver(this); | |
36 } | |
37 | |
38 ~MockObserver() { Unregister(); } | |
39 | |
40 void Unregister() { | |
41 if (notifier_) { | |
42 notifier_->UnregisterObserver(this); | |
43 notifier_ = nullptr; | |
44 } | |
45 } | |
46 | |
47 MOCK_METHOD0(OnChanged, void()); | |
48 | |
49 private: | |
50 NotifierInterface* notifier_; | |
51 }; | |
52 | |
53 class MediaStreamTest: public testing::Test { | |
54 protected: | |
55 virtual void SetUp() { | |
56 stream_ = MediaStream::Create(kStreamLabel1); | |
57 ASSERT_TRUE(stream_.get() != NULL); | |
58 | |
59 video_track_ = | |
60 VideoTrack::Create(kVideoTrackId, FakeVideoTrackSource::Create()); | |
61 ASSERT_TRUE(video_track_.get() != NULL); | |
62 EXPECT_EQ(MediaStreamTrackInterface::kLive, video_track_->state()); | |
63 | |
64 audio_track_ = AudioTrack::Create(kAudioTrackId, NULL); | |
65 | |
66 ASSERT_TRUE(audio_track_.get() != NULL); | |
67 EXPECT_EQ(MediaStreamTrackInterface::kLive, audio_track_->state()); | |
68 | |
69 EXPECT_TRUE(stream_->AddTrack(video_track_)); | |
70 EXPECT_FALSE(stream_->AddTrack(video_track_)); | |
71 EXPECT_TRUE(stream_->AddTrack(audio_track_)); | |
72 EXPECT_FALSE(stream_->AddTrack(audio_track_)); | |
73 } | |
74 | |
75 void ChangeTrack(MediaStreamTrackInterface* track) { | |
76 MockObserver observer(track); | |
77 | |
78 EXPECT_CALL(observer, OnChanged()) | |
79 .Times(Exactly(1)); | |
80 track->set_enabled(false); | |
81 EXPECT_FALSE(track->enabled()); | |
82 } | |
83 | |
84 scoped_refptr<MediaStreamInterface> stream_; | |
85 scoped_refptr<AudioTrackInterface> audio_track_; | |
86 scoped_refptr<VideoTrackInterface> video_track_; | |
87 }; | |
88 | |
89 TEST_F(MediaStreamTest, GetTrackInfo) { | |
90 ASSERT_EQ(1u, stream_->GetVideoTracks().size()); | |
91 ASSERT_EQ(1u, stream_->GetAudioTracks().size()); | |
92 | |
93 // Verify the video track. | |
94 scoped_refptr<webrtc::MediaStreamTrackInterface> video_track( | |
95 stream_->GetVideoTracks()[0]); | |
96 EXPECT_EQ(0, video_track->id().compare(kVideoTrackId)); | |
97 EXPECT_TRUE(video_track->enabled()); | |
98 | |
99 ASSERT_EQ(1u, stream_->GetVideoTracks().size()); | |
100 EXPECT_TRUE(stream_->GetVideoTracks()[0].get() == video_track.get()); | |
101 EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get() | |
102 == video_track.get()); | |
103 video_track = stream_->GetVideoTracks()[0]; | |
104 EXPECT_EQ(0, video_track->id().compare(kVideoTrackId)); | |
105 EXPECT_TRUE(video_track->enabled()); | |
106 | |
107 // Verify the audio track. | |
108 scoped_refptr<webrtc::MediaStreamTrackInterface> audio_track( | |
109 stream_->GetAudioTracks()[0]); | |
110 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId)); | |
111 EXPECT_TRUE(audio_track->enabled()); | |
112 ASSERT_EQ(1u, stream_->GetAudioTracks().size()); | |
113 EXPECT_TRUE(stream_->GetAudioTracks()[0].get() == audio_track.get()); | |
114 EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get() | |
115 == audio_track.get()); | |
116 audio_track = stream_->GetAudioTracks()[0]; | |
117 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId)); | |
118 EXPECT_TRUE(audio_track->enabled()); | |
119 } | |
120 | |
121 TEST_F(MediaStreamTest, RemoveTrack) { | |
122 MockObserver observer(stream_); | |
123 | |
124 EXPECT_CALL(observer, OnChanged()) | |
125 .Times(Exactly(2)); | |
126 | |
127 EXPECT_TRUE(stream_->RemoveTrack(audio_track_)); | |
128 EXPECT_FALSE(stream_->RemoveTrack(audio_track_)); | |
129 EXPECT_EQ(0u, stream_->GetAudioTracks().size()); | |
130 EXPECT_EQ(0u, stream_->GetAudioTracks().size()); | |
131 | |
132 EXPECT_TRUE(stream_->RemoveTrack(video_track_)); | |
133 EXPECT_FALSE(stream_->RemoveTrack(video_track_)); | |
134 | |
135 EXPECT_EQ(0u, stream_->GetVideoTracks().size()); | |
136 EXPECT_EQ(0u, stream_->GetVideoTracks().size()); | |
137 | |
138 EXPECT_FALSE(stream_->RemoveTrack(static_cast<AudioTrackInterface*>(NULL))); | |
139 EXPECT_FALSE(stream_->RemoveTrack(static_cast<VideoTrackInterface*>(NULL))); | |
140 } | |
141 | |
142 TEST_F(MediaStreamTest, ChangeVideoTrack) { | |
143 scoped_refptr<webrtc::VideoTrackInterface> video_track( | |
144 stream_->GetVideoTracks()[0]); | |
145 ChangeTrack(video_track.get()); | |
146 } | |
147 | |
148 TEST_F(MediaStreamTest, ChangeAudioTrack) { | |
149 scoped_refptr<webrtc::AudioTrackInterface> audio_track( | |
150 stream_->GetAudioTracks()[0]); | |
151 ChangeTrack(audio_track.get()); | |
152 } | |
153 | |
154 } // namespace webrtc | |
OLD | NEW |