| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/api/declarative/url_matcher.h" | 5 #include "chrome/browser/extensions/api/declarative/url_matcher.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.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 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 URLMatcherCondition m2; | 23 URLMatcherCondition m2; |
| 24 m2 = m1; | 24 m2 = m1; |
| 25 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m2.criterion()); | 25 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m2.criterion()); |
| 26 EXPECT_EQ(&pattern, m2.substring_pattern()); | 26 EXPECT_EQ(&pattern, m2.substring_pattern()); |
| 27 | 27 |
| 28 URLMatcherCondition m3(m1); | 28 URLMatcherCondition m3(m1); |
| 29 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m3.criterion()); | 29 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m3.criterion()); |
| 30 EXPECT_EQ(&pattern, m3.substring_pattern()); | 30 EXPECT_EQ(&pattern, m3.substring_pattern()); |
| 31 } | 31 } |
| 32 | 32 |
| 33 TEST(URLMatcherSchemeFilter, TestMatching) { |
| 34 URLMatcherSchemeFilter filter1("https"); |
| 35 std::vector<std::string> filter2_content; |
| 36 filter2_content.push_back("http"); |
| 37 filter2_content.push_back("https"); |
| 38 URLMatcherSchemeFilter filter2(filter2_content); |
| 39 |
| 40 GURL matching_url("https://www.foobar.com"); |
| 41 GURL non_matching_url("http://www.foobar.com"); |
| 42 EXPECT_TRUE(filter1.IsMatch(matching_url)); |
| 43 EXPECT_FALSE(filter1.IsMatch(non_matching_url)); |
| 44 EXPECT_TRUE(filter2.IsMatch(matching_url)); |
| 45 EXPECT_TRUE(filter2.IsMatch(non_matching_url)); |
| 46 } |
| 47 |
| 33 TEST(URLMatcherConditionTest, IsFullURLCondition) { | 48 TEST(URLMatcherConditionTest, IsFullURLCondition) { |
| 34 SubstringPattern pattern("example.com", 1); | 49 SubstringPattern pattern("example.com", 1); |
| 35 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, | 50 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, |
| 36 &pattern).IsFullURLCondition()); | 51 &pattern).IsFullURLCondition()); |
| 37 | 52 |
| 38 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS, | 53 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS, |
| 39 &pattern).IsFullURLCondition()); | 54 &pattern).IsFullURLCondition()); |
| 40 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS, | 55 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS, |
| 41 &pattern).IsFullURLCondition()); | 56 &pattern).IsFullURLCondition()); |
| 42 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS, | 57 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS, |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 EXPECT_EQ(1, condition_set->id()); | 331 EXPECT_EQ(1, condition_set->id()); |
| 317 EXPECT_EQ(2u, condition_set->conditions().size()); | 332 EXPECT_EQ(2u, condition_set->conditions().size()); |
| 318 | 333 |
| 319 std::set<SubstringPattern::ID> matching_substring_patterns; | 334 std::set<SubstringPattern::ID> matching_substring_patterns; |
| 320 matching_substring_patterns.insert(m1.substring_pattern()->id()); | 335 matching_substring_patterns.insert(m1.substring_pattern()->id()); |
| 321 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url1)); | 336 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url1)); |
| 322 | 337 |
| 323 matching_substring_patterns.insert(m2.substring_pattern()->id()); | 338 matching_substring_patterns.insert(m2.substring_pattern()->id()); |
| 324 EXPECT_TRUE(condition_set->IsMatch(matching_substring_patterns, url1)); | 339 EXPECT_TRUE(condition_set->IsMatch(matching_substring_patterns, url1)); |
| 325 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url2)); | 340 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url2)); |
| 341 |
| 342 |
| 343 // Test scheme filters. |
| 344 scoped_refptr<URLMatcherConditionSet> condition_set2( |
| 345 new URLMatcherConditionSet(1, conditions, |
| 346 scoped_ptr<URLMatcherSchemeFilter>( |
| 347 new URLMatcherSchemeFilter("https")))); |
| 348 EXPECT_FALSE(condition_set2->IsMatch(matching_substring_patterns, url1)); |
| 349 scoped_refptr<URLMatcherConditionSet> condition_set3( |
| 350 new URLMatcherConditionSet(1, conditions, |
| 351 scoped_ptr<URLMatcherSchemeFilter>( |
| 352 new URLMatcherSchemeFilter("http")))); |
| 353 EXPECT_TRUE(condition_set3->IsMatch(matching_substring_patterns, url1)); |
| 326 } | 354 } |
| 327 | 355 |
| 328 | 356 |
| 329 // | 357 // |
| 330 // URLMatcher | 358 // URLMatcher |
| 331 // | 359 // |
| 332 | 360 |
| 333 TEST(URLMatcherTest, FullTest) { | 361 TEST(URLMatcherTest, FullTest) { |
| 334 GURL url1("http://www.example.com/foo?bar=1"); | 362 GURL url1("http://www.example.com/foo?bar=1"); |
| 335 GURL url2("http://foo.example.com/index.html"); | 363 GURL url2("http://foo.example.com/index.html"); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 // The cached singleton in matcher.condition_factory_ should be destroyed to | 413 // The cached singleton in matcher.condition_factory_ should be destroyed to |
| 386 // free memory. | 414 // free memory. |
| 387 int patternId2 = factory->CreateHostSuffixCondition( | 415 int patternId2 = factory->CreateHostSuffixCondition( |
| 388 "example.com").substring_pattern()->id(); | 416 "example.com").substring_pattern()->id(); |
| 389 // If patternId1 and patternId2 are different that indicates that | 417 // If patternId1 and patternId2 are different that indicates that |
| 390 // matcher.condition_factory_ does not leak memory. | 418 // matcher.condition_factory_ does not leak memory. |
| 391 EXPECT_NE(patternId1, patternId2); | 419 EXPECT_NE(patternId1, patternId2); |
| 392 } | 420 } |
| 393 | 421 |
| 394 } // namespace extensions | 422 } // namespace extensions |
| OLD | NEW |