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/strings/utf_string_conversions.h"
6 #include "chrome/browser/ui/website_settings/permission_bubble_delegate.h"
7 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
8 #include "chrome/browser/ui/website_settings/permission_bubble_view.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 class MockDelegate
: public PermissionBubbleDelegate
{
15 MockDelegate() : granted_(false), cancelled_(false), finished_(false) {}
16 virtual ~MockDelegate() {}
18 // PermissionBubbleDelegate:
19 virtual int GetIconID() const OVERRIDE
{
23 virtual base::string16
GetMessageText() const OVERRIDE
{
24 return base::ASCIIToUTF16("test");
27 virtual base::string16
GetAlternateAcceptButtonText() const OVERRIDE
{
28 return base::ASCIIToUTF16("button");
31 virtual void PermissionGranted() OVERRIDE
{
35 virtual void PermissionDenied() OVERRIDE
{
39 virtual void Cancelled() OVERRIDE
{
44 virtual void RequestFinished() OVERRIDE
{
53 class MockView
: public PermissionBubbleView
{
55 MockView() : shown_(false), delegate_(NULL
) {}
61 permission_delegates_
.clear();
62 permission_states_
.clear();
65 // PermissionBubbleView:
66 virtual void SetDelegate(Delegate
* delegate
) OVERRIDE
{
70 virtual void AddPermissionBubbleDelegate(
71 PermissionBubbleDelegate
* delegate
) OVERRIDE
{}
73 virtual void RemovePermissionBubbleDelegate(
74 PermissionBubbleDelegate
* delegate
) OVERRIDE
{}
77 const std::vector
<PermissionBubbleDelegate
*>& delegates
,
78 const std::vector
<bool>& accept_state
) OVERRIDE
{
80 permission_delegates_
= delegates
;
81 permission_states_
= accept_state
;
84 virtual void Hide() OVERRIDE
{
90 std::vector
<PermissionBubbleDelegate
*> permission_delegates_
;
91 std::vector
<bool> permission_states_
;
96 class PermissionBubbleManagerTest
: public testing::Test
{
98 PermissionBubbleManagerTest();
100 void ToggleAccept(int index
, bool value
) {
101 manager_
->ToggleAccept(index
, value
);
109 MockDelegate delegate1_
;
110 MockDelegate delegate2_
;
112 scoped_ptr
<PermissionBubbleManager
> manager_
;
115 PermissionBubbleManagerTest::PermissionBubbleManagerTest()
116 : manager_(new PermissionBubbleManager(NULL
)) {
119 TEST_F(PermissionBubbleManagerTest
, SingleRequest
) {
120 manager_
->AddPermissionBubbleDelegate(&delegate1_
);
121 manager_
->SetView(&view_
);
123 EXPECT_TRUE(view_
.delegate_
== manager_
.get());
124 EXPECT_TRUE(view_
.shown_
);
125 ASSERT_EQ(static_cast<size_t>(1), view_
.permission_delegates_
.size());
126 EXPECT_EQ(&delegate1_
, view_
.permission_delegates_
[0]);
128 ToggleAccept(0, true);
130 EXPECT_TRUE(delegate1_
.granted_
);
133 TEST_F(PermissionBubbleManagerTest
, TwoRequests
) {
134 manager_
->AddPermissionBubbleDelegate(&delegate1_
);
135 manager_
->AddPermissionBubbleDelegate(&delegate2_
);
136 manager_
->SetView(&view_
);
138 EXPECT_TRUE(view_
.delegate_
== manager_
.get());
139 EXPECT_TRUE(view_
.shown_
);
140 ASSERT_EQ(static_cast<size_t>(2), view_
.permission_delegates_
.size());
141 EXPECT_EQ(&delegate1_
, view_
.permission_delegates_
[0]);
142 EXPECT_EQ(&delegate2_
, view_
.permission_delegates_
[1]);
144 ToggleAccept(0, true);
145 ToggleAccept(1, false);
147 EXPECT_TRUE(delegate1_
.granted_
);
148 EXPECT_FALSE(delegate2_
.granted_
);
151 TEST_F(PermissionBubbleManagerTest
, TwoRequestsTabSwitch
) {
152 manager_
->AddPermissionBubbleDelegate(&delegate1_
);
153 manager_
->AddPermissionBubbleDelegate(&delegate2_
);
154 manager_
->SetView(&view_
);
156 EXPECT_TRUE(view_
.delegate_
== manager_
.get());
157 EXPECT_TRUE(view_
.shown_
);
158 ASSERT_EQ(static_cast<size_t>(2), view_
.permission_delegates_
.size());
159 EXPECT_EQ(&delegate1_
, view_
.permission_delegates_
[0]);
160 EXPECT_EQ(&delegate2_
, view_
.permission_delegates_
[1]);
162 ToggleAccept(0, true);
163 ToggleAccept(1, false);
165 manager_
->SetView(NULL
);
166 EXPECT_FALSE(view_
.shown_
);
167 EXPECT_TRUE(view_
.delegate_
== NULL
);
170 manager_
->SetView(&view_
);
171 EXPECT_TRUE(view_
.shown_
);
172 ASSERT_EQ(static_cast<size_t>(2), view_
.permission_delegates_
.size());
173 EXPECT_EQ(&delegate1_
, view_
.permission_delegates_
[0]);
174 EXPECT_EQ(&delegate2_
, view_
.permission_delegates_
[1]);
175 EXPECT_TRUE(view_
.permission_states_
[0]);
176 EXPECT_FALSE(view_
.permission_states_
[1]);
179 EXPECT_TRUE(delegate1_
.granted_
);
180 EXPECT_FALSE(delegate2_
.granted_
);