| 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(kAllSchemes); |
| 75 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 75 ASSERT_EQ(URLPattern::PARSE_SUCCESS, |
| 76 pattern.Parse("http://*/foo*", URLPattern::PARSE_STRICT)); | 76 pattern.Parse("http://*/foo*", URLPattern::ERROR_ON_PORTS)); |
| 77 | 77 |
| 78 UserScript script; | 78 UserScript script; |
| 79 script.add_url_pattern(pattern); | 79 script.add_url_pattern(pattern); |
| 80 EXPECT_TRUE(script.MatchesURL(GURL("http://monkey.com/foobar"))); | 80 EXPECT_TRUE(script.MatchesURL(GURL("http://monkey.com/foobar"))); |
| 81 EXPECT_FALSE(script.MatchesURL(GURL("http://monkey.com/hotdog"))); | 81 EXPECT_FALSE(script.MatchesURL(GURL("http://monkey.com/hotdog"))); |
| 82 | 82 |
| 83 // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc. | 83 // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc. |
| 84 } | 84 } |
| 85 | 85 |
| 86 TEST(ExtensionUserScriptTest, ExcludeUrlPattern) { | 86 TEST(ExtensionUserScriptTest, ExcludeUrlPattern) { |
| 87 UserScript script; | 87 UserScript script; |
| 88 | 88 |
| 89 URLPattern pattern(kAllSchemes); | 89 URLPattern pattern(kAllSchemes); |
| 90 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 90 ASSERT_EQ(URLPattern::PARSE_SUCCESS, |
| 91 pattern.Parse("http://*.nytimes.com/*", URLPattern::PARSE_STRICT)); | 91 pattern.Parse("http://*.nytimes.com/*", URLPattern::ERROR_ON_PORTS))
; |
| 92 script.add_url_pattern(pattern); | 92 script.add_url_pattern(pattern); |
| 93 | 93 |
| 94 URLPattern exclude(kAllSchemes); | 94 URLPattern exclude(kAllSchemes); |
| 95 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 95 ASSERT_EQ(URLPattern::PARSE_SUCCESS, |
| 96 exclude.Parse("*://*/*business*", URLPattern::PARSE_STRICT)); | 96 exclude.Parse("*://*/*business*", URLPattern::ERROR_ON_PORTS)); |
| 97 script.add_exclude_url_pattern(exclude); | 97 script.add_exclude_url_pattern(exclude); |
| 98 | 98 |
| 99 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/health"))); | 99 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/health"))); |
| 100 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/business"))); | 100 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/business"))); |
| 101 EXPECT_TRUE(script.MatchesURL(GURL("http://business.nytimes.com"))); | 101 EXPECT_TRUE(script.MatchesURL(GURL("http://business.nytimes.com"))); |
| 102 } | 102 } |
| 103 | 103 |
| 104 TEST(ExtensionUserScriptTest, UrlPatternAndIncludeGlobs) { | 104 TEST(ExtensionUserScriptTest, UrlPatternAndIncludeGlobs) { |
| 105 UserScript script; | 105 UserScript script; |
| 106 | 106 |
| 107 URLPattern pattern(kAllSchemes); | 107 URLPattern pattern(kAllSchemes); |
| 108 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 108 ASSERT_EQ(URLPattern::PARSE_SUCCESS, |
| 109 pattern.Parse("http://*.nytimes.com/*", URLPattern::PARSE_STRICT)); | 109 pattern.Parse("http://*.nytimes.com/*", URLPattern::ERROR_ON_PORTS))
; |
| 110 script.add_url_pattern(pattern); | 110 script.add_url_pattern(pattern); |
| 111 | 111 |
| 112 script.add_glob("*nytimes.com/???s/*"); | 112 script.add_glob("*nytimes.com/???s/*"); |
| 113 | 113 |
| 114 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/arts/1.html"))); | 114 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"))); | 115 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"))); | 116 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/sports/1.html"))); |
| 117 } | 117 } |
| 118 | 118 |
| 119 TEST(ExtensionUserScriptTest, UrlPatternAndExcludeGlobs) { | 119 TEST(ExtensionUserScriptTest, UrlPatternAndExcludeGlobs) { |
| 120 UserScript script; | 120 UserScript script; |
| 121 | 121 |
| 122 URLPattern pattern(kAllSchemes); | 122 URLPattern pattern(kAllSchemes); |
| 123 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 123 ASSERT_EQ(URLPattern::PARSE_SUCCESS, |
| 124 pattern.Parse("http://*.nytimes.com/*", URLPattern::PARSE_STRICT)); | 124 pattern.Parse("http://*.nytimes.com/*", URLPattern::ERROR_ON_PORTS))
; |
| 125 script.add_url_pattern(pattern); | 125 script.add_url_pattern(pattern); |
| 126 | 126 |
| 127 script.add_exclude_glob("*science*"); | 127 script.add_exclude_glob("*science*"); |
| 128 | 128 |
| 129 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com"))); | 129 EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com"))); |
| 130 EXPECT_FALSE(script.MatchesURL(GURL("http://science.nytimes.com"))); | 130 EXPECT_FALSE(script.MatchesURL(GURL("http://science.nytimes.com"))); |
| 131 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/science"))); | 131 EXPECT_FALSE(script.MatchesURL(GURL("http://www.nytimes.com/science"))); |
| 132 } | 132 } |
| 133 | 133 |
| 134 TEST(ExtensionUserScriptTest, UrlPatternGlobInteraction) { | 134 TEST(ExtensionUserScriptTest, UrlPatternGlobInteraction) { |
| 135 // If there are both, match intersection(union(globs), union(urlpatterns)). | 135 // If there are both, match intersection(union(globs), union(urlpatterns)). |
| 136 UserScript script; | 136 UserScript script; |
| 137 | 137 |
| 138 URLPattern pattern(kAllSchemes); | 138 URLPattern pattern(kAllSchemes); |
| 139 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 139 ASSERT_EQ(URLPattern::PARSE_SUCCESS, |
| 140 pattern.Parse("http://www.google.com/*", | 140 pattern.Parse("http://www.google.com/*", |
| 141 URLPattern::PARSE_STRICT)); | 141 URLPattern::ERROR_ON_PORTS)); |
| 142 script.add_url_pattern(pattern); | 142 script.add_url_pattern(pattern); |
| 143 | 143 |
| 144 script.add_glob("*bar*"); | 144 script.add_glob("*bar*"); |
| 145 | 145 |
| 146 // No match, because it doesn't match the glob. | 146 // No match, because it doesn't match the glob. |
| 147 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/foo"))); | 147 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/foo"))); |
| 148 | 148 |
| 149 script.add_exclude_glob("*baz*"); | 149 script.add_exclude_glob("*baz*"); |
| 150 | 150 |
| 151 // No match, because it matches the exclude glob. | 151 // No match, because it matches the exclude glob. |
| 152 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/baz"))); | 152 EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/baz"))); |
| 153 | 153 |
| 154 // Match, because it matches the glob, doesn't match the exclude glob. | 154 // Match, because it matches the glob, doesn't match the exclude glob. |
| 155 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/bar"))); | 155 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/bar"))); |
| 156 | 156 |
| 157 // Try with just a single exclude glob. | 157 // Try with just a single exclude glob. |
| 158 script.clear_globs(); | 158 script.clear_globs(); |
| 159 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); | 159 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); |
| 160 | 160 |
| 161 // Try with no globs or exclude globs. | 161 // Try with no globs or exclude globs. |
| 162 script.clear_exclude_globs(); | 162 script.clear_exclude_globs(); |
| 163 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); | 163 EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); |
| 164 } | 164 } |
| 165 | 165 |
| 166 TEST(ExtensionUserScriptTest, Pickle) { | 166 TEST(ExtensionUserScriptTest, Pickle) { |
| 167 URLPattern pattern1(kAllSchemes); | 167 URLPattern pattern1(kAllSchemes); |
| 168 URLPattern pattern2(kAllSchemes); | 168 URLPattern pattern2(kAllSchemes); |
| 169 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 169 ASSERT_EQ(URLPattern::PARSE_SUCCESS, |
| 170 pattern1.Parse("http://*/foo*", URLPattern::PARSE_STRICT)); | 170 pattern1.Parse("http://*/foo*", URLPattern::ERROR_ON_PORTS)); |
| 171 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | 171 ASSERT_EQ(URLPattern::PARSE_SUCCESS, |
| 172 pattern2.Parse("http://bar/baz*", URLPattern::PARSE_STRICT)); | 172 pattern2.Parse("http://bar/baz*", URLPattern::ERROR_ON_PORTS)); |
| 173 | 173 |
| 174 UserScript script1; | 174 UserScript script1; |
| 175 script1.js_scripts().push_back(UserScript::File( | 175 script1.js_scripts().push_back(UserScript::File( |
| 176 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), | 176 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), |
| 177 FilePath(FILE_PATH_LITERAL("foo.user.js")), | 177 FilePath(FILE_PATH_LITERAL("foo.user.js")), |
| 178 GURL("chrome-extension://abc/foo.user.js"))); | 178 GURL("chrome-extension://abc/foo.user.js"))); |
| 179 script1.css_scripts().push_back(UserScript::File( | 179 script1.css_scripts().push_back(UserScript::File( |
| 180 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), | 180 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), |
| 181 FilePath(FILE_PATH_LITERAL("foo.user.css")), | 181 FilePath(FILE_PATH_LITERAL("foo.user.css")), |
| 182 GURL("chrome-extension://abc/foo.user.css"))); | 182 GURL("chrome-extension://abc/foo.user.css"))); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 212 for (size_t i = 0; i < script1.url_patterns().size(); ++i) { | 212 for (size_t i = 0; i < script1.url_patterns().size(); ++i) { |
| 213 EXPECT_EQ(script1.url_patterns()[i].GetAsString(), | 213 EXPECT_EQ(script1.url_patterns()[i].GetAsString(), |
| 214 script2.url_patterns()[i].GetAsString()); | 214 script2.url_patterns()[i].GetAsString()); |
| 215 } | 215 } |
| 216 } | 216 } |
| 217 | 217 |
| 218 TEST(ExtensionUserScriptTest, Defaults) { | 218 TEST(ExtensionUserScriptTest, Defaults) { |
| 219 UserScript script; | 219 UserScript script; |
| 220 ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); | 220 ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); |
| 221 } | 221 } |
| OLD | NEW |