OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/storage/durable_storage_permission_context.h" | 5 #include "chrome/browser/storage/durable_storage_permission_context.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | |
11 #include "chrome/browser/content_settings/cookie_settings_factory.h" | |
12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
13 #include "chrome/browser/permissions/permission_request_id.h" | |
14 #include "chrome/browser/permissions/permission_request_manager.h" | |
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
16 #include "chrome/test/base/testing_profile.h" | |
17 #include "components/bookmarks/test/bookmark_test_helpers.h" | |
18 #include "components/content_settings/core/browser/cookie_settings.h" | |
19 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
20 #include "content/public/browser/permission_manager.h" | |
21 #include "content/public/browser/render_process_host.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
9 | 23 |
10 using bookmarks::BookmarkModel; | 24 using bookmarks::BookmarkModel; |
11 | 25 |
26 namespace { | |
27 | |
28 void DoNothing(ContentSetting content_setting) {} | |
29 | |
30 class TestDurablePermissionContext : public DurableStoragePermissionContext { | |
31 public: | |
32 explicit TestDurablePermissionContext(Profile* profile) | |
33 : DurableStoragePermissionContext(profile), | |
34 permission_set_count_(0), | |
35 last_permission_set_persisted_(false), | |
36 last_permission_set_setting_(CONTENT_SETTING_DEFAULT) {} | |
37 | |
38 int permission_set_count() const { return permission_set_count_; } | |
39 bool last_permission_set_persisted() const { | |
40 return last_permission_set_persisted_; | |
41 } | |
42 ContentSetting last_permission_set_setting() const { | |
43 return last_permission_set_setting_; | |
44 } | |
45 | |
46 ContentSetting GetContentSettingFromMap(const GURL& url_a, | |
47 const GURL& url_b) { | |
48 return HostContentSettingsMapFactory::GetForProfile(profile()) | |
49 ->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), | |
50 CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, | |
51 std::string()); | |
52 } | |
53 | |
54 private: | |
55 // NotificationPermissionContext: | |
56 void NotifyPermissionSet(const PermissionRequestID& id, | |
57 const GURL& requesting_origin, | |
58 const GURL& embedder_origin, | |
59 const BrowserPermissionCallback& callback, | |
60 bool persist, | |
61 ContentSetting content_setting) override { | |
62 permission_set_count_++; | |
63 last_permission_set_persisted_ = persist; | |
64 last_permission_set_setting_ = content_setting; | |
65 DurableStoragePermissionContext::NotifyPermissionSet( | |
66 id, requesting_origin, embedder_origin, callback, persist, | |
67 content_setting); | |
68 } | |
69 | |
70 int permission_set_count_; | |
71 bool last_permission_set_persisted_; | |
72 ContentSetting last_permission_set_setting_; | |
73 }; | |
74 | |
75 } // namespace | |
76 | |
12 class BookmarksOriginTest : public ::testing::Test { | 77 class BookmarksOriginTest : public ::testing::Test { |
13 protected: | 78 protected: |
14 static std::vector<BookmarkModel::URLAndTitle> MakeBookmarks( | 79 static std::vector<BookmarkModel::URLAndTitle> MakeBookmarks( |
15 const std::string urls[], | 80 const std::string urls[], |
16 const int array_size) { | 81 const int array_size) { |
17 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 82 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
18 for (int i = 0; i < array_size; ++i) { | 83 for (int i = 0; i < array_size; ++i) { |
19 BookmarkModel::URLAndTitle bookmark; | 84 BookmarkModel::URLAndTitle bookmark; |
20 bookmark.url = GURL(urls[i]); | 85 bookmark.url = GURL(urls[i]); |
21 EXPECT_TRUE(bookmark.url.is_valid()); | 86 EXPECT_TRUE(bookmark.url.is_valid()); |
(...skipping 20 matching lines...) Expand all Loading... | |
42 std::string urls[] = { | 107 std::string urls[] = { |
43 "http://www.google.com/", | 108 "http://www.google.com/", |
44 "https://www.google.com/", | 109 "https://www.google.com/", |
45 }; | 110 }; |
46 std::vector<BookmarkModel::URLAndTitle> bookmarks = | 111 std::vector<BookmarkModel::URLAndTitle> bookmarks = |
47 MakeBookmarks(urls, arraysize(urls)); | 112 MakeBookmarks(urls, arraysize(urls)); |
48 GURL looking_for("https://dogs.com"); | 113 GURL looking_for("https://dogs.com"); |
49 EXPECT_FALSE(DurableStoragePermissionContext::IsOriginBookmarked( | 114 EXPECT_FALSE(DurableStoragePermissionContext::IsOriginBookmarked( |
50 bookmarks, looking_for)); | 115 bookmarks, looking_for)); |
51 } | 116 } |
117 | |
118 class DurableStoragePermissionContextTest | |
119 : public ChromeRenderViewHostTestHarness { | |
120 protected: | |
121 void SetUp() override { | |
122 ChromeRenderViewHostTestHarness::SetUp(); | |
123 #if defined(OS_ANDROID) | |
124 InfoBarService::CreateForWebContents(web_contents()); | |
125 #else | |
126 PermissionRequestManager::CreateForWebContents(web_contents()); | |
127 #endif | |
128 } | |
129 | |
130 void AddBookmark(const GURL& origin) { | |
131 if (!model_) { | |
132 profile()->CreateBookmarkModel(true); | |
133 model_ = BookmarkModelFactory::GetForBrowserContext(profile()); | |
134 bookmarks::test::WaitForBookmarkModelToLoad(model_); | |
135 } | |
136 | |
137 model_->AddURL(model_->bookmark_bar_node(), 0, | |
138 base::ASCIIToUTF16(origin.spec()), origin); | |
139 } | |
140 | |
141 BookmarkModel* model_ = nullptr; | |
142 }; | |
143 | |
144 TEST_F(DurableStoragePermissionContextTest, Bookmark) { | |
145 TestDurablePermissionContext permission_context( | |
146 profile()->GetOffTheRecordProfile()); | |
147 GURL url("http://www.google.com"); | |
148 AddBookmark(url); | |
149 NavigateAndCommit(url); | |
150 | |
151 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | |
152 web_contents()->GetMainFrame()->GetRoutingID(), | |
153 -1); | |
154 | |
155 ASSERT_EQ(0, permission_context.permission_set_count()); | |
156 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | |
157 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | |
158 permission_context.last_permission_set_setting()); | |
159 | |
160 permission_context.DecidePermission(web_contents(), id, url, url, | |
161 true /* user_gesture */, | |
162 base::Bind(&DoNothing)); | |
163 | |
164 EXPECT_EQ(1, permission_context.permission_set_count()); | |
165 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | |
166 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
167 permission_context.last_permission_set_setting()); | |
168 } | |
169 | |
170 TEST_F(DurableStoragePermissionContextTest, NoBookmark) { | |
171 TestDurablePermissionContext permission_context( | |
172 profile()->GetOffTheRecordProfile()); | |
173 GURL url("http://www.google.com"); | |
174 NavigateAndCommit(url); | |
175 | |
176 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | |
177 web_contents()->GetMainFrame()->GetRoutingID(), | |
178 -1); | |
179 | |
180 ASSERT_EQ(0, permission_context.permission_set_count()); | |
181 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | |
182 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | |
183 permission_context.last_permission_set_setting()); | |
184 | |
185 permission_context.DecidePermission(web_contents(), id, url, url, | |
186 true /* user_gesture */, | |
187 base::Bind(&DoNothing)); | |
188 | |
189 EXPECT_EQ(1, permission_context.permission_set_count()); | |
190 EXPECT_FALSE(permission_context.last_permission_set_persisted()); | |
191 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | |
192 permission_context.last_permission_set_setting()); | |
193 } | |
194 | |
195 TEST_F(DurableStoragePermissionContextTest, CookiesNotAllowed) { | |
jww
2016/10/01 04:46:20
You probably want a "CookiesAllowed" variant of th
dmurph
2016/10/04 22:00:45
Cookies are allowed in all other tests.
| |
196 TestDurablePermissionContext permission_context( | |
197 profile()->GetOffTheRecordProfile()); | |
198 GURL url("http://www.google.com"); | |
199 AddBookmark(url); | |
200 NavigateAndCommit(url); | |
201 | |
202 scoped_refptr<content_settings::CookieSettings> cookie_settings = | |
203 CookieSettingsFactory::GetForProfile(profile()); | |
204 | |
205 cookie_settings->SetCookieSetting(url, CONTENT_SETTING_BLOCK); | |
206 | |
207 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | |
208 web_contents()->GetMainFrame()->GetRoutingID(), | |
209 -1); | |
210 | |
211 ASSERT_EQ(0, permission_context.permission_set_count()); | |
212 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | |
213 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | |
214 permission_context.last_permission_set_setting()); | |
215 | |
216 permission_context.DecidePermission(web_contents(), id, url, url, | |
jww
2016/10/01 04:46:20
Maybe a few other sanity checks of other variants
dmurph
2016/10/04 22:00:45
Added test for different embedder & requestor urls
| |
217 true /* user_gesture */, | |
218 base::Bind(&DoNothing)); | |
219 | |
220 EXPECT_EQ(1, permission_context.permission_set_count()); | |
221 EXPECT_FALSE(permission_context.last_permission_set_persisted()); | |
222 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | |
223 permission_context.last_permission_set_setting()); | |
224 } | |
OLD | NEW |