Home | History | Annotate | Download | only in content_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 "chrome/browser/content_settings/permission_context_base.h"
      6 
      7 #include "base/bind.h"
      8 #include "chrome/browser/content_settings/host_content_settings_map.h"
      9 #include "chrome/browser/content_settings/permission_queue_controller.h"
     10 #include "chrome/browser/infobars/infobar_service.h"
     11 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
     12 #include "chrome/test/base/testing_profile.h"
     13 #include "components/content_settings/core/common/content_settings.h"
     14 #include "components/content_settings/core/common/content_settings_types.h"
     15 #include "components/content_settings/core/common/permission_request_id.h"
     16 #include "content/public/browser/web_contents.h"
     17 #include "content/public/test/mock_render_process_host.h"
     18 #include "content/public/test/web_contents_tester.h"
     19 #include "testing/gtest/include/gtest/gtest.h"
     20 
     21 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness {
     22  protected:
     23   PermissionContextBaseTests() {}
     24   virtual ~PermissionContextBaseTests() {}
     25 
     26  private:
     27   // ChromeRenderViewHostTestHarness:
     28   virtual void SetUp() OVERRIDE {
     29     ChromeRenderViewHostTestHarness::SetUp();
     30     InfoBarService::CreateForWebContents(web_contents());
     31   }
     32 
     33   DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests);
     34 };
     35 
     36 class TestPermissionContext : public PermissionContextBase {
     37  public:
     38   TestPermissionContext(Profile* profile,
     39                   const ContentSettingsType permission_type)
     40    : PermissionContextBase(profile, permission_type),
     41      permission_set_(false),
     42      permission_granted_(false),
     43      tab_context_updated_(false) {}
     44 
     45   virtual ~TestPermissionContext() {}
     46 
     47   PermissionQueueController* GetInfoBarController() {
     48     return GetQueueController();
     49   }
     50 
     51   bool permission_granted() {
     52     return permission_granted_;
     53   }
     54 
     55   bool permission_set() {
     56     return permission_set_;
     57   }
     58 
     59   bool tab_context_updated() {
     60     return tab_context_updated_;
     61   }
     62 
     63   void TrackPermissionDecision(bool granted) {
     64     permission_set_ = true;
     65     permission_granted_ = granted;
     66   }
     67 
     68  protected:
     69   virtual void UpdateTabContext(const PermissionRequestID& id,
     70                                 const GURL& requesting_origin,
     71                                 bool allowed) OVERRIDE {
     72     tab_context_updated_ = true;
     73   }
     74 
     75  private:
     76    bool permission_set_;
     77    bool permission_granted_;
     78    bool tab_context_updated_;
     79 };
     80 
     81 // Simulates clicking Accept. The permission should be granted and
     82 // saved for future use.
     83 TEST_F(PermissionContextBaseTests, TestAskAndGrant) {
     84   TestPermissionContext permission_context(profile(),
     85                       CONTENT_SETTINGS_TYPE_PUSH_MESSAGING);
     86   GURL url("http://www.google.com");
     87   content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
     88 
     89   const PermissionRequestID id(
     90       web_contents()->GetRenderProcessHost()->GetID(),
     91       web_contents()->GetRenderViewHost()->GetRoutingID(),
     92       -1, GURL());
     93   permission_context.RequestPermission(
     94       web_contents(),
     95       id, url, true,
     96       base::Bind(&TestPermissionContext::TrackPermissionDecision,
     97                  base::Unretained(&permission_context)));
     98 
     99   permission_context.GetInfoBarController()->OnPermissionSet(
    100       id, url, url, true, true);
    101   EXPECT_TRUE(permission_context.permission_set());
    102   EXPECT_TRUE(permission_context.permission_granted());
    103   EXPECT_TRUE(permission_context.tab_context_updated());
    104 
    105   ContentSetting setting =
    106       profile()->GetHostContentSettingsMap()->GetContentSetting(
    107           url.GetOrigin(), url.GetOrigin(),
    108           CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, std::string());
    109   EXPECT_EQ(CONTENT_SETTING_ALLOW , setting);
    110 };
    111 
    112 // Simulates clicking Dismiss (X in the infobar.
    113 // The permission should be denied but not saved for future use.
    114 TEST_F(PermissionContextBaseTests, TestAskAndDismiss) {
    115   TestPermissionContext permission_context(profile(),
    116                       CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
    117   GURL url("http://www.google.es");
    118   content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
    119 
    120   const PermissionRequestID id(
    121       web_contents()->GetRenderProcessHost()->GetID(),
    122       web_contents()->GetRenderViewHost()->GetRoutingID(),
    123       -1, GURL());
    124   permission_context.RequestPermission(
    125       web_contents(),
    126       id, url, true,
    127       base::Bind(&TestPermissionContext::TrackPermissionDecision,
    128                  base::Unretained(&permission_context)));
    129 
    130   permission_context.GetInfoBarController()->OnPermissionSet(
    131       id, url, url, false, false);
    132   EXPECT_TRUE(permission_context.permission_set());
    133   EXPECT_FALSE(permission_context.permission_granted());
    134   EXPECT_TRUE(permission_context.tab_context_updated());
    135 
    136   ContentSetting setting =
    137       profile()->GetHostContentSettingsMap()->GetContentSetting(
    138           url.GetOrigin(), url.GetOrigin(),
    139           CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string());
    140   EXPECT_EQ(CONTENT_SETTING_ASK , setting);
    141 };
    142