blob: 4711e9cba0bd46d7b5d294c9eb736c6d706d9c34 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <string>
29
30#include "talk/app/webrtc/audiotrack.h"
31#include "talk/app/webrtc/mediastream.h"
32#include "talk/app/webrtc/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/refcount.h"
34#include "webrtc/base/scoped_ptr.h"
35#include "webrtc/base/gunit.h"
xians@webrtc.org4cb01282014-06-12 14:57:05 +000036#include "testing/gmock/include/gmock/gmock.h"
37#include "testing/gtest/include/gtest/gtest.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038
39static const char kStreamLabel1[] = "local_stream_1";
40static const char kVideoTrackId[] = "dummy_video_cam_1";
41static const char kAudioTrackId[] = "dummy_microphone_1";
42
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000043using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044using ::testing::Exactly;
45
46namespace webrtc {
47
48// Helper class to test Observer.
49class MockObserver : public ObserverInterface {
50 public:
51 MockObserver() {}
52
53 MOCK_METHOD0(OnChanged, void());
54};
55
56class MediaStreamTest: public testing::Test {
57 protected:
58 virtual void SetUp() {
59 stream_ = MediaStream::Create(kStreamLabel1);
60 ASSERT_TRUE(stream_.get() != NULL);
61
62 video_track_ = VideoTrack::Create(kVideoTrackId, NULL);
63 ASSERT_TRUE(video_track_.get() != NULL);
64 EXPECT_EQ(MediaStreamTrackInterface::kInitializing, video_track_->state());
65
66 audio_track_ = AudioTrack::Create(kAudioTrackId, NULL);
67
68 ASSERT_TRUE(audio_track_.get() != NULL);
69 EXPECT_EQ(MediaStreamTrackInterface::kInitializing, audio_track_->state());
70
71 EXPECT_TRUE(stream_->AddTrack(video_track_));
72 EXPECT_FALSE(stream_->AddTrack(video_track_));
73 EXPECT_TRUE(stream_->AddTrack(audio_track_));
74 EXPECT_FALSE(stream_->AddTrack(audio_track_));
75 }
76
77 void ChangeTrack(MediaStreamTrackInterface* track) {
78 MockObserver observer;
79 track->RegisterObserver(&observer);
80
81 EXPECT_CALL(observer, OnChanged())
82 .Times(Exactly(1));
83 track->set_enabled(false);
84 EXPECT_FALSE(track->enabled());
85
86 EXPECT_CALL(observer, OnChanged())
87 .Times(Exactly(1));
88 track->set_state(MediaStreamTrackInterface::kLive);
89 EXPECT_EQ(MediaStreamTrackInterface::kLive, track->state());
90 }
91
92 scoped_refptr<MediaStreamInterface> stream_;
93 scoped_refptr<AudioTrackInterface> audio_track_;
94 scoped_refptr<VideoTrackInterface> video_track_;
95};
96
97TEST_F(MediaStreamTest, GetTrackInfo) {
98 ASSERT_EQ(1u, stream_->GetVideoTracks().size());
99 ASSERT_EQ(1u, stream_->GetAudioTracks().size());
100
101 // Verify the video track.
102 scoped_refptr<webrtc::MediaStreamTrackInterface> video_track(
103 stream_->GetVideoTracks()[0]);
104 EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
105 EXPECT_TRUE(video_track->enabled());
106
107 ASSERT_EQ(1u, stream_->GetVideoTracks().size());
108 EXPECT_TRUE(stream_->GetVideoTracks()[0].get() == video_track.get());
109 EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get()
110 == video_track.get());
111 video_track = stream_->GetVideoTracks()[0];
112 EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
113 EXPECT_TRUE(video_track->enabled());
114
115 // Verify the audio track.
116 scoped_refptr<webrtc::MediaStreamTrackInterface> audio_track(
117 stream_->GetAudioTracks()[0]);
118 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
119 EXPECT_TRUE(audio_track->enabled());
120 ASSERT_EQ(1u, stream_->GetAudioTracks().size());
121 EXPECT_TRUE(stream_->GetAudioTracks()[0].get() == audio_track.get());
122 EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get()
123 == audio_track.get());
124 audio_track = stream_->GetAudioTracks()[0];
125 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
126 EXPECT_TRUE(audio_track->enabled());
127}
128
129TEST_F(MediaStreamTest, RemoveTrack) {
130 MockObserver observer;
131 stream_->RegisterObserver(&observer);
132
133 EXPECT_CALL(observer, OnChanged())
134 .Times(Exactly(2));
135
136 EXPECT_TRUE(stream_->RemoveTrack(audio_track_));
137 EXPECT_FALSE(stream_->RemoveTrack(audio_track_));
138 EXPECT_EQ(0u, stream_->GetAudioTracks().size());
139 EXPECT_EQ(0u, stream_->GetAudioTracks().size());
140
141 EXPECT_TRUE(stream_->RemoveTrack(video_track_));
142 EXPECT_FALSE(stream_->RemoveTrack(video_track_));
143
144 EXPECT_EQ(0u, stream_->GetVideoTracks().size());
145 EXPECT_EQ(0u, stream_->GetVideoTracks().size());
146
147 EXPECT_FALSE(stream_->RemoveTrack(static_cast<AudioTrackInterface*>(NULL)));
148 EXPECT_FALSE(stream_->RemoveTrack(static_cast<VideoTrackInterface*>(NULL)));
149}
150
151TEST_F(MediaStreamTest, ChangeVideoTrack) {
152 scoped_refptr<webrtc::VideoTrackInterface> video_track(
153 stream_->GetVideoTracks()[0]);
154 ChangeTrack(video_track.get());
155}
156
157TEST_F(MediaStreamTest, ChangeAudioTrack) {
158 scoped_refptr<webrtc::AudioTrackInterface> audio_track(
159 stream_->GetAudioTracks()[0]);
160 ChangeTrack(audio_track.get());
161}
162
163} // namespace webrtc