[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / content / browser / renderer_host / media / media_stream_ui_controller_unittest.cc
blob990471f94bcc0af025ca2948f0309ac1c0bd535a
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
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/renderer_host/media/media_stream_settings_requester.h"
11 #include "content/browser/renderer_host/media/media_stream_ui_controller.h"
12 #include "content/common/media/media_stream_options.h"
13 #include "content/public/common/media_stream_request.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using testing::_;
19 namespace content {
21 class MediaStreamDeviceUIControllerTest
22 : public ::testing::Test,
23 public SettingsRequester {
24 public:
25 MediaStreamDeviceUIControllerTest() {}
27 // Mock implementation of SettingsRequester.
28 // TODO(sergeyu): Move mock SettingsRequester to a separate class.
29 MOCK_METHOD2(DevicesAccepted, void(
30 const std::string&, const StreamDeviceInfoArray&));
31 MOCK_METHOD1(SettingsError, void(const std::string&));
32 MOCK_METHOD1(StopStreamFromUI, void(const std::string&));
33 void GetAvailableDevices(MediaStreamDevices* devices) override {
34 devices->push_back(MediaStreamDevice(MEDIA_DEVICE_AUDIO_CAPTURE,
35 "mic",
36 "mic_id",
38 0));
39 devices->push_back(MediaStreamDevice(MEDIA_DEVICE_VIDEO_CAPTURE,
40 "camera",
41 "camera_id"));
44 protected:
45 virtual void SetUp() {
46 message_loop_.reset(new base::MessageLoopForIO);
47 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI,
48 message_loop_.get()));
49 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO,
50 message_loop_.get()));
51 ui_controller_.reset(new MediaStreamUIController(this));
54 virtual void TearDown() {
55 message_loop_->RunUntilIdle();
58 void CreateDummyRequest(const std::string& label, bool audio, bool video) {
59 int dummy_render_process_id = 1;
60 int dummy_render_view_id = 1;
61 StreamOptions components(audio, video );
62 GURL security_origin;
63 ui_controller_->MakeUIRequest(label,
64 dummy_render_process_id,
65 dummy_render_view_id,
66 components,
67 security_origin,
68 MEDIA_GENERATE_STREAM,
69 std::string());
72 scoped_ptr<base::MessageLoop> message_loop_;
73 scoped_ptr<BrowserThreadImpl> ui_thread_;
74 scoped_ptr<BrowserThreadImpl> io_thread_;
75 scoped_ptr<MediaStreamUIController> ui_controller_;
77 private:
78 DISALLOW_COPY_AND_ASSIGN(MediaStreamDeviceUIControllerTest);
81 TEST_F(MediaStreamDeviceUIControllerTest, GenerateRequest) {
82 const std::string label = "dummy_label";
83 CreateDummyRequest(label, true, false);
85 // Expecting an error callback triggered by the non-existing
86 // RenderViewHostImpl.
87 EXPECT_CALL(*this, SettingsError(label));
90 TEST_F(MediaStreamDeviceUIControllerTest, GenerateAndRemoveRequest) {
91 const std::string label = "label";
92 CreateDummyRequest(label, true, false);
94 // Remove the current request, it should not crash.
95 ui_controller_->CancelUIRequest(label);
98 TEST_F(MediaStreamDeviceUIControllerTest, HandleRequestUsingFakeUI) {
99 ui_controller_->UseFakeUI(scoped_ptr<MediaStreamUI>());
101 const std::string label = "label";
102 CreateDummyRequest(label, true, true);
104 // Remove the current request, it should not crash.
105 EXPECT_CALL(*this, DevicesAccepted(label, _));
107 message_loop_->RunUntilIdle();
109 ui_controller_->NotifyUIIndicatorDevicesClosed(label);
112 TEST_F(MediaStreamDeviceUIControllerTest, CreateRequestsAndCancelTheFirst) {
113 ui_controller_->UseFakeUI(scoped_ptr<MediaStreamUI>());
115 // Create the first audio request.
116 const std::string label_1 = "label_1";
117 CreateDummyRequest(label_1, true, false);
119 // Create the second video request.
120 const std::string label_2 = "label_2";
121 CreateDummyRequest(label_2, false, true);
123 // Create the third audio and video request.
124 const std::string label_3 = "label_3";
125 CreateDummyRequest(label_3, true, true);
127 // Remove the first request which has been brought to the UI.
128 ui_controller_->CancelUIRequest(label_1);
130 // We should get callbacks from the rest of the requests.
131 EXPECT_CALL(*this, DevicesAccepted(label_2, _));
132 EXPECT_CALL(*this, DevicesAccepted(label_3, _));
134 message_loop_->RunUntilIdle();
136 ui_controller_->NotifyUIIndicatorDevicesClosed(label_2);
137 ui_controller_->NotifyUIIndicatorDevicesClosed(label_3);
140 TEST_F(MediaStreamDeviceUIControllerTest, CreateRequestsAndCancelTheLast) {
141 ui_controller_->UseFakeUI(scoped_ptr<MediaStreamUI>());
143 // Create the first audio request.
144 const std::string label_1 = "label_1";
145 CreateDummyRequest(label_1, true, false);
147 // Create the second video request.
148 const std::string label_2 = "label_2";
149 CreateDummyRequest(label_2, false, true);
151 // Create the third audio and video request.
152 const std::string label_3 = "label_3";
153 CreateDummyRequest(label_3, true, true);
155 // Remove the last request which is pending in the queue.
156 ui_controller_->CancelUIRequest(label_3);
158 // We should get callbacks from the rest of the requests.
159 EXPECT_CALL(*this, DevicesAccepted(label_1, _));
160 EXPECT_CALL(*this, DevicesAccepted(label_2, _));
162 message_loop_->RunUntilIdle();
164 ui_controller_->NotifyUIIndicatorDevicesClosed(label_1);
165 ui_controller_->NotifyUIIndicatorDevicesClosed(label_2);
168 } // namespace content