1 // Copyright 2014 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 "base/command_line.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
9 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
10 #include "chrome/browser/ui/website_settings/permission_bubble_view.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "content/public/test/test_browser_thread.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 class MockRequest
: public PermissionBubbleRequest
{
19 MockRequest() : granted_(false), cancelled_(false), finished_(false) {}
20 virtual ~MockRequest() {}
22 // PermissionBubbleRequest:
23 virtual base::string16
GetMessageText() const OVERRIDE
{
24 return base::ASCIIToUTF16("test");
27 virtual base::string16
GetMessageTextFragment() const OVERRIDE
{
28 return base::ASCIIToUTF16("test");
31 virtual base::string16
GetAlternateAcceptButtonText() const OVERRIDE
{
32 return base::ASCIIToUTF16("button");
35 virtual base::string16
GetAlternateDenyButtonText() const OVERRIDE
{
36 return base::ASCIIToUTF16("button");
39 virtual void PermissionGranted() OVERRIDE
{
43 virtual void PermissionDenied() OVERRIDE
{
47 virtual void Cancelled() OVERRIDE
{
52 virtual void RequestFinished() OVERRIDE
{
61 class MockView
: public PermissionBubbleView
{
63 MockView() : shown_(false), delegate_(NULL
) {}
69 permission_requests_
.clear();
70 permission_states_
.clear();
73 // PermissionBubbleView:
74 virtual void SetDelegate(Delegate
* delegate
) OVERRIDE
{
79 const std::vector
<PermissionBubbleRequest
*>& requests
,
80 const std::vector
<bool>& accept_state
,
81 bool customization_state_
) OVERRIDE
{
83 permission_requests_
= requests
;
84 permission_states_
= accept_state
;
87 virtual void Hide() OVERRIDE
{
93 std::vector
<PermissionBubbleRequest
*> permission_requests_
;
94 std::vector
<bool> permission_states_
;
99 class PermissionBubbleManagerTest
: public testing::Test
{
101 PermissionBubbleManagerTest();
103 void ToggleAccept(int index
, bool value
) {
104 manager_
->ToggleAccept(index
, value
);
111 void WaitForCoalescing() {
112 base::MessageLoop::current()->RunUntilIdle();
116 MockRequest request1_
;
117 MockRequest request2_
;
119 scoped_ptr
<PermissionBubbleManager
> manager_
;
121 base::MessageLoop message_loop_
;
122 content::TestBrowserThread ui_thread_
;
125 PermissionBubbleManagerTest::PermissionBubbleManagerTest()
126 : manager_(new PermissionBubbleManager(NULL
)),
127 ui_thread_(content::BrowserThread::UI
, &message_loop_
) {
128 manager_
->SetCoalesceIntervalForTesting(0);
131 TEST_F(PermissionBubbleManagerTest
, TestFlag
) {
132 EXPECT_FALSE(PermissionBubbleManager::Enabled());
133 CommandLine::ForCurrentProcess()->AppendSwitch(
134 switches::kEnablePermissionsBubbles
);
135 EXPECT_TRUE(PermissionBubbleManager::Enabled());
138 TEST_F(PermissionBubbleManagerTest
, SingleRequest
) {
139 manager_
->AddRequest(&request1_
);
140 manager_
->SetView(&view_
);
143 EXPECT_TRUE(view_
.delegate_
== manager_
.get());
144 EXPECT_TRUE(view_
.shown_
);
145 ASSERT_EQ(static_cast<size_t>(1), view_
.permission_requests_
.size());
146 EXPECT_EQ(&request1_
, view_
.permission_requests_
[0]);
148 ToggleAccept(0, true);
150 EXPECT_TRUE(request1_
.granted_
);
153 TEST_F(PermissionBubbleManagerTest
, SingleRequestViewFirst
) {
154 manager_
->SetView(&view_
);
155 manager_
->AddRequest(&request1_
);
158 EXPECT_TRUE(view_
.delegate_
== manager_
.get());
159 EXPECT_TRUE(view_
.shown_
);
160 ASSERT_EQ(static_cast<size_t>(1), view_
.permission_requests_
.size());
161 EXPECT_EQ(&request1_
, view_
.permission_requests_
[0]);
163 ToggleAccept(0, true);
165 EXPECT_TRUE(request1_
.granted_
);
168 TEST_F(PermissionBubbleManagerTest
, TwoRequests
) {
169 manager_
->AddRequest(&request1_
);
170 manager_
->AddRequest(&request2_
);
171 manager_
->SetView(&view_
);
174 EXPECT_TRUE(view_
.delegate_
== manager_
.get());
175 EXPECT_TRUE(view_
.shown_
);
176 ASSERT_EQ(static_cast<size_t>(2), view_
.permission_requests_
.size());
177 EXPECT_EQ(&request1_
, view_
.permission_requests_
[0]);
178 EXPECT_EQ(&request2_
, view_
.permission_requests_
[1]);
180 ToggleAccept(0, true);
181 ToggleAccept(1, false);
183 EXPECT_TRUE(request1_
.granted_
);
184 EXPECT_FALSE(request2_
.granted_
);
187 TEST_F(PermissionBubbleManagerTest
, TwoRequestsTabSwitch
) {
188 manager_
->AddRequest(&request1_
);
189 manager_
->AddRequest(&request2_
);
190 manager_
->SetView(&view_
);
193 EXPECT_TRUE(view_
.delegate_
== manager_
.get());
194 EXPECT_TRUE(view_
.shown_
);
195 ASSERT_EQ(static_cast<size_t>(2), view_
.permission_requests_
.size());
196 EXPECT_EQ(&request1_
, view_
.permission_requests_
[0]);
197 EXPECT_EQ(&request2_
, view_
.permission_requests_
[1]);
199 ToggleAccept(0, true);
200 ToggleAccept(1, false);
202 manager_
->SetView(NULL
);
203 EXPECT_FALSE(view_
.shown_
);
204 EXPECT_TRUE(view_
.delegate_
== NULL
);
207 manager_
->SetView(&view_
);
209 EXPECT_TRUE(view_
.shown_
);
210 ASSERT_EQ(static_cast<size_t>(2), view_
.permission_requests_
.size());
211 EXPECT_EQ(&request1_
, view_
.permission_requests_
[0]);
212 EXPECT_EQ(&request2_
, view_
.permission_requests_
[1]);
213 EXPECT_TRUE(view_
.permission_states_
[0]);
214 EXPECT_FALSE(view_
.permission_states_
[1]);
217 EXPECT_TRUE(request1_
.granted_
);
218 EXPECT_FALSE(request2_
.granted_
);
221 TEST_F(PermissionBubbleManagerTest
, NoRequests
) {
222 manager_
->SetView(&view_
);
224 EXPECT_FALSE(view_
.shown_
);
227 TEST_F(PermissionBubbleManagerTest
, NoView
) {
228 manager_
->AddRequest(&request1_
);
230 EXPECT_FALSE(view_
.shown_
);
233 TEST_F(PermissionBubbleManagerTest
, TwoRequestsCoalesce
) {
234 manager_
->SetView(&view_
);
235 manager_
->AddRequest(&request1_
);
236 manager_
->AddRequest(&request2_
);
237 EXPECT_FALSE(view_
.shown_
);
240 EXPECT_TRUE(view_
.shown_
);
241 EXPECT_EQ(2u, view_
.permission_requests_
.size());
244 TEST_F(PermissionBubbleManagerTest
, TwoRequestsDoNotCoalesce
) {
245 manager_
->SetView(&view_
);
246 manager_
->AddRequest(&request1_
);
248 manager_
->AddRequest(&request2_
);
250 EXPECT_TRUE(view_
.shown_
);
251 EXPECT_EQ(1u, view_
.permission_requests_
.size());
254 TEST_F(PermissionBubbleManagerTest
, TwoRequestsShownInTwoBubbles
) {
255 manager_
->SetView(&view_
);
256 manager_
->AddRequest(&request1_
);
258 manager_
->AddRequest(&request2_
);
260 EXPECT_TRUE(view_
.shown_
);
261 ASSERT_EQ(1u, view_
.permission_requests_
.size());
262 EXPECT_EQ(&request1_
, view_
.permission_requests_
[0]);
266 EXPECT_FALSE(view_
.shown_
);
269 EXPECT_TRUE(view_
.shown_
);
270 ASSERT_EQ(1u, view_
.permission_requests_
.size());
271 EXPECT_EQ(&request2_
, view_
.permission_requests_
[0]);
274 TEST_F(PermissionBubbleManagerTest
, TestAddDuplicateRequest
) {
275 manager_
->SetView(&view_
);
276 manager_
->AddRequest(&request1_
);
277 manager_
->AddRequest(&request2_
);
278 manager_
->AddRequest(&request1_
);
281 EXPECT_TRUE(view_
.shown_
);
282 ASSERT_EQ(2u, view_
.permission_requests_
.size());
283 EXPECT_EQ(&request1_
, view_
.permission_requests_
[0]);
284 EXPECT_EQ(&request2_
, view_
.permission_requests_
[1]);