[Android] Lint pylib/host_driven.
[chromium-blink-merge.git] / extensions / common / user_script_unittest.cc
blobffec4f0e1db8b357376e0162a083bb70a345ec56
1 // Copyright 2013 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.
5 #include "base/files/file_path.h"
6 #include "base/pickle.h"
7 #include "extensions/common/user_script.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/gurl.h"
11 namespace extensions {
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;
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")));
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")));
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")));
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")));
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")));
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")));
75 TEST(ExtensionUserScriptTest, UrlPattern) {
76 URLPattern pattern(kAllSchemes);
77 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*/foo*"));
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")));
84 // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc.
87 TEST(ExtensionUserScriptTest, ExcludeUrlPattern) {
88 UserScript script;
90 URLPattern pattern(kAllSchemes);
91 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*"));
92 script.add_url_pattern(pattern);
94 URLPattern exclude(kAllSchemes);
95 ASSERT_EQ(URLPattern::PARSE_SUCCESS, exclude.Parse("*://*/*business*"));
96 script.add_exclude_url_pattern(exclude);
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")));
103 TEST(ExtensionUserScriptTest, UrlPatternAndIncludeGlobs) {
104 UserScript script;
106 URLPattern pattern(kAllSchemes);
107 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*"));
108 script.add_url_pattern(pattern);
110 script.add_glob("*nytimes.com/???s/*");
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")));
117 TEST(ExtensionUserScriptTest, UrlPatternAndExcludeGlobs) {
118 UserScript script;
120 URLPattern pattern(kAllSchemes);
121 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*"));
122 script.add_url_pattern(pattern);
124 script.add_exclude_glob("*science*");
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")));
131 TEST(ExtensionUserScriptTest, UrlPatternGlobInteraction) {
132 // If there are both, match intersection(union(globs), union(urlpatterns)).
133 UserScript script;
135 URLPattern pattern(kAllSchemes);
136 ASSERT_EQ(URLPattern::PARSE_SUCCESS,pattern.Parse("http://www.google.com/*"));
137 script.add_url_pattern(pattern);
139 script.add_glob("*bar*");
141 // No match, because it doesn't match the glob.
142 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/foo")));
144 script.add_exclude_glob("*baz*");
146 // No match, because it matches the exclude glob.
147 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/baz")));
149 // Match, because it matches the glob, doesn't match the exclude glob.
150 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/bar")));
152 // Try with just a single exclude glob.
153 script.clear_globs();
154 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo")));
156 // Try with no globs or exclude globs.
157 script.clear_exclude_globs();
158 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo")));
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://*/*"));
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);
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);
191 Pickle pickle;
192 script1.Pickle(&pickle);
194 PickleIterator iter(pickle);
195 UserScript script2;
196 script2.Unpickle(pickle, &iter);
198 EXPECT_EQ(1U, script2.js_scripts().size());
199 EXPECT_EQ(script1.js_scripts()[0].url(), script2.js_scripts()[0].url());
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());
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]);
211 ASSERT_EQ(script1.url_patterns(), script2.url_patterns());
212 ASSERT_EQ(script1.exclude_url_patterns(), script2.exclude_url_patterns());
215 TEST(ExtensionUserScriptTest, Defaults) {
216 UserScript script;
217 ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location());
220 } // namespace extensions