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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
12 #include "base/scoped_temp_dir.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 "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { | 21 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
22 int schemes = URLPattern::SCHEME_ALL; | 22 int schemes = URLPattern::SCHEME_ALL; |
23 extent->AddPattern(URLPattern(schemes, pattern)); | 23 extent->AddPattern(URLPattern(schemes, pattern)); |
24 } | 24 } |
25 | 25 |
26 } | 26 } |
27 | 27 |
28 namespace extensions { | 28 namespace extensions { |
29 | 29 |
30 TEST(ExtensionFromUserScript, Basic) { | 30 TEST(ExtensionFromUserScript, Basic) { |
31 ScopedTempDir extensions_dir; | 31 base::ScopedTempDir extensions_dir; |
32 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 32 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
33 | 33 |
34 FilePath test_file; | 34 FilePath test_file; |
35 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 35 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
36 test_file = test_file.AppendASCII("extensions") | 36 test_file = test_file.AppendASCII("extensions") |
37 .AppendASCII("user_script_basic.user.js"); | 37 .AppendASCII("user_script_basic.user.js"); |
38 | 38 |
39 string16 error; | 39 string16 error; |
40 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 40 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
41 test_file, GURL("http://www.google.com/foo"), | 41 test_file, GURL("http://www.google.com/foo"), |
42 extensions_dir.path(), &error)); | 42 extensions_dir.path(), &error)); |
43 | 43 |
44 ASSERT_TRUE(extension.get()); | 44 ASSERT_TRUE(extension.get()); |
45 EXPECT_EQ(string16(), error); | 45 EXPECT_EQ(string16(), error); |
46 | 46 |
47 // Use a temp dir so that the extensions dir will clean itself up. | 47 // Use a temp dir so that the extensions dir will clean itself up. |
48 ScopedTempDir ext_dir; | 48 base::ScopedTempDir ext_dir; |
49 EXPECT_TRUE(ext_dir.Set(extension->path())); | 49 EXPECT_TRUE(ext_dir.Set(extension->path())); |
50 | 50 |
51 // Validate generated extension metadata. | 51 // Validate generated extension metadata. |
52 EXPECT_EQ("My user script", extension->name()); | 52 EXPECT_EQ("My user script", extension->name()); |
53 EXPECT_EQ("2.2.2", extension->VersionString()); | 53 EXPECT_EQ("2.2.2", extension->VersionString()); |
54 EXPECT_EQ("Does totally awesome stuff.", extension->description()); | 54 EXPECT_EQ("Does totally awesome stuff.", extension->description()); |
55 EXPECT_EQ("IhCFCg9PMQTAcJdc9ytUP99WME+4yh6aMnM1uupkovo=", | 55 EXPECT_EQ("IhCFCg9PMQTAcJdc9ytUP99WME+4yh6aMnM1uupkovo=", |
56 extension->public_key()); | 56 extension->public_key()); |
57 | 57 |
58 ASSERT_EQ(1u, extension->content_scripts().size()); | 58 ASSERT_EQ(1u, extension->content_scripts().size()); |
(...skipping 12 matching lines...) Expand all Loading... |
71 script.exclude_url_patterns().begin()->GetAsString()); | 71 script.exclude_url_patterns().begin()->GetAsString()); |
72 | 72 |
73 // Make sure the files actually exist on disk. | 73 // Make sure the files actually exist on disk. |
74 EXPECT_TRUE(file_util::PathExists( | 74 EXPECT_TRUE(file_util::PathExists( |
75 extension->path().Append(script.js_scripts()[0].relative_path()))); | 75 extension->path().Append(script.js_scripts()[0].relative_path()))); |
76 EXPECT_TRUE(file_util::PathExists( | 76 EXPECT_TRUE(file_util::PathExists( |
77 extension->path().Append(Extension::kManifestFilename))); | 77 extension->path().Append(Extension::kManifestFilename))); |
78 } | 78 } |
79 | 79 |
80 TEST(ExtensionFromUserScript, NoMetdata) { | 80 TEST(ExtensionFromUserScript, NoMetdata) { |
81 ScopedTempDir extensions_dir; | 81 base::ScopedTempDir extensions_dir; |
82 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 82 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
83 | 83 |
84 FilePath test_file; | 84 FilePath test_file; |
85 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 85 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
86 test_file = test_file.AppendASCII("extensions") | 86 test_file = test_file.AppendASCII("extensions") |
87 .AppendASCII("user_script_no_metadata.user.js"); | 87 .AppendASCII("user_script_no_metadata.user.js"); |
88 | 88 |
89 string16 error; | 89 string16 error; |
90 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 90 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
91 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), | 91 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), |
92 extensions_dir.path(), &error)); | 92 extensions_dir.path(), &error)); |
93 | 93 |
94 ASSERT_TRUE(extension.get()); | 94 ASSERT_TRUE(extension.get()); |
95 EXPECT_EQ(string16(), error); | 95 EXPECT_EQ(string16(), error); |
96 | 96 |
97 // Use a temp dir so that the extensions dir will clean itself up. | 97 // Use a temp dir so that the extensions dir will clean itself up. |
98 ScopedTempDir ext_dir; | 98 base::ScopedTempDir ext_dir; |
99 EXPECT_TRUE(ext_dir.Set(extension->path())); | 99 EXPECT_TRUE(ext_dir.Set(extension->path())); |
100 | 100 |
101 // Validate generated extension metadata. | 101 // Validate generated extension metadata. |
102 EXPECT_EQ("bar.user.js", extension->name()); | 102 EXPECT_EQ("bar.user.js", extension->name()); |
103 EXPECT_EQ("1.0", extension->VersionString()); | 103 EXPECT_EQ("1.0", extension->VersionString()); |
104 EXPECT_EQ("", extension->description()); | 104 EXPECT_EQ("", extension->description()); |
105 EXPECT_EQ("k1WxKx54hX6tfl5gQaXD/m4d9QUMwRdXWM4RW+QkWcY=", | 105 EXPECT_EQ("k1WxKx54hX6tfl5gQaXD/m4d9QUMwRdXWM4RW+QkWcY=", |
106 extension->public_key()); | 106 extension->public_key()); |
107 | 107 |
108 ASSERT_EQ(1u, extension->content_scripts().size()); | 108 ASSERT_EQ(1u, extension->content_scripts().size()); |
109 const UserScript& script = extension->content_scripts()[0]; | 109 const UserScript& script = extension->content_scripts()[0]; |
110 ASSERT_EQ(1u, script.globs().size()); | 110 ASSERT_EQ(1u, script.globs().size()); |
111 EXPECT_EQ("*", script.globs()[0]); | 111 EXPECT_EQ("*", script.globs()[0]); |
112 EXPECT_EQ(0u, script.exclude_globs().size()); | 112 EXPECT_EQ(0u, script.exclude_globs().size()); |
113 | 113 |
114 URLPatternSet expected; | 114 URLPatternSet expected; |
115 AddPattern(&expected, "http://*/*"); | 115 AddPattern(&expected, "http://*/*"); |
116 AddPattern(&expected, "https://*/*"); | 116 AddPattern(&expected, "https://*/*"); |
117 EXPECT_EQ(expected, script.url_patterns()); | 117 EXPECT_EQ(expected, script.url_patterns()); |
118 | 118 |
119 // Make sure the files actually exist on disk. | 119 // Make sure the files actually exist on disk. |
120 EXPECT_TRUE(file_util::PathExists( | 120 EXPECT_TRUE(file_util::PathExists( |
121 extension->path().Append(script.js_scripts()[0].relative_path()))); | 121 extension->path().Append(script.js_scripts()[0].relative_path()))); |
122 EXPECT_TRUE(file_util::PathExists( | 122 EXPECT_TRUE(file_util::PathExists( |
123 extension->path().Append(Extension::kManifestFilename))); | 123 extension->path().Append(Extension::kManifestFilename))); |
124 } | 124 } |
125 | 125 |
126 TEST(ExtensionFromUserScript, NotUTF8) { | 126 TEST(ExtensionFromUserScript, NotUTF8) { |
127 ScopedTempDir extensions_dir; | 127 base::ScopedTempDir extensions_dir; |
128 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 128 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
129 | 129 |
130 FilePath test_file; | 130 FilePath test_file; |
131 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 131 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
132 test_file = test_file.AppendASCII("extensions") | 132 test_file = test_file.AppendASCII("extensions") |
133 .AppendASCII("user_script_not_utf8.user.js"); | 133 .AppendASCII("user_script_not_utf8.user.js"); |
134 | 134 |
135 string16 error; | 135 string16 error; |
136 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 136 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
137 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), | 137 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), |
138 extensions_dir.path(), &error)); | 138 extensions_dir.path(), &error)); |
139 | 139 |
140 ASSERT_FALSE(extension.get()); | 140 ASSERT_FALSE(extension.get()); |
141 EXPECT_EQ(ASCIIToUTF16("User script must be UTF8 encoded."), error); | 141 EXPECT_EQ(ASCIIToUTF16("User script must be UTF8 encoded."), error); |
142 } | 142 } |
143 | 143 |
144 TEST(ExtensionFromUserScript, RunAtDocumentStart) { | 144 TEST(ExtensionFromUserScript, RunAtDocumentStart) { |
145 ScopedTempDir extensions_dir; | 145 base::ScopedTempDir extensions_dir; |
146 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 146 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
147 | 147 |
148 FilePath test_file; | 148 FilePath test_file; |
149 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 149 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
150 test_file = test_file.AppendASCII("extensions") | 150 test_file = test_file.AppendASCII("extensions") |
151 .AppendASCII("user_script_run_at_start.user.js"); | 151 .AppendASCII("user_script_run_at_start.user.js"); |
152 | 152 |
153 string16 error; | 153 string16 error; |
154 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 154 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
155 test_file, GURL("http://www.google.com/foo"), | 155 test_file, GURL("http://www.google.com/foo"), |
156 extensions_dir.path(), &error)); | 156 extensions_dir.path(), &error)); |
157 | 157 |
158 ASSERT_TRUE(extension.get()); | 158 ASSERT_TRUE(extension.get()); |
159 EXPECT_EQ(string16(), error); | 159 EXPECT_EQ(string16(), error); |
160 | 160 |
161 // Use a temp dir so that the extensions dir will clean itself up. | 161 // Use a temp dir so that the extensions dir will clean itself up. |
162 ScopedTempDir ext_dir; | 162 base::ScopedTempDir ext_dir; |
163 EXPECT_TRUE(ext_dir.Set(extension->path())); | 163 EXPECT_TRUE(ext_dir.Set(extension->path())); |
164 | 164 |
165 // Validate generated extension metadata. | 165 // Validate generated extension metadata. |
166 EXPECT_EQ("Document Start Test", extension->name()); | 166 EXPECT_EQ("Document Start Test", extension->name()); |
167 EXPECT_EQ("This script tests document-start", extension->description()); | 167 EXPECT_EQ("This script tests document-start", extension->description()); |
168 EXPECT_EQ("RjmyI7+Gp/YHcW1qnu4xDxkJcL4cV4kTzdCA4BajCbk=", | 168 EXPECT_EQ("RjmyI7+Gp/YHcW1qnu4xDxkJcL4cV4kTzdCA4BajCbk=", |
169 extension->public_key()); | 169 extension->public_key()); |
170 | 170 |
171 // Validate run location. | 171 // Validate run location. |
172 ASSERT_EQ(1u, extension->content_scripts().size()); | 172 ASSERT_EQ(1u, extension->content_scripts().size()); |
173 const UserScript& script = extension->content_scripts()[0]; | 173 const UserScript& script = extension->content_scripts()[0]; |
174 EXPECT_EQ(UserScript::DOCUMENT_START, script.run_location()); | 174 EXPECT_EQ(UserScript::DOCUMENT_START, script.run_location()); |
175 } | 175 } |
176 | 176 |
177 TEST(ExtensionFromUserScript, RunAtDocumentEnd) { | 177 TEST(ExtensionFromUserScript, RunAtDocumentEnd) { |
178 ScopedTempDir extensions_dir; | 178 base::ScopedTempDir extensions_dir; |
179 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 179 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
180 | 180 |
181 FilePath test_file; | 181 FilePath test_file; |
182 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 182 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
183 test_file = test_file.AppendASCII("extensions") | 183 test_file = test_file.AppendASCII("extensions") |
184 .AppendASCII("user_script_run_at_end.user.js"); | 184 .AppendASCII("user_script_run_at_end.user.js"); |
185 | 185 |
186 string16 error; | 186 string16 error; |
187 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 187 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
188 test_file, GURL("http://www.google.com/foo"), | 188 test_file, GURL("http://www.google.com/foo"), |
189 extensions_dir.path(), &error)); | 189 extensions_dir.path(), &error)); |
190 | 190 |
191 ASSERT_TRUE(extension.get()); | 191 ASSERT_TRUE(extension.get()); |
192 EXPECT_EQ(string16(), error); | 192 EXPECT_EQ(string16(), error); |
193 | 193 |
194 // Use a temp dir so that the extensions dir will clean itself up. | 194 // Use a temp dir so that the extensions dir will clean itself up. |
195 ScopedTempDir ext_dir; | 195 base::ScopedTempDir ext_dir; |
196 EXPECT_TRUE(ext_dir.Set(extension->path())); | 196 EXPECT_TRUE(ext_dir.Set(extension->path())); |
197 | 197 |
198 // Validate generated extension metadata. | 198 // Validate generated extension metadata. |
199 EXPECT_EQ("Document End Test", extension->name()); | 199 EXPECT_EQ("Document End Test", extension->name()); |
200 EXPECT_EQ("This script tests document-end", extension->description()); | 200 EXPECT_EQ("This script tests document-end", extension->description()); |
201 EXPECT_EQ("cpr5i8Mi24FzECV8UJe6tanwlU8SWesZosJ915YISvQ=", | 201 EXPECT_EQ("cpr5i8Mi24FzECV8UJe6tanwlU8SWesZosJ915YISvQ=", |
202 extension->public_key()); | 202 extension->public_key()); |
203 | 203 |
204 // Validate run location. | 204 // Validate run location. |
205 ASSERT_EQ(1u, extension->content_scripts().size()); | 205 ASSERT_EQ(1u, extension->content_scripts().size()); |
206 const UserScript& script = extension->content_scripts()[0]; | 206 const UserScript& script = extension->content_scripts()[0]; |
207 EXPECT_EQ(UserScript::DOCUMENT_END, script.run_location()); | 207 EXPECT_EQ(UserScript::DOCUMENT_END, script.run_location()); |
208 } | 208 } |
209 | 209 |
210 TEST(ExtensionFromUserScript, RunAtDocumentIdle) { | 210 TEST(ExtensionFromUserScript, RunAtDocumentIdle) { |
211 ScopedTempDir extensions_dir; | 211 base::ScopedTempDir extensions_dir; |
212 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); | 212 ASSERT_TRUE(extensions_dir.CreateUniqueTempDir()); |
213 | 213 |
214 FilePath test_file; | 214 FilePath test_file; |
215 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); | 215 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); |
216 test_file = test_file.AppendASCII("extensions") | 216 test_file = test_file.AppendASCII("extensions") |
217 .AppendASCII("user_script_run_at_idle.user.js"); | 217 .AppendASCII("user_script_run_at_idle.user.js"); |
218 ASSERT_TRUE(file_util::PathExists(test_file)) << test_file.value(); | 218 ASSERT_TRUE(file_util::PathExists(test_file)) << test_file.value(); |
219 | 219 |
220 string16 error; | 220 string16 error; |
221 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( | 221 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( |
222 test_file, GURL("http://www.google.com/foo"), | 222 test_file, GURL("http://www.google.com/foo"), |
223 extensions_dir.path(), &error)); | 223 extensions_dir.path(), &error)); |
224 | 224 |
225 ASSERT_TRUE(extension.get()); | 225 ASSERT_TRUE(extension.get()); |
226 EXPECT_EQ(string16(), error); | 226 EXPECT_EQ(string16(), error); |
227 | 227 |
228 // Use a temp dir so that the extensions dir will clean itself up. | 228 // Use a temp dir so that the extensions dir will clean itself up. |
229 ScopedTempDir ext_dir; | 229 base::ScopedTempDir ext_dir; |
230 EXPECT_TRUE(ext_dir.Set(extension->path())); | 230 EXPECT_TRUE(ext_dir.Set(extension->path())); |
231 | 231 |
232 // Validate generated extension metadata. | 232 // Validate generated extension metadata. |
233 EXPECT_EQ("Document Idle Test", extension->name()); | 233 EXPECT_EQ("Document Idle Test", extension->name()); |
234 EXPECT_EQ("This script tests document-idle", extension->description()); | 234 EXPECT_EQ("This script tests document-idle", extension->description()); |
235 EXPECT_EQ("kHnHKec3O/RKKo5/Iu1hKqe4wQERthL0639isNtsfiY=", | 235 EXPECT_EQ("kHnHKec3O/RKKo5/Iu1hKqe4wQERthL0639isNtsfiY=", |
236 extension->public_key()); | 236 extension->public_key()); |
237 | 237 |
238 // Validate run location. | 238 // Validate run location. |
239 ASSERT_EQ(1u, extension->content_scripts().size()); | 239 ASSERT_EQ(1u, extension->content_scripts().size()); |
240 const UserScript& script = extension->content_scripts()[0]; | 240 const UserScript& script = extension->content_scripts()[0]; |
241 EXPECT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); | 241 EXPECT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); |
242 } | 242 } |
243 | 243 |
244 } // namespace extensions | 244 } // namespace extensions |
OLD | NEW |