Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / website_settings / permission_bubble_manager_unittest.cc
blob4ac8c77985c6f285f0397993f7bf247dd24da8cb
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"
11 namespace {
13 class MockDelegate : public PermissionBubbleDelegate {
14 public:
15 MockDelegate() : granted_(false), cancelled_(false), finished_(false) {}
16 virtual ~MockDelegate() {}
18 // PermissionBubbleDelegate:
19 virtual int GetIconID() const OVERRIDE {
20 return 11;
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 {
32 granted_ = true;
35 virtual void PermissionDenied() OVERRIDE {
36 granted_ = false;
39 virtual void Cancelled() OVERRIDE {
40 granted_ = false;
41 cancelled_ = true;
44 virtual void RequestFinished() OVERRIDE {
45 finished_ = true;
48 bool granted_;
49 bool cancelled_;
50 bool finished_;
53 class MockView : public PermissionBubbleView {
54 public:
55 MockView() : shown_(false), delegate_(NULL) {}
56 ~MockView() {}
58 void Clear() {
59 shown_ = false;
60 delegate_ = NULL;
61 permission_delegates_.clear();
62 permission_states_.clear();
65 // PermissionBubbleView:
66 virtual void SetDelegate(Delegate* delegate) OVERRIDE {
67 delegate_ = delegate;
70 virtual void AddPermissionBubbleDelegate(
71 PermissionBubbleDelegate* delegate) OVERRIDE {}
73 virtual void RemovePermissionBubbleDelegate(
74 PermissionBubbleDelegate* delegate) OVERRIDE {}
76 virtual void Show(
77 const std::vector<PermissionBubbleDelegate*>& delegates,
78 const std::vector<bool>& accept_state) OVERRIDE {
79 shown_ = true;
80 permission_delegates_ = delegates;
81 permission_states_ = accept_state;
84 virtual void Hide() OVERRIDE {
85 shown_ = false;
88 bool shown_;
89 Delegate* delegate_;
90 std::vector<PermissionBubbleDelegate*> permission_delegates_;
91 std::vector<bool> permission_states_;
94 } // namespace
96 class PermissionBubbleManagerTest : public testing::Test {
97 public:
98 PermissionBubbleManagerTest();
100 void ToggleAccept(int index, bool value) {
101 manager_->ToggleAccept(index, value);
104 void Accept() {
105 manager_->Accept();
108 protected:
109 MockDelegate delegate1_;
110 MockDelegate delegate2_;
111 MockView view_;
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);
129 Accept();
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);
146 Accept();
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);
168 view_.Clear();
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]);
178 Accept();
179 EXPECT_TRUE(delegate1_.granted_);
180 EXPECT_FALSE(delegate2_.granted_);