Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / website_settings / permission_bubble_manager_unittest.cc
blob3bd40fbac2ebf007e7ca8e1450a1cc1b1121ab35
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/mock_permission_bubble_request.h"
9 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
10 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
11 #include "chrome/browser/ui/website_settings/permission_bubble_view.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace {
18 class MockView : public PermissionBubbleView {
19 public:
20 MockView() : shown_(false), can_accept_updates_(true), delegate_(NULL) {}
21 ~MockView() override {}
23 static scoped_ptr<PermissionBubbleView> Create(Browser* browser) {
24 return make_scoped_ptr(new MockView());
27 void Clear() {
28 shown_ = false;
29 can_accept_updates_ = true;
30 delegate_ = NULL;
31 permission_requests_.clear();
32 permission_states_.clear();
35 // PermissionBubbleView:
36 void SetDelegate(Delegate* delegate) override { delegate_ = delegate; }
38 void Show(const std::vector<PermissionBubbleRequest*>& requests,
39 const std::vector<bool>& accept_state) override {
40 shown_ = true;
41 permission_requests_ = requests;
42 permission_states_ = accept_state;
45 void Hide() override { shown_ = false; }
46 bool CanAcceptRequestUpdate() override { return can_accept_updates_; }
47 bool IsVisible() override { return shown_; }
48 void UpdateAnchorPosition() override {}
49 gfx::NativeWindow GetNativeWindow() override { return nullptr; }
51 bool shown_;
52 bool can_accept_updates_;
53 Delegate* delegate_;
54 std::vector<PermissionBubbleRequest*> permission_requests_;
55 std::vector<bool> permission_states_;
58 } // namespace
60 class PermissionBubbleManagerTest : public ChromeRenderViewHostTestHarness {
61 public:
62 PermissionBubbleManagerTest()
63 : ChromeRenderViewHostTestHarness(),
64 request1_("test1"),
65 request2_("test2"),
66 iframe_request_same_domain_("iframe",
67 GURL("http://www.google.com/some/url")),
68 iframe_request_other_domain_("iframe",
69 GURL("http://www.youtube.com")) {}
70 ~PermissionBubbleManagerTest() override {}
72 void SetUp() override {
73 ChromeRenderViewHostTestHarness::SetUp();
74 SetContents(CreateTestWebContents());
75 NavigateAndCommit(GURL("http://www.google.com"));
77 manager_.reset(new PermissionBubbleManager(web_contents()));
78 manager_->view_factory_ = base::Bind(MockView::Create);
81 void TearDown() override {
82 manager_.reset();
83 ChromeRenderViewHostTestHarness::TearDown();
86 void ToggleAccept(int index, bool value) {
87 manager_->ToggleAccept(index, value);
90 void Accept() {
91 manager_->Accept();
94 void Closing() {
95 manager_->Closing();
98 void WaitForFrameLoad() {
99 // PermissionBubbleManager ignores all parameters. Yay?
100 manager_->DocumentLoadedInFrame(NULL);
101 base::MessageLoop::current()->RunUntilIdle();
104 void WaitForCoalescing() {
105 manager_->DocumentOnLoadCompletedInMainFrame();
106 base::MessageLoop::current()->RunUntilIdle();
109 virtual void NavigationEntryCommitted(
110 const content::LoadCommittedDetails& details) {
111 manager_->NavigationEntryCommitted(details);
114 protected:
115 MockPermissionBubbleRequest request1_;
116 MockPermissionBubbleRequest request2_;
117 MockPermissionBubbleRequest iframe_request_same_domain_;
118 MockPermissionBubbleRequest iframe_request_other_domain_;
119 scoped_ptr<PermissionBubbleManager> manager_;
121 MockView* view_() { return static_cast<MockView*>(manager_->view_.get()); }
124 TEST_F(PermissionBubbleManagerTest, TestFlag) {
125 EXPECT_TRUE(PermissionBubbleManager::Enabled());
126 base::CommandLine::ForCurrentProcess()->AppendSwitch(
127 switches::kDisablePermissionsBubbles);
128 EXPECT_FALSE(PermissionBubbleManager::Enabled());
131 TEST_F(PermissionBubbleManagerTest, SingleRequest) {
132 manager_->AddRequest(&request1_);
133 manager_->DisplayPendingRequests();
134 WaitForCoalescing();
136 EXPECT_TRUE(view_()->delegate_ == manager_.get());
137 EXPECT_TRUE(view_()->shown_);
138 ASSERT_EQ(static_cast<size_t>(1), view_()->permission_requests_.size());
139 EXPECT_EQ(&request1_, view_()->permission_requests_[0]);
141 ToggleAccept(0, true);
142 Accept();
143 EXPECT_TRUE(request1_.granted());
146 TEST_F(PermissionBubbleManagerTest, SingleRequestViewFirst) {
147 manager_->DisplayPendingRequests();
148 manager_->AddRequest(&request1_);
149 WaitForCoalescing();
151 EXPECT_TRUE(view_()->delegate_ == manager_.get());
152 EXPECT_TRUE(view_()->shown_);
153 ASSERT_EQ(static_cast<size_t>(1), view_()->permission_requests_.size());
154 EXPECT_EQ(&request1_, view_()->permission_requests_[0]);
156 ToggleAccept(0, true);
157 Accept();
158 EXPECT_TRUE(request1_.granted());
161 TEST_F(PermissionBubbleManagerTest, TwoRequests) {
162 manager_->AddRequest(&request1_);
163 manager_->AddRequest(&request2_);
164 manager_->DisplayPendingRequests();
165 WaitForCoalescing();
167 EXPECT_TRUE(view_()->delegate_ == manager_.get());
168 EXPECT_TRUE(view_()->shown_);
169 ASSERT_EQ(static_cast<size_t>(2), view_()->permission_requests_.size());
170 EXPECT_EQ(&request1_, view_()->permission_requests_[0]);
171 EXPECT_EQ(&request2_, view_()->permission_requests_[1]);
173 ToggleAccept(0, true);
174 ToggleAccept(1, false);
175 Accept();
176 EXPECT_TRUE(request1_.granted());
177 EXPECT_FALSE(request2_.granted());
180 TEST_F(PermissionBubbleManagerTest, TwoRequestsTabSwitch) {
181 manager_->AddRequest(&request1_);
182 manager_->AddRequest(&request2_);
183 manager_->DisplayPendingRequests();
184 WaitForCoalescing();
186 EXPECT_TRUE(view_()->delegate_ == manager_.get());
187 EXPECT_TRUE(view_()->shown_);
188 ASSERT_EQ(static_cast<size_t>(2), view_()->permission_requests_.size());
189 EXPECT_EQ(&request1_, view_()->permission_requests_[0]);
190 EXPECT_EQ(&request2_, view_()->permission_requests_[1]);
192 ToggleAccept(0, true);
193 ToggleAccept(1, false);
195 manager_->HideBubble();
196 EXPECT_FALSE(view_());
198 manager_->DisplayPendingRequests();
199 WaitForCoalescing();
200 EXPECT_TRUE(view_()->shown_);
201 ASSERT_EQ(static_cast<size_t>(2), view_()->permission_requests_.size());
202 EXPECT_EQ(&request1_, view_()->permission_requests_[0]);
203 EXPECT_EQ(&request2_, view_()->permission_requests_[1]);
204 EXPECT_TRUE(view_()->permission_states_[0]);
205 EXPECT_FALSE(view_()->permission_states_[1]);
207 Accept();
208 EXPECT_TRUE(request1_.granted());
209 EXPECT_FALSE(request2_.granted());
212 TEST_F(PermissionBubbleManagerTest, NoRequests) {
213 manager_->DisplayPendingRequests();
214 WaitForCoalescing();
215 EXPECT_FALSE(view_()->shown_);
218 TEST_F(PermissionBubbleManagerTest, NoView) {
219 manager_->AddRequest(&request1_);
220 // Don't display the pending requests.
221 WaitForCoalescing();
222 EXPECT_FALSE(view_());
225 TEST_F(PermissionBubbleManagerTest, TwoRequestsCoalesce) {
226 manager_->DisplayPendingRequests();
227 manager_->AddRequest(&request1_);
228 manager_->AddRequest(&request2_);
229 EXPECT_FALSE(view_()->shown_);
230 WaitForCoalescing();
232 EXPECT_TRUE(view_()->shown_);
233 EXPECT_EQ(2u, view_()->permission_requests_.size());
236 TEST_F(PermissionBubbleManagerTest, TwoRequestsDoNotCoalesce) {
237 manager_->DisplayPendingRequests();
238 manager_->AddRequest(&request1_);
239 WaitForCoalescing();
240 manager_->AddRequest(&request2_);
242 EXPECT_TRUE(view_()->shown_);
243 EXPECT_EQ(1u, view_()->permission_requests_.size());
246 TEST_F(PermissionBubbleManagerTest, TwoRequestsShownInTwoBubbles) {
247 manager_->DisplayPendingRequests();
248 manager_->AddRequest(&request1_);
249 WaitForCoalescing();
250 manager_->AddRequest(&request2_);
252 EXPECT_TRUE(view_()->shown_);
253 ASSERT_EQ(1u, view_()->permission_requests_.size());
254 EXPECT_EQ(&request1_, view_()->permission_requests_[0]);
256 view_()->Hide();
257 Accept();
258 WaitForCoalescing();
260 EXPECT_TRUE(view_()->shown_);
261 ASSERT_EQ(1u, view_()->permission_requests_.size());
262 EXPECT_EQ(&request2_, view_()->permission_requests_[0]);
265 TEST_F(PermissionBubbleManagerTest, TestAddDuplicateRequest) {
266 manager_->DisplayPendingRequests();
267 manager_->AddRequest(&request1_);
268 manager_->AddRequest(&request2_);
269 manager_->AddRequest(&request1_);
271 WaitForCoalescing();
272 EXPECT_TRUE(view_()->shown_);
273 ASSERT_EQ(2u, view_()->permission_requests_.size());
274 EXPECT_EQ(&request1_, view_()->permission_requests_[0]);
275 EXPECT_EQ(&request2_, view_()->permission_requests_[1]);
278 TEST_F(PermissionBubbleManagerTest, SequentialRequests) {
279 manager_->DisplayPendingRequests();
280 manager_->AddRequest(&request1_);
281 WaitForCoalescing();
282 EXPECT_TRUE(view_()->shown_);
284 Accept();
285 EXPECT_TRUE(request1_.granted());
287 EXPECT_FALSE(view_()->shown_);
289 manager_->AddRequest(&request2_);
290 WaitForCoalescing();
291 EXPECT_TRUE(view_()->shown_);
292 Accept();
293 EXPECT_FALSE(view_()->shown_);
294 EXPECT_TRUE(request2_.granted());
297 TEST_F(PermissionBubbleManagerTest, SameRequestRejected) {
298 manager_->DisplayPendingRequests();
299 manager_->AddRequest(&request1_);
300 manager_->AddRequest(&request1_);
301 EXPECT_FALSE(request1_.finished());
303 WaitForCoalescing();
304 EXPECT_TRUE(view_()->shown_);
305 ASSERT_EQ(1u, view_()->permission_requests_.size());
306 EXPECT_EQ(&request1_, view_()->permission_requests_[0]);
309 TEST_F(PermissionBubbleManagerTest, DuplicateRequestRejected) {
310 manager_->DisplayPendingRequests();
311 manager_->AddRequest(&request1_);
312 MockPermissionBubbleRequest dupe_request("test1");
313 manager_->AddRequest(&dupe_request);
314 EXPECT_TRUE(dupe_request.finished());
315 EXPECT_FALSE(request1_.finished());
318 TEST_F(PermissionBubbleManagerTest, DuplicateQueuedRequest) {
319 manager_->DisplayPendingRequests();
320 manager_->AddRequest(&request1_);
321 WaitForCoalescing();
322 manager_->AddRequest(&request2_);
324 MockPermissionBubbleRequest dupe_request("test1");
325 manager_->AddRequest(&dupe_request);
326 EXPECT_TRUE(dupe_request.finished());
327 EXPECT_FALSE(request1_.finished());
329 MockPermissionBubbleRequest dupe_request2("test1");
330 manager_->AddRequest(&dupe_request2);
331 EXPECT_TRUE(dupe_request2.finished());
332 EXPECT_FALSE(request2_.finished());
335 TEST_F(PermissionBubbleManagerTest, ForgetRequestsOnPageNavigation) {
336 manager_->DisplayPendingRequests();
337 manager_->AddRequest(&request1_);
338 WaitForCoalescing();
339 manager_->AddRequest(&request2_);
340 manager_->AddRequest(&iframe_request_other_domain_);
342 EXPECT_TRUE(view_()->shown_);
343 ASSERT_EQ(1u, view_()->permission_requests_.size());
345 NavigateAndCommit(GURL("http://www2.google.com/"));
346 WaitForCoalescing();
348 EXPECT_FALSE(view_()->shown_);
349 EXPECT_TRUE(request1_.finished());
350 EXPECT_TRUE(request2_.finished());
351 EXPECT_TRUE(iframe_request_other_domain_.finished());
354 TEST_F(PermissionBubbleManagerTest, TestCancel) {
355 manager_->HideBubble();
356 manager_->AddRequest(&request1_);
357 WaitForCoalescing();
359 manager_->CancelRequest(&request1_);
360 EXPECT_TRUE(request1_.finished());
361 manager_->DisplayPendingRequests();
362 EXPECT_FALSE(view_()->shown_);
364 manager_->AddRequest(&request2_);
365 WaitForCoalescing();
366 EXPECT_TRUE(view_()->shown_);
369 TEST_F(PermissionBubbleManagerTest, TestCancelWhileDialogShown) {
370 manager_->DisplayPendingRequests();
371 manager_->AddRequest(&request1_);
372 WaitForCoalescing();
374 EXPECT_TRUE(view_()->shown_);
375 EXPECT_FALSE(request1_.finished());
376 manager_->CancelRequest(&request1_);
377 EXPECT_TRUE(request1_.finished());
380 TEST_F(PermissionBubbleManagerTest, TestCancelWhileDialogShownNoUpdate) {
381 manager_->DisplayPendingRequests();
382 view_()->can_accept_updates_ = false;
383 manager_->AddRequest(&request1_);
384 WaitForCoalescing();
386 EXPECT_TRUE(view_()->shown_);
387 EXPECT_FALSE(request1_.finished());
388 manager_->CancelRequest(&request1_);
389 EXPECT_TRUE(request1_.finished());
390 Closing();
393 TEST_F(PermissionBubbleManagerTest, TestCancelPendingRequest) {
394 manager_->DisplayPendingRequests();
395 manager_->AddRequest(&request1_);
396 WaitForCoalescing();
397 manager_->AddRequest(&request2_);
399 EXPECT_TRUE(view_()->shown_);
400 EXPECT_EQ(1u, view_()->permission_requests_.size());
401 manager_->CancelRequest(&request2_);
403 EXPECT_TRUE(view_()->shown_);
404 EXPECT_FALSE(request1_.finished());
405 EXPECT_TRUE(request2_.finished());
408 TEST_F(PermissionBubbleManagerTest, MainFrameNoRequestIFrameRequest) {
409 manager_->DisplayPendingRequests();
410 manager_->AddRequest(&iframe_request_same_domain_);
411 WaitForCoalescing();
412 WaitForFrameLoad();
414 EXPECT_TRUE(view_()->shown_);
415 Closing();
416 EXPECT_TRUE(iframe_request_same_domain_.finished());
419 TEST_F(PermissionBubbleManagerTest, MainFrameAndIFrameRequestSameDomain) {
420 manager_->DisplayPendingRequests();
421 manager_->AddRequest(&request1_);
422 manager_->AddRequest(&iframe_request_same_domain_);
423 WaitForFrameLoad();
424 WaitForCoalescing();
426 EXPECT_TRUE(view_()->shown_);
427 EXPECT_EQ(2u, view_()->permission_requests_.size());
428 Closing();
429 EXPECT_TRUE(request1_.finished());
430 EXPECT_TRUE(iframe_request_same_domain_.finished());
431 EXPECT_FALSE(view_()->shown_);
434 TEST_F(PermissionBubbleManagerTest, MainFrameAndIFrameRequestOtherDomain) {
435 manager_->DisplayPendingRequests();
436 manager_->AddRequest(&request1_);
437 manager_->AddRequest(&iframe_request_other_domain_);
438 WaitForFrameLoad();
439 WaitForCoalescing();
441 EXPECT_TRUE(view_()->shown_);
442 Closing();
443 EXPECT_TRUE(request1_.finished());
444 EXPECT_FALSE(iframe_request_other_domain_.finished());
445 EXPECT_TRUE(view_()->shown_);
446 Closing();
447 EXPECT_TRUE(iframe_request_other_domain_.finished());
450 TEST_F(PermissionBubbleManagerTest, IFrameRequestWhenMainRequestVisible) {
451 manager_->DisplayPendingRequests();
452 manager_->AddRequest(&request1_);
453 WaitForCoalescing();
454 EXPECT_TRUE(view_()->shown_);
456 manager_->AddRequest(&iframe_request_same_domain_);
457 WaitForFrameLoad();
458 EXPECT_EQ(1u, view_()->permission_requests_.size());
459 Closing();
460 EXPECT_TRUE(request1_.finished());
461 EXPECT_FALSE(iframe_request_same_domain_.finished());
462 EXPECT_TRUE(view_()->shown_);
463 EXPECT_EQ(1u, view_()->permission_requests_.size());
464 Closing();
465 EXPECT_TRUE(iframe_request_same_domain_.finished());
468 TEST_F(PermissionBubbleManagerTest,
469 IFrameRequestOtherDomainWhenMainRequestVisible) {
470 manager_->DisplayPendingRequests();
471 manager_->AddRequest(&request1_);
472 WaitForCoalescing();
473 EXPECT_TRUE(view_()->shown_);
475 manager_->AddRequest(&iframe_request_other_domain_);
476 WaitForFrameLoad();
477 Closing();
478 EXPECT_TRUE(request1_.finished());
479 EXPECT_FALSE(iframe_request_other_domain_.finished());
480 EXPECT_TRUE(view_()->shown_);
481 Closing();
482 EXPECT_TRUE(iframe_request_other_domain_.finished());
485 TEST_F(PermissionBubbleManagerTest, IFrameUserGestureRequest) {
486 iframe_request_other_domain_.SetHasUserGesture();
487 manager_->DisplayPendingRequests();
488 manager_->AddRequest(&request1_);
489 manager_->AddRequest(&iframe_request_other_domain_);
490 WaitForFrameLoad();
491 WaitForCoalescing();
492 manager_->AddRequest(&request2_);
494 EXPECT_TRUE(view_()->shown_);
495 Closing();
496 EXPECT_TRUE(request1_.finished());
497 EXPECT_FALSE(iframe_request_other_domain_.finished());
498 EXPECT_FALSE(request2_.finished());
499 EXPECT_TRUE(view_()->shown_);
500 Closing();
501 EXPECT_TRUE(iframe_request_other_domain_.finished());
502 EXPECT_FALSE(request2_.finished());
505 TEST_F(PermissionBubbleManagerTest, AllUserGestureRequests) {
506 iframe_request_other_domain_.SetHasUserGesture();
507 request2_.SetHasUserGesture();
508 manager_->DisplayPendingRequests();
509 manager_->AddRequest(&request1_);
510 manager_->AddRequest(&iframe_request_other_domain_);
511 WaitForCoalescing();
512 WaitForFrameLoad();
513 manager_->AddRequest(&request2_);
515 EXPECT_TRUE(view_()->shown_);
516 Closing();
517 EXPECT_TRUE(request1_.finished());
518 EXPECT_FALSE(request2_.finished());
519 EXPECT_FALSE(iframe_request_other_domain_.finished());
520 EXPECT_TRUE(view_()->shown_);
521 Closing();
522 EXPECT_TRUE(request2_.finished());
523 EXPECT_FALSE(iframe_request_other_domain_.finished());
524 Closing();
525 EXPECT_TRUE(iframe_request_other_domain_.finished());
526 EXPECT_FALSE(view_()->shown_);
529 TEST_F(PermissionBubbleManagerTest, RequestsWithoutUserGesture) {
530 manager_->RequireUserGesture(true);
531 manager_->DisplayPendingRequests();
532 WaitForFrameLoad();
533 WaitForCoalescing();
534 manager_->AddRequest(&request1_);
535 manager_->AddRequest(&iframe_request_other_domain_);
536 manager_->AddRequest(&request2_);
537 base::MessageLoop::current()->RunUntilIdle();
539 EXPECT_FALSE(view_()->shown_);
542 TEST_F(PermissionBubbleManagerTest, RequestsWithUserGesture) {
543 manager_->RequireUserGesture(true);
544 manager_->DisplayPendingRequests();
545 WaitForFrameLoad();
546 WaitForCoalescing();
547 request1_.SetHasUserGesture();
548 manager_->AddRequest(&request1_);
549 manager_->AddRequest(&iframe_request_other_domain_);
550 manager_->AddRequest(&request2_);
551 base::MessageLoop::current()->RunUntilIdle();
553 EXPECT_TRUE(view_()->shown_);
556 TEST_F(PermissionBubbleManagerTest, RequestsDontNeedUserGesture) {
557 manager_->DisplayPendingRequests();
558 WaitForFrameLoad();
559 WaitForCoalescing();
560 manager_->AddRequest(&request1_);
561 manager_->AddRequest(&iframe_request_other_domain_);
562 manager_->AddRequest(&request2_);
563 base::MessageLoop::current()->RunUntilIdle();
565 EXPECT_TRUE(view_()->shown_);