Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "content/common/media_stream_messages.h" | |
| 11 #include "content/renderer/media/media_stream_dispatcher.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const int kRouteId = 0; | |
| 17 const int kAudioSessionId = 3; | |
| 18 const int kVideoSessionId = 5; | |
| 19 const int kRequestId1 = 10; | |
| 20 const int kRequestId2 = 20; | |
| 21 | |
| 22 class MockMediaStreamDispatcherEventHandler | |
| 23 : public MediaStreamDispatcher::EventHandler { | |
| 24 public: | |
| 25 MockMediaStreamDispatcherEventHandler() | |
| 26 : request_id_(-1), | |
| 27 label_(""), | |
|
Leandro Graciá Gil
2011/06/16 17:40:01
This is not required as the default constructor wi
Per K
2011/06/17 15:47:54
Done.
| |
| 28 audio_failed(false), | |
| 29 video_failed(false) {} | |
| 30 | |
| 31 virtual void OnStreamGenerated( | |
| 32 int request_id, | |
| 33 const std::string &label, | |
|
Leandro Graciá Gil
2011/06/16 17:40:01
Same as mentioned in the media_stream_dispatcher.h
Per K
2011/06/17 15:47:54
Done.
| |
| 34 const media_stream::StreamDeviceInfoArray audio_device_array, | |
| 35 const media_stream::StreamDeviceInfoArray video_device_array) { | |
| 36 request_id_ = request_id; | |
| 37 label_ = label; | |
| 38 } | |
| 39 | |
| 40 virtual void OnStreamGenerationFailed(int request_id) { | |
| 41 request_id_ = request_id; | |
| 42 } | |
| 43 | |
| 44 virtual void OnAudioDeviceFailed(const std::string &label, | |
| 45 int index) { | |
| 46 audio_failed = true; | |
| 47 } | |
| 48 | |
| 49 virtual void OnVideoDeviceFailed(const std::string &label, | |
| 50 int index) { | |
| 51 video_failed = true; | |
| 52 } | |
| 53 | |
| 54 int request_id_; | |
| 55 std::string label_; | |
| 56 bool audio_failed; | |
| 57 bool video_failed; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(MockMediaStreamDispatcherEventHandler); | |
| 60 }; | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 TEST(MediaStreamDispatcherTest, Basic) { | |
| 65 scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL)); | |
| 66 scoped_ptr<MockMediaStreamDispatcherEventHandler> | |
| 67 handler(new MockMediaStreamDispatcherEventHandler); | |
| 68 media_stream::GenerateStreamOptions components( | |
| 69 true, media_stream::GenerateStreamOptions::kVideoFacingUser); | |
| 70 std::string security_origin; | |
| 71 | |
| 72 int ipc_request_id1 = dispatcher->next_ipc_id_; | |
| 73 dispatcher->GenerateStream(kRequestId1, handler.get(), components, | |
| 74 security_origin); | |
| 75 int ipc_request_id2 = dispatcher->next_ipc_id_; | |
| 76 EXPECT_NE(ipc_request_id1, ipc_request_id2); | |
| 77 dispatcher->GenerateStream(kRequestId2, handler.get(), components, | |
|
Leandro Graciá Gil
2011/06/16 17:40:01
Probably not part of this test yet, but are you ch
Per K
2011/06/17 15:47:54
The ipc_request_id is unique per MediaStreamDispat
| |
| 78 security_origin); | |
| 79 EXPECT_EQ(dispatcher->requests_.size(), size_t(2)); | |
| 80 | |
| 81 media_stream::StreamDeviceInfoArray audio_device_array(1); | |
| 82 media_stream::StreamDeviceInfo audio_device_info; | |
| 83 audio_device_info.name = "Microphone"; | |
| 84 audio_device_info.stream_type = media_stream::kAudioCapture; | |
| 85 audio_device_info.session_id = kAudioSessionId; | |
| 86 audio_device_array[0] = audio_device_info; | |
| 87 | |
| 88 media_stream::StreamDeviceInfoArray video_device_array(1); | |
| 89 media_stream::StreamDeviceInfo video_device_info; | |
| 90 video_device_info.name = "Camera"; | |
| 91 video_device_info.stream_type = media_stream::kVideoCapture; | |
| 92 video_device_info.session_id = kVideoSessionId; | |
| 93 video_device_array[0] = video_device_info; | |
| 94 | |
| 95 // Complete the creation of stream1. | |
| 96 std::string stream_label1 = std::string("stream1"); | |
| 97 dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerated( | |
| 98 kRouteId, ipc_request_id1, stream_label1, | |
| 99 audio_device_array, video_device_array)); | |
| 100 EXPECT_EQ(handler->request_id_, kRequestId1); | |
| 101 EXPECT_EQ(handler->label_, stream_label1); | |
| 102 | |
| 103 // Complete the creation of stream2. | |
| 104 std::string stream_label2 = std::string("stream2"); | |
| 105 dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerated( | |
| 106 kRouteId, ipc_request_id2, stream_label2, | |
| 107 audio_device_array, video_device_array)); | |
| 108 EXPECT_EQ(handler->request_id_, kRequestId2); | |
| 109 EXPECT_EQ(handler->label_, stream_label2); | |
| 110 | |
| 111 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); | |
| 112 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(2)); | |
| 113 | |
| 114 // Verify stream2. | |
| 115 const media_stream::StreamDeviceInfoArray* device_array = | |
| 116 dispatcher->audio_device_array(stream_label2); | |
| 117 EXPECT_EQ((*device_array)[0].session_id, kAudioSessionId); | |
| 118 EXPECT_EQ((*device_array)[0].name, audio_device_info.name); | |
| 119 | |
| 120 device_array = dispatcher->video_device_array(stream_label2); | |
| 121 EXPECT_EQ((*device_array)[0].session_id, kVideoSessionId); | |
| 122 EXPECT_EQ((*device_array)[0].name, video_device_info.name); | |
| 123 | |
| 124 // Stop stream2. | |
| 125 dispatcher->StopStream(stream_label2); | |
| 126 EXPECT_TRUE(dispatcher->audio_device_array(stream_label2) == NULL); | |
|
scherkus (not reviewing)
2011/06/17 03:31:59
if you're testing for null pointer you can use EXP
Per K
2011/06/17 15:47:54
Done.
| |
| 127 EXPECT_TRUE(dispatcher->video_device_array(stream_label2) == NULL); | |
| 128 | |
| 129 // Stop stream1. | |
| 130 dispatcher->StopStream(stream_label1); | |
| 131 EXPECT_TRUE(dispatcher->audio_device_array(stream_label1) == NULL); | |
| 132 EXPECT_TRUE(dispatcher->video_device_array(stream_label1) == NULL); | |
| 133 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); | |
| 134 | |
| 135 // Verify that the request have been completed. | |
| 136 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); | |
| 137 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); | |
| 138 } | |
| 139 | |
| 140 TEST(MediaStreamDispatcherTest, TestFailure) { | |
| 141 scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL)); | |
| 142 scoped_ptr<MockMediaStreamDispatcherEventHandler> | |
| 143 handler(new MockMediaStreamDispatcherEventHandler); | |
| 144 media_stream::GenerateStreamOptions components( | |
| 145 true, media_stream::GenerateStreamOptions::kVideoFacingUser); | |
| 146 std::string security_origin; | |
| 147 | |
| 148 // Test failure when creating a stream. | |
|
scherkus (not reviewing)
2011/06/17 03:31:59
indentation
Per K
2011/06/17 15:47:54
Done.
| |
| 149 int ipc_request_id1 = dispatcher->next_ipc_id_; | |
| 150 dispatcher->GenerateStream(kRequestId1, handler.get(), components, | |
| 151 security_origin); | |
|
scherkus (not reviewing)
2011/06/17 03:31:59
indentation
Per K
2011/06/17 15:47:54
Done.
| |
| 152 dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerationFailed( | |
| 153 kRouteId, ipc_request_id1)); | |
| 154 | |
| 155 // Verify that the request have been completed. | |
| 156 EXPECT_EQ(handler->request_id_, kRequestId1); | |
| 157 EXPECT_EQ(dispatcher->requests_.size(), size_t(0)); | |
| 158 | |
| 159 // Create a new stream. | |
| 160 ipc_request_id1 = dispatcher->next_ipc_id_; | |
| 161 dispatcher->GenerateStream(kRequestId1, handler.get(), components, | |
| 162 security_origin); | |
|
scherkus (not reviewing)
2011/06/17 03:31:59
indentation
Per K
2011/06/17 15:47:54
Done.
| |
| 163 | |
| 164 media_stream::StreamDeviceInfoArray audio_device_array(1); | |
| 165 media_stream::StreamDeviceInfo audio_device_info; | |
| 166 audio_device_info.name = "Microphone"; | |
| 167 audio_device_info.stream_type = media_stream::kAudioCapture; | |
| 168 audio_device_info.session_id = kAudioSessionId; | |
| 169 audio_device_array[0] = audio_device_info; | |
| 170 | |
| 171 media_stream::StreamDeviceInfoArray video_device_array(1); | |
| 172 media_stream::StreamDeviceInfo video_device_info; | |
| 173 video_device_info.name = "Camera"; | |
| 174 video_device_info.stream_type = media_stream::kVideoCapture; | |
| 175 video_device_info.session_id = kVideoSessionId; | |
| 176 video_device_array[0] = video_device_info; | |
| 177 | |
| 178 // Complete the creation of stream1. | |
| 179 std::string stream_label1 = std::string("stream1"); | |
| 180 dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerated( | |
| 181 kRouteId, ipc_request_id1, stream_label1, | |
| 182 audio_device_array, video_device_array)); | |
| 183 EXPECT_EQ(handler->request_id_, kRequestId1); | |
| 184 EXPECT_EQ(handler->label_, stream_label1); | |
| 185 | |
| 186 const media_stream::StreamDeviceInfoArray* device_array = | |
| 187 dispatcher->video_device_array(stream_label1); | |
| 188 EXPECT_EQ((*device_array)[0].session_id, kVideoSessionId); | |
| 189 EXPECT_EQ((*device_array)[0].name, video_device_info.name); | |
| 190 | |
| 191 // Test failure of the video device. | |
| 192 dispatcher->OnMessageReceived( | |
| 193 MediaStreamHostMsg_VideoDeviceFailed(kRouteId, stream_label1, 0)); | |
| 194 EXPECT_EQ(handler->video_failed, true); | |
| 195 EXPECT_EQ(handler->audio_failed, false); | |
| 196 // Make sure the audio device still exist but not the video device. | |
| 197 EXPECT_EQ(dispatcher->video_device_array(stream_label1)->size(), size_t(0)); | |
| 198 | |
| 199 EXPECT_EQ(dispatcher->audio_device_array(stream_label1)->size(), size_t(1)); | |
| 200 | |
| 201 // Test failure of the audio device. | |
| 202 dispatcher->OnMessageReceived( | |
| 203 MediaStreamHostMsg_AudioDeviceFailed(kRouteId, stream_label1, 0)); | |
| 204 EXPECT_EQ(handler->audio_failed, true); | |
| 205 // Make sure the audio device is removed. | |
| 206 EXPECT_EQ(dispatcher->audio_device_array(stream_label1)->size(), size_t(0)); | |
| 207 | |
| 208 // Stop stream1. | |
| 209 dispatcher->StopStream(stream_label1); | |
| 210 EXPECT_TRUE(dispatcher->audio_device_array(stream_label1) == NULL); | |
| 211 EXPECT_TRUE(dispatcher->video_device_array(stream_label1) == NULL); | |
| 212 EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0)); | |
| 213 } | |
| OLD | NEW |