blob: dd63356e925469cbde9834dad824b526c6a48717 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <string>
12
Henrik Kjellander15583c12016-02-10 10:53:12 +010013#include "webrtc/api/audiotrack.h"
14#include "webrtc/api/mediastream.h"
15#include "webrtc/api/videotrack.h"
nisseaf510af2016-03-21 08:20:42 -070016#include "webrtc/api/test/fakevideotracksource.h"
xians@webrtc.org4cb01282014-06-12 14:57:05 +000017#include "testing/gmock/include/gmock/gmock.h"
18#include "testing/gtest/include/gtest/gtest.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000019#include "webrtc/base/gunit.h"
20#include "webrtc/base/refcount.h"
21#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23static const char kStreamLabel1[] = "local_stream_1";
24static const char kVideoTrackId[] = "dummy_video_cam_1";
25static const char kAudioTrackId[] = "dummy_microphone_1";
26
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000027using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028using ::testing::Exactly;
29
30namespace webrtc {
31
32// Helper class to test Observer.
33class MockObserver : public ObserverInterface {
34 public:
tommi6eca7e32015-12-15 04:27:11 -080035 explicit MockObserver(NotifierInterface* notifier) : notifier_(notifier) {
36 notifier_->RegisterObserver(this);
37 }
38
39 ~MockObserver() { Unregister(); }
40
41 void Unregister() {
42 if (notifier_) {
43 notifier_->UnregisterObserver(this);
44 notifier_ = nullptr;
45 }
46 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48 MOCK_METHOD0(OnChanged, void());
tommi6eca7e32015-12-15 04:27:11 -080049
50 private:
51 NotifierInterface* notifier_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052};
53
54class MediaStreamTest: public testing::Test {
55 protected:
56 virtual void SetUp() {
57 stream_ = MediaStream::Create(kStreamLabel1);
58 ASSERT_TRUE(stream_.get() != NULL);
59
nisseaf510af2016-03-21 08:20:42 -070060 video_track_ =
61 VideoTrack::Create(kVideoTrackId, FakeVideoTrackSource::Create());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 ASSERT_TRUE(video_track_.get() != NULL);
perkjc8f952d2016-03-23 00:33:56 -070063 EXPECT_EQ(MediaStreamTrackInterface::kLive, video_track_->state());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
65 audio_track_ = AudioTrack::Create(kAudioTrackId, NULL);
66
67 ASSERT_TRUE(audio_track_.get() != NULL);
perkjc8f952d2016-03-23 00:33:56 -070068 EXPECT_EQ(MediaStreamTrackInterface::kLive, audio_track_->state());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069
70 EXPECT_TRUE(stream_->AddTrack(video_track_));
71 EXPECT_FALSE(stream_->AddTrack(video_track_));
72 EXPECT_TRUE(stream_->AddTrack(audio_track_));
73 EXPECT_FALSE(stream_->AddTrack(audio_track_));
74 }
75
76 void ChangeTrack(MediaStreamTrackInterface* track) {
tommi6eca7e32015-12-15 04:27:11 -080077 MockObserver observer(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
79 EXPECT_CALL(observer, OnChanged())
80 .Times(Exactly(1));
81 track->set_enabled(false);
82 EXPECT_FALSE(track->enabled());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 }
84
85 scoped_refptr<MediaStreamInterface> stream_;
86 scoped_refptr<AudioTrackInterface> audio_track_;
87 scoped_refptr<VideoTrackInterface> video_track_;
88};
89
90TEST_F(MediaStreamTest, GetTrackInfo) {
91 ASSERT_EQ(1u, stream_->GetVideoTracks().size());
92 ASSERT_EQ(1u, stream_->GetAudioTracks().size());
93
94 // Verify the video track.
95 scoped_refptr<webrtc::MediaStreamTrackInterface> video_track(
96 stream_->GetVideoTracks()[0]);
97 EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
98 EXPECT_TRUE(video_track->enabled());
99
100 ASSERT_EQ(1u, stream_->GetVideoTracks().size());
101 EXPECT_TRUE(stream_->GetVideoTracks()[0].get() == video_track.get());
102 EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get()
103 == video_track.get());
104 video_track = stream_->GetVideoTracks()[0];
105 EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
106 EXPECT_TRUE(video_track->enabled());
107
108 // Verify the audio track.
109 scoped_refptr<webrtc::MediaStreamTrackInterface> audio_track(
110 stream_->GetAudioTracks()[0]);
111 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
112 EXPECT_TRUE(audio_track->enabled());
113 ASSERT_EQ(1u, stream_->GetAudioTracks().size());
114 EXPECT_TRUE(stream_->GetAudioTracks()[0].get() == audio_track.get());
115 EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get()
116 == audio_track.get());
117 audio_track = stream_->GetAudioTracks()[0];
118 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
119 EXPECT_TRUE(audio_track->enabled());
120}
121
122TEST_F(MediaStreamTest, RemoveTrack) {
tommi6eca7e32015-12-15 04:27:11 -0800123 MockObserver observer(stream_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
125 EXPECT_CALL(observer, OnChanged())
126 .Times(Exactly(2));
127
128 EXPECT_TRUE(stream_->RemoveTrack(audio_track_));
129 EXPECT_FALSE(stream_->RemoveTrack(audio_track_));
130 EXPECT_EQ(0u, stream_->GetAudioTracks().size());
131 EXPECT_EQ(0u, stream_->GetAudioTracks().size());
132
133 EXPECT_TRUE(stream_->RemoveTrack(video_track_));
134 EXPECT_FALSE(stream_->RemoveTrack(video_track_));
135
136 EXPECT_EQ(0u, stream_->GetVideoTracks().size());
137 EXPECT_EQ(0u, stream_->GetVideoTracks().size());
138
139 EXPECT_FALSE(stream_->RemoveTrack(static_cast<AudioTrackInterface*>(NULL)));
140 EXPECT_FALSE(stream_->RemoveTrack(static_cast<VideoTrackInterface*>(NULL)));
141}
142
143TEST_F(MediaStreamTest, ChangeVideoTrack) {
144 scoped_refptr<webrtc::VideoTrackInterface> video_track(
145 stream_->GetVideoTracks()[0]);
146 ChangeTrack(video_track.get());
147}
148
149TEST_F(MediaStreamTest, ChangeAudioTrack) {
150 scoped_refptr<webrtc::AudioTrackInterface> audio_track(
151 stream_->GetAudioTracks()[0]);
152 ChangeTrack(audio_track.get());
153}
154
155} // namespace webrtc