Home | History | Annotate | Download | only in website_settings
      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.
      4 
      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"
     15 
     16 namespace {
     17 
     18 class MockView : public PermissionBubbleView {
     19  public:
     20   MockView() : shown_(false), can_accept_updates_(true), delegate_(NULL) {}
     21   virtual ~MockView() {}
     22 
     23   void Clear() {
     24     shown_ = false;
     25     can_accept_updates_ = true;
     26     delegate_ = NULL;
     27     permission_requests_.clear();
     28     permission_states_.clear();
     29   }
     30 
     31   // PermissionBubbleView:
     32   virtual void SetDelegate(Delegate* delegate) OVERRIDE {
     33     delegate_ = delegate;
     34   }
     35 
     36   virtual void Show(
     37       const std::vector<PermissionBubbleRequest*>& requests,
     38       const std::vector<bool>& accept_state,
     39       bool customization_state_) OVERRIDE {
     40     shown_ = true;
     41     permission_requests_ = requests;
     42     permission_states_ = accept_state;
     43   }
     44 
     45   virtual void Hide() OVERRIDE {
     46     shown_ = false;
     47   }
     48 
     49   virtual bool CanAcceptRequestUpdate() OVERRIDE {
     50     return can_accept_updates_;
     51   }
     52 
     53   bool shown_;
     54   bool can_accept_updates_;
     55   Delegate* delegate_;
     56   std::vector<PermissionBubbleRequest*> permission_requests_;
     57   std::vector<bool> permission_states_;
     58 };
     59 
     60 }  // namespace
     61 
     62 class PermissionBubbleManagerTest : public ChromeRenderViewHostTestHarness {
     63  public:
     64   PermissionBubbleManagerTest()
     65       : ChromeRenderViewHostTestHarness(),
     66         request1_("test1"),
     67         request2_("test2"),
     68         iframe_request_same_domain_("iframe",
     69                                     GURL("http://www.google.com/some/url")),
     70         iframe_request_other_domain_("iframe",
     71                                      GURL("http://www.youtube.com")) {}
     72   virtual ~PermissionBubbleManagerTest() {}
     73 
     74   virtual void SetUp() OVERRIDE {
     75     ChromeRenderViewHostTestHarness::SetUp();
     76     SetContents(CreateTestWebContents());
     77     NavigateAndCommit(GURL("http://www.google.com"));
     78 
     79     manager_.reset(new PermissionBubbleManager(web_contents()));
     80   }
     81 
     82   virtual void TearDown() OVERRIDE {
     83     manager_.reset();
     84     ChromeRenderViewHostTestHarness::TearDown();
     85   }
     86 
     87   void ToggleAccept(int index, bool value) {
     88     manager_->ToggleAccept(index, value);
     89   }
     90 
     91   void Accept() {
     92     manager_->Accept();
     93   }
     94 
     95   void Closing() {
     96     manager_->Closing();
     97   }
     98 
     99   void WaitForFrameLoad() {
    100     manager_->DocumentLoadedInFrame(0, NULL);
    101     base::MessageLoop::current()->RunUntilIdle();
    102   }
    103 
    104   void WaitForCoalescing() {
    105     manager_->DocumentOnLoadCompletedInMainFrame();
    106     base::MessageLoop::current()->RunUntilIdle();
    107   }
    108 
    109   virtual void NavigationEntryCommitted(
    110       const content::LoadCommittedDetails& details) {
    111     manager_->NavigationEntryCommitted(details);
    112   }
    113 
    114  protected:
    115   MockPermissionBubbleRequest request1_;
    116   MockPermissionBubbleRequest request2_;
    117   MockPermissionBubbleRequest iframe_request_same_domain_;
    118   MockPermissionBubbleRequest iframe_request_other_domain_;
    119   MockView view_;
    120   scoped_ptr<PermissionBubbleManager> manager_;
    121 };
    122 
    123 TEST_F(PermissionBubbleManagerTest, TestFlag) {
    124   EXPECT_FALSE(PermissionBubbleManager::Enabled());
    125   CommandLine::ForCurrentProcess()->AppendSwitch(
    126       switches::kEnablePermissionsBubbles);
    127   EXPECT_TRUE(PermissionBubbleManager::Enabled());
    128 }
    129 
    130 TEST_F(PermissionBubbleManagerTest, SingleRequest) {
    131   manager_->AddRequest(&request1_);
    132   manager_->SetView(&view_);
    133   WaitForCoalescing();
    134 
    135   EXPECT_TRUE(view_.delegate_ == manager_.get());
    136   EXPECT_TRUE(view_.shown_);
    137   ASSERT_EQ(static_cast<size_t>(1), view_.permission_requests_.size());
    138   EXPECT_EQ(&request1_, view_.permission_requests_[0]);
    139 
    140   ToggleAccept(0, true);
    141   Accept();
    142   EXPECT_TRUE(request1_.granted());
    143 }
    144 
    145 TEST_F(PermissionBubbleManagerTest, SingleRequestViewFirst) {
    146   manager_->SetView(&view_);
    147   manager_->AddRequest(&request1_);
    148   WaitForCoalescing();
    149 
    150   EXPECT_TRUE(view_.delegate_ == manager_.get());
    151   EXPECT_TRUE(view_.shown_);
    152   ASSERT_EQ(static_cast<size_t>(1), view_.permission_requests_.size());
    153   EXPECT_EQ(&request1_, view_.permission_requests_[0]);
    154 
    155   ToggleAccept(0, true);
    156   Accept();
    157   EXPECT_TRUE(request1_.granted());
    158 }
    159 
    160 TEST_F(PermissionBubbleManagerTest, TwoRequests) {
    161   manager_->AddRequest(&request1_);
    162   manager_->AddRequest(&request2_);
    163   manager_->SetView(&view_);
    164   WaitForCoalescing();
    165 
    166   EXPECT_TRUE(view_.delegate_ == manager_.get());
    167   EXPECT_TRUE(view_.shown_);
    168   ASSERT_EQ(static_cast<size_t>(2), view_.permission_requests_.size());
    169   EXPECT_EQ(&request1_, view_.permission_requests_[0]);
    170   EXPECT_EQ(&request2_, view_.permission_requests_[1]);
    171 
    172   ToggleAccept(0, true);
    173   ToggleAccept(1, false);
    174   Accept();
    175   EXPECT_TRUE(request1_.granted());
    176   EXPECT_FALSE(request2_.granted());
    177 }
    178 
    179 TEST_F(PermissionBubbleManagerTest, TwoRequestsTabSwitch) {
    180   manager_->AddRequest(&request1_);
    181   manager_->AddRequest(&request2_);
    182   manager_->SetView(&view_);
    183   WaitForCoalescing();
    184 
    185   EXPECT_TRUE(view_.delegate_ == manager_.get());
    186   EXPECT_TRUE(view_.shown_);
    187   ASSERT_EQ(static_cast<size_t>(2), view_.permission_requests_.size());
    188   EXPECT_EQ(&request1_, view_.permission_requests_[0]);
    189   EXPECT_EQ(&request2_, view_.permission_requests_[1]);
    190 
    191   ToggleAccept(0, true);
    192   ToggleAccept(1, false);
    193 
    194   manager_->SetView(NULL);
    195   EXPECT_FALSE(view_.shown_);
    196   EXPECT_TRUE(view_.delegate_ == NULL);
    197   view_.Clear();
    198 
    199   manager_->SetView(&view_);
    200   WaitForCoalescing();
    201   EXPECT_TRUE(view_.shown_);
    202   ASSERT_EQ(static_cast<size_t>(2), view_.permission_requests_.size());
    203   EXPECT_EQ(&request1_, view_.permission_requests_[0]);
    204   EXPECT_EQ(&request2_, view_.permission_requests_[1]);
    205   EXPECT_TRUE(view_.permission_states_[0]);
    206   EXPECT_FALSE(view_.permission_states_[1]);
    207 
    208   Accept();
    209   EXPECT_TRUE(request1_.granted());
    210   EXPECT_FALSE(request2_.granted());
    211 }
    212 
    213 TEST_F(PermissionBubbleManagerTest, NoRequests) {
    214   manager_->SetView(&view_);
    215   WaitForCoalescing();
    216   EXPECT_FALSE(view_.shown_);
    217 }
    218 
    219 TEST_F(PermissionBubbleManagerTest, NoView) {
    220   manager_->AddRequest(&request1_);
    221   WaitForCoalescing();
    222   EXPECT_FALSE(view_.shown_);
    223 }
    224 
    225 TEST_F(PermissionBubbleManagerTest, TwoRequestsCoalesce) {
    226   manager_->SetView(&view_);
    227   manager_->AddRequest(&request1_);
    228   manager_->AddRequest(&request2_);
    229   EXPECT_FALSE(view_.shown_);
    230   WaitForCoalescing();
    231 
    232   EXPECT_TRUE(view_.shown_);
    233   EXPECT_EQ(2u, view_.permission_requests_.size());
    234 }
    235 
    236 TEST_F(PermissionBubbleManagerTest, TwoRequestsDoNotCoalesce) {
    237   manager_->SetView(&view_);
    238   manager_->AddRequest(&request1_);
    239   WaitForCoalescing();
    240   manager_->AddRequest(&request2_);
    241 
    242   EXPECT_TRUE(view_.shown_);
    243   EXPECT_EQ(1u, view_.permission_requests_.size());
    244 }
    245 
    246 TEST_F(PermissionBubbleManagerTest, TwoRequestsShownInTwoBubbles) {
    247   manager_->SetView(&view_);
    248   manager_->AddRequest(&request1_);
    249   WaitForCoalescing();
    250   manager_->AddRequest(&request2_);
    251 
    252   EXPECT_TRUE(view_.shown_);
    253   ASSERT_EQ(1u, view_.permission_requests_.size());
    254   EXPECT_EQ(&request1_, view_.permission_requests_[0]);
    255 
    256   view_.Hide();
    257   Accept();
    258   WaitForCoalescing();
    259 
    260   EXPECT_TRUE(view_.shown_);
    261   ASSERT_EQ(1u, view_.permission_requests_.size());
    262   EXPECT_EQ(&request2_, view_.permission_requests_[0]);
    263 }
    264 
    265 TEST_F(PermissionBubbleManagerTest, TestAddDuplicateRequest) {
    266   manager_->SetView(&view_);
    267   manager_->AddRequest(&request1_);
    268   manager_->AddRequest(&request2_);
    269   manager_->AddRequest(&request1_);
    270 
    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]);
    276 }
    277 
    278 TEST_F(PermissionBubbleManagerTest, SequentialRequests) {
    279   manager_->SetView(&view_);
    280   manager_->AddRequest(&request1_);
    281   WaitForCoalescing();
    282   EXPECT_TRUE(view_.shown_);
    283 
    284   Accept();
    285   EXPECT_TRUE(request1_.granted());
    286 
    287   EXPECT_FALSE(view_.shown_);
    288 
    289   manager_->AddRequest(&request2_);
    290   WaitForCoalescing();
    291   EXPECT_TRUE(view_.shown_);
    292   Accept();
    293   EXPECT_FALSE(view_.shown_);
    294   EXPECT_TRUE(request2_.granted());
    295 }
    296 
    297 TEST_F(PermissionBubbleManagerTest, SameRequestRejected) {
    298   manager_->SetView(&view_);
    299   manager_->AddRequest(&request1_);
    300   manager_->AddRequest(&request1_);
    301   EXPECT_FALSE(request1_.finished());
    302 
    303   WaitForCoalescing();
    304   EXPECT_TRUE(view_.shown_);
    305   ASSERT_EQ(1u, view_.permission_requests_.size());
    306   EXPECT_EQ(&request1_, view_.permission_requests_[0]);
    307 }
    308 
    309 TEST_F(PermissionBubbleManagerTest, DuplicateRequestRejected) {
    310   manager_->SetView(&view_);
    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());
    316 }
    317 
    318 TEST_F(PermissionBubbleManagerTest, DuplicateQueuedRequest) {
    319   manager_->SetView(&view_);
    320   manager_->AddRequest(&request1_);
    321   WaitForCoalescing();
    322   manager_->AddRequest(&request2_);
    323 
    324   MockPermissionBubbleRequest dupe_request("test1");
    325   manager_->AddRequest(&dupe_request);
    326   EXPECT_TRUE(dupe_request.finished());
    327   EXPECT_FALSE(request1_.finished());
    328 
    329   MockPermissionBubbleRequest dupe_request2("test1");
    330   manager_->AddRequest(&dupe_request2);
    331   EXPECT_TRUE(dupe_request2.finished());
    332   EXPECT_FALSE(request2_.finished());
    333 }
    334 
    335 TEST_F(PermissionBubbleManagerTest, ForgetRequestsOnPageNavigation) {
    336   manager_->SetView(&view_);
    337   manager_->AddRequest(&request1_);
    338   WaitForCoalescing();
    339   manager_->AddRequest(&request2_);
    340 
    341   EXPECT_TRUE(view_.shown_);
    342   ASSERT_EQ(1u, view_.permission_requests_.size());
    343 
    344   NavigateAndCommit(GURL("http://www2.google.com/"));
    345   WaitForCoalescing();
    346 
    347   EXPECT_TRUE(request1_.finished());
    348   EXPECT_TRUE(request2_.finished());
    349 }
    350 
    351 TEST_F(PermissionBubbleManagerTest, TestCancel) {
    352   manager_->SetView(NULL);
    353   manager_->AddRequest(&request1_);
    354   WaitForCoalescing();
    355 
    356   manager_->CancelRequest(&request1_);
    357   EXPECT_TRUE(request1_.finished());
    358   manager_->SetView(&view_);
    359   EXPECT_FALSE(view_.shown_);
    360 
    361   manager_->AddRequest(&request2_);
    362   WaitForCoalescing();
    363   EXPECT_TRUE(view_.shown_);
    364 }
    365 
    366 TEST_F(PermissionBubbleManagerTest, TestCancelWhileDialogShown) {
    367   manager_->SetView(&view_);
    368   manager_->AddRequest(&request1_);
    369   WaitForCoalescing();
    370 
    371   EXPECT_TRUE(view_.shown_);
    372   EXPECT_FALSE(request1_.finished());
    373   manager_->CancelRequest(&request1_);
    374   EXPECT_TRUE(request1_.finished());
    375 }
    376 
    377 TEST_F(PermissionBubbleManagerTest, TestCancelWhileDialogShownNoUpdate) {
    378   manager_->SetView(&view_);
    379   view_.can_accept_updates_ = false;
    380   manager_->AddRequest(&request1_);
    381   WaitForCoalescing();
    382 
    383   EXPECT_TRUE(view_.shown_);
    384   EXPECT_FALSE(request1_.finished());
    385   manager_->CancelRequest(&request1_);
    386   EXPECT_TRUE(request1_.finished());
    387   Closing();
    388 }
    389 
    390 TEST_F(PermissionBubbleManagerTest, TestCancelPendingRequest) {
    391   manager_->SetView(&view_);
    392   manager_->AddRequest(&request1_);
    393   WaitForCoalescing();
    394   manager_->AddRequest(&request2_);
    395 
    396   EXPECT_TRUE(view_.shown_);
    397   EXPECT_EQ(1u, view_.permission_requests_.size());
    398   manager_->CancelRequest(&request2_);
    399 
    400   EXPECT_TRUE(view_.shown_);
    401   EXPECT_FALSE(request1_.finished());
    402   EXPECT_TRUE(request2_.finished());
    403 }
    404 
    405 TEST_F(PermissionBubbleManagerTest, MainFrameNoRequestIFrameRequest) {
    406   manager_->SetView(&view_);
    407   manager_->AddRequest(&iframe_request_same_domain_);
    408   WaitForCoalescing();
    409   WaitForFrameLoad();
    410 
    411   EXPECT_TRUE(view_.shown_);
    412   Closing();
    413   EXPECT_TRUE(iframe_request_same_domain_.finished());
    414 }
    415 
    416 TEST_F(PermissionBubbleManagerTest, MainFrameAndIFrameRequestSameDomain) {
    417   manager_->SetView(&view_);
    418   manager_->AddRequest(&request1_);
    419   manager_->AddRequest(&iframe_request_same_domain_);
    420   WaitForFrameLoad();
    421   WaitForCoalescing();
    422 
    423   EXPECT_TRUE(view_.shown_);
    424   EXPECT_EQ(2u, view_.permission_requests_.size());
    425   Closing();
    426   EXPECT_TRUE(request1_.finished());
    427   EXPECT_TRUE(iframe_request_same_domain_.finished());
    428   EXPECT_FALSE(view_.shown_);
    429 }
    430 
    431 TEST_F(PermissionBubbleManagerTest, MainFrameAndIFrameRequestOtherDomain) {
    432   manager_->SetView(&view_);
    433   manager_->AddRequest(&request1_);
    434   manager_->AddRequest(&iframe_request_other_domain_);
    435   WaitForFrameLoad();
    436   WaitForCoalescing();
    437 
    438   EXPECT_TRUE(view_.shown_);
    439   Closing();
    440   EXPECT_TRUE(request1_.finished());
    441   EXPECT_FALSE(iframe_request_other_domain_.finished());
    442   EXPECT_TRUE(view_.shown_);
    443   Closing();
    444   EXPECT_TRUE(iframe_request_other_domain_.finished());
    445 }
    446 
    447 TEST_F(PermissionBubbleManagerTest, IFrameRequestWhenMainRequestVisible) {
    448   manager_->SetView(&view_);
    449   manager_->AddRequest(&request1_);
    450   WaitForCoalescing();
    451   EXPECT_TRUE(view_.shown_);
    452 
    453   manager_->AddRequest(&iframe_request_same_domain_);
    454   WaitForFrameLoad();
    455   EXPECT_EQ(1u, view_.permission_requests_.size());
    456   Closing();
    457   EXPECT_TRUE(request1_.finished());
    458   EXPECT_FALSE(iframe_request_same_domain_.finished());
    459   EXPECT_TRUE(view_.shown_);
    460   EXPECT_EQ(1u, view_.permission_requests_.size());
    461   Closing();
    462   EXPECT_TRUE(iframe_request_same_domain_.finished());
    463 }
    464 
    465 TEST_F(PermissionBubbleManagerTest,
    466        IFrameRequestOtherDomainWhenMainRequestVisible) {
    467   manager_->SetView(&view_);
    468   manager_->AddRequest(&request1_);
    469   WaitForCoalescing();
    470   EXPECT_TRUE(view_.shown_);
    471 
    472   manager_->AddRequest(&iframe_request_other_domain_);
    473   WaitForFrameLoad();
    474   Closing();
    475   EXPECT_TRUE(request1_.finished());
    476   EXPECT_FALSE(iframe_request_other_domain_.finished());
    477   EXPECT_TRUE(view_.shown_);
    478   Closing();
    479   EXPECT_TRUE(iframe_request_other_domain_.finished());
    480 }
    481 
    482 TEST_F(PermissionBubbleManagerTest, IFrameUserGestureRequest) {
    483   iframe_request_other_domain_.SetHasUserGesture();
    484   manager_->SetView(&view_);
    485   manager_->AddRequest(&request1_);
    486   manager_->AddRequest(&iframe_request_other_domain_);
    487   WaitForFrameLoad();
    488   WaitForCoalescing();
    489   manager_->AddRequest(&request2_);
    490 
    491   EXPECT_TRUE(view_.shown_);
    492   Closing();
    493   EXPECT_TRUE(request1_.finished());
    494   EXPECT_FALSE(iframe_request_other_domain_.finished());
    495   EXPECT_FALSE(request2_.finished());
    496   EXPECT_TRUE(view_.shown_);
    497   Closing();
    498   EXPECT_TRUE(iframe_request_other_domain_.finished());
    499   EXPECT_FALSE(request2_.finished());
    500 }
    501 
    502 TEST_F(PermissionBubbleManagerTest, AllUserGestureRequests) {
    503   iframe_request_other_domain_.SetHasUserGesture();
    504   request2_.SetHasUserGesture();
    505   manager_->SetView(&view_);
    506   manager_->AddRequest(&request1_);
    507   manager_->AddRequest(&iframe_request_other_domain_);
    508   WaitForCoalescing();
    509   WaitForFrameLoad();
    510   manager_->AddRequest(&request2_);
    511 
    512   EXPECT_TRUE(view_.shown_);
    513   Closing();
    514   EXPECT_TRUE(request1_.finished());
    515   EXPECT_FALSE(request2_.finished());
    516   EXPECT_FALSE(iframe_request_other_domain_.finished());
    517   EXPECT_TRUE(view_.shown_);
    518   Closing();
    519   EXPECT_TRUE(request2_.finished());
    520   EXPECT_FALSE(iframe_request_other_domain_.finished());
    521   Closing();
    522   EXPECT_TRUE(iframe_request_other_domain_.finished());
    523   EXPECT_FALSE(view_.shown_);
    524 }
    525