Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/declarative/url_component_patterns.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/string_util.h" | |
| 12 #include "googleurl/src/gurl.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 using namespace extensions; | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 bool Matches(const SubstringPattern& pattern, std::string text) { | |
| 20 return text.find(pattern.pattern()) != std::string::npos; | |
| 21 } | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 TEST(UrlComponentPatternsTest, GURLCharacterSet) { | |
| 26 // GURL guarantees that neither domain, nor path, nor query may contain | |
| 27 // non ASCII-7 characters. We test this here, because a change to this | |
| 28 // guarantee breaks this implementation horribly. | |
| 29 GURL url("http://www.föö.com/föö?föö#föö"); | |
| 30 EXPECT_TRUE(IsStringASCII(url.host())); | |
| 31 EXPECT_TRUE(IsStringASCII(url.path())); | |
| 32 EXPECT_TRUE(IsStringASCII(url.query())); | |
| 33 EXPECT_FALSE(IsStringASCII(url.ref())); | |
| 34 } | |
| 35 | |
| 36 // Basic tests of SubstringPattern objects | |
| 37 TEST(UrlComponentPatternsTest, TestSingletonProperty) { | |
| 38 UrlComponentPatterns matcher; | |
| 39 SubstringPattern p1 = matcher.CreateHostEqualsPattern("www.google.com"); | |
| 40 SubstringPattern p2 = matcher.CreateHostEqualsPattern("www.google.com"); | |
| 41 EXPECT_EQ(p1, p2); | |
| 42 SubstringPattern p3 = matcher.CreateHostEqualsPattern("www.google.de"); | |
| 43 EXPECT_NE(p2.id(), p3.id()); | |
| 44 EXPECT_NE(p2.pattern(), p3.pattern()); | |
| 45 } | |
| 46 | |
| 47 TEST(UrlComponentPatternsTest, TestComponentSearches) { | |
| 48 GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8" | |
| 49 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); | |
| 50 UrlComponentPatterns matcher; | |
| 51 std::string url = matcher.CanonlicalizeURLForComponentSearches(gurl); | |
| 52 | |
| 53 // Test host component. | |
| 54 EXPECT_TRUE(Matches(matcher.CreateHostPrefixPattern(""), url)); | |
|
Matt Perry
2012/02/14 01:38:34
nit: take a look at EXPECT_PRED2, which I believe
battre
2012/02/14 19:32:21
It does, but:
- It is one character longer per lin
| |
| 55 EXPECT_TRUE(Matches(matcher.CreateHostPrefixPattern("www.goog"), url)); | |
| 56 EXPECT_TRUE(Matches(matcher.CreateHostPrefixPattern("www.google.com"), url)); | |
| 57 EXPECT_TRUE(Matches(matcher.CreateHostPrefixPattern(".www.google.com"), url)); | |
| 58 EXPECT_FALSE( | |
| 59 Matches(matcher.CreateHostPrefixPattern("www.google.com/"), url)); | |
| 60 EXPECT_FALSE(Matches(matcher.CreateHostPrefixPattern("webhp"), url)); | |
|
Matt Perry
2012/02/14 01:38:34
Test that "google.com" does not match. In general,
battre
2012/02/14 19:32:21
Done.
| |
| 61 | |
| 62 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPattern(""), url)); | |
| 63 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPattern("com"), url)); | |
| 64 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPattern(".com"), url)); | |
| 65 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPattern("www.google.com"), url)); | |
| 66 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPattern(".www.google.com"), url)); | |
| 67 EXPECT_FALSE( | |
| 68 Matches(matcher.CreateHostSuffixPattern("www.google.com/"), url)); | |
| 69 EXPECT_FALSE(Matches(matcher.CreateHostSuffixPattern("webhp"), url)); | |
| 70 | |
| 71 EXPECT_FALSE(Matches(matcher.CreateHostEqualsPattern(""), url)); | |
| 72 EXPECT_FALSE(Matches(matcher.CreateHostEqualsPattern("www"), url)); | |
| 73 EXPECT_TRUE(Matches(matcher.CreateHostEqualsPattern("www.google.com"), url)); | |
| 74 EXPECT_FALSE( | |
| 75 Matches(matcher.CreateHostEqualsPattern("www.google.com/"), url)); | |
| 76 | |
| 77 | |
| 78 // Test path component. | |
| 79 EXPECT_TRUE(Matches(matcher.CreatePathPrefixPattern(""), url)); | |
| 80 EXPECT_TRUE(Matches(matcher.CreatePathPrefixPattern("/web"), url)); | |
| 81 EXPECT_TRUE(Matches(matcher.CreatePathPrefixPattern("/webhp"), url)); | |
| 82 EXPECT_FALSE(Matches(matcher.CreatePathPrefixPattern("/webhp?"), url)); | |
| 83 | |
| 84 EXPECT_TRUE(Matches(matcher.CreatePathSuffixPattern(""), url)); | |
| 85 EXPECT_TRUE(Matches(matcher.CreatePathSuffixPattern("webhp"), url)); | |
| 86 EXPECT_TRUE(Matches(matcher.CreatePathSuffixPattern("/webhp"), url)); | |
| 87 EXPECT_FALSE(Matches(matcher.CreatePathSuffixPattern("/webhp?"), url)); | |
| 88 | |
| 89 EXPECT_TRUE(Matches(matcher.CreatePathEqualsPattern("/webhp"), url)); | |
| 90 EXPECT_FALSE(Matches(matcher.CreatePathEqualsPattern("/webhp?"), url)); | |
| 91 EXPECT_FALSE(Matches(matcher.CreatePathEqualsPattern("www.google.com"), url)); | |
| 92 | |
| 93 | |
| 94 // Test query component. | |
| 95 EXPECT_TRUE(Matches(matcher.CreateQueryPrefixPattern(""), url)); | |
| 96 EXPECT_TRUE(Matches(matcher.CreateQueryPrefixPattern("?sourceid"), url)); | |
| 97 EXPECT_FALSE(Matches(matcher.CreatePathPrefixPattern("sourceid"), url)); | |
| 98 | |
| 99 EXPECT_TRUE(Matches(matcher.CreateQuerySuffixPattern(""), url)); | |
| 100 EXPECT_TRUE(Matches(matcher.CreateQuerySuffixPattern("ion=1"), url)); | |
| 101 EXPECT_FALSE(Matches(matcher.CreateQuerySuffixPattern("www"), url)); | |
| 102 | |
| 103 EXPECT_TRUE(Matches(matcher.CreateQueryEqualsPattern( | |
| 104 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); | |
| 105 EXPECT_FALSE( | |
| 106 Matches(matcher.CreateQueryEqualsPattern("www.google.com"), url)); | |
| 107 | |
| 108 | |
| 109 // Test adjacent components | |
| 110 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixPattern( | |
| 111 "google.com", "/webhp"), url)); | |
| 112 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixPattern( | |
| 113 "", "/webhp"), url)); | |
| 114 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixPattern( | |
| 115 "google.com", ""), url)); | |
| 116 EXPECT_FALSE(Matches(matcher.CreateHostSuffixPathPrefixPattern( | |
| 117 "www", ""), url)); | |
| 118 } | |
| 119 | |
| 120 TEST(UrlComponentPatternsTest, TestFullSearches) { | |
| 121 GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8" | |
| 122 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); | |
| 123 UrlComponentPatterns matcher; | |
| 124 std::string url = matcher.CanonlicalizeURLForFullSearches(gurl); | |
| 125 | |
| 126 EXPECT_TRUE(Matches(matcher.CreateURLPrefixPattern(""), url)); | |
| 127 EXPECT_TRUE(Matches(matcher.CreateURLPrefixPattern("www.goog"), url)); | |
| 128 EXPECT_TRUE(Matches(matcher.CreateURLPrefixPattern("www.google.com"), url)); | |
| 129 EXPECT_TRUE(Matches(matcher.CreateURLPrefixPattern(".www.google.com"), url)); | |
| 130 EXPECT_TRUE(Matches(matcher.CreateURLPrefixPattern("www.google.com/"), url)); | |
| 131 EXPECT_FALSE(Matches(matcher.CreateHostPrefixPattern("webhp"), url)); | |
|
Matt Perry
2012/02/14 01:38:34
did you mean CreateURLPrefixPattern?
battre
2012/02/14 19:32:21
Done.
| |
| 132 | |
| 133 EXPECT_TRUE(Matches(matcher.CreateURLSuffixPattern(""), url)); | |
| 134 EXPECT_TRUE(Matches(matcher.CreateURLSuffixPattern("ion=1"), url)); | |
| 135 EXPECT_FALSE(Matches(matcher.CreateURLSuffixPattern("www"), url)); | |
| 136 | |
| 137 EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern(""), url)); | |
| 138 EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern("www.goog"), url)); | |
| 139 EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern(".www.goog"), url)); | |
| 140 EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern("webhp"), url)); | |
| 141 EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern("?"), url)); | |
| 142 EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern("sourceid"), url)); | |
| 143 EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern("ion=1"), url)); | |
| 144 EXPECT_FALSE(Matches(matcher.CreateURLSuffixPattern("foobar"), url)); | |
|
Matt Perry
2012/02/14 01:38:34
also test that url_contains("search") does not mat
battre
2012/02/14 19:32:21
Done.
| |
| 145 | |
| 146 EXPECT_TRUE(Matches(matcher.CreateURLEqualsPattern( | |
| 147 "www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); | |
| 148 EXPECT_FALSE(Matches(matcher.CreateURLEqualsPattern("www.google.com"), url)); | |
| 149 | |
| 150 } | |
| OLD | NEW |