blob: fee92b737a095320642ee3c574d816174021261d [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"
xians@webrtc.org4cb01282014-06-12 14:57:05 +000016#include "testing/gmock/include/gmock/gmock.h"
17#include "testing/gtest/include/gtest/gtest.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000018#include "webrtc/base/gunit.h"
19#include "webrtc/base/refcount.h"
20#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22static const char kStreamLabel1[] = "local_stream_1";
23static const char kVideoTrackId[] = "dummy_video_cam_1";
24static const char kAudioTrackId[] = "dummy_microphone_1";
25
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027using ::testing::Exactly;
28
29namespace webrtc {
30
31// Helper class to test Observer.
32class MockObserver : public ObserverInterface {
33 public:
tommi6eca7e32015-12-15 04:27:11 -080034 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 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
47 MOCK_METHOD0(OnChanged, void());
tommi6eca7e32015-12-15 04:27:11 -080048
49 private:
50 NotifierInterface* notifier_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051};
52
53class 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_ = VideoTrack::Create(kVideoTrackId, NULL);
60 ASSERT_TRUE(video_track_.get() != NULL);
61 EXPECT_EQ(MediaStreamTrackInterface::kInitializing, video_track_->state());
62
63 audio_track_ = AudioTrack::Create(kAudioTrackId, NULL);
64
65 ASSERT_TRUE(audio_track_.get() != NULL);
66 EXPECT_EQ(MediaStreamTrackInterface::kInitializing, audio_track_->state());
67
68 EXPECT_TRUE(stream_->AddTrack(video_track_));
69 EXPECT_FALSE(stream_->AddTrack(video_track_));
70 EXPECT_TRUE(stream_->AddTrack(audio_track_));
71 EXPECT_FALSE(stream_->AddTrack(audio_track_));
72 }
73
74 void ChangeTrack(MediaStreamTrackInterface* track) {
tommi6eca7e32015-12-15 04:27:11 -080075 MockObserver observer(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
77 EXPECT_CALL(observer, OnChanged())
78 .Times(Exactly(1));
79 track->set_enabled(false);
80 EXPECT_FALSE(track->enabled());
81
82 EXPECT_CALL(observer, OnChanged())
83 .Times(Exactly(1));
84 track->set_state(MediaStreamTrackInterface::kLive);
85 EXPECT_EQ(MediaStreamTrackInterface::kLive, track->state());
86 }
87
88 scoped_refptr<MediaStreamInterface> stream_;
89 scoped_refptr<AudioTrackInterface> audio_track_;
90 scoped_refptr<VideoTrackInterface> video_track_;
91};
92
93TEST_F(MediaStreamTest, GetTrackInfo) {
94 ASSERT_EQ(1u, stream_->GetVideoTracks().size());
95 ASSERT_EQ(1u, stream_->GetAudioTracks().size());
96
97 // Verify the video track.
98 scoped_refptr<webrtc::MediaStreamTrackInterface> video_track(
99 stream_->GetVideoTracks()[0]);
100 EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
101 EXPECT_TRUE(video_track->enabled());
102
103 ASSERT_EQ(1u, stream_->GetVideoTracks().size());
104 EXPECT_TRUE(stream_->GetVideoTracks()[0].get() == video_track.get());
105 EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get()
106 == video_track.get());
107 video_track = stream_->GetVideoTracks()[0];
108 EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
109 EXPECT_TRUE(video_track->enabled());
110
111 // Verify the audio track.
112 scoped_refptr<webrtc::MediaStreamTrackInterface> audio_track(
113 stream_->GetAudioTracks()[0]);
114 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
115 EXPECT_TRUE(audio_track->enabled());
116 ASSERT_EQ(1u, stream_->GetAudioTracks().size());
117 EXPECT_TRUE(stream_->GetAudioTracks()[0].get() == audio_track.get());
118 EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get()
119 == audio_track.get());
120 audio_track = stream_->GetAudioTracks()[0];
121 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
122 EXPECT_TRUE(audio_track->enabled());
123}
124
125TEST_F(MediaStreamTest, RemoveTrack) {
tommi6eca7e32015-12-15 04:27:11 -0800126 MockObserver observer(stream_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
128 EXPECT_CALL(observer, OnChanged())
129 .Times(Exactly(2));
130
131 EXPECT_TRUE(stream_->RemoveTrack(audio_track_));
132 EXPECT_FALSE(stream_->RemoveTrack(audio_track_));
133 EXPECT_EQ(0u, stream_->GetAudioTracks().size());
134 EXPECT_EQ(0u, stream_->GetAudioTracks().size());
135
136 EXPECT_TRUE(stream_->RemoveTrack(video_track_));
137 EXPECT_FALSE(stream_->RemoveTrack(video_track_));
138
139 EXPECT_EQ(0u, stream_->GetVideoTracks().size());
140 EXPECT_EQ(0u, stream_->GetVideoTracks().size());
141
142 EXPECT_FALSE(stream_->RemoveTrack(static_cast<AudioTrackInterface*>(NULL)));
143 EXPECT_FALSE(stream_->RemoveTrack(static_cast<VideoTrackInterface*>(NULL)));
144}
145
146TEST_F(MediaStreamTest, ChangeVideoTrack) {
147 scoped_refptr<webrtc::VideoTrackInterface> video_track(
148 stream_->GetVideoTracks()[0]);
149 ChangeTrack(video_track.get());
150}
151
152TEST_F(MediaStreamTest, ChangeAudioTrack) {
153 scoped_refptr<webrtc::AudioTrackInterface> audio_track(
154 stream_->GetAudioTracks()[0]);
155 ChangeTrack(audio_track.get());
156}
157
158} // namespace webrtc