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

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

Powered by Google App Engine
This is Rietveld 408576698