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.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(ExternalDataUseObserverTest, SingleRegex) { |
| 21 scoped_ptr<data_usage::DataUseAggregator> data_use_aggregator( |
| 22 new data_usage::DataUseAggregator()); |
| 23 scoped_ptr<ExternalDataUseObserver> external_data_use_observer( |
| 24 new ExternalDataUseObserver(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.Google.com", "http://www.google.com/", true}, |
| 33 {"http://www.googleacom", "http://www.google.com/", true}, |
| 34 {"http://www.googleaacom", "http://www.google.com/", false}, |
| 35 {"http://www.google.com", "https://www.google.com/", false}, |
| 36 {"http://www.google.com", "{http|https}://www\\.google\\.com/search.*", |
| 37 false}, |
| 38 {"https://www.google.com/search=test", |
| 39 "https://www\\.google\\.com/search.*", true}, |
| 40 {"https://www.googleacom/search=test", |
| 41 "https://www\\.google\\.com/search.*", false}, |
| 42 {"https://www.google.com/Search=test", |
| 43 "https://www\\.google\\.com/search.*", true}, |
| 44 {"www.google.com", "http://www.google.com", false}, |
| 45 {"www.google.com:80", "http://www.google.com", false}, |
| 46 {"http://www.google.com:80", "http://www.google.com", false}, |
| 47 {"http://www.google.com:80/", "http://www.google.com/", true}, |
| 48 {"", "http://www.google.com", false}, |
| 49 {"", "", false}, |
| 50 {"https://www.google.com", "http://www.google.com", false}, |
| 51 }; |
| 52 |
| 53 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 54 external_data_use_observer->RegisterURLRegexes( |
| 55 std::vector<std::string>(1, tests[i].regex)); |
| 56 EXPECT_EQ(tests[i].expect_match, |
| 57 external_data_use_observer->Matches(GURL(tests[i].url))) |
| 58 << i; |
| 59 } |
| 60 } |
| 61 |
| 62 TEST(ExternalDataUseObserverTest, TwoRegex) { |
| 63 scoped_ptr<data_usage::DataUseAggregator> data_use_aggregator( |
| 64 new data_usage::DataUseAggregator()); |
| 65 scoped_ptr<ExternalDataUseObserver> external_data_use_observer( |
| 66 new ExternalDataUseObserver(data_use_aggregator.get())); |
| 67 |
| 68 const struct { |
| 69 std::string url; |
| 70 std::string regex1; |
| 71 std::string regex2; |
| 72 bool expect_match; |
| 73 } tests[] = { |
| 74 {"http://www.google.com", "http://www.google.com/", |
| 75 "https://www.google.com/", true}, |
| 76 {"http://www.googleacom", "http://www.google.com/", |
| 77 "http://www.google.com/", true}, |
| 78 {"https://www.google.com", "http://www.google.com/", |
| 79 "https://www.google.com/", true}, |
| 80 {"https://www.googleacom", "http://www.google.com/", |
| 81 "https://www.google.com/", true}, |
| 82 {"http://www.google.com", "{http|https}://www\\.google\\.com/search.*", |
| 83 "", false}, |
| 84 {"http://www.google.com/search=test", |
| 85 "http://www\\.google\\.com/search.*", |
| 86 "https://www\\.google\\.com/search.*", true}, |
| 87 {"https://www.google.com/search=test", |
| 88 "http://www\\.google\\.com/search.*", |
| 89 "https://www\\.google\\.com/search.*", true}, |
| 90 {"http://google.com/search=test", "http://www\\.google\\.com/search.*", |
| 91 "https://www\\.google\\.com/search.*", false}, |
| 92 {"https://www.googleacom/search=test", "", |
| 93 "https://www\\.google\\.com/search.*", false}, |
| 94 {"https://www.google.com/Search=test", "", |
| 95 "https://www\\.google\\.com/search.*", true}, |
| 96 {"www.google.com", "http://www.google.com", "", false}, |
| 97 {"www.google.com:80", "http://www.google.com", "", false}, |
| 98 {"http://www.google.com:80", "http://www.google.com", "", false}, |
| 99 {"", "http://www.google.com", "", false}, |
| 100 {"https://www.google.com", "http://www.google.com", "", false}, |
| 101 }; |
| 102 |
| 103 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 104 std::vector<std::string> url_regexes; |
| 105 url_regexes.push_back(tests[i].regex1); |
| 106 url_regexes.push_back(tests[i].regex2); |
| 107 external_data_use_observer->RegisterURLRegexes(url_regexes); |
| 108 EXPECT_EQ(tests[i].expect_match, |
| 109 external_data_use_observer->Matches(GURL(tests[i].url))) |
| 110 << i; |
| 111 } |
| 112 } |
| 113 |
| 114 TEST(ExternalDataUseObserverTest, MultipleRegex) { |
| 115 scoped_ptr<data_usage::DataUseAggregator> data_use_aggregator( |
| 116 new data_usage::DataUseAggregator()); |
| 117 scoped_ptr<ExternalDataUseObserver> external_data_use_observer( |
| 118 new ExternalDataUseObserver(data_use_aggregator.get())); |
| 119 |
| 120 std::vector<std::string> url_regexes; |
| 121 url_regexes.push_back("http://www\\.google\\.com/#q=.*"); |
| 122 url_regexes.push_back("https://www\\.google\\.com/#q=.*"); |
| 123 url_regexes.push_back("http://www\\.google\\.co\\.in/#q=.*"); |
| 124 url_regexes.push_back("https://www\\.google\\.co\\.in/#q=.*"); |
| 125 external_data_use_observer->RegisterURLRegexes(url_regexes); |
| 126 |
| 127 const struct { |
| 128 std::string url; |
| 129 bool expect_match; |
| 130 } tests[] = { |
| 131 {"", false}, |
| 132 {"http://www.google.com", false}, |
| 133 {"http://www.googleacom", false}, |
| 134 {"https://www.google.com", false}, |
| 135 {"https://www.googleacom", false}, |
| 136 {"http://www.google.com", false}, |
| 137 {"quic://www.google.com/q=test", false}, |
| 138 {"http://www.google.com/q=test", false}, |
| 139 {"http://www.google.com/.q=test", false}, |
| 140 {"http://www.google.com/#q=test", true}, |
| 141 {"https://www.google.com/#q=test", true}, |
| 142 {"http://www.google.co.in/#q=test", true}, |
| 143 {"https://www.google.co.in/#q=test", true}, |
| 144 {"http://www.google.co.br/#q=test", false}, |
| 145 {"http://google.com/#q=test", false}, |
| 146 {"https://www.googleacom/#q=test", false}, |
| 147 {"https://www.google.com/#Q=test", true}, // case in-sensitive |
| 148 {"www.google.com/#q=test", false}, |
| 149 {"www.google.com:80/#q=test", false}, |
| 150 {"http://www.google.com:80/#q=test", true}, |
| 151 }; |
| 152 |
| 153 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 154 EXPECT_EQ(tests[i].expect_match, |
| 155 external_data_use_observer->Matches(GURL(tests[i].url))) |
| 156 << i << tests[i].url; |
| 157 } |
| 158 } |
| 159 |
| 160 TEST(ExternalDataUseObserverTest, ChangeRegex) { |
| 161 scoped_ptr<data_usage::DataUseAggregator> data_use_aggregator( |
| 162 new data_usage::DataUseAggregator()); |
| 163 scoped_ptr<ExternalDataUseObserver> external_data_use_observer( |
| 164 new ExternalDataUseObserver(data_use_aggregator.get())); |
| 165 |
| 166 // When no regex is specified, the URL match should fail. |
| 167 EXPECT_FALSE(external_data_use_observer->Matches(GURL(""))); |
| 168 EXPECT_FALSE( |
| 169 external_data_use_observer->Matches(GURL("http://www.google.com"))); |
| 170 |
| 171 std::vector<std::string> url_regexes; |
| 172 url_regexes.push_back("http://www\\.google\\.com/#q=.*"); |
| 173 url_regexes.push_back("https://www\\.google\\.com/#q=.*"); |
| 174 external_data_use_observer->RegisterURLRegexes(url_regexes); |
| 175 |
| 176 EXPECT_FALSE(external_data_use_observer->Matches(GURL(""))); |
| 177 EXPECT_TRUE( |
| 178 external_data_use_observer->Matches(GURL("http://www.google.com#q=abc"))); |
| 179 EXPECT_FALSE(external_data_use_observer->Matches( |
| 180 GURL("http://www.google.co.in#q=abc"))); |
| 181 |
| 182 // Change the regular expressions to verify that the new regexes replace |
| 183 // the ones specified before. |
| 184 url_regexes.clear(); |
| 185 url_regexes.push_back("http://www\\.google\\.co\\.in/#q=.*"); |
| 186 url_regexes.push_back("https://www\\.google\\.co\\.in/#q=.*"); |
| 187 external_data_use_observer->RegisterURLRegexes(url_regexes); |
| 188 EXPECT_FALSE(external_data_use_observer->Matches(GURL(""))); |
| 189 EXPECT_FALSE( |
| 190 external_data_use_observer->Matches(GURL("http://www.google.com#q=abc"))); |
| 191 EXPECT_TRUE(external_data_use_observer->Matches( |
| 192 GURL("http://www.google.co.in#q=abc"))); |
| 193 } |
| 194 |
| 195 } // namespace android |
| 196 |
| 197 } // namespace chrome |
OLD | NEW |