| Index: chrome/browser/extensions/api/declarative/url_matcher_unittest.cc
|
| diff --git a/chrome/browser/extensions/api/declarative/url_matcher_unittest.cc b/chrome/browser/extensions/api/declarative/url_matcher_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a9de3befdfb5f3a9dc04bfea47252c0d1ef8890e
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/declarative/url_matcher_unittest.cc
|
| @@ -0,0 +1,195 @@
|
| +// 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_matcher.h"
|
| +
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace extensions {
|
| +
|
| +TEST(UrlMatcherConditionTest, Constructors) {
|
| + UrlMatcherCondition m1(UrlMatcherCondition::HOST_SUFFIX, "example.com");
|
| + EXPECT_EQ(UrlMatcherCondition::HOST_SUFFIX, m1.criterion());
|
| + EXPECT_EQ("example.com", m1.value());
|
| + EXPECT_EQ(SubstringPattern(), m1.substring_pattern());
|
| +
|
| + UrlMatcherCondition m2(UrlMatcherCondition::HOST_PREFIX, "foo.com");
|
| + m2 = m1;
|
| + EXPECT_EQ(UrlMatcherCondition::HOST_SUFFIX, m2.criterion());
|
| + EXPECT_EQ("example.com", m2.value());
|
| + EXPECT_EQ(SubstringPattern(), m2.substring_pattern());
|
| +
|
| + UrlMatcherCondition m3(m2);
|
| + EXPECT_EQ(UrlMatcherCondition::HOST_SUFFIX, m3.criterion());
|
| + EXPECT_EQ("example.com", m3.value());
|
| + EXPECT_EQ(SubstringPattern(), m3.substring_pattern());
|
| +}
|
| +
|
| +TEST(UrlMatcherConditionTest, IsFullUrlCondition) {
|
| + EXPECT_FALSE(UrlMatcherCondition(UrlMatcherCondition::HOST_SUFFIX,
|
| + "example.com").IsFullUrlCondition());
|
| +
|
| + EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::HOST_CONTAINS,
|
| + "example.com").IsFullUrlCondition());
|
| + EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::PATH_CONTAINS,
|
| + "example.com").IsFullUrlCondition());
|
| + EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::QUERY_CONTAINS,
|
| + "example.com").IsFullUrlCondition());
|
| +
|
| + EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::URL_PREFIX,
|
| + "example.com").IsFullUrlCondition());
|
| + EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::URL_SUFFIX,
|
| + "example.com").IsFullUrlCondition());
|
| + EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::URL_CONTAINS,
|
| + "example.com").IsFullUrlCondition());
|
| + EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::URL_EQUALS,
|
| + "example.com").IsFullUrlCondition());
|
| +}
|
| +
|
| +TEST(UrlMatcherConditionTest, SubstringMatching) {
|
| + UrlComponentPatterns url_component_patterns;
|
| +
|
| + // Check that substring_pattern is filled.
|
| + UrlMatcherCondition m1(UrlMatcherCondition::HOST_SUFFIX, "example.com");
|
| + m1.BuildSubstringPattern(&url_component_patterns);
|
| + EXPECT_EQ(url_component_patterns.CreateHostSuffixPattern("example.com"),
|
| + m1.substring_pattern());
|
| +
|
| + // Check that patterns are recycled.
|
| + UrlMatcherCondition m2(UrlMatcherCondition::HOST_SUFFIX, "example.com");
|
| + m2.BuildSubstringPattern(&url_component_patterns);
|
| + EXPECT_EQ(m1.substring_pattern(), m1.substring_pattern());
|
| +
|
| + // Check that SubstringPattern::ID are handled correctly.
|
| + GURL url("http://www.example.com/index.html");
|
| +
|
| + std::set<SubstringPattern::ID> not_matching;
|
| + not_matching.insert(m1.substring_pattern().id() + 1);
|
| + EXPECT_FALSE(m1.IsMatch(not_matching, url));
|
| +
|
| + std::set<SubstringPattern::ID> matching;
|
| + matching.insert(m1.substring_pattern().id());
|
| + EXPECT_TRUE(m1.IsMatch(matching, url));
|
| +
|
| + // Check that URLs are tested correctly for containment.
|
| + UrlMatcherCondition m3(UrlMatcherCondition::HOST_CONTAINS, "example");
|
| + m3.BuildSubstringPattern(&url_component_patterns);
|
| + matching.insert(m3.substring_pattern().id());
|
| + EXPECT_TRUE(m3.IsFullUrlCondition());
|
| + EXPECT_FALSE(m3.IsMatch(matching, GURL("http://www.foo.com/example")));
|
| + EXPECT_TRUE(m3.IsMatch(matching, GURL("http://www.example.com/foo")));
|
| +}
|
| +
|
| +TEST(UrlMatcherConditionSetTest, Constructors) {
|
| + UrlComponentPatterns url_component_patterns;
|
| + UrlMatcherCondition m1(UrlMatcherCondition::HOST_SUFFIX, "example.com");
|
| + m1.BuildSubstringPattern(&url_component_patterns);
|
| + UrlMatcherCondition m2(UrlMatcherCondition::PATH_CONTAINS, "foo");
|
| + m2.BuildSubstringPattern(&url_component_patterns);
|
| +
|
| + std::vector<UrlMatcherCondition> conditions;
|
| + conditions.push_back(m1);
|
| + conditions.push_back(m2);
|
| +
|
| + UrlMatcherConditionSet condition_set(1, conditions);
|
| + EXPECT_EQ(1, condition_set.id());
|
| + EXPECT_EQ(2u, condition_set.conditions().size());
|
| +
|
| + std::vector<UrlMatcherCondition> other_conditions;
|
| + other_conditions.push_back(m1);
|
| + UrlMatcherConditionSet condition_set2(2, other_conditions);
|
| + condition_set2 = condition_set;
|
| + EXPECT_EQ(1, condition_set2.id());
|
| + EXPECT_EQ(2u, condition_set2.conditions().size());
|
| +
|
| + UrlMatcherConditionSet condition_set3(condition_set);
|
| + EXPECT_EQ(1, condition_set2.id());
|
| + EXPECT_EQ(2u, condition_set2.conditions().size());
|
| +}
|
| +
|
| +TEST(UrlMatcherConditionSetTest, Matching) {
|
| + GURL url1("http://www.example.com/foo?bar=1");
|
| + GURL url2("http://foo.example.com/index.html");
|
| +
|
| + UrlComponentPatterns url_component_patterns;
|
| + UrlMatcherCondition m1(UrlMatcherCondition::HOST_SUFFIX, "example.com");
|
| + m1.BuildSubstringPattern(&url_component_patterns);
|
| + UrlMatcherCondition m2(UrlMatcherCondition::PATH_CONTAINS, "foo");
|
| + m2.BuildSubstringPattern(&url_component_patterns);
|
| +
|
| + std::vector<UrlMatcherCondition> conditions;
|
| + conditions.push_back(m1);
|
| + conditions.push_back(m2);
|
| +
|
| + UrlMatcherConditionSet condition_set(1, conditions);
|
| + EXPECT_EQ(1, condition_set.id());
|
| + EXPECT_EQ(2u, condition_set.conditions().size());
|
| +
|
| + std::set<SubstringPattern::ID> matching_substring_patterns;
|
| + matching_substring_patterns.insert(m1.substring_pattern().id());
|
| + EXPECT_FALSE(condition_set.IsMatch(matching_substring_patterns, url1));
|
| +
|
| + matching_substring_patterns.insert(m2.substring_pattern().id());
|
| + EXPECT_TRUE(condition_set.IsMatch(matching_substring_patterns, url1));
|
| + EXPECT_FALSE(condition_set.IsMatch(matching_substring_patterns, url2));
|
| +}
|
| +
|
| +TEST(UrlMatcherTest, FullTest) {
|
| + GURL url1("http://www.example.com/foo?bar=1");
|
| + GURL url2("http://foo.example.com/index.html");
|
| +
|
| + UrlMatcher matcher;
|
| +
|
| + // First insert.
|
| + std::vector<UrlMatcherCondition> conditions1;
|
| + conditions1.push_back(UrlMatcherCondition(UrlMatcherCondition::HOST_SUFFIX,
|
| + "example.com"));
|
| + conditions1.push_back(UrlMatcherCondition(UrlMatcherCondition::PATH_CONTAINS,
|
| + "foo"));
|
| + UrlMatcherConditionSet condition_set1(1, conditions1);
|
| + std::vector<UrlMatcherConditionSet> insert1;
|
| + insert1.push_back(condition_set1);
|
| + matcher.AddConditionSets(insert1);
|
| + EXPECT_EQ(1u, matcher.MatchUrl(url1).size());
|
| + EXPECT_EQ(0u, matcher.MatchUrl(url2).size());
|
| +
|
| + // Second insert.
|
| + std::vector<UrlMatcherCondition> conditions2;
|
| + conditions2.push_back(UrlMatcherCondition(UrlMatcherCondition::HOST_SUFFIX,
|
| + "example.com"));
|
| + UrlMatcherConditionSet condition_set2(2, conditions2);
|
| + std::vector<UrlMatcherConditionSet> insert2;
|
| + insert2.push_back(condition_set2);
|
| + matcher.AddConditionSets(insert2);
|
| + EXPECT_EQ(2u, matcher.MatchUrl(url1).size());
|
| + EXPECT_EQ(1u, matcher.MatchUrl(url2).size());
|
| +
|
| + // This should be the cached singleton in matcher.url_component_patterns_.
|
| + SubstringPattern pattern1 =
|
| + matcher.url_component_patterns_.CreateHostSuffixPattern("example.com");
|
| +
|
| + // Removal of last insert.
|
| + std::vector<UrlMatcherConditionSet::ID> remove2;
|
| + remove2.push_back(condition_set2.id());
|
| + matcher.RemoveConditionSets(remove2);
|
| + EXPECT_EQ(1u, matcher.MatchUrl(url1).size());
|
| + EXPECT_EQ(0u, matcher.MatchUrl(url2).size());
|
| +
|
| + // Removal of first insert.
|
| + std::vector<UrlMatcherConditionSet::ID> remove1;
|
| + remove1.push_back(condition_set1.id());
|
| + matcher.RemoveConditionSets(remove1);
|
| + EXPECT_EQ(0u, matcher.MatchUrl(url1).size());
|
| + EXPECT_EQ(0u, matcher.MatchUrl(url2).size());
|
| +
|
| + // The cached singleton in matcher.url_component_patterns_ should be
|
| + // destroyed to free memory.
|
| + SubstringPattern pattern2 =
|
| + matcher.url_component_patterns_.CreateHostSuffixPattern("example.com");
|
| + // If pattern1 and pattern2 are different that indicates that
|
| + // matcher.url_component_patterns_ does not leak memory.
|
| + EXPECT_NE(pattern1, pattern2);
|
| +}
|
| +
|
| +} // namespace extensions
|
|
|