- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / media / media_stream_ui_controller_unittest.cc
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.
4
5 #include <string>
6
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"
16
17 using testing::_;
18
19 namespace content {
20
21 class MediaStreamDeviceUIControllerTest
22     : public ::testing::Test,
23       public SettingsRequester {
24  public:
25   MediaStreamDeviceUIControllerTest() {}
26
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",
37                                          0,
38                                          0));
39     devices->push_back(MediaStreamDevice(MEDIA_DEVICE_VIDEO_CAPTURE,
40                                          "camera",
41                                          "camera_id"));
42   }
43
44  protected:
45   virtual void SetUp() {
46     message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO));
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));
52   }
53
54   virtual void TearDown() {
55     message_loop_->RunUntilIdle();
56   }
57
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(
62         audio ? MEDIA_DEVICE_AUDIO_CAPTURE : MEDIA_NO_SERVICE,
63         video ? MEDIA_DEVICE_VIDEO_CAPTURE : MEDIA_NO_SERVICE);
64     GURL security_origin;
65     ui_controller_->MakeUIRequest(label,
66                                   dummy_render_process_id,
67                                   dummy_render_view_id,
68                                   components,
69                                   security_origin,
70                                   MEDIA_GENERATE_STREAM,
71                                   std::string());
72   }
73
74   scoped_ptr<base::MessageLoop> message_loop_;
75   scoped_ptr<BrowserThreadImpl> ui_thread_;
76   scoped_ptr<BrowserThreadImpl> io_thread_;
77   scoped_ptr<MediaStreamUIController> ui_controller_;
78
79  private:
80   DISALLOW_COPY_AND_ASSIGN(MediaStreamDeviceUIControllerTest);
81 };
82
83 TEST_F(MediaStreamDeviceUIControllerTest, GenerateRequest) {
84   const std::string label = "dummy_label";
85   CreateDummyRequest(label, true, false);
86
87   // Expecting an error callback triggered by the non-existing
88   // RenderViewHostImpl.
89   EXPECT_CALL(*this, SettingsError(label));
90 }
91
92 TEST_F(MediaStreamDeviceUIControllerTest, GenerateAndRemoveRequest) {
93   const std::string label = "label";
94   CreateDummyRequest(label, true, false);
95
96   // Remove the current request, it should not crash.
97   ui_controller_->CancelUIRequest(label);
98 }
99
100 TEST_F(MediaStreamDeviceUIControllerTest, HandleRequestUsingFakeUI) {
101   ui_controller_->UseFakeUI(scoped_ptr<MediaStreamUI>());
102
103   const std::string label = "label";
104   CreateDummyRequest(label, true, true);
105
106   // Remove the current request, it should not crash.
107   EXPECT_CALL(*this, DevicesAccepted(label, _));
108
109   message_loop_->RunUntilIdle();
110
111   ui_controller_->NotifyUIIndicatorDevicesClosed(label);
112 }
113
114 TEST_F(MediaStreamDeviceUIControllerTest, CreateRequestsAndCancelTheFirst) {
115   ui_controller_->UseFakeUI(scoped_ptr<MediaStreamUI>());
116
117   // Create the first audio request.
118   const std::string label_1 = "label_1";
119   CreateDummyRequest(label_1, true, false);
120
121   // Create the second video request.
122   const std::string label_2 = "label_2";
123   CreateDummyRequest(label_2, false, true);
124
125   // Create the third audio and video request.
126   const std::string label_3 = "label_3";
127   CreateDummyRequest(label_3, true, true);
128
129   // Remove the first request which has been brought to the UI.
130   ui_controller_->CancelUIRequest(label_1);
131
132   // We should get callbacks from the rest of the requests.
133   EXPECT_CALL(*this, DevicesAccepted(label_2, _));
134   EXPECT_CALL(*this, DevicesAccepted(label_3, _));
135
136   message_loop_->RunUntilIdle();
137
138   ui_controller_->NotifyUIIndicatorDevicesClosed(label_2);
139   ui_controller_->NotifyUIIndicatorDevicesClosed(label_3);
140 }
141
142 TEST_F(MediaStreamDeviceUIControllerTest, CreateRequestsAndCancelTheLast) {
143   ui_controller_->UseFakeUI(scoped_ptr<MediaStreamUI>());
144
145   // Create the first audio request.
146   const std::string label_1 = "label_1";
147   CreateDummyRequest(label_1, true, false);
148
149   // Create the second video request.
150   const std::string label_2 = "label_2";
151   CreateDummyRequest(label_2, false, true);
152
153   // Create the third audio and video request.
154   const std::string label_3 = "label_3";
155   CreateDummyRequest(label_3, true, true);
156
157   // Remove the last request which is pending in the queue.
158   ui_controller_->CancelUIRequest(label_3);
159
160   // We should get callbacks from the rest of the requests.
161   EXPECT_CALL(*this, DevicesAccepted(label_1, _));
162   EXPECT_CALL(*this, DevicesAccepted(label_2, _));
163
164   message_loop_->RunUntilIdle();
165
166   ui_controller_->NotifyUIIndicatorDevicesClosed(label_1);
167   ui_controller_->NotifyUIIndicatorDevicesClosed(label_2);
168 }
169
170 }  // namespace content