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

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

Issue 11724002: Move ContentScripts out of Extension (Closed) Base URL: http://git.chromium.org/chromium/src.git@dc_unref_browser_action
Patch Set: Latest master for CQ Created 7 years, 9 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/extensions/convert_user_script.h" 14 #include "chrome/browser/extensions/convert_user_script.h"
15 #include "chrome/common/chrome_paths.h" 15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/extensions/extension.h" 16 #include "chrome/common/extensions/extension.h"
17 #include "chrome/common/extensions/manifest_handler.h"
18 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
17 #include "extensions/common/constants.h" 19 #include "extensions/common/constants.h"
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 21
20 namespace extensions { 22 namespace extensions {
21 23
22 namespace { 24 namespace {
23 25
24 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { 26 static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
25 int schemes = URLPattern::SCHEME_ALL; 27 int schemes = URLPattern::SCHEME_ALL;
26 extent->AddPattern(URLPattern(schemes, pattern)); 28 extent->AddPattern(URLPattern(schemes, pattern));
27 } 29 }
28 30
29 } 31 }
30 32
31 TEST(ExtensionFromUserScript, Basic) { 33 class ExtensionFromUserScript : public testing::Test {
34 public:
35 virtual void SetUp() OVERRIDE {
36 testing::Test::SetUp();
37 (new ContentScriptsHandler)->Register();
38 }
39
40 virtual void TearDown() OVERRIDE {
41 testing::Test::TearDown();
42 ManifestHandler::ClearRegistryForTesting();
43 }
44 };
45
46 TEST_F(ExtensionFromUserScript, Basic) {
32 base::ScopedTempDir extensions_dir; 47 base::ScopedTempDir extensions_dir;
33 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); 48 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
34 49
35 base::FilePath test_file; 50 base::FilePath test_file;
36 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); 51 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file));
37 test_file = test_file.AppendASCII("extensions") 52 test_file = test_file.AppendASCII("extensions")
38 .AppendASCII("user_script_basic.user.js"); 53 .AppendASCII("user_script_basic.user.js");
39 54
40 string16 error; 55 string16 error;
41 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( 56 scoped_refptr<Extension> extension(ConvertUserScriptToExtension(
42 test_file, GURL("http://www.google.com/foo"), 57 test_file, GURL("http://www.google.com/foo"),
43 extensions_dir.path(), &error)); 58 extensions_dir.path(), &error));
44 59
45 ASSERT_TRUE(extension.get()); 60 ASSERT_TRUE(extension.get());
46 EXPECT_EQ(string16(), error); 61 EXPECT_EQ(string16(), error);
47 62
48 // Use a temp dir so that the extensions dir will clean itself up. 63 // Use a temp dir so that the extensions dir will clean itself up.
49 base::ScopedTempDir ext_dir; 64 base::ScopedTempDir ext_dir;
50 EXPECT_TRUE(ext_dir.Set(extension->path())); 65 EXPECT_TRUE(ext_dir.Set(extension->path()));
51 66
52 // Validate generated extension metadata. 67 // Validate generated extension metadata.
53 EXPECT_EQ("My user script", extension->name()); 68 EXPECT_EQ("My user script", extension->name());
54 EXPECT_EQ("2.2.2", extension->VersionString()); 69 EXPECT_EQ("2.2.2", extension->VersionString());
55 EXPECT_EQ("Does totally awesome stuff.", extension->description()); 70 EXPECT_EQ("Does totally awesome stuff.", extension->description());
56 EXPECT_EQ("IhCFCg9PMQTAcJdc9ytUP99WME+4yh6aMnM1uupkovo=", 71 EXPECT_EQ("IhCFCg9PMQTAcJdc9ytUP99WME+4yh6aMnM1uupkovo=",
57 extension->public_key()); 72 extension->public_key());
58 73
59 ASSERT_EQ(1u, extension->content_scripts().size()); 74 ASSERT_EQ(1u, ContentScriptsInfo::GetContentScripts(extension).size());
60 const UserScript& script = extension->content_scripts()[0]; 75 const UserScript& script =
76 ContentScriptsInfo::GetContentScripts(extension)[0];
61 EXPECT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); 77 EXPECT_EQ(UserScript::DOCUMENT_IDLE, script.run_location());
62 ASSERT_EQ(2u, script.globs().size()); 78 ASSERT_EQ(2u, script.globs().size());
63 EXPECT_EQ("http://www.google.com/*", script.globs().at(0)); 79 EXPECT_EQ("http://www.google.com/*", script.globs().at(0));
64 EXPECT_EQ("http://www.yahoo.com/*", script.globs().at(1)); 80 EXPECT_EQ("http://www.yahoo.com/*", script.globs().at(1));
65 ASSERT_EQ(1u, script.exclude_globs().size()); 81 ASSERT_EQ(1u, script.exclude_globs().size());
66 EXPECT_EQ("*foo*", script.exclude_globs().at(0)); 82 EXPECT_EQ("*foo*", script.exclude_globs().at(0));
67 ASSERT_EQ(1u, script.url_patterns().patterns().size()); 83 ASSERT_EQ(1u, script.url_patterns().patterns().size());
68 EXPECT_EQ("http://www.google.com/*", 84 EXPECT_EQ("http://www.google.com/*",
69 script.url_patterns().begin()->GetAsString()); 85 script.url_patterns().begin()->GetAsString());
70 ASSERT_EQ(1u, script.exclude_url_patterns().patterns().size()); 86 ASSERT_EQ(1u, script.exclude_url_patterns().patterns().size());
71 EXPECT_EQ("http://www.google.com/foo*", 87 EXPECT_EQ("http://www.google.com/foo*",
72 script.exclude_url_patterns().begin()->GetAsString()); 88 script.exclude_url_patterns().begin()->GetAsString());
73 89
74 // Make sure the files actually exist on disk. 90 // Make sure the files actually exist on disk.
75 EXPECT_TRUE(file_util::PathExists( 91 EXPECT_TRUE(file_util::PathExists(
76 extension->path().Append(script.js_scripts()[0].relative_path()))); 92 extension->path().Append(script.js_scripts()[0].relative_path())));
77 EXPECT_TRUE(file_util::PathExists( 93 EXPECT_TRUE(file_util::PathExists(
78 extension->path().Append(kManifestFilename))); 94 extension->path().Append(kManifestFilename)));
79 } 95 }
80 96
81 TEST(ExtensionFromUserScript, NoMetdata) { 97 TEST_F(ExtensionFromUserScript, NoMetdata) {
82 base::ScopedTempDir extensions_dir; 98 base::ScopedTempDir extensions_dir;
83 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); 99 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
84 100
85 base::FilePath test_file; 101 base::FilePath test_file;
86 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); 102 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file));
87 test_file = test_file.AppendASCII("extensions") 103 test_file = test_file.AppendASCII("extensions")
88 .AppendASCII("user_script_no_metadata.user.js"); 104 .AppendASCII("user_script_no_metadata.user.js");
89 105
90 string16 error; 106 string16 error;
91 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( 107 scoped_refptr<Extension> extension(ConvertUserScriptToExtension(
92 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), 108 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"),
93 extensions_dir.path(), &error)); 109 extensions_dir.path(), &error));
94 110
95 ASSERT_TRUE(extension.get()); 111 ASSERT_TRUE(extension.get());
96 EXPECT_EQ(string16(), error); 112 EXPECT_EQ(string16(), error);
97 113
98 // Use a temp dir so that the extensions dir will clean itself up. 114 // Use a temp dir so that the extensions dir will clean itself up.
99 base::ScopedTempDir ext_dir; 115 base::ScopedTempDir ext_dir;
100 EXPECT_TRUE(ext_dir.Set(extension->path())); 116 EXPECT_TRUE(ext_dir.Set(extension->path()));
101 117
102 // Validate generated extension metadata. 118 // Validate generated extension metadata.
103 EXPECT_EQ("bar.user.js", extension->name()); 119 EXPECT_EQ("bar.user.js", extension->name());
104 EXPECT_EQ("1.0", extension->VersionString()); 120 EXPECT_EQ("1.0", extension->VersionString());
105 EXPECT_EQ("", extension->description()); 121 EXPECT_EQ("", extension->description());
106 EXPECT_EQ("k1WxKx54hX6tfl5gQaXD/m4d9QUMwRdXWM4RW+QkWcY=", 122 EXPECT_EQ("k1WxKx54hX6tfl5gQaXD/m4d9QUMwRdXWM4RW+QkWcY=",
107 extension->public_key()); 123 extension->public_key());
108 124
109 ASSERT_EQ(1u, extension->content_scripts().size()); 125 ASSERT_EQ(1u, ContentScriptsInfo::GetContentScripts(extension).size());
110 const UserScript& script = extension->content_scripts()[0]; 126 const UserScript& script =
127 ContentScriptsInfo::GetContentScripts(extension)[0];
111 ASSERT_EQ(1u, script.globs().size()); 128 ASSERT_EQ(1u, script.globs().size());
112 EXPECT_EQ("*", script.globs()[0]); 129 EXPECT_EQ("*", script.globs()[0]);
113 EXPECT_EQ(0u, script.exclude_globs().size()); 130 EXPECT_EQ(0u, script.exclude_globs().size());
114 131
115 URLPatternSet expected; 132 URLPatternSet expected;
116 AddPattern(&expected, "http://*/*"); 133 AddPattern(&expected, "http://*/*");
117 AddPattern(&expected, "https://*/*"); 134 AddPattern(&expected, "https://*/*");
118 EXPECT_EQ(expected, script.url_patterns()); 135 EXPECT_EQ(expected, script.url_patterns());
119 136
120 // Make sure the files actually exist on disk. 137 // Make sure the files actually exist on disk.
121 EXPECT_TRUE(file_util::PathExists( 138 EXPECT_TRUE(file_util::PathExists(
122 extension->path().Append(script.js_scripts()[0].relative_path()))); 139 extension->path().Append(script.js_scripts()[0].relative_path())));
123 EXPECT_TRUE(file_util::PathExists( 140 EXPECT_TRUE(file_util::PathExists(
124 extension->path().Append(kManifestFilename))); 141 extension->path().Append(kManifestFilename)));
125 } 142 }
126 143
127 TEST(ExtensionFromUserScript, NotUTF8) { 144 TEST_F(ExtensionFromUserScript, NotUTF8) {
128 base::ScopedTempDir extensions_dir; 145 base::ScopedTempDir extensions_dir;
129 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); 146 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
130 147
131 base::FilePath test_file; 148 base::FilePath test_file;
132 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); 149 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file));
133 test_file = test_file.AppendASCII("extensions") 150 test_file = test_file.AppendASCII("extensions")
134 .AppendASCII("user_script_not_utf8.user.js"); 151 .AppendASCII("user_script_not_utf8.user.js");
135 152
136 string16 error; 153 string16 error;
137 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( 154 scoped_refptr<Extension> extension(ConvertUserScriptToExtension(
138 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), 155 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"),
139 extensions_dir.path(), &error)); 156 extensions_dir.path(), &error));
140 157
141 ASSERT_FALSE(extension.get()); 158 ASSERT_FALSE(extension.get());
142 EXPECT_EQ(ASCIIToUTF16("User script must be UTF8 encoded."), error); 159 EXPECT_EQ(ASCIIToUTF16("User script must be UTF8 encoded."), error);
143 } 160 }
144 161
145 TEST(ExtensionFromUserScript, RunAtDocumentStart) { 162 TEST_F(ExtensionFromUserScript, RunAtDocumentStart) {
146 base::ScopedTempDir extensions_dir; 163 base::ScopedTempDir extensions_dir;
147 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); 164 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
148 165
149 base::FilePath test_file; 166 base::FilePath test_file;
150 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); 167 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file));
151 test_file = test_file.AppendASCII("extensions") 168 test_file = test_file.AppendASCII("extensions")
152 .AppendASCII("user_script_run_at_start.user.js"); 169 .AppendASCII("user_script_run_at_start.user.js");
153 170
154 string16 error; 171 string16 error;
155 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( 172 scoped_refptr<Extension> extension(ConvertUserScriptToExtension(
156 test_file, GURL("http://www.google.com/foo"), 173 test_file, GURL("http://www.google.com/foo"),
157 extensions_dir.path(), &error)); 174 extensions_dir.path(), &error));
158 175
159 ASSERT_TRUE(extension.get()); 176 ASSERT_TRUE(extension.get());
160 EXPECT_EQ(string16(), error); 177 EXPECT_EQ(string16(), error);
161 178
162 // Use a temp dir so that the extensions dir will clean itself up. 179 // Use a temp dir so that the extensions dir will clean itself up.
163 base::ScopedTempDir ext_dir; 180 base::ScopedTempDir ext_dir;
164 EXPECT_TRUE(ext_dir.Set(extension->path())); 181 EXPECT_TRUE(ext_dir.Set(extension->path()));
165 182
166 // Validate generated extension metadata. 183 // Validate generated extension metadata.
167 EXPECT_EQ("Document Start Test", extension->name()); 184 EXPECT_EQ("Document Start Test", extension->name());
168 EXPECT_EQ("This script tests document-start", extension->description()); 185 EXPECT_EQ("This script tests document-start", extension->description());
169 EXPECT_EQ("RjmyI7+Gp/YHcW1qnu4xDxkJcL4cV4kTzdCA4BajCbk=", 186 EXPECT_EQ("RjmyI7+Gp/YHcW1qnu4xDxkJcL4cV4kTzdCA4BajCbk=",
170 extension->public_key()); 187 extension->public_key());
171 188
172 // Validate run location. 189 // Validate run location.
173 ASSERT_EQ(1u, extension->content_scripts().size()); 190 ASSERT_EQ(1u, ContentScriptsInfo::GetContentScripts(extension).size());
174 const UserScript& script = extension->content_scripts()[0]; 191 const UserScript& script =
192 ContentScriptsInfo::GetContentScripts(extension)[0];
175 EXPECT_EQ(UserScript::DOCUMENT_START, script.run_location()); 193 EXPECT_EQ(UserScript::DOCUMENT_START, script.run_location());
176 } 194 }
177 195
178 TEST(ExtensionFromUserScript, RunAtDocumentEnd) { 196 TEST_F(ExtensionFromUserScript, RunAtDocumentEnd) {
179 base::ScopedTempDir extensions_dir; 197 base::ScopedTempDir extensions_dir;
180 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); 198 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
181 199
182 base::FilePath test_file; 200 base::FilePath test_file;
183 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); 201 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file));
184 test_file = test_file.AppendASCII("extensions") 202 test_file = test_file.AppendASCII("extensions")
185 .AppendASCII("user_script_run_at_end.user.js"); 203 .AppendASCII("user_script_run_at_end.user.js");
186 204
187 string16 error; 205 string16 error;
188 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( 206 scoped_refptr<Extension> extension(ConvertUserScriptToExtension(
189 test_file, GURL("http://www.google.com/foo"), 207 test_file, GURL("http://www.google.com/foo"),
190 extensions_dir.path(), &error)); 208 extensions_dir.path(), &error));
191 209
192 ASSERT_TRUE(extension.get()); 210 ASSERT_TRUE(extension.get());
193 EXPECT_EQ(string16(), error); 211 EXPECT_EQ(string16(), error);
194 212
195 // Use a temp dir so that the extensions dir will clean itself up. 213 // Use a temp dir so that the extensions dir will clean itself up.
196 base::ScopedTempDir ext_dir; 214 base::ScopedTempDir ext_dir;
197 EXPECT_TRUE(ext_dir.Set(extension->path())); 215 EXPECT_TRUE(ext_dir.Set(extension->path()));
198 216
199 // Validate generated extension metadata. 217 // Validate generated extension metadata.
200 EXPECT_EQ("Document End Test", extension->name()); 218 EXPECT_EQ("Document End Test", extension->name());
201 EXPECT_EQ("This script tests document-end", extension->description()); 219 EXPECT_EQ("This script tests document-end", extension->description());
202 EXPECT_EQ("cpr5i8Mi24FzECV8UJe6tanwlU8SWesZosJ915YISvQ=", 220 EXPECT_EQ("cpr5i8Mi24FzECV8UJe6tanwlU8SWesZosJ915YISvQ=",
203 extension->public_key()); 221 extension->public_key());
204 222
205 // Validate run location. 223 // Validate run location.
206 ASSERT_EQ(1u, extension->content_scripts().size()); 224 ASSERT_EQ(1u, ContentScriptsInfo::GetContentScripts(extension).size());
207 const UserScript& script = extension->content_scripts()[0]; 225 const UserScript& script =
226 ContentScriptsInfo::GetContentScripts(extension)[0];
208 EXPECT_EQ(UserScript::DOCUMENT_END, script.run_location()); 227 EXPECT_EQ(UserScript::DOCUMENT_END, script.run_location());
209 } 228 }
210 229
211 TEST(ExtensionFromUserScript, RunAtDocumentIdle) { 230 TEST_F(ExtensionFromUserScript, RunAtDocumentIdle) {
212 base::ScopedTempDir extensions_dir; 231 base::ScopedTempDir extensions_dir;
213 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); 232 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir());
214 233
215 base::FilePath test_file; 234 base::FilePath test_file;
216 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); 235 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file));
217 test_file = test_file.AppendASCII("extensions") 236 test_file = test_file.AppendASCII("extensions")
218 .AppendASCII("user_script_run_at_idle.user.js"); 237 .AppendASCII("user_script_run_at_idle.user.js");
219 ASSERT_TRUE(file_util::PathExists(test_file)) << test_file.value(); 238 ASSERT_TRUE(file_util::PathExists(test_file)) << test_file.value();
220 239
221 string16 error; 240 string16 error;
222 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( 241 scoped_refptr<Extension> extension(ConvertUserScriptToExtension(
223 test_file, GURL("http://www.google.com/foo"), 242 test_file, GURL("http://www.google.com/foo"),
224 extensions_dir.path(), &error)); 243 extensions_dir.path(), &error));
225 244
226 ASSERT_TRUE(extension.get()); 245 ASSERT_TRUE(extension.get());
227 EXPECT_EQ(string16(), error); 246 EXPECT_EQ(string16(), error);
228 247
229 // Use a temp dir so that the extensions dir will clean itself up. 248 // Use a temp dir so that the extensions dir will clean itself up.
230 base::ScopedTempDir ext_dir; 249 base::ScopedTempDir ext_dir;
231 EXPECT_TRUE(ext_dir.Set(extension->path())); 250 EXPECT_TRUE(ext_dir.Set(extension->path()));
232 251
233 // Validate generated extension metadata. 252 // Validate generated extension metadata.
234 EXPECT_EQ("Document Idle Test", extension->name()); 253 EXPECT_EQ("Document Idle Test", extension->name());
235 EXPECT_EQ("This script tests document-idle", extension->description()); 254 EXPECT_EQ("This script tests document-idle", extension->description());
236 EXPECT_EQ("kHnHKec3O/RKKo5/Iu1hKqe4wQERthL0639isNtsfiY=", 255 EXPECT_EQ("kHnHKec3O/RKKo5/Iu1hKqe4wQERthL0639isNtsfiY=",
237 extension->public_key()); 256 extension->public_key());
238 257
239 // Validate run location. 258 // Validate run location.
240 ASSERT_EQ(1u, extension->content_scripts().size()); 259 ASSERT_EQ(1u, ContentScriptsInfo::GetContentScripts(extension).size());
241 const UserScript& script = extension->content_scripts()[0]; 260 const UserScript& script =
261 ContentScriptsInfo::GetContentScripts(extension)[0];
242 EXPECT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); 262 EXPECT_EQ(UserScript::DOCUMENT_IDLE, script.run_location());
243 } 263 }
244 264
245 } // namespace extensions 265 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/content_scripts_parser.cc ('k') | chrome/browser/extensions/extension_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698