OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/files/file_path.h" | |
6 #include "base/pickle.h" | |
7 #include "chrome/common/extensions/user_script.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 #include "url/gurl.h" | |
10 | |
11 namespace extensions { | |
12 | |
13 static const int kAllSchemes = | |
14 URLPattern::SCHEME_HTTP | | |
15 URLPattern::SCHEME_HTTPS | | |
16 URLPattern::SCHEME_FILE | | |
17 URLPattern::SCHEME_FTP | | |
18 URLPattern::SCHEME_CHROMEUI; | |
19 | |
20 TEST(ExtensionUserScriptTest, Glob_HostString) { | |
21 UserScript script; | |
22 script.add_glob("*mail.google.com*"); | |
23 script.add_glob("*mail.yahoo.com*"); | |
24 script.add_glob("*mail.msn.com*"); | |
25 EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com"))); | |
26 EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com/foo"))); | |
27 EXPECT_TRUE(script.MatchesURL(GURL("https://mail.google.com/foo"))); | |
28 EXPECT_TRUE(script.MatchesURL(GURL("ftp://mail.google.com/foo"))); | |
29 EXPECT_TRUE(script.MatchesURL(GURL("http://woo.mail.google.com/foo"))); | |
30 EXPECT_TRUE(script.MatchesURL(GURL("http://mail.yahoo.com/bar"))); | |
31 EXPECT_TRUE(script.MatchesURL(GURL("http://mail.msn.com/baz"))); | |
32 EXPECT_FALSE(script.MatchesURL(GURL("http://www.hotmail.com"))); | |
33 | |
34 script.add_exclude_glob("*foo*"); | |
35 EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com"))); | |
36 EXPECT_FALSE(script.MatchesURL(GURL("http://mail.google.com/foo"))); | |
37 } | |
38 | |
39 TEST(ExtensionUserScriptTest, Glob_TrailingSlash) { | |
40 UserScript script; | |
41 script.add_glob("*mail.google.com/"); | |
42 // GURL normalizes the URL to have a trailing "/" | |
43 EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com"))); | |
44 EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com/"))); | |
45 EXPECT_FALSE(script.MatchesURL(GURL("http://mail.google.com/foo"))); | |
46 } | |
47 | |
48 TEST(ExtensionUserScriptTest, Glob_TrailingSlashStar) { | |
49 UserScript script; | |
50 script.add_glob("http://mail.google.com/*"); | |
51 // GURL normalizes the URL to have a trailing "/" | |
52 EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com"))); | |
53 EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com/foo"))); | |
54 EXPECT_FALSE(script.MatchesURL(GURL("https://mail.google.com/foo"))); | |
55 } | |
56 | |
57 TEST(ExtensionUserScriptTest, Glob_Star) { | |
58 UserScript script; | |
59 script.add_glob("*"); | |
60 EXPECT_TRUE(script.MatchesURL(GURL("http://foo.com/bar"))); | |
61 EXPECT_TRUE(script.MatchesURL(GURL("http://hot.com/dog"))); | |
62 EXPECT_TRUE(script.MatchesURL(GURL("https://hot.com/dog"))); | |
63 EXPECT_TRUE(script.MatchesURL(GURL("file:///foo/bar"))); | |
64 EXPECT_TRUE(script.MatchesURL(GURL("file://localhost/foo/bar"))); | |
65 } | |
66 | |
67 TEST(ExtensionUserScriptTest, Glob_StringAnywhere) { | |
68 UserScript script; | |
69 script.add_glob("*foo*"); | |
70 EXPECT_TRUE(script.MatchesURL(GURL("http://foo.com/bar"))); | |
71 EXPECT_TRUE(script.MatchesURL(GURL("http://baz.org/foo/bar"))); | |
72 EXPECT_FALSE(script.MatchesURL(GURL("http://baz.org"))); | |
73 } | |
74 | |
75 TEST(ExtensionUserScriptTest, UrlPattern) { | |
76 URLPattern pattern(kAllSchemes); | |
77 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*/foo*")); | |
78 | |
79 UserScript script; | |
80 script.add_url_pattern(pattern); | |
81 EXPECT_TRUE(script.MatchesURL(GURL("http://monkey.com/foobar"))); | |
82 EXPECT_FALSE(script.MatchesURL(GURL("http://monkey.com/hotdog"))); | |
83 | |
84 // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc. | |
85 } | |
86 | |
87 TEST(ExtensionUserScriptTest, ExcludeUrlPattern) { | |
88 UserScript script; | |
89 | |
90 URLPattern pattern(kAllSchemes); | |
91 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*")); | |
92 script.add_url_pattern(pattern); | |
93 | |
94 URLPattern exclude(kAllSchemes); | |
95 ASSERT_EQ(URLPattern::PARSE_SUCCESS, exclude.Parse("*://*/*business*")); | |
96 script.add_exclude_url_pattern(exclude); | |
97 | |
98 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/health"))); | |
99 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/business"))); | |
100 EXPECT_TRUE(script.MatchesURL(GURL("http://business.nytimes.com"))); | |
101 } | |
102 | |
103 TEST(ExtensionUserScriptTest, UrlPatternAndIncludeGlobs) { | |
104 UserScript script; | |
105 | |
106 URLPattern pattern(kAllSchemes); | |
107 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*")); | |
108 script.add_url_pattern(pattern); | |
109 | |
110 script.add_glob("*nytimes.com/???s/*"); | |
111 | |
112 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/arts/1.html"))); | |
113 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/jobs/1.html"))); | |
114 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/sports/1.html"))); | |
115 } | |
116 | |
117 TEST(ExtensionUserScriptTest, UrlPatternAndExcludeGlobs) { | |
118 UserScript script; | |
119 | |
120 URLPattern pattern(kAllSchemes); | |
121 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*")); | |
122 script.add_url_pattern(pattern); | |
123 | |
124 script.add_exclude_glob("*science*"); | |
125 | |
126 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com"))); | |
127 EXPECT_FALSE(script.MatchesURL(GURL("http://science.nytimes.com"))); | |
128 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/science"))); | |
129 } | |
130 | |
131 TEST(ExtensionUserScriptTest, UrlPatternGlobInteraction) { | |
132 // If there are both, match intersection(union(globs), union(urlpatterns)). | |
133 UserScript script; | |
134 | |
135 URLPattern pattern(kAllSchemes); | |
136 ASSERT_EQ(URLPattern::PARSE_SUCCESS,pattern.Parse("http://www.google.com/*")); | |
137 script.add_url_pattern(pattern); | |
138 | |
139 script.add_glob("*bar*"); | |
140 | |
141 // No match, because it doesn't match the glob. | |
142 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/foo"))); | |
143 | |
144 script.add_exclude_glob("*baz*"); | |
145 | |
146 // No match, because it matches the exclude glob. | |
147 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/baz"))); | |
148 | |
149 // Match, because it matches the glob, doesn't match the exclude glob. | |
150 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/bar"))); | |
151 | |
152 // Try with just a single exclude glob. | |
153 script.clear_globs(); | |
154 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); | |
155 | |
156 // Try with no globs or exclude globs. | |
157 script.clear_exclude_globs(); | |
158 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); | |
159 } | |
160 | |
161 TEST(ExtensionUserScriptTest, Pickle) { | |
162 URLPattern pattern1(kAllSchemes); | |
163 URLPattern pattern2(kAllSchemes); | |
164 URLPattern exclude1(kAllSchemes); | |
165 URLPattern exclude2(kAllSchemes); | |
166 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern1.Parse("http://*/foo*")); | |
167 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern2.Parse("http://bar/baz*")); | |
168 ASSERT_EQ(URLPattern::PARSE_SUCCESS, exclude1.Parse("*://*/*bar")); | |
169 ASSERT_EQ(URLPattern::PARSE_SUCCESS, exclude2.Parse("https://*/*")); | |
170 | |
171 UserScript script1; | |
172 script1.js_scripts().push_back(UserScript::File( | |
173 base::FilePath(FILE_PATH_LITERAL("c:\\foo\\")), | |
174 base::FilePath(FILE_PATH_LITERAL("foo.user.js")), | |
175 GURL("chrome-extension://abc/foo.user.js"))); | |
176 script1.css_scripts().push_back(UserScript::File( | |
177 base::FilePath(FILE_PATH_LITERAL("c:\\foo\\")), | |
178 base::FilePath(FILE_PATH_LITERAL("foo.user.css")), | |
179 GURL("chrome-extension://abc/foo.user.css"))); | |
180 script1.css_scripts().push_back(UserScript::File( | |
181 base::FilePath(FILE_PATH_LITERAL("c:\\foo\\")), | |
182 base::FilePath(FILE_PATH_LITERAL("foo2.user.css")), | |
183 GURL("chrome-extension://abc/foo2.user.css"))); | |
184 script1.set_run_location(UserScript::DOCUMENT_START); | |
185 | |
186 script1.add_url_pattern(pattern1); | |
187 script1.add_url_pattern(pattern2); | |
188 script1.add_exclude_url_pattern(exclude1); | |
189 script1.add_exclude_url_pattern(exclude2); | |
190 | |
191 Pickle pickle; | |
192 script1.Pickle(&pickle); | |
193 | |
194 PickleIterator iter(pickle); | |
195 UserScript script2; | |
196 script2.Unpickle(pickle, &iter); | |
197 | |
198 EXPECT_EQ(1U, script2.js_scripts().size()); | |
199 EXPECT_EQ(script1.js_scripts()[0].url(), script2.js_scripts()[0].url()); | |
200 | |
201 EXPECT_EQ(2U, script2.css_scripts().size()); | |
202 for (size_t i = 0; i < script2.js_scripts().size(); ++i) { | |
203 EXPECT_EQ(script1.css_scripts()[i].url(), script2.css_scripts()[i].url()); | |
204 } | |
205 | |
206 ASSERT_EQ(script1.globs().size(), script2.globs().size()); | |
207 for (size_t i = 0; i < script1.globs().size(); ++i) { | |
208 EXPECT_EQ(script1.globs()[i], script2.globs()[i]); | |
209 } | |
210 | |
211 ASSERT_EQ(script1.url_patterns(), script2.url_patterns()); | |
212 ASSERT_EQ(script1.exclude_url_patterns(), script2.exclude_url_patterns()); | |
213 } | |
214 | |
215 TEST(ExtensionUserScriptTest, Defaults) { | |
216 UserScript script; | |
217 ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); | |
218 } | |
219 | |
220 } // namespace extensions | |
OLD | NEW |