OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/android/data_usage/data_use_matcher.h" | 5 #include "chrome/browser/android/data_usage/data_use_matcher.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/test/histogram_tester.h" | |
16 #include "base/time/tick_clock.h" | 17 #include "base/time/tick_clock.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "chrome/browser/android/data_usage/external_data_use_observer.h" | 19 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
27 const char kUMAMatchingRulesCountValidHistogram[] = | |
28 "DataUsage.MatchingRulesCount.Valid"; | |
29 const char kUMAMatchingRulesCountInvalidHistogram[] = | |
30 "DataUsage.MatchingRulesCount.Invalid"; | |
31 const char kUMAURLRegexMatchDurationHistogram[] = | |
32 "DataUsage.Perf.URLRegexMatchDuration"; | |
33 | |
26 const char kRegexFoo[] = "http://foo.com/"; | 34 const char kRegexFoo[] = "http://foo.com/"; |
27 const char kLabelFoo[] = "label_foo"; | 35 const char kLabelFoo[] = "label_foo"; |
28 const char kAppFoo[] = "com.example.foo"; | 36 const char kAppFoo[] = "com.example.foo"; |
29 | 37 |
30 const uint32_t kDefaultMatchingRuleExpirationDurationSeconds = | 38 const uint32_t kDefaultMatchingRuleExpirationDurationSeconds = |
31 60 * 60 * 24; // 24 hours. | 39 60 * 60 * 24; // 24 hours. |
32 | 40 |
33 class NowTestTickClock : public base::TickClock { | 41 class NowTestTickClock : public base::TickClock { |
34 public: | 42 public: |
35 NowTestTickClock() {} | 43 NowTestTickClock() {} |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 content::TestBrowserThreadBundle thread_bundle_; | 95 content::TestBrowserThreadBundle thread_bundle_; |
88 DataUseMatcher data_use_matcher_; | 96 DataUseMatcher data_use_matcher_; |
89 DISALLOW_COPY_AND_ASSIGN(DataUseMatcherTest); | 97 DISALLOW_COPY_AND_ASSIGN(DataUseMatcherTest); |
90 }; | 98 }; |
91 | 99 |
92 TEST_F(DataUseMatcherTest, SingleRegex) { | 100 TEST_F(DataUseMatcherTest, SingleRegex) { |
93 const struct { | 101 const struct { |
94 std::string url; | 102 std::string url; |
95 std::string regex; | 103 std::string regex; |
96 bool expect_match; | 104 bool expect_match; |
105 int expect_valid_rules; | |
tbansal1
2016/01/15 17:11:19
s/expect_valid_rules/expect_count_valid_rules/
Raj
2016/01/15 18:27:02
Done.
| |
106 int expect_url_matches; | |
tbansal1
2016/01/15 17:11:18
s/expect_url_matches/expect_count_url_match_durati
Raj
2016/01/15 18:27:02
Done.
| |
97 } tests[] = { | 107 } tests[] = { |
98 {"http://www.google.com", "http://www.google.com/", true}, | 108 {"http://www.google.com", "http://www.google.com/", true, 1, 1}, |
99 {"http://www.Google.com", "http://www.google.com/", true}, | 109 {"http://www.Google.com", "http://www.google.com/", true, 1, 1}, |
100 {"http://www.googleacom", "http://www.google.com/", true}, | 110 {"http://www.googleacom", "http://www.google.com/", true, 1, 1}, |
101 {"http://www.googleaacom", "http://www.google.com/", false}, | 111 {"http://www.googleaacom", "http://www.google.com/", false, 1, 1}, |
102 {"http://www.google.com", "https://www.google.com/", false}, | 112 {"http://www.google.com", "https://www.google.com/", false, 1, 1}, |
103 {"http://www.google.com", "{http|https}://www[.]google[.]com/search.*", | 113 {"http://www.google.com", "{http|https}://www[.]google[.]com/search.*", |
104 false}, | 114 false, 1, 1}, |
105 {"https://www.google.com/search=test", | 115 {"https://www.google.com/search=test", |
106 "https://www[.]google[.]com/search.*", true}, | 116 "https://www[.]google[.]com/search.*", true, 1, 1}, |
107 {"https://www.googleacom/search=test", | 117 {"https://www.googleacom/search=test", |
108 "https://www[.]google[.]com/search.*", false}, | 118 "https://www[.]google[.]com/search.*", false, 1, 1}, |
109 {"https://www.google.com/Search=test", | 119 {"https://www.google.com/Search=test", |
110 "https://www[.]google[.]com/search.*", true}, | 120 "https://www[.]google[.]com/search.*", true, 1, 1}, |
111 {"www.google.com", "http://www.google.com", false}, | 121 {"www.google.com", "http://www.google.com", false, 1, 0}, |
112 {"www.google.com:80", "http://www.google.com", false}, | 122 {"www.google.com:80", "http://www.google.com", false, 1, 1}, |
113 {"http://www.google.com:80", "http://www.google.com", false}, | 123 {"http://www.google.com:80", "http://www.google.com", false, 1, 1}, |
114 {"http://www.google.com:80/", "http://www.google.com/", true}, | 124 {"http://www.google.com:80/", "http://www.google.com/", true, 1, 1}, |
115 {"", "http://www.google.com", false}, | 125 {"", "http://www.google.com", false, 1, 0}, |
116 {"", "", false}, | 126 {"", "", false, 0, 0}, |
117 {"https://www.google.com", "http://www.google.com", false}, | 127 {"https://www.google.com", "http://www.google.com", false, 1, 1}, |
128 {"https://www.google.com", "[", false, 0}, | |
129 {"https://www.google.com", "]", false, 1, 1}, | |
118 }; | 130 }; |
119 | 131 |
120 for (size_t i = 0; i < arraysize(tests); ++i) { | 132 for (size_t i = 0; i < arraysize(tests); ++i) { |
133 base::HistogramTester histogram_tester; | |
121 std::string label(""); | 134 std::string label(""); |
122 RegisterURLRegexes( | 135 RegisterURLRegexes( |
123 // App package name not specified in the matching rule. | 136 // App package name not specified in the matching rule. |
124 std::vector<std::string>(1, std::string()), | 137 std::vector<std::string>(1, std::string()), |
125 std::vector<std::string>(1, tests[i].regex), | 138 std::vector<std::string>(1, tests[i].regex), |
126 std::vector<std::string>(1, "label")); | 139 std::vector<std::string>(1, "label")); |
140 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, | |
141 tests[i].expect_valid_rules, 1); | |
142 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, | |
143 1 - tests[i].expect_valid_rules, 1); | |
127 EXPECT_EQ(tests[i].expect_match, | 144 EXPECT_EQ(tests[i].expect_match, |
128 data_use_matcher()->MatchesURL(GURL(tests[i].url), &label)) | 145 data_use_matcher()->MatchesURL(GURL(tests[i].url), &label)) |
129 << i; | 146 << i; |
147 histogram_tester.ExpectTotalCount(kUMAURLRegexMatchDurationHistogram, | |
148 tests[i].expect_url_matches); | |
130 | 149 |
131 // Verify label matches the expected label. | 150 // Verify label matches the expected label. |
132 std::string expected_label = ""; | 151 std::string expected_label = ""; |
133 if (tests[i].expect_match) | 152 if (tests[i].expect_match) |
134 expected_label = "label"; | 153 expected_label = "label"; |
135 | 154 |
136 EXPECT_EQ(expected_label, label); | 155 EXPECT_EQ(expected_label, label); |
137 EXPECT_FALSE(data_use_matcher()->MatchesAppPackageName( | 156 EXPECT_FALSE(data_use_matcher()->MatchesAppPackageName( |
138 "com.example.helloworld", &label)) | 157 "com.example.helloworld", &label)) |
139 << i; | 158 << i; |
140 // Empty package name should not match against empty package name in the | 159 // Empty package name should not match against empty package name in the |
141 // matching rule. | 160 // matching rule. |
142 EXPECT_FALSE( | 161 EXPECT_FALSE( |
143 data_use_matcher()->MatchesAppPackageName(std::string(), &label)) | 162 data_use_matcher()->MatchesAppPackageName(std::string(), &label)) |
144 << i; | 163 << i; |
145 } | 164 } |
146 } | 165 } |
147 | 166 |
148 TEST_F(DataUseMatcherTest, TwoRegex) { | 167 TEST_F(DataUseMatcherTest, TwoRegex) { |
149 const struct { | 168 const struct { |
150 std::string url; | 169 std::string url; |
151 std::string regex1; | 170 std::string regex1; |
152 std::string regex2; | 171 std::string regex2; |
153 bool expect_match; | 172 bool expect_match; |
173 int expect_valid_rules; | |
174 int expect_url_matches; | |
154 } tests[] = { | 175 } tests[] = { |
155 {"http://www.google.com", "http://www.google.com/", | 176 {"http://www.google.com", "http://www.google.com/", |
156 "https://www.google.com/", true}, | 177 "https://www.google.com/", true, 1, 1}, |
157 {"http://www.googleacom", "http://www.google.com/", | 178 {"http://www.googleacom", "http://www.google.com/", |
158 "http://www.google.com/", true}, | 179 "http://www.google.com/", true, 1, 1}, |
159 {"https://www.google.com", "http://www.google.com/", | 180 {"https://www.google.com", "http://www.google.com/", |
160 "https://www.google.com/", true}, | 181 "https://www.google.com/", true, 1, 1}, |
161 {"https://www.googleacom", "http://www.google.com/", | 182 {"https://www.googleacom", "http://www.google.com/", |
162 "https://www.google.com/", true}, | 183 "https://www.google.com/", true, 1, 1}, |
163 {"http://www.google.com", "{http|https}://www[.]google[.]com/search.*", | 184 {"http://www.google.com", "{http|https}://www[.]google[.]com/search.*", |
164 "", false}, | 185 "", false, 1, 1}, |
165 {"http://www.google.com/search=test", | 186 {"http://www.google.com/search=test", |
166 "http://www[.]google[.]com/search.*", | 187 "http://www[.]google[.]com/search.*", |
167 "https://www[.]google[.]com/search.*", true}, | 188 "https://www[.]google[.]com/search.*", true, 1, 1}, |
168 {"https://www.google.com/search=test", | 189 {"https://www.google.com/search=test", |
169 "http://www[.]google[.]com/search.*", | 190 "http://www[.]google[.]com/search.*", |
170 "https://www[.]google[.]com/search.*", true}, | 191 "https://www[.]google[.]com/search.*", true, 1, 1}, |
171 {"http://google.com/search=test", "http://www[.]google[.]com/search.*", | 192 {"http://google.com/search=test", "http://www[.]google[.]com/search.*", |
172 "https://www[.]google[.]com/search.*", false}, | 193 "https://www[.]google[.]com/search.*", false, 1, 1}, |
173 {"https://www.googleacom/search=test", "", | 194 {"https://www.googleacom/search=test", "", |
174 "https://www[.]google[.]com/search.*", false}, | 195 "https://www[.]google[.]com/search.*", false, 1, 1}, |
175 {"https://www.google.com/Search=test", "", | 196 {"https://www.google.com/Search=test", "", |
176 "https://www[.]google[.]com/search.*", true}, | 197 "https://www[.]google[.]com/search.*", true, 1, 1}, |
177 {"www.google.com", "http://www.google.com", "", false}, | 198 {"www.google.com", "http://www.google.com", "", false, 1, 0}, |
178 {"www.google.com:80", "http://www.google.com", "", false}, | 199 {"www.google.com:80", "http://www.google.com", "", false, 1, 1}, |
179 {"http://www.google.com:80", "http://www.google.com", "", false}, | 200 {"http://www.google.com:80", "http://www.google.com", "", false, 1, 1}, |
180 {"", "http://www.google.com", "", false}, | 201 {"", "http://www.google.com", "", false, 1, 0}, |
181 {"https://www.google.com", "http://www.google.com", "", false}, | 202 {"https://www.google.com", "http://www.google.com", "", false, 1, 1}, |
182 }; | 203 }; |
183 | 204 |
184 for (size_t i = 0; i < arraysize(tests); ++i) { | 205 for (size_t i = 0; i < arraysize(tests); ++i) { |
206 base::HistogramTester histogram_tester; | |
185 std::string got_label(""); | 207 std::string got_label(""); |
186 std::vector<std::string> url_regexes; | 208 std::vector<std::string> url_regexes; |
187 url_regexes.push_back(tests[i].regex1 + "|" + tests[i].regex2); | 209 url_regexes.push_back(tests[i].regex1 + "|" + tests[i].regex2); |
188 const std::string label("label"); | 210 const std::string label("label"); |
189 RegisterURLRegexes( | 211 RegisterURLRegexes( |
190 std::vector<std::string>(url_regexes.size(), "com.example.helloworld"), | 212 std::vector<std::string>(url_regexes.size(), "com.example.helloworld"), |
191 url_regexes, std::vector<std::string>(url_regexes.size(), label)); | 213 url_regexes, std::vector<std::string>(url_regexes.size(), label)); |
214 histogram_tester.ExpectTotalCount(kUMAMatchingRulesCountValidHistogram, 1); | |
215 histogram_tester.ExpectTotalCount(kUMAMatchingRulesCountInvalidHistogram, | |
216 1); | |
217 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, | |
218 tests[i].expect_valid_rules, 1); | |
219 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, | |
220 1 - tests[i].expect_valid_rules, 1); | |
192 EXPECT_EQ(tests[i].expect_match, | 221 EXPECT_EQ(tests[i].expect_match, |
193 data_use_matcher()->MatchesURL(GURL(tests[i].url), &got_label)) | 222 data_use_matcher()->MatchesURL(GURL(tests[i].url), &got_label)) |
194 << i; | 223 << i; |
224 histogram_tester.ExpectTotalCount(kUMAURLRegexMatchDurationHistogram, | |
225 tests[i].expect_url_matches); | |
195 const std::string expected_label = | 226 const std::string expected_label = |
196 tests[i].expect_match ? label : std::string(); | 227 tests[i].expect_match ? label : std::string(); |
197 EXPECT_EQ(expected_label, got_label); | 228 EXPECT_EQ(expected_label, got_label); |
198 | 229 |
199 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName( | 230 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName( |
200 "com.example.helloworld", &got_label)) | 231 "com.example.helloworld", &got_label)) |
201 << i; | 232 << i; |
202 EXPECT_EQ(label, got_label); | 233 EXPECT_EQ(label, got_label); |
203 } | 234 } |
204 } | 235 } |
205 | 236 |
206 TEST_F(DataUseMatcherTest, MultipleRegex) { | 237 TEST_F(DataUseMatcherTest, MultipleRegex) { |
238 base::HistogramTester histogram_tester; | |
207 std::vector<std::string> url_regexes; | 239 std::vector<std::string> url_regexes; |
208 url_regexes.push_back( | 240 url_regexes.push_back( |
209 "https?://www[.]google[.]com/#q=.*|https?://www[.]google[.]com[.]ph/" | 241 "https?://www[.]google[.]com/#q=.*|https?://www[.]google[.]com[.]ph/" |
210 "#q=.*|https?://www[.]google[.]com[.]ph/[?]gws_rd=ssl#q=.*"); | 242 "#q=.*|https?://www[.]google[.]com[.]ph/[?]gws_rd=ssl#q=.*"); |
211 RegisterURLRegexes( | 243 RegisterURLRegexes( |
212 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | 244 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, |
213 std::vector<std::string>(url_regexes.size(), "label")); | 245 std::vector<std::string>(url_regexes.size(), "label")); |
246 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, 1, | |
247 1); | |
248 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, 0, | |
249 1); | |
214 | 250 |
215 const struct { | 251 const struct { |
216 std::string url; | 252 std::string url; |
217 bool expect_match; | 253 bool expect_match; |
218 } tests[] = { | 254 } tests[] = { |
219 {"", false}, | 255 {"", false}, |
220 {"http://www.google.com", false}, | 256 {"http://www.google.com", false}, |
221 {"http://www.googleacom", false}, | 257 {"http://www.googleacom", false}, |
222 {"https://www.google.com", false}, | 258 {"https://www.google.com", false}, |
223 {"https://www.googleacom", false}, | 259 {"https://www.googleacom", false}, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | 314 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, |
279 std::vector<std::string>(url_regexes.size(), "label")); | 315 std::vector<std::string>(url_regexes.size(), "label")); |
280 EXPECT_FALSE(data_use_matcher()->MatchesURL(GURL(""), &label)); | 316 EXPECT_FALSE(data_use_matcher()->MatchesURL(GURL(""), &label)); |
281 EXPECT_FALSE(data_use_matcher()->MatchesURL( | 317 EXPECT_FALSE(data_use_matcher()->MatchesURL( |
282 GURL("http://www.google.com#q=abc"), &label)); | 318 GURL("http://www.google.com#q=abc"), &label)); |
283 EXPECT_TRUE(data_use_matcher()->MatchesURL( | 319 EXPECT_TRUE(data_use_matcher()->MatchesURL( |
284 GURL("http://www.google.co.in#q=abc"), &label)); | 320 GURL("http://www.google.co.in#q=abc"), &label)); |
285 } | 321 } |
286 | 322 |
287 TEST_F(DataUseMatcherTest, MultipleAppPackageName) { | 323 TEST_F(DataUseMatcherTest, MultipleAppPackageName) { |
324 base::HistogramTester histogram_tester; | |
288 std::vector<std::string> url_regexes; | 325 std::vector<std::string> url_regexes; |
289 url_regexes.push_back( | 326 url_regexes.push_back( |
290 "http://www[.]foo[.]com/#q=.*|https://www[.]foo[.]com/#q=.*"); | 327 "http://www[.]foo[.]com/#q=.*|https://www[.]foo[.]com/#q=.*"); |
291 url_regexes.push_back( | 328 url_regexes.push_back( |
292 "http://www[.]bar[.]com/#q=.*|https://www[.]bar[.]com/#q=.*"); | 329 "http://www[.]bar[.]com/#q=.*|https://www[.]bar[.]com/#q=.*"); |
293 url_regexes.push_back(""); | 330 url_regexes.push_back(""); |
294 | 331 |
295 std::vector<std::string> labels; | 332 std::vector<std::string> labels; |
296 const char kLabelBar[] = "label_bar"; | 333 const char kLabelBar[] = "label_bar"; |
297 const char kLabelBaz[] = "label_baz"; | 334 const char kLabelBaz[] = "label_baz"; |
298 labels.push_back(kLabelFoo); | 335 labels.push_back(kLabelFoo); |
299 labels.push_back(kLabelBar); | 336 labels.push_back(kLabelBar); |
300 labels.push_back(kLabelBaz); | 337 labels.push_back(kLabelBaz); |
301 | 338 |
302 std::vector<std::string> app_package_names; | 339 std::vector<std::string> app_package_names; |
303 const char kAppBar[] = "com.example.bar"; | 340 const char kAppBar[] = "com.example.bar"; |
304 const char kAppBaz[] = "com.example.baz"; | 341 const char kAppBaz[] = "com.example.baz"; |
305 app_package_names.push_back(kAppFoo); | 342 app_package_names.push_back(kAppFoo); |
306 app_package_names.push_back(kAppBar); | 343 app_package_names.push_back(kAppBar); |
307 app_package_names.push_back(kAppBaz); | 344 app_package_names.push_back(kAppBaz); |
308 | 345 |
309 RegisterURLRegexes(app_package_names, url_regexes, labels); | 346 RegisterURLRegexes(app_package_names, url_regexes, labels); |
347 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, 3, | |
348 1); | |
349 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, 0, | |
350 1); | |
310 | 351 |
311 // Test if labels are matched properly for app package names. | 352 // Test if labels are matched properly for app package names. |
312 std::string got_label; | 353 std::string got_label; |
313 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label)); | 354 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label)); |
314 EXPECT_EQ(kLabelFoo, got_label); | 355 EXPECT_EQ(kLabelFoo, got_label); |
315 | 356 |
316 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppBar, &got_label)); | 357 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppBar, &got_label)); |
317 EXPECT_EQ(kLabelBar, got_label); | 358 EXPECT_EQ(kLabelBar, got_label); |
318 | 359 |
319 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppBaz, &got_label)); | 360 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppBaz, &got_label)); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
380 << test.app_package_name; | 421 << test.app_package_name; |
381 DCHECK_EQ(base::TimeTicks::UnixEpoch() + test.expected_expiration_duration, | 422 DCHECK_EQ(base::TimeTicks::UnixEpoch() + test.expected_expiration_duration, |
382 expiration) | 423 expiration) |
383 << test.app_package_name; | 424 << test.app_package_name; |
384 } | 425 } |
385 } | 426 } |
386 | 427 |
387 // Tests if the expiration time encoded as milliseconds since epoch is parsed | 428 // Tests if the expiration time encoded as milliseconds since epoch is parsed |
388 // correctly. | 429 // correctly. |
389 TEST_F(DataUseMatcherTest, EncodeExpirationTimeInPackageName) { | 430 TEST_F(DataUseMatcherTest, EncodeExpirationTimeInPackageName) { |
431 base::HistogramTester histogram_tester; | |
390 NowTestTickClock* tick_clock = new NowTestTickClock(); | 432 NowTestTickClock* tick_clock = new NowTestTickClock(); |
391 | 433 |
392 // |tick_clock| will be owned by |data_use_matcher_|. | 434 // |tick_clock| will be owned by |data_use_matcher_|. |
393 data_use_matcher()->tick_clock_.reset(tick_clock); | 435 data_use_matcher()->tick_clock_.reset(tick_clock); |
394 | 436 |
395 std::vector<std::string> url_regexes, labels, app_package_names; | 437 std::vector<std::string> url_regexes, labels, app_package_names; |
396 url_regexes.push_back(kRegexFoo); | 438 url_regexes.push_back(kRegexFoo); |
397 labels.push_back(kLabelFoo); | 439 labels.push_back(kLabelFoo); |
398 | 440 |
399 // Set current time to to Epoch. | 441 // Set current time to Epoch. |
400 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch()); | 442 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch()); |
401 | 443 |
402 app_package_names.push_back(base::StringPrintf("%s|%d", kAppFoo, 10000)); | 444 app_package_names.push_back(base::StringPrintf("%s|%d", kAppFoo, 10000)); |
403 RegisterURLRegexes(app_package_names, url_regexes, labels); | 445 RegisterURLRegexes(app_package_names, url_regexes, labels); |
404 EXPECT_FALSE(IsExpired(0)); | 446 EXPECT_FALSE(IsExpired(0)); |
447 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, 1, | |
448 1); | |
449 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, 0, | |
450 1); | |
405 // Fast forward 10 seconds, and matching rule expires. | 451 // Fast forward 10 seconds, and matching rule expires. |
406 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + | 452 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + |
407 base::TimeDelta::FromMilliseconds(10000 + 1)); | 453 base::TimeDelta::FromMilliseconds(10000 + 1)); |
408 EXPECT_TRUE(IsExpired(0)); | 454 EXPECT_TRUE(IsExpired(0)); |
409 | 455 |
410 // Empty app package name. | 456 // Empty app package name. |
411 app_package_names.clear(); | 457 app_package_names.clear(); |
412 app_package_names.push_back(base::StringPrintf("|%d", 20000)); | 458 app_package_names.push_back(base::StringPrintf("|%d", 20000)); |
413 RegisterURLRegexes(app_package_names, url_regexes, labels); | 459 RegisterURLRegexes(app_package_names, url_regexes, labels); |
414 EXPECT_FALSE(IsExpired(0)); | 460 EXPECT_FALSE(IsExpired(0)); |
415 // Fast forward 20 seconds, and matching rule expires. | 461 // Fast forward 20 seconds, and matching rule expires. |
416 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + | 462 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + |
417 base::TimeDelta::FromMilliseconds(20000 + 1)); | 463 base::TimeDelta::FromMilliseconds(20000 + 1)); |
418 EXPECT_TRUE(IsExpired(0)); | 464 EXPECT_TRUE(IsExpired(0)); |
419 } | 465 } |
420 | 466 |
421 // Tests if the expiration time encoded in Java format is parsed correctly. | 467 // Tests if the expiration time encoded in Java format is parsed correctly. |
422 TEST_F(DataUseMatcherTest, EncodeJavaExpirationTimeInPackageName) { | 468 TEST_F(DataUseMatcherTest, EncodeJavaExpirationTimeInPackageName) { |
469 base::HistogramTester histogram_tester; | |
423 std::vector<std::string> url_regexes, labels, app_package_names; | 470 std::vector<std::string> url_regexes, labels, app_package_names; |
424 url_regexes.push_back(kRegexFoo); | 471 url_regexes.push_back(kRegexFoo); |
425 labels.push_back(kLabelFoo); | 472 labels.push_back(kLabelFoo); |
426 | 473 |
427 base::TimeTicks start_ticks = base::TimeTicks::Now(); | 474 base::TimeTicks start_ticks = base::TimeTicks::Now(); |
428 base::Time expiration_time = | 475 base::Time expiration_time = |
429 base::Time::Now() + base::TimeDelta::FromMilliseconds(10000); | 476 base::Time::Now() + base::TimeDelta::FromMilliseconds(10000); |
430 | 477 |
431 app_package_names.push_back(base::StringPrintf( | 478 app_package_names.push_back(base::StringPrintf( |
432 "%s|%lld", kAppFoo, | 479 "%s|%lld", kAppFoo, |
433 static_cast<long long int>(expiration_time.ToJavaTime()))); | 480 static_cast<long long int>(expiration_time.ToJavaTime()))); |
434 RegisterURLRegexes(app_package_names, url_regexes, labels); | 481 RegisterURLRegexes(app_package_names, url_regexes, labels); |
435 EXPECT_FALSE(IsExpired(0)); | 482 EXPECT_FALSE(IsExpired(0)); |
483 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, 1, | |
484 1); | |
485 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, 0, | |
486 1); | |
436 | 487 |
437 // Check if expiration duration is close to 10 seconds. | 488 // Check if expiration duration is close to 10 seconds. |
438 EXPECT_GE(base::TimeDelta::FromMilliseconds(10001), | 489 EXPECT_GE(base::TimeDelta::FromMilliseconds(10001), |
439 data_use_matcher()->matching_rules_[0]->expiration() - | 490 data_use_matcher()->matching_rules_[0]->expiration() - |
440 data_use_matcher()->tick_clock_->NowTicks()); | 491 data_use_matcher()->tick_clock_->NowTicks()); |
441 EXPECT_LE(base::TimeDelta::FromMilliseconds(9999), | 492 EXPECT_LE(base::TimeDelta::FromMilliseconds(9999), |
442 data_use_matcher()->matching_rules_[0]->expiration() - start_ticks); | 493 data_use_matcher()->matching_rules_[0]->expiration() - start_ticks); |
443 } | 494 } |
444 | 495 |
445 // Tests that expired matching rules are ignored by MatchesURL and | 496 // Tests that expired matching rules are ignored by MatchesURL and |
446 // MatchesAppPackageName. | 497 // MatchesAppPackageName. |
447 TEST_F(DataUseMatcherTest, MatchesIgnoresExpiredRules) { | 498 TEST_F(DataUseMatcherTest, MatchesIgnoresExpiredRules) { |
499 base::HistogramTester histogram_tester; | |
448 std::vector<std::string> url_regexes, labels, app_package_names; | 500 std::vector<std::string> url_regexes, labels, app_package_names; |
449 std::string got_label; | 501 std::string got_label; |
450 NowTestTickClock* tick_clock = new NowTestTickClock(); | 502 NowTestTickClock* tick_clock = new NowTestTickClock(); |
451 | 503 |
452 // |tick_clock| will be owned by |data_use_matcher_|. | 504 // |tick_clock| will be owned by |data_use_matcher_|. |
453 data_use_matcher()->tick_clock_.reset(tick_clock); | 505 data_use_matcher()->tick_clock_.reset(tick_clock); |
454 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch()); | 506 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch()); |
455 | 507 |
456 url_regexes.push_back(kRegexFoo); | 508 url_regexes.push_back(kRegexFoo); |
457 labels.push_back(kLabelFoo); | 509 labels.push_back(kLabelFoo); |
458 app_package_names.push_back(base::StringPrintf("%s|%d", kAppFoo, 10000)); | 510 app_package_names.push_back(base::StringPrintf("%s|%d", kAppFoo, 10000)); |
459 RegisterURLRegexes(app_package_names, url_regexes, labels); | 511 RegisterURLRegexes(app_package_names, url_regexes, labels); |
512 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, 1, | |
513 1); | |
514 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, 0, | |
515 1); | |
460 | 516 |
461 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + | 517 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + |
462 base::TimeDelta::FromMilliseconds(1)); | 518 base::TimeDelta::FromMilliseconds(1)); |
463 | 519 |
464 EXPECT_FALSE(IsExpired(0)); | 520 EXPECT_FALSE(IsExpired(0)); |
465 EXPECT_TRUE(data_use_matcher()->MatchesURL(GURL(kRegexFoo), &got_label)); | 521 EXPECT_TRUE(data_use_matcher()->MatchesURL(GURL(kRegexFoo), &got_label)); |
466 EXPECT_EQ(kLabelFoo, got_label); | 522 EXPECT_EQ(kLabelFoo, got_label); |
467 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label)); | 523 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label)); |
468 EXPECT_EQ(kLabelFoo, got_label); | 524 EXPECT_EQ(kLabelFoo, got_label); |
469 | 525 |
470 // Advance time to make it expired. | 526 // Advance time to make it expired. |
471 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + | 527 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + |
472 base::TimeDelta::FromMilliseconds(10001)); | 528 base::TimeDelta::FromMilliseconds(10001)); |
473 | 529 |
474 EXPECT_TRUE(IsExpired(0)); | 530 EXPECT_TRUE(IsExpired(0)); |
475 EXPECT_FALSE(data_use_matcher()->MatchesURL(GURL(kRegexFoo), &got_label)); | 531 EXPECT_FALSE(data_use_matcher()->MatchesURL(GURL(kRegexFoo), &got_label)); |
476 EXPECT_FALSE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label)); | 532 EXPECT_FALSE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label)); |
477 } | 533 } |
478 | 534 |
479 } // namespace android | 535 } // namespace android |
480 | 536 |
481 } // namespace chrome | 537 } // namespace chrome |
OLD | NEW |