Chromium Code Reviews| Index: chrome/browser/extensions/api/declarative/url_component_patterns_unittest.cc |
| diff --git a/chrome/browser/extensions/api/declarative/url_component_patterns_unittest.cc b/chrome/browser/extensions/api/declarative/url_component_patterns_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..46901c8a5b9fae1bfd1a4caf123e4a21e24dadf9 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/declarative/url_component_patterns_unittest.cc |
| @@ -0,0 +1,150 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/api/declarative/url_component_patterns.h" |
| + |
| +#include <set> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/string_util.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using namespace extensions; |
| + |
| +namespace { |
| + |
| +bool Matches(const SubstringPattern& pattern, std::string text) { |
| + return text.find(pattern.pattern()) != std::string::npos; |
| +} |
| + |
| +} // namespace |
| + |
| +TEST(UrlComponentPatternsTest, GURLCharacterSet) { |
| + // GURL guarantees that neither domain, nor path, nor query may contain |
| + // non ASCII-7 characters. We test this here, because a change to this |
| + // guarantee breaks this implementation horribly. |
| + GURL url("http://www.föö.com/föö?föö#föö"); |
| + EXPECT_TRUE(IsStringASCII(url.host())); |
| + EXPECT_TRUE(IsStringASCII(url.path())); |
| + EXPECT_TRUE(IsStringASCII(url.query())); |
| + EXPECT_FALSE(IsStringASCII(url.ref())); |
| +} |
| + |
| +// Basic tests of SubstringPattern objects |
| +TEST(UrlComponentPatternsTest, TestSingletonProperty) { |
| + UrlComponentPatterns matcher; |
| + SubstringPattern p1 = matcher.CreateHostEqualsPattern("www.google.com"); |
| + SubstringPattern p2 = matcher.CreateHostEqualsPattern("www.google.com"); |
| + EXPECT_EQ(p1, p2); |
| + SubstringPattern p3 = matcher.CreateHostEqualsPattern("www.google.de"); |
| + EXPECT_NE(p2.id(), p3.id()); |
| + EXPECT_NE(p2.pattern(), p3.pattern()); |
| +} |
| + |
| +TEST(UrlComponentPatternsTest, TestComponentSearches) { |
| + GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8" |
| + "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); |
| + UrlComponentPatterns matcher; |
| + std::string url = matcher.CanonlicalizeURLForComponentSearches(gurl); |
| + |
| + // Test host component. |
| + 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
|
| + EXPECT_TRUE(Matches(matcher.CreateHostPrefixPattern("www.goog"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateHostPrefixPattern("www.google.com"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateHostPrefixPattern(".www.google.com"), url)); |
| + EXPECT_FALSE( |
| + Matches(matcher.CreateHostPrefixPattern("www.google.com/"), url)); |
| + 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.
|
| + |
| + EXPECT_TRUE(Matches(matcher.CreateHostSuffixPattern(""), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateHostSuffixPattern("com"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateHostSuffixPattern(".com"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateHostSuffixPattern("www.google.com"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateHostSuffixPattern(".www.google.com"), url)); |
| + EXPECT_FALSE( |
| + Matches(matcher.CreateHostSuffixPattern("www.google.com/"), url)); |
| + EXPECT_FALSE(Matches(matcher.CreateHostSuffixPattern("webhp"), url)); |
| + |
| + EXPECT_FALSE(Matches(matcher.CreateHostEqualsPattern(""), url)); |
| + EXPECT_FALSE(Matches(matcher.CreateHostEqualsPattern("www"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateHostEqualsPattern("www.google.com"), url)); |
| + EXPECT_FALSE( |
| + Matches(matcher.CreateHostEqualsPattern("www.google.com/"), url)); |
| + |
| + |
| + // Test path component. |
| + EXPECT_TRUE(Matches(matcher.CreatePathPrefixPattern(""), url)); |
| + EXPECT_TRUE(Matches(matcher.CreatePathPrefixPattern("/web"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreatePathPrefixPattern("/webhp"), url)); |
| + EXPECT_FALSE(Matches(matcher.CreatePathPrefixPattern("/webhp?"), url)); |
| + |
| + EXPECT_TRUE(Matches(matcher.CreatePathSuffixPattern(""), url)); |
| + EXPECT_TRUE(Matches(matcher.CreatePathSuffixPattern("webhp"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreatePathSuffixPattern("/webhp"), url)); |
| + EXPECT_FALSE(Matches(matcher.CreatePathSuffixPattern("/webhp?"), url)); |
| + |
| + EXPECT_TRUE(Matches(matcher.CreatePathEqualsPattern("/webhp"), url)); |
| + EXPECT_FALSE(Matches(matcher.CreatePathEqualsPattern("/webhp?"), url)); |
| + EXPECT_FALSE(Matches(matcher.CreatePathEqualsPattern("www.google.com"), url)); |
| + |
| + |
| + // Test query component. |
| + EXPECT_TRUE(Matches(matcher.CreateQueryPrefixPattern(""), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateQueryPrefixPattern("?sourceid"), url)); |
| + EXPECT_FALSE(Matches(matcher.CreatePathPrefixPattern("sourceid"), url)); |
| + |
| + EXPECT_TRUE(Matches(matcher.CreateQuerySuffixPattern(""), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateQuerySuffixPattern("ion=1"), url)); |
| + EXPECT_FALSE(Matches(matcher.CreateQuerySuffixPattern("www"), url)); |
| + |
| + EXPECT_TRUE(Matches(matcher.CreateQueryEqualsPattern( |
| + "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); |
| + EXPECT_FALSE( |
| + Matches(matcher.CreateQueryEqualsPattern("www.google.com"), url)); |
| + |
| + |
| + // Test adjacent components |
| + EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixPattern( |
| + "google.com", "/webhp"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixPattern( |
| + "", "/webhp"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixPattern( |
| + "google.com", ""), url)); |
| + EXPECT_FALSE(Matches(matcher.CreateHostSuffixPathPrefixPattern( |
| + "www", ""), url)); |
| +} |
| + |
| +TEST(UrlComponentPatternsTest, TestFullSearches) { |
| + GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8" |
| + "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); |
| + UrlComponentPatterns matcher; |
| + std::string url = matcher.CanonlicalizeURLForFullSearches(gurl); |
| + |
| + EXPECT_TRUE(Matches(matcher.CreateURLPrefixPattern(""), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLPrefixPattern("www.goog"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLPrefixPattern("www.google.com"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLPrefixPattern(".www.google.com"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLPrefixPattern("www.google.com/"), url)); |
| + 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.
|
| + |
| + EXPECT_TRUE(Matches(matcher.CreateURLSuffixPattern(""), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLSuffixPattern("ion=1"), url)); |
| + EXPECT_FALSE(Matches(matcher.CreateURLSuffixPattern("www"), url)); |
| + |
| + EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern(""), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern("www.goog"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern(".www.goog"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern("webhp"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern("?"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern("sourceid"), url)); |
| + EXPECT_TRUE(Matches(matcher.CreateURLContainsPattern("ion=1"), url)); |
| + 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.
|
| + |
| + EXPECT_TRUE(Matches(matcher.CreateURLEqualsPattern( |
| + "www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); |
| + EXPECT_FALSE(Matches(matcher.CreateURLEqualsPattern("www.google.com"), url)); |
| + |
| +} |