NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / website_settings / permission_bubble_manager_unittest.cc
blobfcbc57b0301f687f18d647f1b11816b380156fb1
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"
15 namespace {
17 class MockRequest : public PermissionBubbleRequest {
18 public:
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 {
40 granted_ = true;
43 virtual void PermissionDenied() OVERRIDE {
44 granted_ = false;
47 virtual void Cancelled() OVERRIDE {
48 granted_ = false;
49 cancelled_ = true;
52 virtual void RequestFinished() OVERRIDE {
53 finished_ = true;
56 bool granted_;
57 bool cancelled_;
58 bool finished_;
61 class MockView : public PermissionBubbleView {
62 public:
63 MockView() : shown_(false), delegate_(NULL) {}
64 ~MockView() {}
66 void Clear() {
67 shown_ = false;
68 delegate_ = NULL;
69 permission_requests_.clear();
70 permission_states_.clear();
73 // PermissionBubbleView:
74 virtual void SetDelegate(Delegate* delegate) OVERRIDE {
75 delegate_ = delegate;
78 virtual void Show(
79 const std::vector<PermissionBubbleRequest*>& requests,
80 const std::vector<bool>& accept_state,
81 bool customization_state_) OVERRIDE {
82 shown_ = true;
83 permission_requests_ = requests;
84 permission_states_ = accept_state;
87 virtual void Hide() OVERRIDE {
88 shown_ = false;
91 bool shown_;
92 Delegate* delegate_;
93 std::vector<PermissionBubbleRequest*> permission_requests_;
94 std::vector<bool> permission_states_;
97 } // namespace
99 class PermissionBubbleManagerTest : public testing::Test {
100 public:
101 PermissionBubbleManagerTest();
103 void ToggleAccept(int index, bool value) {
104 manager_->ToggleAccept(index, value);
107 void Accept() {
108 manager_->Accept();
111 void WaitForCoalescing() {
112 base::MessageLoop::current()->RunUntilIdle();
115 protected:
116 MockRequest request1_;
117 MockRequest request2_;
118 MockView view_;
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_);
141 WaitForCoalescing();
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);
149 Accept();
150 EXPECT_TRUE(request1_.granted_);
153 TEST_F(PermissionBubbleManagerTest, SingleRequestViewFirst) {
154 manager_->SetView(&view_);
155 manager_->AddRequest(&request1_);
156 WaitForCoalescing();
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);
164 Accept();
165 EXPECT_TRUE(request1_.granted_);
168 TEST_F(PermissionBubbleManagerTest, TwoRequests) {
169 manager_->AddRequest(&request1_);
170 manager_->AddRequest(&request2_);
171 manager_->SetView(&view_);
172 WaitForCoalescing();
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);
182 Accept();
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_);
191 WaitForCoalescing();
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);
205 view_.Clear();
207 manager_->SetView(&view_);
208 WaitForCoalescing();
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]);
216 Accept();
217 EXPECT_TRUE(request1_.granted_);
218 EXPECT_FALSE(request2_.granted_);
221 TEST_F(PermissionBubbleManagerTest, NoRequests) {
222 manager_->SetView(&view_);
223 WaitForCoalescing();
224 EXPECT_FALSE(view_.shown_);
227 TEST_F(PermissionBubbleManagerTest, NoView) {
228 manager_->AddRequest(&request1_);
229 WaitForCoalescing();
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_);
238 WaitForCoalescing();
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_);
247 WaitForCoalescing();
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_);
257 WaitForCoalescing();
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]);
264 view_.Hide();
265 Accept();
266 EXPECT_FALSE(view_.shown_);
268 WaitForCoalescing();
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_);
280 WaitForCoalescing();
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]);