| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/file_path.h" | 5 #include "base/file_path.h" |
| 6 #include "base/pickle.h" | 6 #include "base/pickle.h" |
| 7 #include "chrome/common/extensions/user_script.h" | 7 #include "chrome/common/extensions/user_script.h" |
| 8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 TEST(ExtensionUserScriptTest, Glob_StringAnywhere) { | 65 TEST(ExtensionUserScriptTest, Glob_StringAnywhere) { |
| 66 UserScript script; | 66 UserScript script; |
| 67 script.add_glob("*foo*"); | 67 script.add_glob("*foo*"); |
| 68 EXPECT_TRUE(script.MatchesURL(GURL("http://foo.com/bar"))); | 68 EXPECT_TRUE(script.MatchesURL(GURL("http://foo.com/bar"))); |
| 69 EXPECT_TRUE(script.MatchesURL(GURL("http://baz.org/foo/bar"))); | 69 EXPECT_TRUE(script.MatchesURL(GURL("http://baz.org/foo/bar"))); |
| 70 EXPECT_FALSE(script.MatchesURL(GURL("http://baz.org"))); | 70 EXPECT_FALSE(script.MatchesURL(GURL("http://baz.org"))); |
| 71 } | 71 } |
| 72 | 72 |
| 73 TEST(ExtensionUserScriptTest, UrlPattern) { | 73 TEST(ExtensionUserScriptTest, UrlPattern) { |
| 74 URLPattern pattern(kAllSchemes); | 74 URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); |
| 75 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 75 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*/foo*")); |
| 76 pattern.Parse("http://*/foo*", URLPattern::ERROR_ON_PORTS)); | |
| 77 | 76 |
| 78 UserScript script; | 77 UserScript script; |
| 79 script.add_url_pattern(pattern); | 78 script.add_url_pattern(pattern); |
| 80 EXPECT_TRUE(script.MatchesURL(GURL("http://monkey.com/foobar"))); | 79 EXPECT_TRUE(script.MatchesURL(GURL("http://monkey.com/foobar"))); |
| 81 EXPECT_FALSE(script.MatchesURL(GURL("http://monkey.com/hotdog"))); | 80 EXPECT_FALSE(script.MatchesURL(GURL("http://monkey.com/hotdog"))); |
| 82 | 81 |
| 83 // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc. | 82 // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc. |
| 84 } | 83 } |
| 85 | 84 |
| 86 TEST(ExtensionUserScriptTest, ExcludeUrlPattern) { | 85 TEST(ExtensionUserScriptTest, ExcludeUrlPattern) { |
| 87 UserScript script; | 86 UserScript script; |
| 88 | 87 |
| 89 URLPattern pattern(kAllSchemes); | 88 URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); |
| 90 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 89 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*")); |
| 91 pattern.Parse("http://*.nytimes.com/*", URLPattern::ERROR_ON_PORTS))
; | |
| 92 script.add_url_pattern(pattern); | 90 script.add_url_pattern(pattern); |
| 93 | 91 |
| 94 URLPattern exclude(kAllSchemes); | 92 URLPattern exclude(URLPattern::ERROR_ON_PORTS, kAllSchemes); |
| 95 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 93 ASSERT_EQ(URLPattern::PARSE_SUCCESS, exclude.Parse("*://*/*business*")); |
| 96 exclude.Parse("*://*/*business*", URLPattern::ERROR_ON_PORTS)); | |
| 97 script.add_exclude_url_pattern(exclude); | 94 script.add_exclude_url_pattern(exclude); |
| 98 | 95 |
| 99 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/health"))); | 96 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/health"))); |
| 100 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/business"))); | 97 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/business"))); |
| 101 EXPECT_TRUE(script.MatchesURL(GURL("http://business.nytimes.com"))); | 98 EXPECT_TRUE(script.MatchesURL(GURL("http://business.nytimes.com"))); |
| 102 } | 99 } |
| 103 | 100 |
| 104 TEST(ExtensionUserScriptTest, UrlPatternAndIncludeGlobs) { | 101 TEST(ExtensionUserScriptTest, UrlPatternAndIncludeGlobs) { |
| 105 UserScript script; | 102 UserScript script; |
| 106 | 103 |
| 107 URLPattern pattern(kAllSchemes); | 104 URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); |
| 108 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 105 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*")); |
| 109 pattern.Parse("http://*.nytimes.com/*", URLPattern::ERROR_ON_PORTS))
; | |
| 110 script.add_url_pattern(pattern); | 106 script.add_url_pattern(pattern); |
| 111 | 107 |
| 112 script.add_glob("*nytimes.com/???s/*"); | 108 script.add_glob("*nytimes.com/???s/*"); |
| 113 | 109 |
| 114 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/arts/1.html"))); | 110 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/arts/1.html"))); |
| 115 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/jobs/1.html"))); | 111 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/jobs/1.html"))); |
| 116 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/sports/1.html"))); | 112 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/sports/1.html"))); |
| 117 } | 113 } |
| 118 | 114 |
| 119 TEST(ExtensionUserScriptTest, UrlPatternAndExcludeGlobs) { | 115 TEST(ExtensionUserScriptTest, UrlPatternAndExcludeGlobs) { |
| 120 UserScript script; | 116 UserScript script; |
| 121 | 117 |
| 122 URLPattern pattern(kAllSchemes); | 118 URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); |
| 123 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 119 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*")); |
| 124 pattern.Parse("http://*.nytimes.com/*", URLPattern::ERROR_ON_PORTS))
; | |
| 125 script.add_url_pattern(pattern); | 120 script.add_url_pattern(pattern); |
| 126 | 121 |
| 127 script.add_exclude_glob("*science*"); | 122 script.add_exclude_glob("*science*"); |
| 128 | 123 |
| 129 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com"))); | 124 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com"))); |
| 130 EXPECT_FALSE(script.MatchesURL(GURL("http://science.nytimes.com"))); | 125 EXPECT_FALSE(script.MatchesURL(GURL("http://science.nytimes.com"))); |
| 131 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/science"))); | 126 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/science"))); |
| 132 } | 127 } |
| 133 | 128 |
| 134 TEST(ExtensionUserScriptTest, UrlPatternGlobInteraction) { | 129 TEST(ExtensionUserScriptTest, UrlPatternGlobInteraction) { |
| 135 // If there are both, match intersection(union(globs), union(urlpatterns)). | 130 // If there are both, match intersection(union(globs), union(urlpatterns)). |
| 136 UserScript script; | 131 UserScript script; |
| 137 | 132 |
| 138 URLPattern pattern(kAllSchemes); | 133 URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); |
| 139 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 134 ASSERT_EQ(URLPattern::PARSE_SUCCESS,pattern.Parse("http://www.google.com/*")); |
| 140 pattern.Parse("http://www.google.com/*", | |
| 141 URLPattern::ERROR_ON_PORTS)); | |
| 142 script.add_url_pattern(pattern); | 135 script.add_url_pattern(pattern); |
| 143 | 136 |
| 144 script.add_glob("*bar*"); | 137 script.add_glob("*bar*"); |
| 145 | 138 |
| 146 // No match, because it doesn't match the glob. | 139 // No match, because it doesn't match the glob. |
| 147 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/foo"))); | 140 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/foo"))); |
| 148 | 141 |
| 149 script.add_exclude_glob("*baz*"); | 142 script.add_exclude_glob("*baz*"); |
| 150 | 143 |
| 151 // No match, because it matches the exclude glob. | 144 // No match, because it matches the exclude glob. |
| 152 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/baz"))); | 145 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/baz"))); |
| 153 | 146 |
| 154 // Match, because it matches the glob, doesn't match the exclude glob. | 147 // Match, because it matches the glob, doesn't match the exclude glob. |
| 155 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/bar"))); | 148 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/bar"))); |
| 156 | 149 |
| 157 // Try with just a single exclude glob. | 150 // Try with just a single exclude glob. |
| 158 script.clear_globs(); | 151 script.clear_globs(); |
| 159 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); | 152 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); |
| 160 | 153 |
| 161 // Try with no globs or exclude globs. | 154 // Try with no globs or exclude globs. |
| 162 script.clear_exclude_globs(); | 155 script.clear_exclude_globs(); |
| 163 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); | 156 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); |
| 164 } | 157 } |
| 165 | 158 |
| 166 TEST(ExtensionUserScriptTest, Pickle) { | 159 TEST(ExtensionUserScriptTest, Pickle) { |
| 167 URLPattern pattern1(kAllSchemes); | 160 URLPattern pattern1(URLPattern::ERROR_ON_PORTS, kAllSchemes); |
| 168 URLPattern pattern2(kAllSchemes); | 161 URLPattern pattern2(URLPattern::ERROR_ON_PORTS, kAllSchemes); |
| 169 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 162 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern1.Parse("http://*/foo*")); |
| 170 pattern1.Parse("http://*/foo*", URLPattern::ERROR_ON_PORTS)); | 163 ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern2.Parse("http://bar/baz*")); |
| 171 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | |
| 172 pattern2.Parse("http://bar/baz*", URLPattern::ERROR_ON_PORTS)); | |
| 173 | 164 |
| 174 UserScript script1; | 165 UserScript script1; |
| 175 script1.js_scripts().push_back(UserScript::File( | 166 script1.js_scripts().push_back(UserScript::File( |
| 176 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), | 167 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), |
| 177 FilePath(FILE_PATH_LITERAL("foo.user.js")), | 168 FilePath(FILE_PATH_LITERAL("foo.user.js")), |
| 178 GURL("chrome-extension://abc/foo.user.js"))); | 169 GURL("chrome-extension://abc/foo.user.js"))); |
| 179 script1.css_scripts().push_back(UserScript::File( | 170 script1.css_scripts().push_back(UserScript::File( |
| 180 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), | 171 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), |
| 181 FilePath(FILE_PATH_LITERAL("foo.user.css")), | 172 FilePath(FILE_PATH_LITERAL("foo.user.css")), |
| 182 GURL("chrome-extension://abc/foo.user.css"))); | 173 GURL("chrome-extension://abc/foo.user.css"))); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 209 EXPECT_EQ(script1.globs()[i], script2.globs()[i]); | 200 EXPECT_EQ(script1.globs()[i], script2.globs()[i]); |
| 210 } | 201 } |
| 211 | 202 |
| 212 ASSERT_EQ(script1.url_patterns(), script2.url_patterns()); | 203 ASSERT_EQ(script1.url_patterns(), script2.url_patterns()); |
| 213 } | 204 } |
| 214 | 205 |
| 215 TEST(ExtensionUserScriptTest, Defaults) { | 206 TEST(ExtensionUserScriptTest, Defaults) { |
| 216 UserScript script; | 207 UserScript script; |
| 217 ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); | 208 ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); |
| 218 } | 209 } |
| OLD | NEW |