| 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) {} | 42 : request_id_(-1), did_receive_devices_changed_(false) {} |
| 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 |
| 92 void ResetStoredParameters() { | 94 void ResetStoredParameters() { |
| 93 request_id_ = -1; | 95 request_id_ = -1; |
| 94 label_ = ""; | 96 label_ = ""; |
| 95 device_stopped_label_ = ""; | 97 device_stopped_label_ = ""; |
| 96 audio_device_ = StreamDeviceInfo(); | 98 audio_device_ = StreamDeviceInfo(); |
| 97 video_device_ = StreamDeviceInfo(); | 99 video_device_ = StreamDeviceInfo(); |
| 98 } | 100 } |
| 99 | 101 |
| 100 int request_id_; | 102 int request_id_; |
| 101 std::string label_; | 103 std::string label_; |
| 102 std::string device_stopped_label_; | 104 std::string device_stopped_label_; |
| 103 StreamDeviceInfo audio_device_; | 105 StreamDeviceInfo audio_device_; |
| 104 StreamDeviceInfo video_device_; | 106 StreamDeviceInfo video_device_; |
| 107 bool did_receive_devices_changed_; |
| 105 }; | 108 }; |
| 106 | 109 |
| 107 class MediaStreamDispatcherUnderTest : public MediaStreamDispatcher { | 110 class MediaStreamDispatcherUnderTest : public MediaStreamDispatcher { |
| 108 public: | 111 public: |
| 109 MediaStreamDispatcherUnderTest() : MediaStreamDispatcher(NULL) {} | 112 MediaStreamDispatcherUnderTest() : MediaStreamDispatcher(NULL) {} |
| 110 | 113 |
| 111 using MediaStreamDispatcher::GetNextIpcIdForTest; | 114 using MediaStreamDispatcher::GetNextIpcIdForTest; |
| 112 using RenderFrameObserver::OnMessageReceived; | 115 using RenderFrameObserver::OnMessageReceived; |
| 113 }; | 116 }; |
| 114 | 117 |
| 115 class MediaStreamDispatcherTest : public ::testing::Test { | 118 class MediaStreamDispatcherTest : public ::testing::Test { |
| 116 public: | 119 public: |
| 117 MediaStreamDispatcherTest() | 120 MediaStreamDispatcherTest() |
| 118 : dispatcher_(new MediaStreamDispatcherUnderTest()), | 121 : dispatcher_(new MediaStreamDispatcherUnderTest()), |
| 119 handler_(new MockMediaStreamDispatcherEventHandler), | 122 handler_(new MockMediaStreamDispatcherEventHandler), |
| 120 security_origin_(url::Origin(GURL("http://test.com"))) {} | 123 security_origin_(GURL("http://test.com")) {} |
| 121 | 124 |
| 122 // Generates a request for a MediaStream and returns the request id that is | 125 // Generates a request for a MediaStream and returns the request id that is |
| 123 // used in IPC. Use this returned id in CompleteGenerateStream to identify | 126 // used in IPC. Use this returned id in CompleteGenerateStream to identify |
| 124 // the request. | 127 // the request. |
| 125 int GenerateStream(const StreamControls& controls, int request_id) { | 128 int GenerateStream(const StreamControls& controls, int request_id) { |
| 126 int next_ipc_id = dispatcher_->GetNextIpcIdForTest(); | 129 int next_ipc_id = dispatcher_->GetNextIpcIdForTest(); |
| 127 dispatcher_->GenerateStream(request_id, handler_.get()->AsWeakPtr(), | 130 dispatcher_->GenerateStream(request_id, handler_.get()->AsWeakPtr(), |
| 128 controls, security_origin_); | 131 controls, security_origin_); |
| 129 return next_ipc_id; | 132 return next_ipc_id; |
| 130 } | 133 } |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 | 406 |
| 404 dispatcher_->OnMessageReceived( | 407 dispatcher_->OnMessageReceived( |
| 405 MediaStreamMsg_DeviceStopped(kRouteId, label, handler_->video_device_)); | 408 MediaStreamMsg_DeviceStopped(kRouteId, label, handler_->video_device_)); |
| 406 // Verify that MediaStreamDispatcherEventHandler::OnDeviceStopped has been | 409 // Verify that MediaStreamDispatcherEventHandler::OnDeviceStopped has been |
| 407 // called. | 410 // called. |
| 408 EXPECT_EQ(label, handler_->device_stopped_label_); | 411 EXPECT_EQ(label, handler_->device_stopped_label_); |
| 409 EXPECT_EQ(dispatcher_->video_session_id(label, 0), | 412 EXPECT_EQ(dispatcher_->video_session_id(label, 0), |
| 410 StreamDeviceInfo::kNoId); | 413 StreamDeviceInfo::kNoId); |
| 411 } | 414 } |
| 412 | 415 |
| 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 |
| 413 } // namespace content | 440 } // namespace content |
| OLD | NEW |