Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: chrome/browser/android/data_usage/data_use_matcher_unittest.cc

Issue 1582043002: Add histograms for data usage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed holte comments Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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_count_valid_rules;
106 int expect_count_url_match_duration_samples;
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_count_valid_rules, 1);
142 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram,
143 1 - tests[i].expect_count_valid_rules,
144 1);
127 EXPECT_EQ(tests[i].expect_match, 145 EXPECT_EQ(tests[i].expect_match,
128 data_use_matcher()->MatchesURL(GURL(tests[i].url), &label)) 146 data_use_matcher()->MatchesURL(GURL(tests[i].url), &label))
129 << i; 147 << i;
148 histogram_tester.ExpectTotalCount(
149 kUMAURLRegexMatchDurationHistogram,
150 tests[i].expect_count_url_match_duration_samples);
130 151
131 // Verify label matches the expected label. 152 // Verify label matches the expected label.
132 std::string expected_label = ""; 153 std::string expected_label = "";
133 if (tests[i].expect_match) 154 if (tests[i].expect_match)
134 expected_label = "label"; 155 expected_label = "label";
135 156
136 EXPECT_EQ(expected_label, label); 157 EXPECT_EQ(expected_label, label);
137 EXPECT_FALSE(data_use_matcher()->MatchesAppPackageName( 158 EXPECT_FALSE(data_use_matcher()->MatchesAppPackageName(
138 "com.example.helloworld", &label)) 159 "com.example.helloworld", &label))
139 << i; 160 << i;
140 // Empty package name should not match against empty package name in the 161 // Empty package name should not match against empty package name in the
141 // matching rule. 162 // matching rule.
142 EXPECT_FALSE( 163 EXPECT_FALSE(
143 data_use_matcher()->MatchesAppPackageName(std::string(), &label)) 164 data_use_matcher()->MatchesAppPackageName(std::string(), &label))
144 << i; 165 << i;
145 } 166 }
146 } 167 }
147 168
148 TEST_F(DataUseMatcherTest, TwoRegex) { 169 TEST_F(DataUseMatcherTest, TwoRegex) {
149 const struct { 170 const struct {
150 std::string url; 171 std::string url;
151 std::string regex1; 172 std::string regex1;
152 std::string regex2; 173 std::string regex2;
153 bool expect_match; 174 bool expect_match;
175 int expect_count_valid_rules;
176 int expect_count_url_match_duration_samples;
154 } tests[] = { 177 } tests[] = {
155 {"http://www.google.com", "http://www.google.com/", 178 {"http://www.google.com", "http://www.google.com/",
156 "https://www.google.com/", true}, 179 "https://www.google.com/", true, 1, 1},
157 {"http://www.googleacom", "http://www.google.com/", 180 {"http://www.googleacom", "http://www.google.com/",
158 "http://www.google.com/", true}, 181 "http://www.google.com/", true, 1, 1},
159 {"https://www.google.com", "http://www.google.com/", 182 {"https://www.google.com", "http://www.google.com/",
160 "https://www.google.com/", true}, 183 "https://www.google.com/", true, 1, 1},
161 {"https://www.googleacom", "http://www.google.com/", 184 {"https://www.googleacom", "http://www.google.com/",
162 "https://www.google.com/", true}, 185 "https://www.google.com/", true, 1, 1},
163 {"http://www.google.com", "{http|https}://www[.]google[.]com/search.*", 186 {"http://www.google.com", "{http|https}://www[.]google[.]com/search.*",
164 "", false}, 187 "", false, 1, 1},
165 {"http://www.google.com/search=test", 188 {"http://www.google.com/search=test",
166 "http://www[.]google[.]com/search.*", 189 "http://www[.]google[.]com/search.*",
167 "https://www[.]google[.]com/search.*", true}, 190 "https://www[.]google[.]com/search.*", true, 1, 1},
168 {"https://www.google.com/search=test", 191 {"https://www.google.com/search=test",
169 "http://www[.]google[.]com/search.*", 192 "http://www[.]google[.]com/search.*",
170 "https://www[.]google[.]com/search.*", true}, 193 "https://www[.]google[.]com/search.*", true, 1, 1},
171 {"http://google.com/search=test", "http://www[.]google[.]com/search.*", 194 {"http://google.com/search=test", "http://www[.]google[.]com/search.*",
172 "https://www[.]google[.]com/search.*", false}, 195 "https://www[.]google[.]com/search.*", false, 1, 1},
173 {"https://www.googleacom/search=test", "", 196 {"https://www.googleacom/search=test", "",
174 "https://www[.]google[.]com/search.*", false}, 197 "https://www[.]google[.]com/search.*", false, 1, 1},
175 {"https://www.google.com/Search=test", "", 198 {"https://www.google.com/Search=test", "",
176 "https://www[.]google[.]com/search.*", true}, 199 "https://www[.]google[.]com/search.*", true, 1, 1},
177 {"www.google.com", "http://www.google.com", "", false}, 200 {"www.google.com", "http://www.google.com", "", false, 1, 0},
178 {"www.google.com:80", "http://www.google.com", "", false}, 201 {"www.google.com:80", "http://www.google.com", "", false, 1, 1},
179 {"http://www.google.com:80", "http://www.google.com", "", false}, 202 {"http://www.google.com:80", "http://www.google.com", "", false, 1, 1},
180 {"", "http://www.google.com", "", false}, 203 {"", "http://www.google.com", "", false, 1, 0},
181 {"https://www.google.com", "http://www.google.com", "", false}, 204 {"https://www.google.com", "http://www.google.com", "", false, 1, 1},
182 }; 205 };
183 206
184 for (size_t i = 0; i < arraysize(tests); ++i) { 207 for (size_t i = 0; i < arraysize(tests); ++i) {
208 base::HistogramTester histogram_tester;
185 std::string got_label(""); 209 std::string got_label("");
186 std::vector<std::string> url_regexes; 210 std::vector<std::string> url_regexes;
187 url_regexes.push_back(tests[i].regex1 + "|" + tests[i].regex2); 211 url_regexes.push_back(tests[i].regex1 + "|" + tests[i].regex2);
188 const std::string label("label"); 212 const std::string label("label");
189 RegisterURLRegexes( 213 RegisterURLRegexes(
190 std::vector<std::string>(url_regexes.size(), "com.example.helloworld"), 214 std::vector<std::string>(url_regexes.size(), "com.example.helloworld"),
191 url_regexes, std::vector<std::string>(url_regexes.size(), label)); 215 url_regexes, std::vector<std::string>(url_regexes.size(), label));
216 histogram_tester.ExpectTotalCount(kUMAMatchingRulesCountValidHistogram, 1);
217 histogram_tester.ExpectTotalCount(kUMAMatchingRulesCountInvalidHistogram,
218 1);
219 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram,
220 tests[i].expect_count_valid_rules, 1);
221 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram,
222 1 - tests[i].expect_count_valid_rules,
223 1);
192 EXPECT_EQ(tests[i].expect_match, 224 EXPECT_EQ(tests[i].expect_match,
193 data_use_matcher()->MatchesURL(GURL(tests[i].url), &got_label)) 225 data_use_matcher()->MatchesURL(GURL(tests[i].url), &got_label))
194 << i; 226 << i;
227 histogram_tester.ExpectTotalCount(
228 kUMAURLRegexMatchDurationHistogram,
229 tests[i].expect_count_url_match_duration_samples);
195 const std::string expected_label = 230 const std::string expected_label =
196 tests[i].expect_match ? label : std::string(); 231 tests[i].expect_match ? label : std::string();
197 EXPECT_EQ(expected_label, got_label); 232 EXPECT_EQ(expected_label, got_label);
198 233
199 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName( 234 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(
200 "com.example.helloworld", &got_label)) 235 "com.example.helloworld", &got_label))
201 << i; 236 << i;
202 EXPECT_EQ(label, got_label); 237 EXPECT_EQ(label, got_label);
203 } 238 }
204 } 239 }
205 240
206 TEST_F(DataUseMatcherTest, MultipleRegex) { 241 TEST_F(DataUseMatcherTest, MultipleRegex) {
242 base::HistogramTester histogram_tester;
207 std::vector<std::string> url_regexes; 243 std::vector<std::string> url_regexes;
208 url_regexes.push_back( 244 url_regexes.push_back(
209 "https?://www[.]google[.]com/#q=.*|https?://www[.]google[.]com[.]ph/" 245 "https?://www[.]google[.]com/#q=.*|https?://www[.]google[.]com[.]ph/"
210 "#q=.*|https?://www[.]google[.]com[.]ph/[?]gws_rd=ssl#q=.*"); 246 "#q=.*|https?://www[.]google[.]com[.]ph/[?]gws_rd=ssl#q=.*");
211 RegisterURLRegexes( 247 RegisterURLRegexes(
212 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, 248 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes,
213 std::vector<std::string>(url_regexes.size(), "label")); 249 std::vector<std::string>(url_regexes.size(), "label"));
250 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, 1,
251 1);
252 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, 0,
253 1);
214 254
215 const struct { 255 const struct {
216 std::string url; 256 std::string url;
217 bool expect_match; 257 bool expect_match;
218 } tests[] = { 258 } tests[] = {
219 {"", false}, 259 {"", false},
220 {"http://www.google.com", false}, 260 {"http://www.google.com", false},
221 {"http://www.googleacom", false}, 261 {"http://www.googleacom", false},
222 {"https://www.google.com", false}, 262 {"https://www.google.com", false},
223 {"https://www.googleacom", false}, 263 {"https://www.googleacom", false},
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, 318 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes,
279 std::vector<std::string>(url_regexes.size(), "label")); 319 std::vector<std::string>(url_regexes.size(), "label"));
280 EXPECT_FALSE(data_use_matcher()->MatchesURL(GURL(""), &label)); 320 EXPECT_FALSE(data_use_matcher()->MatchesURL(GURL(""), &label));
281 EXPECT_FALSE(data_use_matcher()->MatchesURL( 321 EXPECT_FALSE(data_use_matcher()->MatchesURL(
282 GURL("http://www.google.com#q=abc"), &label)); 322 GURL("http://www.google.com#q=abc"), &label));
283 EXPECT_TRUE(data_use_matcher()->MatchesURL( 323 EXPECT_TRUE(data_use_matcher()->MatchesURL(
284 GURL("http://www.google.co.in#q=abc"), &label)); 324 GURL("http://www.google.co.in#q=abc"), &label));
285 } 325 }
286 326
287 TEST_F(DataUseMatcherTest, MultipleAppPackageName) { 327 TEST_F(DataUseMatcherTest, MultipleAppPackageName) {
328 base::HistogramTester histogram_tester;
288 std::vector<std::string> url_regexes; 329 std::vector<std::string> url_regexes;
289 url_regexes.push_back( 330 url_regexes.push_back(
290 "http://www[.]foo[.]com/#q=.*|https://www[.]foo[.]com/#q=.*"); 331 "http://www[.]foo[.]com/#q=.*|https://www[.]foo[.]com/#q=.*");
291 url_regexes.push_back( 332 url_regexes.push_back(
292 "http://www[.]bar[.]com/#q=.*|https://www[.]bar[.]com/#q=.*"); 333 "http://www[.]bar[.]com/#q=.*|https://www[.]bar[.]com/#q=.*");
293 url_regexes.push_back(""); 334 url_regexes.push_back("");
294 335
295 std::vector<std::string> labels; 336 std::vector<std::string> labels;
296 const char kLabelBar[] = "label_bar"; 337 const char kLabelBar[] = "label_bar";
297 const char kLabelBaz[] = "label_baz"; 338 const char kLabelBaz[] = "label_baz";
298 labels.push_back(kLabelFoo); 339 labels.push_back(kLabelFoo);
299 labels.push_back(kLabelBar); 340 labels.push_back(kLabelBar);
300 labels.push_back(kLabelBaz); 341 labels.push_back(kLabelBaz);
301 342
302 std::vector<std::string> app_package_names; 343 std::vector<std::string> app_package_names;
303 const char kAppBar[] = "com.example.bar"; 344 const char kAppBar[] = "com.example.bar";
304 const char kAppBaz[] = "com.example.baz"; 345 const char kAppBaz[] = "com.example.baz";
305 app_package_names.push_back(kAppFoo); 346 app_package_names.push_back(kAppFoo);
306 app_package_names.push_back(kAppBar); 347 app_package_names.push_back(kAppBar);
307 app_package_names.push_back(kAppBaz); 348 app_package_names.push_back(kAppBaz);
308 349
309 RegisterURLRegexes(app_package_names, url_regexes, labels); 350 RegisterURLRegexes(app_package_names, url_regexes, labels);
351 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, 3,
352 1);
353 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, 0,
354 1);
310 355
311 // Test if labels are matched properly for app package names. 356 // Test if labels are matched properly for app package names.
312 std::string got_label; 357 std::string got_label;
313 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label)); 358 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label));
314 EXPECT_EQ(kLabelFoo, got_label); 359 EXPECT_EQ(kLabelFoo, got_label);
315 360
316 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppBar, &got_label)); 361 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppBar, &got_label));
317 EXPECT_EQ(kLabelBar, got_label); 362 EXPECT_EQ(kLabelBar, got_label);
318 363
319 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppBaz, &got_label)); 364 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppBaz, &got_label));
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 << test.app_package_name; 425 << test.app_package_name;
381 DCHECK_EQ(base::TimeTicks::UnixEpoch() + test.expected_expiration_duration, 426 DCHECK_EQ(base::TimeTicks::UnixEpoch() + test.expected_expiration_duration,
382 expiration) 427 expiration)
383 << test.app_package_name; 428 << test.app_package_name;
384 } 429 }
385 } 430 }
386 431
387 // Tests if the expiration time encoded as milliseconds since epoch is parsed 432 // Tests if the expiration time encoded as milliseconds since epoch is parsed
388 // correctly. 433 // correctly.
389 TEST_F(DataUseMatcherTest, EncodeExpirationTimeInPackageName) { 434 TEST_F(DataUseMatcherTest, EncodeExpirationTimeInPackageName) {
435 base::HistogramTester histogram_tester;
390 NowTestTickClock* tick_clock = new NowTestTickClock(); 436 NowTestTickClock* tick_clock = new NowTestTickClock();
391 437
392 // |tick_clock| will be owned by |data_use_matcher_|. 438 // |tick_clock| will be owned by |data_use_matcher_|.
393 data_use_matcher()->tick_clock_.reset(tick_clock); 439 data_use_matcher()->tick_clock_.reset(tick_clock);
394 440
395 std::vector<std::string> url_regexes, labels, app_package_names; 441 std::vector<std::string> url_regexes, labels, app_package_names;
396 url_regexes.push_back(kRegexFoo); 442 url_regexes.push_back(kRegexFoo);
397 labels.push_back(kLabelFoo); 443 labels.push_back(kLabelFoo);
398 444
399 // Set current time to to Epoch. 445 // Set current time to Epoch.
400 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch()); 446 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch());
401 447
402 app_package_names.push_back(base::StringPrintf("%s|%d", kAppFoo, 10000)); 448 app_package_names.push_back(base::StringPrintf("%s|%d", kAppFoo, 10000));
403 RegisterURLRegexes(app_package_names, url_regexes, labels); 449 RegisterURLRegexes(app_package_names, url_regexes, labels);
404 EXPECT_FALSE(IsExpired(0)); 450 EXPECT_FALSE(IsExpired(0));
451 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, 1,
452 1);
453 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, 0,
454 1);
405 // Fast forward 10 seconds, and matching rule expires. 455 // Fast forward 10 seconds, and matching rule expires.
406 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + 456 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() +
407 base::TimeDelta::FromMilliseconds(10000 + 1)); 457 base::TimeDelta::FromMilliseconds(10000 + 1));
408 EXPECT_TRUE(IsExpired(0)); 458 EXPECT_TRUE(IsExpired(0));
409 459
410 // Empty app package name. 460 // Empty app package name.
411 app_package_names.clear(); 461 app_package_names.clear();
412 app_package_names.push_back(base::StringPrintf("|%d", 20000)); 462 app_package_names.push_back(base::StringPrintf("|%d", 20000));
413 RegisterURLRegexes(app_package_names, url_regexes, labels); 463 RegisterURLRegexes(app_package_names, url_regexes, labels);
414 EXPECT_FALSE(IsExpired(0)); 464 EXPECT_FALSE(IsExpired(0));
415 // Fast forward 20 seconds, and matching rule expires. 465 // Fast forward 20 seconds, and matching rule expires.
416 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + 466 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() +
417 base::TimeDelta::FromMilliseconds(20000 + 1)); 467 base::TimeDelta::FromMilliseconds(20000 + 1));
418 EXPECT_TRUE(IsExpired(0)); 468 EXPECT_TRUE(IsExpired(0));
419 } 469 }
420 470
421 // Tests if the expiration time encoded in Java format is parsed correctly. 471 // Tests if the expiration time encoded in Java format is parsed correctly.
422 TEST_F(DataUseMatcherTest, EncodeJavaExpirationTimeInPackageName) { 472 TEST_F(DataUseMatcherTest, EncodeJavaExpirationTimeInPackageName) {
473 base::HistogramTester histogram_tester;
423 std::vector<std::string> url_regexes, labels, app_package_names; 474 std::vector<std::string> url_regexes, labels, app_package_names;
424 url_regexes.push_back(kRegexFoo); 475 url_regexes.push_back(kRegexFoo);
425 labels.push_back(kLabelFoo); 476 labels.push_back(kLabelFoo);
426 477
427 base::TimeTicks start_ticks = base::TimeTicks::Now(); 478 base::TimeTicks start_ticks = base::TimeTicks::Now();
428 base::Time expiration_time = 479 base::Time expiration_time =
429 base::Time::Now() + base::TimeDelta::FromMilliseconds(10000); 480 base::Time::Now() + base::TimeDelta::FromMilliseconds(10000);
430 481
431 app_package_names.push_back(base::StringPrintf( 482 app_package_names.push_back(base::StringPrintf(
432 "%s|%lld", kAppFoo, 483 "%s|%lld", kAppFoo,
433 static_cast<long long int>(expiration_time.ToJavaTime()))); 484 static_cast<long long int>(expiration_time.ToJavaTime())));
434 RegisterURLRegexes(app_package_names, url_regexes, labels); 485 RegisterURLRegexes(app_package_names, url_regexes, labels);
435 EXPECT_FALSE(IsExpired(0)); 486 EXPECT_FALSE(IsExpired(0));
487 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, 1,
488 1);
489 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, 0,
490 1);
436 491
437 // Check if expiration duration is close to 10 seconds. 492 // Check if expiration duration is close to 10 seconds.
438 EXPECT_GE(base::TimeDelta::FromMilliseconds(10001), 493 EXPECT_GE(base::TimeDelta::FromMilliseconds(10001),
439 data_use_matcher()->matching_rules_[0]->expiration() - 494 data_use_matcher()->matching_rules_[0]->expiration() -
440 data_use_matcher()->tick_clock_->NowTicks()); 495 data_use_matcher()->tick_clock_->NowTicks());
441 EXPECT_LE(base::TimeDelta::FromMilliseconds(9999), 496 EXPECT_LE(base::TimeDelta::FromMilliseconds(9999),
442 data_use_matcher()->matching_rules_[0]->expiration() - start_ticks); 497 data_use_matcher()->matching_rules_[0]->expiration() - start_ticks);
443 } 498 }
444 499
445 // Tests that expired matching rules are ignored by MatchesURL and 500 // Tests that expired matching rules are ignored by MatchesURL and
446 // MatchesAppPackageName. 501 // MatchesAppPackageName.
447 TEST_F(DataUseMatcherTest, MatchesIgnoresExpiredRules) { 502 TEST_F(DataUseMatcherTest, MatchesIgnoresExpiredRules) {
503 base::HistogramTester histogram_tester;
448 std::vector<std::string> url_regexes, labels, app_package_names; 504 std::vector<std::string> url_regexes, labels, app_package_names;
449 std::string got_label; 505 std::string got_label;
450 NowTestTickClock* tick_clock = new NowTestTickClock(); 506 NowTestTickClock* tick_clock = new NowTestTickClock();
451 507
452 // |tick_clock| will be owned by |data_use_matcher_|. 508 // |tick_clock| will be owned by |data_use_matcher_|.
453 data_use_matcher()->tick_clock_.reset(tick_clock); 509 data_use_matcher()->tick_clock_.reset(tick_clock);
454 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch()); 510 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch());
455 511
456 url_regexes.push_back(kRegexFoo); 512 url_regexes.push_back(kRegexFoo);
457 labels.push_back(kLabelFoo); 513 labels.push_back(kLabelFoo);
458 app_package_names.push_back(base::StringPrintf("%s|%d", kAppFoo, 10000)); 514 app_package_names.push_back(base::StringPrintf("%s|%d", kAppFoo, 10000));
459 RegisterURLRegexes(app_package_names, url_regexes, labels); 515 RegisterURLRegexes(app_package_names, url_regexes, labels);
516 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountValidHistogram, 1,
517 1);
518 histogram_tester.ExpectUniqueSample(kUMAMatchingRulesCountInvalidHistogram, 0,
519 1);
460 520
461 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + 521 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() +
462 base::TimeDelta::FromMilliseconds(1)); 522 base::TimeDelta::FromMilliseconds(1));
463 523
464 EXPECT_FALSE(IsExpired(0)); 524 EXPECT_FALSE(IsExpired(0));
465 EXPECT_TRUE(data_use_matcher()->MatchesURL(GURL(kRegexFoo), &got_label)); 525 EXPECT_TRUE(data_use_matcher()->MatchesURL(GURL(kRegexFoo), &got_label));
466 EXPECT_EQ(kLabelFoo, got_label); 526 EXPECT_EQ(kLabelFoo, got_label);
467 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label)); 527 EXPECT_TRUE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label));
468 EXPECT_EQ(kLabelFoo, got_label); 528 EXPECT_EQ(kLabelFoo, got_label);
469 529
470 // Advance time to make it expired. 530 // Advance time to make it expired.
471 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() + 531 tick_clock->set_now_ticks(base::TimeTicks::UnixEpoch() +
472 base::TimeDelta::FromMilliseconds(10001)); 532 base::TimeDelta::FromMilliseconds(10001));
473 533
474 EXPECT_TRUE(IsExpired(0)); 534 EXPECT_TRUE(IsExpired(0));
475 EXPECT_FALSE(data_use_matcher()->MatchesURL(GURL(kRegexFoo), &got_label)); 535 EXPECT_FALSE(data_use_matcher()->MatchesURL(GURL(kRegexFoo), &got_label));
476 EXPECT_FALSE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label)); 536 EXPECT_FALSE(data_use_matcher()->MatchesAppPackageName(kAppFoo, &got_label));
477 } 537 }
478 538
479 } // namespace android 539 } // namespace android
480 540
481 } // namespace chrome 541 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/android/data_usage/data_use_matcher.cc ('k') | chrome/browser/android/data_usage/external_data_use_observer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698