File: permission_context_base_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (180 lines) | stat: -rw-r--r-- 6,329 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/content_settings/permission_context_base.h"

#include "base/bind.h"
#include "base/command_line.h"
#include "chrome/browser/content_settings/permission_queue_controller.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/permission_request_id.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"

class TestPermissionContext : public PermissionContextBase {
 public:
  TestPermissionContext(Profile* profile,
                  const ContentSettingsType permission_type)
   : PermissionContextBase(profile, permission_type),
     permission_set_(false),
     permission_granted_(false),
     tab_context_updated_(false) {}

  ~TestPermissionContext() override {}

  PermissionQueueController* GetInfoBarController() {
    return GetQueueController();
  }

  bool permission_granted() {
    return permission_granted_;
  }

  bool permission_set() {
    return permission_set_;
  }

  bool tab_context_updated() {
    return tab_context_updated_;
  }

  void TrackPermissionDecision(bool granted) {
    permission_set_ = true;
    permission_granted_ = granted;
  }

 protected:
  void UpdateTabContext(const PermissionRequestID& id,
                        const GURL& requesting_origin,
                        bool allowed) override {
    tab_context_updated_ = true;
  }

 private:
   bool permission_set_;
   bool permission_granted_;
   bool tab_context_updated_;
};

class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness {
 protected:
  PermissionContextBaseTests() {}

  // Accept or dismiss the permission bubble or infobar.
  void RespondToPermission(TestPermissionContext* context,
                           const PermissionRequestID& id,
                           const GURL& url,
                           bool accept) {
    if (!PermissionBubbleManager::Enabled()) {
      context->GetInfoBarController()->OnPermissionSet(
          id, url, url, accept, accept);
      return;
    }

    PermissionBubbleManager* manager =
        PermissionBubbleManager::FromWebContents(web_contents());
    if (accept)
      manager->Accept();
    else
      manager->Closing();
  }

  // Use either the bubble or the infobar.
  void StartUsingPermissionBubble() {
    base::CommandLine::ForCurrentProcess()->AppendSwitch(
        switches::kEnablePermissionsBubbles);
    EXPECT_TRUE(PermissionBubbleManager::Enabled());
  }

  void TestAskAndGrant_TestContent() {
    TestPermissionContext permission_context(
        profile(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
    GURL url("http://www.google.com");
    content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);

    const PermissionRequestID id(
        web_contents()->GetRenderProcessHost()->GetID(),
        web_contents()->GetRenderViewHost()->GetRoutingID(),
        -1, GURL());
    permission_context.RequestPermission(
        web_contents(),
        id, url, true,
        base::Bind(&TestPermissionContext::TrackPermissionDecision,
                   base::Unretained(&permission_context)));

    RespondToPermission(&permission_context, id, url, true);
    EXPECT_TRUE(permission_context.permission_set());
    EXPECT_TRUE(permission_context.permission_granted());
    EXPECT_TRUE(permission_context.tab_context_updated());

    ContentSetting setting =
        profile()->GetHostContentSettingsMap()->GetContentSetting(
            url.GetOrigin(), url.GetOrigin(),
            CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string());
    EXPECT_EQ(CONTENT_SETTING_ALLOW , setting);
  }

  void TestAskAndDismiss_TestContent() {
    TestPermissionContext permission_context(
        profile(), CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
    GURL url("http://www.google.es");
    content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);

    const PermissionRequestID id(
        web_contents()->GetRenderProcessHost()->GetID(),
        web_contents()->GetRenderViewHost()->GetRoutingID(),
        -1, GURL());
    permission_context.RequestPermission(
        web_contents(),
        id, url, true,
        base::Bind(&TestPermissionContext::TrackPermissionDecision,
                   base::Unretained(&permission_context)));

    RespondToPermission(&permission_context, id, url, false);
    EXPECT_TRUE(permission_context.permission_set());
    EXPECT_FALSE(permission_context.permission_granted());
    EXPECT_TRUE(permission_context.tab_context_updated());

    ContentSetting setting =
        profile()->GetHostContentSettingsMap()->GetContentSetting(
            url.GetOrigin(), url.GetOrigin(),
            CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string());
    EXPECT_EQ(CONTENT_SETTING_ASK , setting);
  }

 private:
  // ChromeRenderViewHostTestHarness:
  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();
    InfoBarService::CreateForWebContents(web_contents());
    PermissionBubbleManager::CreateForWebContents(web_contents());
  }

  DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests);
};

// Simulates clicking Accept. The permission should be granted and
// saved for future use.
TEST_F(PermissionContextBaseTests, TestAskAndGrant) {
  TestAskAndGrant_TestContent();
  StartUsingPermissionBubble();
  TestAskAndGrant_TestContent();
};

// Simulates clicking Dismiss (X) in the infobar/bubble.
// The permission should be denied but not saved for future use.
TEST_F(PermissionContextBaseTests, TestAskAndDismiss) {
  TestAskAndDismiss_TestContent();
  StartUsingPermissionBubble();
  TestAskAndDismiss_TestContent();
};