OLD | NEW |
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 "chrome/browser/extensions/convert_user_script.h" | 5 #include "chrome/browser/extensions/convert_user_script.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 TEST_F(ExtensionFromUserScript, Basic) { | 36 TEST_F(ExtensionFromUserScript, Basic) { |
37 base::ScopedTempDir extensions_dir; | 37 base::ScopedTempDir extensions_dir; |
38 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 38 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
39 | 39 |
40 base::FilePath test_file; | 40 base::FilePath test_file; |
41 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 41 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
42 test_file = test_file.AppendASCII("extensions") | 42 test_file = test_file.AppendASCII("extensions") |
43 .AppendASCII("user_script_basic.user.js"); | 43 .AppendASCII("user_script_basic.user.js"); |
44 | 44 |
45 base::string16 error; | 45 base::string16 error; |
46 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 46 scoped_refptr<Extension> extension( |
47 test_file, GURL("http://www.google.com/foo"), | 47 ConvertUserScriptToExtension(test_file, GURL("http://www.google.com/foo"), |
48 extensions_dir.path(), &error)); | 48 extensions_dir.GetPath(), &error)); |
49 | 49 |
50 ASSERT_TRUE(extension.get()); | 50 ASSERT_TRUE(extension.get()); |
51 EXPECT_EQ(base::string16(), error); | 51 EXPECT_EQ(base::string16(), error); |
52 | 52 |
53 // Use a temp dir so that the extensions dir will clean itself up. | 53 // Use a temp dir so that the extensions dir will clean itself up. |
54 base::ScopedTempDir ext_dir; | 54 base::ScopedTempDir ext_dir; |
55 EXPECT_TRUE(ext_dir.Set(extension->path())); | 55 EXPECT_TRUE(ext_dir.Set(extension->path())); |
56 | 56 |
57 // Validate generated extension metadata. | 57 // Validate generated extension metadata. |
58 EXPECT_EQ("My user script", extension->name()); | 58 EXPECT_EQ("My user script", extension->name()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 91 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
92 | 92 |
93 base::FilePath test_file; | 93 base::FilePath test_file; |
94 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 94 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
95 test_file = test_file.AppendASCII("extensions") | 95 test_file = test_file.AppendASCII("extensions") |
96 .AppendASCII("user_script_no_metadata.user.js"); | 96 .AppendASCII("user_script_no_metadata.user.js"); |
97 | 97 |
98 base::string16 error; | 98 base::string16 error; |
99 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 99 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
100 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), | 100 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), |
101 extensions_dir.path(), &error)); | 101 extensions_dir.GetPath(), &error)); |
102 | 102 |
103 ASSERT_TRUE(extension.get()); | 103 ASSERT_TRUE(extension.get()); |
104 EXPECT_EQ(base::string16(), error); | 104 EXPECT_EQ(base::string16(), error); |
105 | 105 |
106 // Use a temp dir so that the extensions dir will clean itself up. | 106 // Use a temp dir so that the extensions dir will clean itself up. |
107 base::ScopedTempDir ext_dir; | 107 base::ScopedTempDir ext_dir; |
108 EXPECT_TRUE(ext_dir.Set(extension->path())); | 108 EXPECT_TRUE(ext_dir.Set(extension->path())); |
109 | 109 |
110 // Validate generated extension metadata. | 110 // Validate generated extension metadata. |
111 EXPECT_EQ("bar.user.js", extension->name()); | 111 EXPECT_EQ("bar.user.js", extension->name()); |
(...skipping 28 matching lines...) Expand all Loading... |
140 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 140 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
141 | 141 |
142 base::FilePath test_file; | 142 base::FilePath test_file; |
143 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 143 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
144 test_file = test_file.AppendASCII("extensions") | 144 test_file = test_file.AppendASCII("extensions") |
145 .AppendASCII("user_script_not_utf8.user.js"); | 145 .AppendASCII("user_script_not_utf8.user.js"); |
146 | 146 |
147 base::string16 error; | 147 base::string16 error; |
148 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 148 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
149 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), | 149 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), |
150 extensions_dir.path(), &error)); | 150 extensions_dir.GetPath(), &error)); |
151 | 151 |
152 ASSERT_FALSE(extension.get()); | 152 ASSERT_FALSE(extension.get()); |
153 EXPECT_EQ(base::ASCIIToUTF16("User script must be UTF8 encoded."), error); | 153 EXPECT_EQ(base::ASCIIToUTF16("User script must be UTF8 encoded."), error); |
154 } | 154 } |
155 | 155 |
156 TEST_F(ExtensionFromUserScript, RunAtDocumentStart) { | 156 TEST_F(ExtensionFromUserScript, RunAtDocumentStart) { |
157 base::ScopedTempDir extensions_dir; | 157 base::ScopedTempDir extensions_dir; |
158 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 158 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
159 | 159 |
160 base::FilePath test_file; | 160 base::FilePath test_file; |
161 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 161 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
162 test_file = test_file.AppendASCII("extensions") | 162 test_file = test_file.AppendASCII("extensions") |
163 .AppendASCII("user_script_run_at_start.user.js"); | 163 .AppendASCII("user_script_run_at_start.user.js"); |
164 | 164 |
165 base::string16 error; | 165 base::string16 error; |
166 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 166 scoped_refptr<Extension> extension( |
167 test_file, GURL("http://www.google.com/foo"), | 167 ConvertUserScriptToExtension(test_file, GURL("http://www.google.com/foo"), |
168 extensions_dir.path(), &error)); | 168 extensions_dir.GetPath(), &error)); |
169 | 169 |
170 ASSERT_TRUE(extension.get()); | 170 ASSERT_TRUE(extension.get()); |
171 EXPECT_EQ(base::string16(), error); | 171 EXPECT_EQ(base::string16(), error); |
172 | 172 |
173 // Use a temp dir so that the extensions dir will clean itself up. | 173 // Use a temp dir so that the extensions dir will clean itself up. |
174 base::ScopedTempDir ext_dir; | 174 base::ScopedTempDir ext_dir; |
175 EXPECT_TRUE(ext_dir.Set(extension->path())); | 175 EXPECT_TRUE(ext_dir.Set(extension->path())); |
176 | 176 |
177 // Validate generated extension metadata. | 177 // Validate generated extension metadata. |
178 EXPECT_EQ("Document Start Test", extension->name()); | 178 EXPECT_EQ("Document Start Test", extension->name()); |
(...skipping 12 matching lines...) Expand all Loading... |
191 TEST_F(ExtensionFromUserScript, RunAtDocumentEnd) { | 191 TEST_F(ExtensionFromUserScript, RunAtDocumentEnd) { |
192 base::ScopedTempDir extensions_dir; | 192 base::ScopedTempDir extensions_dir; |
193 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 193 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
194 | 194 |
195 base::FilePath test_file; | 195 base::FilePath test_file; |
196 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 196 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
197 test_file = test_file.AppendASCII("extensions") | 197 test_file = test_file.AppendASCII("extensions") |
198 .AppendASCII("user_script_run_at_end.user.js"); | 198 .AppendASCII("user_script_run_at_end.user.js"); |
199 | 199 |
200 base::string16 error; | 200 base::string16 error; |
201 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 201 scoped_refptr<Extension> extension( |
202 test_file, GURL("http://www.google.com/foo"), | 202 ConvertUserScriptToExtension(test_file, GURL("http://www.google.com/foo"), |
203 extensions_dir.path(), &error)); | 203 extensions_dir.GetPath(), &error)); |
204 | 204 |
205 ASSERT_TRUE(extension.get()); | 205 ASSERT_TRUE(extension.get()); |
206 EXPECT_EQ(base::string16(), error); | 206 EXPECT_EQ(base::string16(), error); |
207 | 207 |
208 // Use a temp dir so that the extensions dir will clean itself up. | 208 // Use a temp dir so that the extensions dir will clean itself up. |
209 base::ScopedTempDir ext_dir; | 209 base::ScopedTempDir ext_dir; |
210 EXPECT_TRUE(ext_dir.Set(extension->path())); | 210 EXPECT_TRUE(ext_dir.Set(extension->path())); |
211 | 211 |
212 // Validate generated extension metadata. | 212 // Validate generated extension metadata. |
213 EXPECT_EQ("Document End Test", extension->name()); | 213 EXPECT_EQ("Document End Test", extension->name()); |
(...skipping 13 matching lines...) Expand all Loading... |
227 base::ScopedTempDir extensions_dir; | 227 base::ScopedTempDir extensions_dir; |
228 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 228 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
229 | 229 |
230 base::FilePath test_file; | 230 base::FilePath test_file; |
231 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 231 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
232 test_file = test_file.AppendASCII("extensions") | 232 test_file = test_file.AppendASCII("extensions") |
233 .AppendASCII("user_script_run_at_idle.user.js"); | 233 .AppendASCII("user_script_run_at_idle.user.js"); |
234 ASSERT_TRUE(base::PathExists(test_file)) << test_file.value(); | 234 ASSERT_TRUE(base::PathExists(test_file)) << test_file.value(); |
235 | 235 |
236 base::string16 error; | 236 base::string16 error; |
237 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 237 scoped_refptr<Extension> extension( |
238 test_file, GURL("http://www.google.com/foo"), | 238 ConvertUserScriptToExtension(test_file, GURL("http://www.google.com/foo"), |
239 extensions_dir.path(), &error)); | 239 extensions_dir.GetPath(), &error)); |
240 | 240 |
241 ASSERT_TRUE(extension.get()); | 241 ASSERT_TRUE(extension.get()); |
242 EXPECT_EQ(base::string16(), error); | 242 EXPECT_EQ(base::string16(), error); |
243 | 243 |
244 // Use a temp dir so that the extensions dir will clean itself up. | 244 // Use a temp dir so that the extensions dir will clean itself up. |
245 base::ScopedTempDir ext_dir; | 245 base::ScopedTempDir ext_dir; |
246 EXPECT_TRUE(ext_dir.Set(extension->path())); | 246 EXPECT_TRUE(ext_dir.Set(extension->path())); |
247 | 247 |
248 // Validate generated extension metadata. | 248 // Validate generated extension metadata. |
249 EXPECT_EQ("Document Idle Test", extension->name()); | 249 EXPECT_EQ("Document Idle Test", extension->name()); |
250 EXPECT_EQ("This script tests document-idle", extension->description()); | 250 EXPECT_EQ("This script tests document-idle", extension->description()); |
251 EXPECT_EQ("kHnHKec3O/RKKo5/Iu1hKqe4wQERthL0639isNtsfiY=", | 251 EXPECT_EQ("kHnHKec3O/RKKo5/Iu1hKqe4wQERthL0639isNtsfiY=", |
252 extension->public_key()); | 252 extension->public_key()); |
253 EXPECT_EQ(Manifest::TYPE_USER_SCRIPT, extension->GetType()); | 253 EXPECT_EQ(Manifest::TYPE_USER_SCRIPT, extension->GetType()); |
254 | 254 |
255 // Validate run location. | 255 // Validate run location. |
256 ASSERT_EQ(1u, ContentScriptsInfo::GetContentScripts(extension.get()).size()); | 256 ASSERT_EQ(1u, ContentScriptsInfo::GetContentScripts(extension.get()).size()); |
257 const UserScript& script = | 257 const UserScript& script = |
258 *ContentScriptsInfo::GetContentScripts(extension.get())[0]; | 258 *ContentScriptsInfo::GetContentScripts(extension.get())[0]; |
259 EXPECT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); | 259 EXPECT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); |
260 } | 260 } |
261 | 261 |
262 } // namespace extensions | 262 } // namespace extensions |
OLD | NEW |