| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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/android/datausage/external_data_use_observer_android.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "components/data_usage/core/data_use.h" |
| 12 #include "components/data_usage/core/data_use_aggregator.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace chrome { |
| 17 |
| 18 namespace android { |
| 19 |
| 20 TEST(ExternalDataUseObserverAndroidTest, SingleRegex) { |
| 21 scoped_ptr<data_usage::DataUseAggregator> data_use_aggregator( |
| 22 new data_usage::DataUseAggregator()); |
| 23 scoped_ptr<ExternalDataUseObserverAndroid> external_data_use_observer_android( |
| 24 new ExternalDataUseObserverAndroid(data_use_aggregator.get())); |
| 25 |
| 26 const struct { |
| 27 std::string url; |
| 28 std::string regex; |
| 29 bool expect_match; |
| 30 } tests[] = { |
| 31 {"http://www.google.com", "http://www.google.com/", true}, |
| 32 {"http://www.googleacom", "http://www.google.com/", true}, |
| 33 {"http://www.googleaacom", "http://www.google.com/", false}, |
| 34 {"http://www.google.com", "https://www.google.com/", false}, |
| 35 {"http://www.google.com", "{http|https}://www\\.google\\.com/search.*", |
| 36 false}, |
| 37 {"https://www.google.com/search=test", |
| 38 "https://www\\.google\\.com/search.*", true}, |
| 39 {"https://www.googleacom/search=test", |
| 40 "https://www\\.google\\.com/search.*", false}, |
| 41 {"https://www.google.com/Search=test", |
| 42 "https://www\\.google\\.com/search.*", false}, |
| 43 {"www.google.com", "http://www.google.com", false}, |
| 44 {"www.google.com:80", "http://www.google.com", false}, |
| 45 {"http://www.google.com:80", "http://www.google.com", false}, |
| 46 {"http://www.google.com:80/", "http://www.google.com/", true}, |
| 47 {"", "http://www.google.com", false}, |
| 48 {"", "", false}, |
| 49 {"https://www.google.com", "http://www.google.com", false}, |
| 50 }; |
| 51 |
| 52 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 53 external_data_use_observer_android->RegisterURLRegexes( |
| 54 std::vector<std::string>(1, tests[i].regex)); |
| 55 EXPECT_EQ(tests[i].expect_match, |
| 56 external_data_use_observer_android->Matches(GURL(tests[i].url))) |
| 57 << i; |
| 58 } |
| 59 } |
| 60 |
| 61 TEST(ExternalDataUseObserverAndroidTest, TwoRegex) { |
| 62 scoped_ptr<data_usage::DataUseAggregator> data_use_aggregator( |
| 63 new data_usage::DataUseAggregator()); |
| 64 scoped_ptr<ExternalDataUseObserverAndroid> external_data_use_observer_android( |
| 65 new ExternalDataUseObserverAndroid(data_use_aggregator.get())); |
| 66 |
| 67 const struct { |
| 68 std::string url; |
| 69 std::string regex1; |
| 70 std::string regex2; |
| 71 bool expect_match; |
| 72 } tests[] = { |
| 73 {"http://www.google.com", "http://www.google.com/", |
| 74 "https://www.google.com/", true}, |
| 75 {"http://www.googleacom", "http://www.google.com/", |
| 76 "http://www.google.com/", true}, |
| 77 {"https://www.google.com", "http://www.google.com/", |
| 78 "https://www.google.com/", true}, |
| 79 {"https://www.googleacom", "http://www.google.com/", |
| 80 "https://www.google.com/", true}, |
| 81 {"http://www.google.com", "{http|https}://www\\.google\\.com/search.*", |
| 82 "", false}, |
| 83 {"http://www.google.com/search=test", |
| 84 "http://www\\.google\\.com/search.*", |
| 85 "https://www\\.google\\.com/search.*", true}, |
| 86 {"https://www.google.com/search=test", |
| 87 "http://www\\.google\\.com/search.*", |
| 88 "https://www\\.google\\.com/search.*", true}, |
| 89 {"http://google.com/search=test", "http://www\\.google\\.com/search.*", |
| 90 "https://www\\.google\\.com/search.*", false}, |
| 91 {"https://www.googleacom/search=test", "", |
| 92 "https://www\\.google\\.com/search.*", false}, |
| 93 {"https://www.google.com/Search=test", "", |
| 94 "https://www\\.google\\.com/search.*", false}, |
| 95 {"www.google.com", "http://www.google.com", "", false}, |
| 96 {"www.google.com:80", "http://www.google.com", "", false}, |
| 97 {"http://www.google.com:80", "http://www.google.com", "", false}, |
| 98 {"", "http://www.google.com", "", false}, |
| 99 {"https://www.google.com", "http://www.google.com", "", false}, |
| 100 }; |
| 101 |
| 102 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 103 std::vector<std::string> url_regexes; |
| 104 url_regexes.push_back(tests[i].regex1); |
| 105 url_regexes.push_back(tests[i].regex2); |
| 106 external_data_use_observer_android->RegisterURLRegexes(url_regexes); |
| 107 EXPECT_EQ(tests[i].expect_match, |
| 108 external_data_use_observer_android->Matches(GURL(tests[i].url))) |
| 109 << i; |
| 110 } |
| 111 } |
| 112 |
| 113 TEST(ExternalDataUseObserverAndroidTest, MultipleRegex) { |
| 114 scoped_ptr<data_usage::DataUseAggregator> data_use_aggregator( |
| 115 new data_usage::DataUseAggregator()); |
| 116 scoped_ptr<ExternalDataUseObserverAndroid> external_data_use_observer_android( |
| 117 new ExternalDataUseObserverAndroid(data_use_aggregator.get())); |
| 118 |
| 119 std::vector<std::string> url_regexes; |
| 120 url_regexes.push_back("http://www\\.google\\.com/#q=.*"); |
| 121 url_regexes.push_back("https://www\\.google\\.com/#q=.*"); |
| 122 url_regexes.push_back("http://www\\.google\\.co\\.in/#q=.*"); |
| 123 url_regexes.push_back("https://www\\.google\\.co\\.in/#q=.*"); |
| 124 external_data_use_observer_android->RegisterURLRegexes(url_regexes); |
| 125 |
| 126 const struct { |
| 127 std::string url; |
| 128 bool expect_match; |
| 129 } tests[] = { |
| 130 {"", false}, |
| 131 {"http://www.google.com", false}, |
| 132 {"http://www.googleacom", false}, |
| 133 {"https://www.google.com", false}, |
| 134 {"https://www.googleacom", false}, |
| 135 {"http://www.google.com", false}, |
| 136 {"quic://www.google.com/q=test", false}, |
| 137 {"http://www.google.com/q=test", false}, |
| 138 {"http://www.google.com/.q=test", false}, |
| 139 {"http://www.google.com/#q=test", true}, |
| 140 {"https://www.google.com/#q=test", true}, |
| 141 {"http://www.google.co.in/#q=test", true}, |
| 142 {"https://www.google.co.in/#q=test", true}, |
| 143 {"http://www.google.co.br/#q=test", false}, |
| 144 {"http://google.com/#q=test", false}, |
| 145 {"https://www.googleacom/#q=test", false}, |
| 146 {"https://www.google.com/#Q=test", false}, |
| 147 {"www.google.com/#q=test", false}, |
| 148 {"www.google.com:80/#q=test", false}, |
| 149 {"http://www.google.com:80/#q=test", true}, |
| 150 }; |
| 151 |
| 152 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 153 EXPECT_EQ(tests[i].expect_match, |
| 154 external_data_use_observer_android->Matches(GURL(tests[i].url))) |
| 155 << i; |
| 156 } |
| 157 } |
| 158 |
| 159 } // namespace android |
| 160 |
| 161 } // namespace chrome |
| OLD | NEW |