| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 |
| 11 static const int kAllSchemes = |
| 12 URLPattern::SCHEME_HTTP | |
| 13 URLPattern::SCHEME_HTTPS | |
| 14 URLPattern::SCHEME_FILE | |
| 15 URLPattern::SCHEME_FTP | |
| 16 URLPattern::SCHEME_CHROMEUI; |
| 17 |
| 11 TEST(UserScriptTest, Match1) { | 18 TEST(UserScriptTest, Match1) { |
| 12 UserScript script; | 19 UserScript script; |
| 13 script.add_glob("*mail.google.com*"); | 20 script.add_glob("*mail.google.com*"); |
| 14 script.add_glob("*mail.yahoo.com*"); | 21 script.add_glob("*mail.yahoo.com*"); |
| 15 script.add_glob("*mail.msn.com*"); | 22 script.add_glob("*mail.msn.com*"); |
| 16 EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com"))); | 23 EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com"))); |
| 17 EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com/foo"))); | 24 EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com/foo"))); |
| 18 EXPECT_TRUE(script.MatchesUrl(GURL("https://mail.google.com/foo"))); | 25 EXPECT_TRUE(script.MatchesUrl(GURL("https://mail.google.com/foo"))); |
| 19 EXPECT_TRUE(script.MatchesUrl(GURL("ftp://mail.google.com/foo"))); | 26 EXPECT_TRUE(script.MatchesUrl(GURL("ftp://mail.google.com/foo"))); |
| 20 EXPECT_TRUE(script.MatchesUrl(GURL("http://woo.mail.google.com/foo"))); | 27 EXPECT_TRUE(script.MatchesUrl(GURL("http://woo.mail.google.com/foo"))); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 63 |
| 57 TEST(UserScriptTest, Match5) { | 64 TEST(UserScriptTest, Match5) { |
| 58 UserScript script; | 65 UserScript script; |
| 59 script.add_glob("*foo*"); | 66 script.add_glob("*foo*"); |
| 60 EXPECT_TRUE(script.MatchesUrl(GURL("http://foo.com/bar"))); | 67 EXPECT_TRUE(script.MatchesUrl(GURL("http://foo.com/bar"))); |
| 61 EXPECT_TRUE(script.MatchesUrl(GURL("http://baz.org/foo/bar"))); | 68 EXPECT_TRUE(script.MatchesUrl(GURL("http://baz.org/foo/bar"))); |
| 62 EXPECT_FALSE(script.MatchesUrl(GURL("http://baz.org"))); | 69 EXPECT_FALSE(script.MatchesUrl(GURL("http://baz.org"))); |
| 63 } | 70 } |
| 64 | 71 |
| 65 TEST(UserScriptTest, Match6) { | 72 TEST(UserScriptTest, Match6) { |
| 66 URLPattern pattern(URLPattern::SCHEMES_ALL); | 73 URLPattern pattern(kAllSchemes); |
| 67 ASSERT_TRUE(pattern.Parse("http://*/foo*")); | 74 ASSERT_TRUE(pattern.Parse("http://*/foo*")); |
| 68 | 75 |
| 69 UserScript script; | 76 UserScript script; |
| 70 script.add_url_pattern(pattern); | 77 script.add_url_pattern(pattern); |
| 71 EXPECT_TRUE(script.MatchesUrl(GURL("http://monkey.com/foobar"))); | 78 EXPECT_TRUE(script.MatchesUrl(GURL("http://monkey.com/foobar"))); |
| 72 EXPECT_FALSE(script.MatchesUrl(GURL("http://monkey.com/hotdog"))); | 79 EXPECT_FALSE(script.MatchesUrl(GURL("http://monkey.com/hotdog"))); |
| 73 | 80 |
| 74 // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc. | 81 // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc. |
| 75 } | 82 } |
| 76 | 83 |
| 77 TEST(UserScriptTest, UrlPatternGlobInteraction) { | 84 TEST(UserScriptTest, UrlPatternGlobInteraction) { |
| 78 // If there are both, match intersection(union(globs), union(urlpatterns)). | 85 // If there are both, match intersection(union(globs), union(urlpatterns)). |
| 79 UserScript script; | 86 UserScript script; |
| 80 | 87 |
| 81 URLPattern pattern(URLPattern::SCHEMES_ALL); | 88 URLPattern pattern(kAllSchemes); |
| 82 ASSERT_TRUE(pattern.Parse("http://www.google.com/*")); | 89 ASSERT_TRUE(pattern.Parse("http://www.google.com/*")); |
| 83 script.add_url_pattern(pattern); | 90 script.add_url_pattern(pattern); |
| 84 | 91 |
| 85 script.add_glob("*bar*"); | 92 script.add_glob("*bar*"); |
| 86 | 93 |
| 87 // No match, because it doesn't match the glob. | 94 // No match, because it doesn't match the glob. |
| 88 EXPECT_FALSE(script.MatchesUrl(GURL("http://www.google.com/foo"))); | 95 EXPECT_FALSE(script.MatchesUrl(GURL("http://www.google.com/foo"))); |
| 89 | 96 |
| 90 script.add_exclude_glob("*baz*"); | 97 script.add_exclude_glob("*baz*"); |
| 91 | 98 |
| 92 // No match, because it matches the exclude glob. | 99 // No match, because it matches the exclude glob. |
| 93 EXPECT_FALSE(script.MatchesUrl(GURL("http://www.google.com/baz"))); | 100 EXPECT_FALSE(script.MatchesUrl(GURL("http://www.google.com/baz"))); |
| 94 | 101 |
| 95 // Match, because it matches the glob, doesn't match the exclude glob. | 102 // Match, because it matches the glob, doesn't match the exclude glob. |
| 96 EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/bar"))); | 103 EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/bar"))); |
| 97 | 104 |
| 98 // Try with just a single exclude glob. | 105 // Try with just a single exclude glob. |
| 99 script.clear_globs(); | 106 script.clear_globs(); |
| 100 EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/foo"))); | 107 EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/foo"))); |
| 101 | 108 |
| 102 // Try with no globs or exclude globs. | 109 // Try with no globs or exclude globs. |
| 103 script.clear_exclude_globs(); | 110 script.clear_exclude_globs(); |
| 104 EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/foo"))); | 111 EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/foo"))); |
| 105 } | 112 } |
| 106 | 113 |
| 107 TEST(UserScriptTest, Pickle) { | 114 TEST(UserScriptTest, Pickle) { |
| 108 URLPattern pattern1(URLPattern::SCHEMES_ALL); | 115 URLPattern pattern1(kAllSchemes); |
| 109 URLPattern pattern2(URLPattern::SCHEMES_ALL); | 116 URLPattern pattern2(kAllSchemes); |
| 110 ASSERT_TRUE(pattern1.Parse("http://*/foo*")); | 117 ASSERT_TRUE(pattern1.Parse("http://*/foo*")); |
| 111 ASSERT_TRUE(pattern2.Parse("http://bar/baz*")); | 118 ASSERT_TRUE(pattern2.Parse("http://bar/baz*")); |
| 112 | 119 |
| 113 UserScript script1; | 120 UserScript script1; |
| 114 script1.js_scripts().push_back(UserScript::File( | 121 script1.js_scripts().push_back(UserScript::File( |
| 115 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), | 122 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), |
| 116 FilePath(FILE_PATH_LITERAL("foo.user.js")), | 123 FilePath(FILE_PATH_LITERAL("foo.user.js")), |
| 117 GURL("chrome-user-script:/foo.user.js"))); | 124 GURL("chrome-user-script:/foo.user.js"))); |
| 118 script1.css_scripts().push_back(UserScript::File( | 125 script1.css_scripts().push_back(UserScript::File( |
| 119 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), | 126 FilePath(FILE_PATH_LITERAL("c:\\foo\\")), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 for (size_t i = 0; i < script1.url_patterns().size(); ++i) { | 158 for (size_t i = 0; i < script1.url_patterns().size(); ++i) { |
| 152 EXPECT_EQ(script1.url_patterns()[i].GetAsString(), | 159 EXPECT_EQ(script1.url_patterns()[i].GetAsString(), |
| 153 script2.url_patterns()[i].GetAsString()); | 160 script2.url_patterns()[i].GetAsString()); |
| 154 } | 161 } |
| 155 } | 162 } |
| 156 | 163 |
| 157 TEST(UserScriptTest, Defaults) { | 164 TEST(UserScriptTest, Defaults) { |
| 158 UserScript script; | 165 UserScript script; |
| 159 ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); | 166 ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); |
| 160 } | 167 } |
| OLD | NEW |