OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/content_settings/permission_context_base.h" | 5 #include "chrome/browser/content_settings/permission_context_base.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "chrome/browser/content_settings/host_content_settings_map.h" | 8 #include "chrome/browser/content_settings/host_content_settings_map.h" |
9 #include "chrome/browser/content_settings/permission_queue_controller.h" | 9 #include "chrome/browser/content_settings/permission_queue_controller.h" |
10 #include "chrome/browser/content_settings/permission_request_id.h" | 10 #include "chrome/browser/content_settings/permission_request_id.h" |
11 #include "chrome/browser/infobars/infobar_service.h" | 11 #include "chrome/browser/infobars/infobar_service.h" |
| 12 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
12 #include "chrome/common/content_settings.h" | 13 #include "chrome/common/content_settings.h" |
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
14 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
15 #include "components/content_settings/core/common/content_settings_types.h" | 16 #include "components/content_settings/core/common/content_settings_types.h" |
16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
17 #include "content/public/test/mock_render_process_host.h" | 18 #include "content/public/test/mock_render_process_host.h" |
18 #include "content/public/test/web_contents_tester.h" | 19 #include "content/public/test/web_contents_tester.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
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 { | 22 class TestPermissionContext : public PermissionContextBase { |
37 public: | 23 public: |
38 TestPermissionContext(Profile* profile, | 24 TestPermissionContext(Profile* profile, |
39 const ContentSettingsType permission_type) | 25 const ContentSettingsType permission_type) |
40 : PermissionContextBase(profile, permission_type), | 26 : PermissionContextBase(profile, permission_type), |
41 permission_set_(false), | 27 permission_set_(false), |
42 permission_granted_(false), | 28 permission_granted_(false), |
43 tab_context_updated_(false) {} | 29 tab_context_updated_(false) {} |
44 | 30 |
45 virtual ~TestPermissionContext() {} | 31 virtual ~TestPermissionContext() {} |
(...skipping 25 matching lines...) Expand all Loading... |
71 bool allowed) OVERRIDE { | 57 bool allowed) OVERRIDE { |
72 tab_context_updated_ = true; | 58 tab_context_updated_ = true; |
73 } | 59 } |
74 | 60 |
75 private: | 61 private: |
76 bool permission_set_; | 62 bool permission_set_; |
77 bool permission_granted_; | 63 bool permission_granted_; |
78 bool tab_context_updated_; | 64 bool tab_context_updated_; |
79 }; | 65 }; |
80 | 66 |
| 67 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
| 68 protected: |
| 69 PermissionContextBaseTests() {} |
| 70 virtual ~PermissionContextBaseTests() {} |
| 71 |
| 72 // Accept or dismiss the permission bubble or infobar. |
| 73 void RespondToPermission(TestPermissionContext* context, |
| 74 const PermissionRequestID& id, |
| 75 const GURL& url, |
| 76 bool accept) { |
| 77 if (!PermissionBubbleManager::Enabled()) { |
| 78 context->GetInfoBarController()->OnPermissionSet( |
| 79 id, url, url, accept, accept); |
| 80 return; |
| 81 } |
| 82 |
| 83 PermissionBubbleManager* manager = |
| 84 PermissionBubbleManager::FromWebContents(web_contents()); |
| 85 if (accept) |
| 86 manager->Accept(); |
| 87 else |
| 88 manager->Closing(); |
| 89 } |
| 90 |
| 91 private: |
| 92 // ChromeRenderViewHostTestHarness: |
| 93 virtual void SetUp() OVERRIDE { |
| 94 ChromeRenderViewHostTestHarness::SetUp(); |
| 95 InfoBarService::CreateForWebContents(web_contents()); |
| 96 PermissionBubbleManager::CreateForWebContents(web_contents()); |
| 97 } |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests); |
| 100 }; |
| 101 |
81 // Simulates clicking Accept. The permission should be granted and | 102 // Simulates clicking Accept. The permission should be granted and |
82 // saved for future use. | 103 // saved for future use. |
83 TEST_F(PermissionContextBaseTests, TestAskAndGrant) { | 104 TEST_F(PermissionContextBaseTests, TestAskAndGrant) { |
84 TestPermissionContext permission_context(profile(), | 105 TestPermissionContext permission_context(profile(), |
85 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); | 106 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
86 GURL url("http://www.google.com"); | 107 GURL url("http://www.google.com"); |
87 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 108 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
88 | 109 |
89 const PermissionRequestID id( | 110 const PermissionRequestID id( |
90 web_contents()->GetRenderProcessHost()->GetID(), | 111 web_contents()->GetRenderProcessHost()->GetID(), |
91 web_contents()->GetRenderViewHost()->GetRoutingID(), | 112 web_contents()->GetRenderViewHost()->GetRoutingID(), |
92 -1, GURL()); | 113 -1, GURL()); |
93 permission_context.RequestPermission( | 114 permission_context.RequestPermission( |
94 web_contents(), | 115 web_contents(), |
95 id, url, true, | 116 id, url, true, |
96 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 117 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
97 base::Unretained(&permission_context))); | 118 base::Unretained(&permission_context))); |
98 | 119 |
99 permission_context.GetInfoBarController()->OnPermissionSet( | 120 RespondToPermission(&permission_context, id, url, true); |
100 id, url, url, true, true); | |
101 EXPECT_TRUE(permission_context.permission_set()); | 121 EXPECT_TRUE(permission_context.permission_set()); |
102 EXPECT_TRUE(permission_context.permission_granted()); | 122 EXPECT_TRUE(permission_context.permission_granted()); |
103 EXPECT_TRUE(permission_context.tab_context_updated()); | 123 EXPECT_TRUE(permission_context.tab_context_updated()); |
104 | 124 |
105 ContentSetting setting = | 125 ContentSetting setting = |
106 profile()->GetHostContentSettingsMap()->GetContentSetting( | 126 profile()->GetHostContentSettingsMap()->GetContentSetting( |
107 url.GetOrigin(), url.GetOrigin(), | 127 url.GetOrigin(), url.GetOrigin(), |
108 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, std::string()); | 128 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, std::string()); |
109 EXPECT_EQ(CONTENT_SETTING_ALLOW , setting); | 129 EXPECT_EQ(CONTENT_SETTING_ALLOW , setting); |
110 }; | 130 }; |
111 | 131 |
112 // Simulates clicking Dismiss (X in the infobar. | 132 // Simulates clicking Dismiss (X in the infobar. |
113 // The permission should be denied but not saved for future use. | 133 // The permission should be denied but not saved for future use. |
114 TEST_F(PermissionContextBaseTests, TestAskAndDismiss) { | 134 TEST_F(PermissionContextBaseTests, TestAskAndDismiss) { |
115 TestPermissionContext permission_context(profile(), | 135 TestPermissionContext permission_context(profile(), |
116 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 136 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
117 GURL url("http://www.google.es"); | 137 GURL url("http://www.google.es"); |
118 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 138 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
119 | 139 |
120 const PermissionRequestID id( | 140 const PermissionRequestID id( |
121 web_contents()->GetRenderProcessHost()->GetID(), | 141 web_contents()->GetRenderProcessHost()->GetID(), |
122 web_contents()->GetRenderViewHost()->GetRoutingID(), | 142 web_contents()->GetRenderViewHost()->GetRoutingID(), |
123 -1, GURL()); | 143 -1, GURL()); |
124 permission_context.RequestPermission( | 144 permission_context.RequestPermission( |
125 web_contents(), | 145 web_contents(), |
126 id, url, true, | 146 id, url, true, |
127 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 147 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
128 base::Unretained(&permission_context))); | 148 base::Unretained(&permission_context))); |
129 | 149 |
130 permission_context.GetInfoBarController()->OnPermissionSet( | 150 RespondToPermission(&permission_context, id, url, false); |
131 id, url, url, false, false); | |
132 EXPECT_TRUE(permission_context.permission_set()); | 151 EXPECT_TRUE(permission_context.permission_set()); |
133 EXPECT_FALSE(permission_context.permission_granted()); | 152 EXPECT_FALSE(permission_context.permission_granted()); |
134 EXPECT_TRUE(permission_context.tab_context_updated()); | 153 EXPECT_TRUE(permission_context.tab_context_updated()); |
135 | 154 |
136 ContentSetting setting = | 155 ContentSetting setting = |
137 profile()->GetHostContentSettingsMap()->GetContentSetting( | 156 profile()->GetHostContentSettingsMap()->GetContentSetting( |
138 url.GetOrigin(), url.GetOrigin(), | 157 url.GetOrigin(), url.GetOrigin(), |
139 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string()); | 158 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string()); |
140 EXPECT_EQ(CONTENT_SETTING_ASK , setting); | 159 EXPECT_EQ(CONTENT_SETTING_ASK , setting); |
141 }; | 160 }; |
OLD | NEW |