Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: chrome/browser/extensions/extension_user_script_loader_unittest.cc

Issue 2227193002: Make UserScript non-copyable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: uplaod with base Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "extensions/browser/extension_user_script_loader.h" 5 #include "extensions/browser/extension_user_script_loader.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 11
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/files/scoped_temp_dir.h" 14 #include "base/files/scoped_temp_dir.h"
15 #include "base/location.h" 15 #include "base/location.h"
16 #include "base/memory/ptr_util.h"
16 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
18 #include "chrome/browser/chrome_notification_types.h" 19 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/test/base/testing_profile.h" 20 #include "chrome/test/base/testing_profile.h"
20 #include "content/public/browser/notification_observer.h" 21 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h" 22 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/notification_service.h" 23 #include "content/public/browser/notification_service.h"
23 #include "content/public/test/test_browser_thread.h" 24 #include "content/public/test/test_browser_thread.h"
25 #include "extensions/browser/browser_user_script.h"
24 #include "extensions/common/host_id.h" 26 #include "extensions/common/host_id.h"
25 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
26 28
27 using content::BrowserThread; 29 using content::BrowserThread;
28 using extensions::URLPatternSet; 30 using extensions::URLPatternSet;
29 31
30 namespace { 32 namespace {
31 33
32 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { 34 static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
33 int schemes = URLPattern::SCHEME_ALL; 35 int schemes = URLPattern::SCHEME_ALL;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 "// \n" 119 "// \n"
118 "// @othergarbage\n" 120 "// @othergarbage\n"
119 "// @include *mail.yahoo.com*\r\n" 121 "// @include *mail.yahoo.com*\r\n"
120 "// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK 122 "// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK
121 "//@include not-recognized\n" // must have one space after "//" 123 "//@include not-recognized\n" // must have one space after "//"
122 "// ==/UserScript== trailing garbage\n" 124 "// ==/UserScript== trailing garbage\n"
123 "\n" 125 "\n"
124 "\n" 126 "\n"
125 "alert('hoo!');\n"); 127 "alert('hoo!');\n");
126 128
127 UserScript script; 129 BrowserUserScript script;
128 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); 130 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
129 ASSERT_EQ(3U, script.globs().size()); 131 ASSERT_EQ(3U, script.globs().size());
130 EXPECT_EQ("*mail.google.com*", script.globs()[0]); 132 EXPECT_EQ("*mail.google.com*", script.globs()[0]);
131 EXPECT_EQ("*mail.yahoo.com*", script.globs()[1]); 133 EXPECT_EQ("*mail.yahoo.com*", script.globs()[1]);
132 EXPECT_EQ("*mail.msn.com*", script.globs()[2]); 134 EXPECT_EQ("*mail.msn.com*", script.globs()[2]);
133 } 135 }
134 136
135 TEST_F(ExtensionUserScriptLoaderTest, Parse2) { 137 TEST_F(ExtensionUserScriptLoaderTest, Parse2) {
136 const std::string text("default to @include *"); 138 const std::string text("default to @include *");
137 139
138 UserScript script; 140 BrowserUserScript script;
139 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); 141 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
140 ASSERT_EQ(1U, script.globs().size()); 142 ASSERT_EQ(1U, script.globs().size());
141 EXPECT_EQ("*", script.globs()[0]); 143 EXPECT_EQ("*", script.globs()[0]);
142 } 144 }
143 145
144 TEST_F(ExtensionUserScriptLoaderTest, Parse3) { 146 TEST_F(ExtensionUserScriptLoaderTest, Parse3) {
145 const std::string text( 147 const std::string text(
146 "// ==UserScript==\n" 148 "// ==UserScript==\n"
147 "// @include *foo*\n" 149 "// @include *foo*\n"
148 "// ==/UserScript=="); // no trailing newline 150 "// ==/UserScript=="); // no trailing newline
149 151
150 UserScript script; 152 BrowserUserScript script;
151 ExtensionUserScriptLoader::ParseMetadataHeader(text, &script); 153 ExtensionUserScriptLoader::ParseMetadataHeader(text, &script);
152 ASSERT_EQ(1U, script.globs().size()); 154 ASSERT_EQ(1U, script.globs().size());
153 EXPECT_EQ("*foo*", script.globs()[0]); 155 EXPECT_EQ("*foo*", script.globs()[0]);
154 } 156 }
155 157
156 TEST_F(ExtensionUserScriptLoaderTest, Parse4) { 158 TEST_F(ExtensionUserScriptLoaderTest, Parse4) {
157 const std::string text( 159 const std::string text(
158 "// ==UserScript==\n" 160 "// ==UserScript==\n"
159 "// @match http://*.mail.google.com/*\n" 161 "// @match http://*.mail.google.com/*\n"
160 "// @match \t http://mail.yahoo.com/*\n" 162 "// @match \t http://mail.yahoo.com/*\n"
161 "// ==/UserScript==\n"); 163 "// ==/UserScript==\n");
162 164
163 URLPatternSet expected_patterns; 165 URLPatternSet expected_patterns;
164 AddPattern(&expected_patterns, "http://*.mail.google.com/*"); 166 AddPattern(&expected_patterns, "http://*.mail.google.com/*");
165 AddPattern(&expected_patterns, "http://mail.yahoo.com/*"); 167 AddPattern(&expected_patterns, "http://mail.yahoo.com/*");
166 168
167 UserScript script; 169 BrowserUserScript script;
168 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); 170 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
169 EXPECT_EQ(0U, script.globs().size()); 171 EXPECT_EQ(0U, script.globs().size());
170 EXPECT_EQ(expected_patterns, script.url_patterns()); 172 EXPECT_EQ(expected_patterns, script.url_patterns());
171 } 173 }
172 174
173 TEST_F(ExtensionUserScriptLoaderTest, Parse5) { 175 TEST_F(ExtensionUserScriptLoaderTest, Parse5) {
174 const std::string text( 176 const std::string text(
175 "// ==UserScript==\n" 177 "// ==UserScript==\n"
176 "// @match http://*mail.google.com/*\n" 178 "// @match http://*mail.google.com/*\n"
177 "// ==/UserScript==\n"); 179 "// ==/UserScript==\n");
178 180
179 // Invalid @match value. 181 // Invalid @match value.
180 UserScript script; 182 BrowserUserScript script;
181 EXPECT_FALSE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); 183 EXPECT_FALSE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
182 } 184 }
183 185
184 TEST_F(ExtensionUserScriptLoaderTest, Parse6) { 186 TEST_F(ExtensionUserScriptLoaderTest, Parse6) {
185 const std::string text( 187 const std::string text(
186 "// ==UserScript==\n" 188 "// ==UserScript==\n"
187 "// @include http://*.mail.google.com/*\n" 189 "// @include http://*.mail.google.com/*\n"
188 "// @match \t http://mail.yahoo.com/*\n" 190 "// @match \t http://mail.yahoo.com/*\n"
189 "// ==/UserScript==\n"); 191 "// ==/UserScript==\n");
190 192
191 // Allowed to match @include and @match. 193 // Allowed to match @include and @match.
192 UserScript script; 194 BrowserUserScript script;
193 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); 195 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
194 } 196 }
195 197
196 TEST_F(ExtensionUserScriptLoaderTest, Parse7) { 198 TEST_F(ExtensionUserScriptLoaderTest, Parse7) {
197 // Greasemonkey allows there to be any leading text before the comment marker. 199 // Greasemonkey allows there to be any leading text before the comment marker.
198 const std::string text( 200 const std::string text(
199 "// ==UserScript==\n" 201 "// ==UserScript==\n"
200 "adsasdfasf// @name hello\n" 202 "adsasdfasf// @name hello\n"
201 " // @description\twiggity woo\n" 203 " // @description\twiggity woo\n"
202 "\t// @match \t http://mail.yahoo.com/*\n" 204 "\t// @match \t http://mail.yahoo.com/*\n"
203 "// ==/UserScript==\n"); 205 "// ==/UserScript==\n");
204 206
205 UserScript script; 207 BrowserUserScript script;
206 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); 208 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
207 ASSERT_EQ("hello", script.name()); 209 ASSERT_EQ("hello", script.name());
208 ASSERT_EQ("wiggity woo", script.description()); 210 ASSERT_EQ("wiggity woo", script.description());
209 ASSERT_EQ(1U, script.url_patterns().patterns().size()); 211 ASSERT_EQ(1U, script.url_patterns().patterns().size());
210 EXPECT_EQ("http://mail.yahoo.com/*", 212 EXPECT_EQ("http://mail.yahoo.com/*",
211 script.url_patterns().begin()->GetAsString()); 213 script.url_patterns().begin()->GetAsString());
212 } 214 }
213 215
214 TEST_F(ExtensionUserScriptLoaderTest, Parse8) { 216 TEST_F(ExtensionUserScriptLoaderTest, Parse8) {
215 const std::string text( 217 const std::string text(
216 "// ==UserScript==\n" 218 "// ==UserScript==\n"
217 "// @name myscript\n" 219 "// @name myscript\n"
218 "// @match http://www.google.com/*\n" 220 "// @match http://www.google.com/*\n"
219 "// @exclude_match http://www.google.com/foo*\n" 221 "// @exclude_match http://www.google.com/foo*\n"
220 "// ==/UserScript==\n"); 222 "// ==/UserScript==\n");
221 223
222 UserScript script; 224 BrowserUserScript script;
223 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); 225 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
224 ASSERT_EQ("myscript", script.name()); 226 ASSERT_EQ("myscript", script.name());
225 ASSERT_EQ(1U, script.url_patterns().patterns().size()); 227 ASSERT_EQ(1U, script.url_patterns().patterns().size());
226 EXPECT_EQ("http://www.google.com/*", 228 EXPECT_EQ("http://www.google.com/*",
227 script.url_patterns().begin()->GetAsString()); 229 script.url_patterns().begin()->GetAsString());
228 ASSERT_EQ(1U, script.exclude_url_patterns().patterns().size()); 230 ASSERT_EQ(1U, script.exclude_url_patterns().patterns().size());
229 EXPECT_EQ("http://www.google.com/foo*", 231 EXPECT_EQ("http://www.google.com/foo*",
230 script.exclude_url_patterns().begin()->GetAsString()); 232 script.exclude_url_patterns().begin()->GetAsString());
231 } 233 }
232 234
233 TEST_F(ExtensionUserScriptLoaderTest, SkipBOMAtTheBeginning) { 235 TEST_F(ExtensionUserScriptLoaderTest, SkipBOMAtTheBeginning) {
234 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js"); 236 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js");
235 const std::string content("\xEF\xBB\xBF alert('hello');"); 237 const std::string content("\xEF\xBB\xBF alert('hello');");
236 size_t written = base::WriteFile(path, content.c_str(), content.size()); 238 size_t written = base::WriteFile(path, content.c_str(), content.size());
237 ASSERT_EQ(written, content.size()); 239 ASSERT_EQ(written, content.size());
238 240
239 UserScript user_script; 241 std::unique_ptr<BrowserUserScript> user_script(new BrowserUserScript);
240 user_script.js_scripts().push_back( 242 std::unique_ptr<BrowserScriptFile> file1(
241 UserScript::File(temp_dir_.path(), path.BaseName(), GURL())); 243 new BrowserScriptFile(temp_dir_.path(), path.BaseName(), GURL()));
244 user_script->js_scripts().push_back(std::move(file1));
242 245
243 UserScriptList user_scripts; 246 BrowserUserScriptList user_scripts;
244 user_scripts.push_back(user_script); 247 user_scripts.push_back(std::move(user_script));
245 248
246 TestingProfile profile; 249 TestingProfile profile;
247 ExtensionUserScriptLoader loader( 250 ExtensionUserScriptLoader loader(
248 &profile, 251 &profile,
249 HostID(), 252 HostID(),
250 true /* listen_for_extension_system_loaded */); 253 true /* listen_for_extension_system_loaded */);
251 loader.LoadScriptsForTest(&user_scripts); 254 loader.LoadScriptsForTest(&user_scripts);
252 255
253 EXPECT_EQ(content.substr(3), 256 EXPECT_EQ(content.substr(3),
254 user_scripts[0].js_scripts()[0].GetContent().as_string()); 257 user_scripts[0]->js_scripts()[0]->GetContent().as_string());
255 } 258 }
256 259
257 TEST_F(ExtensionUserScriptLoaderTest, LeaveBOMNotAtTheBeginning) { 260 TEST_F(ExtensionUserScriptLoaderTest, LeaveBOMNotAtTheBeginning) {
258 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js"); 261 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js");
259 const std::string content("alert('here's a BOOM: \xEF\xBB\xBF');"); 262 const std::string content("alert('here's a BOOM: \xEF\xBB\xBF');");
260 size_t written = base::WriteFile(path, content.c_str(), content.size()); 263 size_t written = base::WriteFile(path, content.c_str(), content.size());
261 ASSERT_EQ(written, content.size()); 264 ASSERT_EQ(written, content.size());
262 265
263 UserScript user_script; 266 std::unique_ptr<BrowserUserScript> user_script(new BrowserUserScript);
264 user_script.js_scripts().push_back(UserScript::File( 267 user_script->js_scripts().push_back(base::WrapUnique(
265 temp_dir_.path(), path.BaseName(), GURL())); 268 new BrowserScriptFile(temp_dir_.path(), path.BaseName(), GURL())));
266 269
267 UserScriptList user_scripts; 270 BrowserUserScriptList user_scripts;
268 user_scripts.push_back(user_script); 271 user_scripts.push_back(std::move(user_script));
269 272
270 TestingProfile profile; 273 TestingProfile profile;
271 ExtensionUserScriptLoader loader( 274 ExtensionUserScriptLoader loader(
272 &profile, 275 &profile,
273 HostID(), 276 HostID(),
274 true /* listen_for_extension_system_loaded */); 277 true /* listen_for_extension_system_loaded */);
275 loader.LoadScriptsForTest(&user_scripts); 278 loader.LoadScriptsForTest(&user_scripts);
276 279
277 EXPECT_EQ(content, user_scripts[0].js_scripts()[0].GetContent().as_string()); 280 EXPECT_EQ(content,
281 user_scripts[0]->js_scripts()[0]->GetContent().as_string());
278 } 282 }
279 283
280 } // namespace extensions 284 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698