henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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" |
| 33 | #include "talk/base/refcount.h" |
| 34 | #include "talk/base/scoped_ptr.h" |
| 35 | #include "talk/base/gunit.h" |
| 36 | #include "testing/base/public/gmock.h" |
| 37 | |
| 38 | static const char kStreamLabel1[] = "local_stream_1"; |
| 39 | static const char kVideoTrackId[] = "dummy_video_cam_1"; |
| 40 | static const char kAudioTrackId[] = "dummy_microphone_1"; |
| 41 | |
| 42 | using talk_base::scoped_refptr; |
| 43 | using ::testing::Exactly; |
| 44 | |
| 45 | namespace webrtc { |
| 46 | |
| 47 | // Helper class to test Observer. |
| 48 | class MockObserver : public ObserverInterface { |
| 49 | public: |
| 50 | MockObserver() {} |
| 51 | |
| 52 | MOCK_METHOD0(OnChanged, void()); |
| 53 | }; |
| 54 | |
| 55 | class MediaStreamTest: public testing::Test { |
| 56 | protected: |
| 57 | virtual void SetUp() { |
| 58 | stream_ = MediaStream::Create(kStreamLabel1); |
| 59 | ASSERT_TRUE(stream_.get() != NULL); |
| 60 | |
| 61 | video_track_ = VideoTrack::Create(kVideoTrackId, NULL); |
| 62 | ASSERT_TRUE(video_track_.get() != NULL); |
| 63 | EXPECT_EQ(MediaStreamTrackInterface::kInitializing, video_track_->state()); |
| 64 | |
| 65 | audio_track_ = AudioTrack::Create(kAudioTrackId, NULL); |
| 66 | |
| 67 | ASSERT_TRUE(audio_track_.get() != NULL); |
| 68 | EXPECT_EQ(MediaStreamTrackInterface::kInitializing, audio_track_->state()); |
| 69 | |
| 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) { |
| 77 | MockObserver observer; |
| 78 | track->RegisterObserver(&observer); |
| 79 | |
| 80 | EXPECT_CALL(observer, OnChanged()) |
| 81 | .Times(Exactly(1)); |
| 82 | track->set_enabled(false); |
| 83 | EXPECT_FALSE(track->enabled()); |
| 84 | |
| 85 | EXPECT_CALL(observer, OnChanged()) |
| 86 | .Times(Exactly(1)); |
| 87 | track->set_state(MediaStreamTrackInterface::kLive); |
| 88 | EXPECT_EQ(MediaStreamTrackInterface::kLive, track->state()); |
| 89 | } |
| 90 | |
| 91 | scoped_refptr<MediaStreamInterface> stream_; |
| 92 | scoped_refptr<AudioTrackInterface> audio_track_; |
| 93 | scoped_refptr<VideoTrackInterface> video_track_; |
| 94 | }; |
| 95 | |
| 96 | TEST_F(MediaStreamTest, GetTrackInfo) { |
| 97 | ASSERT_EQ(1u, stream_->GetVideoTracks().size()); |
| 98 | ASSERT_EQ(1u, stream_->GetAudioTracks().size()); |
| 99 | |
| 100 | // Verify the video track. |
| 101 | scoped_refptr<webrtc::MediaStreamTrackInterface> video_track( |
| 102 | stream_->GetVideoTracks()[0]); |
| 103 | EXPECT_EQ(0, video_track->id().compare(kVideoTrackId)); |
| 104 | EXPECT_TRUE(video_track->enabled()); |
| 105 | |
| 106 | ASSERT_EQ(1u, stream_->GetVideoTracks().size()); |
| 107 | EXPECT_TRUE(stream_->GetVideoTracks()[0].get() == video_track.get()); |
| 108 | EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get() |
| 109 | == video_track.get()); |
| 110 | video_track = stream_->GetVideoTracks()[0]; |
| 111 | EXPECT_EQ(0, video_track->id().compare(kVideoTrackId)); |
| 112 | EXPECT_TRUE(video_track->enabled()); |
| 113 | |
| 114 | // Verify the audio track. |
| 115 | scoped_refptr<webrtc::MediaStreamTrackInterface> audio_track( |
| 116 | stream_->GetAudioTracks()[0]); |
| 117 | EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId)); |
| 118 | EXPECT_TRUE(audio_track->enabled()); |
| 119 | ASSERT_EQ(1u, stream_->GetAudioTracks().size()); |
| 120 | EXPECT_TRUE(stream_->GetAudioTracks()[0].get() == audio_track.get()); |
| 121 | EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get() |
| 122 | == audio_track.get()); |
| 123 | audio_track = stream_->GetAudioTracks()[0]; |
| 124 | EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId)); |
| 125 | EXPECT_TRUE(audio_track->enabled()); |
| 126 | } |
| 127 | |
| 128 | TEST_F(MediaStreamTest, RemoveTrack) { |
| 129 | MockObserver observer; |
| 130 | stream_->RegisterObserver(&observer); |
| 131 | |
| 132 | EXPECT_CALL(observer, OnChanged()) |
| 133 | .Times(Exactly(2)); |
| 134 | |
| 135 | EXPECT_TRUE(stream_->RemoveTrack(audio_track_)); |
| 136 | EXPECT_FALSE(stream_->RemoveTrack(audio_track_)); |
| 137 | EXPECT_EQ(0u, stream_->GetAudioTracks().size()); |
| 138 | EXPECT_EQ(0u, stream_->GetAudioTracks().size()); |
| 139 | |
| 140 | EXPECT_TRUE(stream_->RemoveTrack(video_track_)); |
| 141 | EXPECT_FALSE(stream_->RemoveTrack(video_track_)); |
| 142 | |
| 143 | EXPECT_EQ(0u, stream_->GetVideoTracks().size()); |
| 144 | EXPECT_EQ(0u, stream_->GetVideoTracks().size()); |
| 145 | |
| 146 | EXPECT_FALSE(stream_->RemoveTrack(static_cast<AudioTrackInterface*>(NULL))); |
| 147 | EXPECT_FALSE(stream_->RemoveTrack(static_cast<VideoTrackInterface*>(NULL))); |
| 148 | } |
| 149 | |
| 150 | TEST_F(MediaStreamTest, ChangeVideoTrack) { |
| 151 | scoped_refptr<webrtc::VideoTrackInterface> video_track( |
| 152 | stream_->GetVideoTracks()[0]); |
| 153 | ChangeTrack(video_track.get()); |
| 154 | } |
| 155 | |
| 156 | TEST_F(MediaStreamTest, ChangeAudioTrack) { |
| 157 | scoped_refptr<webrtc::AudioTrackInterface> audio_track( |
| 158 | stream_->GetAudioTracks()[0]); |
| 159 | ChangeTrack(audio_track.get()); |
| 160 | } |
| 161 | |
| 162 | } // namespace webrtc |