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