| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/media_stream_dispatcher.h" | 5 #include "content/renderer/media/media_stream_dispatcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 const int kRequestId4 = 40; | 32 const int kRequestId4 = 40; |
| 33 | 33 |
| 34 const MediaStreamType kAudioType = MEDIA_DEVICE_AUDIO_CAPTURE; | 34 const MediaStreamType kAudioType = MEDIA_DEVICE_AUDIO_CAPTURE; |
| 35 const MediaStreamType kVideoType = MEDIA_DEVICE_VIDEO_CAPTURE; | 35 const MediaStreamType kVideoType = MEDIA_DEVICE_VIDEO_CAPTURE; |
| 36 | 36 |
| 37 class MockMediaStreamDispatcherEventHandler | 37 class MockMediaStreamDispatcherEventHandler |
| 38 : public MediaStreamDispatcherEventHandler, | 38 : public MediaStreamDispatcherEventHandler, |
| 39 public base::SupportsWeakPtr<MockMediaStreamDispatcherEventHandler> { | 39 public base::SupportsWeakPtr<MockMediaStreamDispatcherEventHandler> { |
| 40 public: | 40 public: |
| 41 MockMediaStreamDispatcherEventHandler() | 41 MockMediaStreamDispatcherEventHandler() |
| 42 : request_id_(-1), did_receive_devices_changed_(false) {} | 42 : request_id_(-1) {} |
| 43 | 43 |
| 44 void OnStreamGenerated( | 44 void OnStreamGenerated( |
| 45 int request_id, | 45 int request_id, |
| 46 const std::string& label, | 46 const std::string& label, |
| 47 const StreamDeviceInfoArray& audio_device_array, | 47 const StreamDeviceInfoArray& audio_device_array, |
| 48 const StreamDeviceInfoArray& video_device_array) override { | 48 const StreamDeviceInfoArray& video_device_array) override { |
| 49 request_id_ = request_id; | 49 request_id_ = request_id; |
| 50 label_ = label; | 50 label_ = label; |
| 51 if (audio_device_array.size()) { | 51 if (audio_device_array.size()) { |
| 52 DCHECK(audio_device_array.size() == 1); | 52 DCHECK(audio_device_array.size() == 1); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 82 | 82 |
| 83 void OnDeviceOpened(int request_id, | 83 void OnDeviceOpened(int request_id, |
| 84 const std::string& label, | 84 const std::string& label, |
| 85 const StreamDeviceInfo& video_device) override { | 85 const StreamDeviceInfo& video_device) override { |
| 86 request_id_ = request_id; | 86 request_id_ = request_id; |
| 87 label_ = label; | 87 label_ = label; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void OnDeviceOpenFailed(int request_id) override { request_id_ = request_id; } | 90 void OnDeviceOpenFailed(int request_id) override { request_id_ = request_id; } |
| 91 | 91 |
| 92 void OnDevicesChanged() override { did_receive_devices_changed_ = true; } | |
| 93 | |
| 94 void ResetStoredParameters() { | 92 void ResetStoredParameters() { |
| 95 request_id_ = -1; | 93 request_id_ = -1; |
| 96 label_ = ""; | 94 label_ = ""; |
| 97 device_stopped_label_ = ""; | 95 device_stopped_label_ = ""; |
| 98 audio_device_ = StreamDeviceInfo(); | 96 audio_device_ = StreamDeviceInfo(); |
| 99 video_device_ = StreamDeviceInfo(); | 97 video_device_ = StreamDeviceInfo(); |
| 100 } | 98 } |
| 101 | 99 |
| 102 int request_id_; | 100 int request_id_; |
| 103 std::string label_; | 101 std::string label_; |
| 104 std::string device_stopped_label_; | 102 std::string device_stopped_label_; |
| 105 StreamDeviceInfo audio_device_; | 103 StreamDeviceInfo audio_device_; |
| 106 StreamDeviceInfo video_device_; | 104 StreamDeviceInfo video_device_; |
| 107 bool did_receive_devices_changed_; | |
| 108 }; | 105 }; |
| 109 | 106 |
| 110 class MediaStreamDispatcherUnderTest : public MediaStreamDispatcher { | 107 class MediaStreamDispatcherUnderTest : public MediaStreamDispatcher { |
| 111 public: | 108 public: |
| 112 MediaStreamDispatcherUnderTest() : MediaStreamDispatcher(NULL) {} | 109 MediaStreamDispatcherUnderTest() : MediaStreamDispatcher(NULL) {} |
| 113 | 110 |
| 114 using MediaStreamDispatcher::GetNextIpcIdForTest; | 111 using MediaStreamDispatcher::GetNextIpcIdForTest; |
| 115 using RenderFrameObserver::OnMessageReceived; | 112 using RenderFrameObserver::OnMessageReceived; |
| 116 }; | 113 }; |
| 117 | 114 |
| 118 class MediaStreamDispatcherTest : public ::testing::Test { | 115 class MediaStreamDispatcherTest : public ::testing::Test { |
| 119 public: | 116 public: |
| 120 MediaStreamDispatcherTest() | 117 MediaStreamDispatcherTest() |
| 121 : dispatcher_(new MediaStreamDispatcherUnderTest()), | 118 : dispatcher_(new MediaStreamDispatcherUnderTest()), |
| 122 handler_(new MockMediaStreamDispatcherEventHandler), | 119 handler_(new MockMediaStreamDispatcherEventHandler), |
| 123 security_origin_(GURL("http://test.com")) {} | 120 security_origin_(url::Origin(GURL("http://test.com"))) {} |
| 124 | 121 |
| 125 // Generates a request for a MediaStream and returns the request id that is | 122 // Generates a request for a MediaStream and returns the request id that is |
| 126 // used in IPC. Use this returned id in CompleteGenerateStream to identify | 123 // used in IPC. Use this returned id in CompleteGenerateStream to identify |
| 127 // the request. | 124 // the request. |
| 128 int GenerateStream(const StreamControls& controls, int request_id) { | 125 int GenerateStream(const StreamControls& controls, int request_id) { |
| 129 int next_ipc_id = dispatcher_->GetNextIpcIdForTest(); | 126 int next_ipc_id = dispatcher_->GetNextIpcIdForTest(); |
| 130 dispatcher_->GenerateStream(request_id, handler_.get()->AsWeakPtr(), | 127 dispatcher_->GenerateStream(request_id, handler_.get()->AsWeakPtr(), |
| 131 controls, security_origin_); | 128 controls, security_origin_); |
| 132 return next_ipc_id; | 129 return next_ipc_id; |
| 133 } | 130 } |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 | 403 |
| 407 dispatcher_->OnMessageReceived( | 404 dispatcher_->OnMessageReceived( |
| 408 MediaStreamMsg_DeviceStopped(kRouteId, label, handler_->video_device_)); | 405 MediaStreamMsg_DeviceStopped(kRouteId, label, handler_->video_device_)); |
| 409 // Verify that MediaStreamDispatcherEventHandler::OnDeviceStopped has been | 406 // Verify that MediaStreamDispatcherEventHandler::OnDeviceStopped has been |
| 410 // called. | 407 // called. |
| 411 EXPECT_EQ(label, handler_->device_stopped_label_); | 408 EXPECT_EQ(label, handler_->device_stopped_label_); |
| 412 EXPECT_EQ(dispatcher_->video_session_id(label, 0), | 409 EXPECT_EQ(dispatcher_->video_session_id(label, 0), |
| 413 StreamDeviceInfo::kNoId); | 410 StreamDeviceInfo::kNoId); |
| 414 } | 411 } |
| 415 | 412 |
| 416 // Test that the MediaStreamDispatcherEventHandler is notified when the message | |
| 417 // MediaStreamMsg_DevicesChanged is received. | |
| 418 TEST_F(MediaStreamDispatcherTest, DevicesChanged) { | |
| 419 std::unique_ptr<MockMediaStreamDispatcherEventHandler> handler1( | |
| 420 new MockMediaStreamDispatcherEventHandler); | |
| 421 std::unique_ptr<MockMediaStreamDispatcherEventHandler> handler2( | |
| 422 new MockMediaStreamDispatcherEventHandler); | |
| 423 dispatcher_->SubscribeToDeviceChangeNotifications(handler1->AsWeakPtr(), | |
| 424 security_origin_); | |
| 425 dispatcher_->SubscribeToDeviceChangeNotifications(handler2->AsWeakPtr(), | |
| 426 security_origin_); | |
| 427 EXPECT_DEATH(dispatcher_->SubscribeToDeviceChangeNotifications( | |
| 428 handler2->AsWeakPtr(), security_origin_), | |
| 429 ""); | |
| 430 dispatcher_->OnMessageReceived(MediaStreamMsg_DevicesChanged(kRouteId)); | |
| 431 dispatcher_->CancelDeviceChangeNotifications(handler1->AsWeakPtr()); | |
| 432 dispatcher_->CancelDeviceChangeNotifications(handler2->AsWeakPtr()); | |
| 433 | |
| 434 EXPECT_TRUE(handler1->did_receive_devices_changed_); | |
| 435 EXPECT_TRUE(handler2->did_receive_devices_changed_); | |
| 436 EXPECT_DEATH( | |
| 437 dispatcher_->CancelDeviceChangeNotifications(handler2->AsWeakPtr()), ""); | |
| 438 } | |
| 439 | |
| 440 } // namespace content | 413 } // namespace content |
| OLD | NEW |