Update To 11.40.268.0
[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::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));
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(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());
70   }
71
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_;
76
77  private:
78   DISALLOW_COPY_AND_ASSIGN(MediaStreamDeviceUIControllerTest);
79 };
80
81 TEST_F(MediaStreamDeviceUIControllerTest, GenerateRequest) {
82   const std::string label = "dummy_label";
83   CreateDummyRequest(label, true, false);
84
85   // Expecting an error callback triggered by the non-existing
86   // RenderViewHostImpl.
87   EXPECT_CALL(*this, SettingsError(label));
88 }
89
90 TEST_F(MediaStreamDeviceUIControllerTest, GenerateAndRemoveRequest) {
91   const std::string label = "label";
92   CreateDummyRequest(label, true, false);
93
94   // Remove the current request, it should not crash.
95   ui_controller_->CancelUIRequest(label);
96 }
97
98 TEST_F(MediaStreamDeviceUIControllerTest, HandleRequestUsingFakeUI) {
99   ui_controller_->UseFakeUI(scoped_ptr<MediaStreamUI>());
100
101   const std::string label = "label";
102   CreateDummyRequest(label, true, true);
103
104   // Remove the current request, it should not crash.
105   EXPECT_CALL(*this, DevicesAccepted(label, _));
106
107   message_loop_->RunUntilIdle();
108
109   ui_controller_->NotifyUIIndicatorDevicesClosed(label);
110 }
111
112 TEST_F(MediaStreamDeviceUIControllerTest, CreateRequestsAndCancelTheFirst) {
113   ui_controller_->UseFakeUI(scoped_ptr<MediaStreamUI>());
114
115   // Create the first audio request.
116   const std::string label_1 = "label_1";
117   CreateDummyRequest(label_1, true, false);
118
119   // Create the second video request.
120   const std::string label_2 = "label_2";
121   CreateDummyRequest(label_2, false, true);
122
123   // Create the third audio and video request.
124   const std::string label_3 = "label_3";
125   CreateDummyRequest(label_3, true, true);
126
127   // Remove the first request which has been brought to the UI.
128   ui_controller_->CancelUIRequest(label_1);
129
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, _));
133
134   message_loop_->RunUntilIdle();
135
136   ui_controller_->NotifyUIIndicatorDevicesClosed(label_2);
137   ui_controller_->NotifyUIIndicatorDevicesClosed(label_3);
138 }
139
140 TEST_F(MediaStreamDeviceUIControllerTest, CreateRequestsAndCancelTheLast) {
141   ui_controller_->UseFakeUI(scoped_ptr<MediaStreamUI>());
142
143   // Create the first audio request.
144   const std::string label_1 = "label_1";
145   CreateDummyRequest(label_1, true, false);
146
147   // Create the second video request.
148   const std::string label_2 = "label_2";
149   CreateDummyRequest(label_2, false, true);
150
151   // Create the third audio and video request.
152   const std::string label_3 = "label_3";
153   CreateDummyRequest(label_3, true, true);
154
155   // Remove the last request which is pending in the queue.
156   ui_controller_->CancelUIRequest(label_3);
157
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, _));
161
162   message_loop_->RunUntilIdle();
163
164   ui_controller_->NotifyUIIndicatorDevicesClosed(label_1);
165   ui_controller_->NotifyUIIndicatorDevicesClosed(label_2);
166 }
167
168 }  // namespace content