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/external_data_use_observer.h" | 5 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/test/histogram_tester.h" |
16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
17 #include "chrome/browser/android/data_usage/data_use_tab_model_test_utils.h" | 18 #include "chrome/browser/android/data_usage/data_use_tab_model_test_utils.h" |
18 #include "components/data_usage/core/data_use.h" | 19 #include "components/data_usage/core/data_use.h" |
19 #include "components/data_usage/core/data_use_aggregator.h" | 20 #include "components/data_usage/core/data_use_aggregator.h" |
20 #include "components/data_usage/core/data_use_amortizer.h" | 21 #include "components/data_usage/core/data_use_amortizer.h" |
21 #include "components/data_usage/core/data_use_annotator.h" | 22 #include "components/data_usage/core/data_use_annotator.h" |
| 23 #include "components/sessions/core/session_id.h" |
22 #include "components/variations/variations_associated_data.h" | 24 #include "components/variations/variations_associated_data.h" |
23 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
25 #include "net/base/network_change_notifier.h" | 27 #include "net/base/network_change_notifier.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
27 #include "url/gurl.h" | 29 #include "url/gurl.h" |
28 | 30 |
| 31 namespace { |
| 32 |
| 33 const char kDefaultLabel[] = "label"; |
| 34 const SessionID::id_type kDefaultTabId = 0; |
| 35 const char kDefaultURL[] = "http://www.google.com/#q=abc"; |
| 36 const char kFooMccMnc[] = "foo_mccmnc"; |
| 37 const char kBarMccMnc[] = "bar_mccmnc"; |
| 38 const char kBazMccMnc[] = "baz_mccmnc"; |
| 39 const char kFooLabel[] = "foo_label"; |
| 40 const char kBarLabel[] = "bar_label"; |
| 41 |
| 42 } // namespace |
| 43 |
29 namespace chrome { | 44 namespace chrome { |
30 | 45 |
31 namespace android { | 46 namespace android { |
32 | 47 |
33 class ExternalDataUseObserverTest : public testing::Test { | 48 class ExternalDataUseObserverTest : public testing::Test { |
34 public: | 49 public: |
35 void SetUp() override { | 50 void SetUp() override { |
36 thread_bundle_.reset(new content::TestBrowserThreadBundle( | 51 thread_bundle_.reset(new content::TestBrowserThreadBundle( |
37 content::TestBrowserThreadBundle::IO_MAINLOOP)); | 52 content::TestBrowserThreadBundle::IO_MAINLOOP)); |
38 io_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( | 53 io_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( |
39 content::BrowserThread::IO); | 54 content::BrowserThread::IO); |
40 ui_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( | 55 ui_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( |
41 content::BrowserThread::UI); | 56 content::BrowserThread::UI); |
42 data_use_aggregator_.reset( | 57 data_use_aggregator_.reset( |
43 new data_usage::DataUseAggregator(nullptr, nullptr)); | 58 new data_usage::DataUseAggregator(nullptr, nullptr)); |
44 external_data_use_observer_.reset(new ExternalDataUseObserver( | 59 external_data_use_observer_.reset(new ExternalDataUseObserver( |
45 data_use_aggregator_.get(), io_task_runner_.get(), | 60 data_use_aggregator_.get(), io_task_runner_.get(), |
46 ui_task_runner_.get())); | 61 ui_task_runner_.get())); |
47 // Wait for |external_data_use_observer_| to create the Java object. | 62 // Wait for |external_data_use_observer_| to create the Java object. |
48 base::RunLoop().RunUntilIdle(); | 63 base::RunLoop().RunUntilIdle(); |
49 | 64 |
50 test_data_use_tab_model_ = new TestDataUseTabModel( | 65 test_data_use_tab_model_ = new TestDataUseTabModel( |
51 external_data_use_observer_.get(), ui_task_runner_.get()); | 66 external_data_use_observer_.get(), ui_task_runner_.get()); |
52 external_data_use_observer_->data_use_tab_model_.reset( | 67 external_data_use_observer_->data_use_tab_model_.reset( |
53 test_data_use_tab_model_); | 68 test_data_use_tab_model_); |
54 } | 69 } |
55 | 70 |
56 scoped_ptr<ExternalDataUseObserver> Create() const { | 71 // Replaces |external_data_use_observer_| with a new ExternalDataUseObserver. |
57 scoped_ptr<ExternalDataUseObserver> external_data_use_observer( | 72 void ReplaceExternalDataUseObserver( |
58 new ExternalDataUseObserver(data_use_aggregator_.get(), | 73 std::map<std::string, std::string> variation_params) { |
59 io_task_runner_.get(), | 74 variations::testing::ClearAllVariationParams(); |
60 ui_task_runner_.get())); | 75 ASSERT_TRUE(variations::AssociateVariationParams( |
61 // Wait for |external_data_use_observer| to create the Java object. | 76 ExternalDataUseObserver::kExternalDataUseObserverFieldTrial, "Enabled", |
62 base::RunLoop().RunUntilIdle(); | 77 variation_params)); |
63 return external_data_use_observer; | 78 |
| 79 base::FieldTrialList field_trial_list(nullptr); |
| 80 |
| 81 base::FieldTrialList::CreateFieldTrial( |
| 82 ExternalDataUseObserver::kExternalDataUseObserverFieldTrial, "Enabled"); |
| 83 |
| 84 external_data_use_observer_.reset(new ExternalDataUseObserver( |
| 85 data_use_aggregator_.get(), io_task_runner_.get(), |
| 86 ui_task_runner_.get())); |
| 87 // |test_data_use_tab_model| is owned by |observer|. |
| 88 TestDataUseTabModel* test_data_use_tab_model(new TestDataUseTabModel( |
| 89 external_data_use_observer_.get(), ui_task_runner_.get())); |
| 90 external_data_use_observer_->data_use_tab_model_.reset( |
| 91 test_data_use_tab_model); |
64 } | 92 } |
65 | 93 |
66 void FetchMatchingRulesDone(const std::vector<std::string>& app_package_name, | 94 void FetchMatchingRulesDone(const std::vector<std::string>& app_package_name, |
67 const std::vector<std::string>& domain_path_regex, | 95 const std::vector<std::string>& domain_path_regex, |
68 const std::vector<std::string>& label) { | 96 const std::vector<std::string>& label) { |
69 external_data_use_observer_->FetchMatchingRulesDone( | 97 external_data_use_observer_->FetchMatchingRulesDone( |
70 &app_package_name, &domain_path_regex, &label); | 98 &app_package_name, &domain_path_regex, &label); |
71 } | 99 } |
72 | 100 |
| 101 // Adds a default matching rule to |external_data_use_observer_|. |
| 102 void AddDefaultMatchingRule() { |
| 103 std::vector<std::string> url_regexes; |
| 104 url_regexes.push_back( |
| 105 "http://www[.]google[.]com/#q=.*|https://www[.]google[.]com/#q=.*"); |
| 106 FetchMatchingRulesDone( |
| 107 std::vector<std::string>(url_regexes.size(), std::string()), |
| 108 url_regexes, |
| 109 std::vector<std::string>(url_regexes.size(), kDefaultLabel)); |
| 110 } |
| 111 |
| 112 // Notifies DataUseTabModel that tab tracking has started on kDefaultTabId. |
| 113 void TriggerTabTrackingOnDefaultTab() { |
| 114 external_data_use_observer_->GetDataUseTabModel()->OnNavigationEvent( |
| 115 kDefaultTabId, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, |
| 116 GURL(kDefaultURL), std::string()); |
| 117 } |
| 118 |
| 119 // Returns a default data_usage::DataUse object. |
| 120 data_usage::DataUse default_data_use() { |
| 121 return data_usage::DataUse( |
| 122 GURL(kDefaultURL), base::TimeTicks::Now(), GURL(), kDefaultTabId, |
| 123 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "", |
| 124 default_upload_bytes(), default_download_bytes()); |
| 125 } |
| 126 |
73 ExternalDataUseObserver* external_data_use_observer() const { | 127 ExternalDataUseObserver* external_data_use_observer() const { |
74 return external_data_use_observer_.get(); | 128 return external_data_use_observer_.get(); |
75 } | 129 } |
76 | 130 |
77 TestDataUseTabModel* test_data_use_tab_model() const { | 131 TestDataUseTabModel* test_data_use_tab_model() const { |
78 return test_data_use_tab_model_; | 132 return test_data_use_tab_model_; |
79 } | 133 } |
80 | 134 |
81 const ExternalDataUseObserver::DataUseReports& buffered_data_reports() const { | 135 const ExternalDataUseObserver::DataUseReports& buffered_data_reports() const { |
82 return external_data_use_observer_->buffered_data_reports_; | 136 return external_data_use_observer_->buffered_data_reports_; |
83 } | 137 } |
84 | 138 |
| 139 int64_t default_upload_bytes() const { return 1; } |
| 140 |
| 141 int64_t default_download_bytes() const { |
| 142 return external_data_use_observer_->data_use_report_min_bytes_; |
| 143 } |
| 144 |
85 private: | 145 private: |
86 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; | 146 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; |
87 scoped_ptr<data_usage::DataUseAggregator> data_use_aggregator_; | 147 scoped_ptr<data_usage::DataUseAggregator> data_use_aggregator_; |
88 scoped_ptr<ExternalDataUseObserver> external_data_use_observer_; | 148 scoped_ptr<ExternalDataUseObserver> external_data_use_observer_; |
89 | 149 |
90 // Owned by |external_data_use_observer_|. | 150 // Owned by |external_data_use_observer_|. |
91 TestDataUseTabModel* test_data_use_tab_model_; | 151 TestDataUseTabModel* test_data_use_tab_model_; |
92 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 152 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
93 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 153 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
94 }; | 154 }; |
(...skipping 25 matching lines...) Expand all Loading... |
120 {"", "", false}, | 180 {"", "", false}, |
121 {"https://www.google.com", "http://www.google.com", false}, | 181 {"https://www.google.com", "http://www.google.com", false}, |
122 }; | 182 }; |
123 | 183 |
124 std::string label("test"); | 184 std::string label("test"); |
125 for (size_t i = 0; i < arraysize(tests); ++i) { | 185 for (size_t i = 0; i < arraysize(tests); ++i) { |
126 FetchMatchingRulesDone( | 186 FetchMatchingRulesDone( |
127 // App package name not specified in the matching rule. | 187 // App package name not specified in the matching rule. |
128 std::vector<std::string>(1, std::string()), | 188 std::vector<std::string>(1, std::string()), |
129 std::vector<std::string>(1, tests[i].regex), | 189 std::vector<std::string>(1, tests[i].regex), |
130 std::vector<std::string>(1, "label")); | 190 std::vector<std::string>(1, kDefaultLabel)); |
131 EXPECT_EQ(tests[i].expect_match, | 191 EXPECT_EQ(tests[i].expect_match, |
132 external_data_use_observer()->Matches(GURL(tests[i].url), &label)) | 192 external_data_use_observer()->Matches(GURL(tests[i].url), &label)) |
133 << i; | 193 << i; |
134 | 194 |
135 // Verify label matches the expected label. | 195 // Verify label matches the expected label. |
136 std::string expected_label = ""; | 196 std::string expected_label = ""; |
137 if (tests[i].expect_match) | 197 if (tests[i].expect_match) |
138 expected_label = "label"; | 198 expected_label = kDefaultLabel; |
139 | 199 |
140 EXPECT_EQ(expected_label, label); | 200 EXPECT_EQ(expected_label, label); |
141 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( | 201 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( |
142 "com.example.helloworld", &label)) | 202 "com.example.helloworld", &label)) |
143 << i; | 203 << i; |
144 // Empty package name should not match against empty package name in the | 204 // Empty package name should not match against empty package name in the |
145 // matching rule. | 205 // matching rule. |
146 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( | 206 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( |
147 std::string(), &label)) | 207 std::string(), &label)) |
148 << i; | 208 << i; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 {"www.google.com:80", "http://www.google.com", "", false}, | 242 {"www.google.com:80", "http://www.google.com", "", false}, |
183 {"http://www.google.com:80", "http://www.google.com", "", false}, | 243 {"http://www.google.com:80", "http://www.google.com", "", false}, |
184 {"", "http://www.google.com", "", false}, | 244 {"", "http://www.google.com", "", false}, |
185 {"https://www.google.com", "http://www.google.com", "", false}, | 245 {"https://www.google.com", "http://www.google.com", "", false}, |
186 }; | 246 }; |
187 | 247 |
188 std::string got_label; | 248 std::string got_label; |
189 for (size_t i = 0; i < arraysize(tests); ++i) { | 249 for (size_t i = 0; i < arraysize(tests); ++i) { |
190 std::vector<std::string> url_regexes; | 250 std::vector<std::string> url_regexes; |
191 url_regexes.push_back(tests[i].regex1 + "|" + tests[i].regex2); | 251 url_regexes.push_back(tests[i].regex1 + "|" + tests[i].regex2); |
192 const std::string label("label"); | 252 const std::string label(kDefaultLabel); |
193 FetchMatchingRulesDone( | 253 FetchMatchingRulesDone( |
194 std::vector<std::string>(url_regexes.size(), "com.example.helloworld"), | 254 std::vector<std::string>(url_regexes.size(), "com.example.helloworld"), |
195 url_regexes, std::vector<std::string>(url_regexes.size(), label)); | 255 url_regexes, std::vector<std::string>(url_regexes.size(), label)); |
196 EXPECT_EQ(tests[i].expect_match, external_data_use_observer()->Matches( | 256 EXPECT_EQ(tests[i].expect_match, external_data_use_observer()->Matches( |
197 GURL(tests[i].url), &got_label)) | 257 GURL(tests[i].url), &got_label)) |
198 << i; | 258 << i; |
199 const std::string expected_label = | 259 const std::string expected_label = |
200 tests[i].expect_match ? label : std::string(); | 260 tests[i].expect_match ? label : std::string(); |
201 EXPECT_EQ(expected_label, got_label); | 261 EXPECT_EQ(expected_label, got_label); |
202 | 262 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 // When no regex is specified, the URL match should fail. | 318 // When no regex is specified, the URL match should fail. |
259 EXPECT_FALSE(external_data_use_observer()->Matches(GURL(""), &label)); | 319 EXPECT_FALSE(external_data_use_observer()->Matches(GURL(""), &label)); |
260 EXPECT_FALSE(external_data_use_observer()->Matches( | 320 EXPECT_FALSE(external_data_use_observer()->Matches( |
261 GURL("http://www.google.com"), &label)); | 321 GURL("http://www.google.com"), &label)); |
262 | 322 |
263 std::vector<std::string> url_regexes; | 323 std::vector<std::string> url_regexes; |
264 url_regexes.push_back("http://www[.]google[.]com/#q=.*"); | 324 url_regexes.push_back("http://www[.]google[.]com/#q=.*"); |
265 url_regexes.push_back("https://www[.]google[.]com/#q=.*"); | 325 url_regexes.push_back("https://www[.]google[.]com/#q=.*"); |
266 FetchMatchingRulesDone( | 326 FetchMatchingRulesDone( |
267 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | 327 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, |
268 std::vector<std::string>(url_regexes.size(), "label")); | 328 std::vector<std::string>(url_regexes.size(), kDefaultLabel)); |
269 | 329 |
270 EXPECT_FALSE(external_data_use_observer()->Matches(GURL(""), &label)); | 330 EXPECT_FALSE(external_data_use_observer()->Matches(GURL(""), &label)); |
271 EXPECT_TRUE(external_data_use_observer()->Matches( | 331 EXPECT_TRUE(external_data_use_observer()->Matches( |
272 GURL("http://www.google.com#q=abc"), &label)); | 332 GURL("http://www.google.com#q=abc"), &label)); |
273 EXPECT_FALSE(external_data_use_observer()->Matches( | 333 EXPECT_FALSE(external_data_use_observer()->Matches( |
274 GURL("http://www.google.co.in#q=abc"), &label)); | 334 GURL("http://www.google.co.in#q=abc"), &label)); |
275 | 335 |
276 // Change the regular expressions to verify that the new regexes replace | 336 // Change the regular expressions to verify that the new regexes replace |
277 // the ones specified before. | 337 // the ones specified before. |
278 url_regexes.clear(); | 338 url_regexes.clear(); |
279 url_regexes.push_back("http://www[.]google[.]co[.]in/#q=.*"); | 339 url_regexes.push_back("http://www[.]google[.]co[.]in/#q=.*"); |
280 url_regexes.push_back("https://www[.]google[.]co[.]in/#q=.*"); | 340 url_regexes.push_back("https://www[.]google[.]co[.]in/#q=.*"); |
281 FetchMatchingRulesDone( | 341 FetchMatchingRulesDone( |
282 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | 342 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, |
283 std::vector<std::string>(url_regexes.size(), "label")); | 343 std::vector<std::string>(url_regexes.size(), kDefaultLabel)); |
284 EXPECT_FALSE(external_data_use_observer()->Matches(GURL(""), &label)); | 344 EXPECT_FALSE(external_data_use_observer()->Matches(GURL(""), &label)); |
285 EXPECT_FALSE(external_data_use_observer()->Matches( | 345 EXPECT_FALSE(external_data_use_observer()->Matches( |
286 GURL("http://www.google.com#q=abc"), &label)); | 346 GURL("http://www.google.com#q=abc"), &label)); |
287 EXPECT_TRUE(external_data_use_observer()->Matches( | 347 EXPECT_TRUE(external_data_use_observer()->Matches( |
288 GURL("http://www.google.co.in#q=abc"), &label)); | 348 GURL("http://www.google.co.in#q=abc"), &label)); |
289 } | 349 } |
290 | 350 |
291 // Tests that tab model is notified when tracking labels are removed. | 351 // Tests that tab model is notified when tracking labels are removed. |
292 TEST_F(ExternalDataUseObserverTest, LabelRemoved) { | 352 TEST_F(ExternalDataUseObserverTest, LabelRemoved) { |
293 std::vector<std::string> labels; | 353 std::vector<std::string> labels; |
(...skipping 14 matching lines...) Expand all Loading... |
308 labels.push_back("label_1"); | 368 labels.push_back("label_1"); |
309 labels.push_back("label_4"); | 369 labels.push_back("label_4"); |
310 labels.push_back("label_5"); | 370 labels.push_back("label_5"); |
311 FetchMatchingRulesDone( | 371 FetchMatchingRulesDone( |
312 std::vector<std::string>(labels.size(), std::string()), | 372 std::vector<std::string>(labels.size(), std::string()), |
313 std::vector<std::string>(labels.size(), "http://foobar.com"), labels); | 373 std::vector<std::string>(labels.size(), "http://foobar.com"), labels); |
314 } | 374 } |
315 | 375 |
316 // Verifies that buffer size does not exceed the specified limit. | 376 // Verifies that buffer size does not exceed the specified limit. |
317 TEST_F(ExternalDataUseObserverTest, BufferSize) { | 377 TEST_F(ExternalDataUseObserverTest, BufferSize) { |
318 const char kLabel[] = "label"; | 378 base::HistogramTester histogram_tester; |
319 | 379 |
320 std::vector<std::string> url_regexes; | 380 AddDefaultMatchingRule(); |
321 url_regexes.push_back( | |
322 "http://www[.]google[.]com/#q=.*|https://www[.]google[.]com/#q=.*"); | |
323 | |
324 FetchMatchingRulesDone( | |
325 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | |
326 std::vector<std::string>(url_regexes.size(), kLabel)); | |
327 | |
328 const int64_t bytes_downloaded = | |
329 external_data_use_observer()->data_use_report_min_bytes_; | |
330 const int64_t bytes_uploaded = 1; | |
331 | 381 |
332 // Push more entries than the buffer size. Buffer size should not be exceeded. | 382 // Push more entries than the buffer size. Buffer size should not be exceeded. |
333 for (size_t i = 0; i < ExternalDataUseObserver::kMaxBufferSize * 2; ++i) { | 383 for (size_t i = 0; i < ExternalDataUseObserver::kMaxBufferSize * 2; ++i) { |
334 data_usage::DataUse data_use( | 384 data_usage::DataUse data_use = default_data_use(); |
335 GURL("http://www.google.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | 385 data_use.mcc_mnc = "mccmnc" + base::Int64ToString(i); |
336 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, | |
337 "mccmnc" + base::Int64ToString(i), bytes_uploaded, bytes_downloaded); | |
338 external_data_use_observer()->OnDataUse(data_use); | 386 external_data_use_observer()->OnDataUse(data_use); |
339 } | 387 } |
340 | 388 |
341 EXPECT_LE(0, external_data_use_observer()->total_bytes_buffered_); | 389 EXPECT_LE(0, external_data_use_observer()->total_bytes_buffered_); |
342 | 390 |
343 // Verify that total buffered bytes is computed correctly. | 391 // Verify that total buffered bytes is computed correctly. |
344 EXPECT_EQ(static_cast<int64_t>(ExternalDataUseObserver::kMaxBufferSize * | 392 EXPECT_EQ( |
345 (bytes_downloaded + bytes_uploaded)), | 393 static_cast<int64_t>(ExternalDataUseObserver::kMaxBufferSize * |
346 external_data_use_observer()->total_bytes_buffered_); | 394 (default_upload_bytes() + default_download_bytes())), |
| 395 external_data_use_observer()->total_bytes_buffered_); |
347 EXPECT_EQ(ExternalDataUseObserver::kMaxBufferSize, | 396 EXPECT_EQ(ExternalDataUseObserver::kMaxBufferSize, |
348 buffered_data_reports().size()); | 397 buffered_data_reports().size()); |
349 | 398 |
350 // Verify the label of the data use report. | 399 // Verify the label of the data use report. |
351 for (const auto& it : buffered_data_reports()) | 400 for (const auto& it : buffered_data_reports()) |
352 EXPECT_EQ(kLabel, it.first.label); | 401 EXPECT_EQ(kDefaultLabel, it.first.label); |
| 402 |
| 403 // Verify that metrics were updated correctly for the lost reports. |
| 404 histogram_tester.ExpectUniqueSample( |
| 405 "DataUsage.ReportSubmissionResult", |
| 406 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_LOST, |
| 407 ExternalDataUseObserver::kMaxBufferSize - 1); |
| 408 histogram_tester.ExpectUniqueSample( |
| 409 "DataUsage.ReportSubmission.Lost.Bytes", |
| 410 default_upload_bytes() + default_download_bytes(), |
| 411 ExternalDataUseObserver::kMaxBufferSize - 1); |
353 } | 412 } |
354 | 413 |
355 // Tests that buffered data use reports are merged correctly. | 414 // Tests that buffered data use reports are merged correctly. |
356 TEST_F(ExternalDataUseObserverTest, ReportsMergedCorrectly) { | 415 TEST_F(ExternalDataUseObserverTest, ReportsMergedCorrectly) { |
357 const char kLabel[] = "label"; | 416 AddDefaultMatchingRule(); |
358 | |
359 std::vector<std::string> url_regexes; | |
360 url_regexes.push_back( | |
361 "http://www[.]google[.]com/#q=.*|https://www[.]google[.]com/#q=.*"); | |
362 | |
363 FetchMatchingRulesDone( | |
364 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | |
365 std::vector<std::string>(url_regexes.size(), kLabel)); | |
366 | 417 |
367 const size_t num_iterations = ExternalDataUseObserver::kMaxBufferSize * 2; | 418 const size_t num_iterations = ExternalDataUseObserver::kMaxBufferSize * 2; |
368 | 419 |
369 for (size_t i = 0; i < num_iterations; ++i) { | 420 for (size_t i = 0; i < num_iterations; ++i) { |
370 data_usage::DataUse data_use_foo( | 421 data_usage::DataUse data_use_foo = default_data_use(); |
371 GURL("http://www.google.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | 422 data_use_foo.mcc_mnc = kFooMccMnc; |
372 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_foo", | |
373 external_data_use_observer()->data_use_report_min_bytes_, 1); | |
374 external_data_use_observer()->OnDataUse(data_use_foo); | 423 external_data_use_observer()->OnDataUse(data_use_foo); |
375 | 424 |
376 data_usage::DataUse data_use_bar( | 425 data_usage::DataUse data_use_bar = default_data_use(); |
377 GURL("http://www.google.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | 426 data_use_bar.mcc_mnc = kBarMccMnc; |
378 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_bar", | |
379 external_data_use_observer()->data_use_report_min_bytes_, 1); | |
380 external_data_use_observer()->OnDataUse(data_use_bar); | 427 external_data_use_observer()->OnDataUse(data_use_bar); |
381 | 428 |
382 data_usage::DataUse data_use_baz( | 429 data_usage::DataUse data_use_baz = default_data_use(); |
383 GURL("http://www.google.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | 430 data_use_baz.mcc_mnc = kBazMccMnc; |
384 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_baz", | |
385 external_data_use_observer()->data_use_report_min_bytes_, 1); | |
386 external_data_use_observer()->OnDataUse(data_use_baz); | 431 external_data_use_observer()->OnDataUse(data_use_baz); |
387 } | 432 } |
388 | 433 |
389 EXPECT_EQ(3U, buffered_data_reports().size()); | 434 EXPECT_EQ(3U, buffered_data_reports().size()); |
390 | 435 |
391 // One of the foo reports should have been submitted, and all the other foo | 436 // One of the foo reports should have been submitted, and all the other foo |
392 // reports should have been merged together. All of the bar and baz reports | 437 // reports should have been merged together. All of the bar and baz reports |
393 // should have been merged together respectively. | 438 // should have been merged together respectively. |
394 const struct { | 439 const struct { |
395 std::string mcc_mnc; | 440 std::string mcc_mnc; |
396 size_t number_of_merged_reports; | 441 size_t number_of_merged_reports; |
397 } expected_data_use_reports[] = {{"mccmnc_foo", num_iterations - 1}, | 442 } expected_data_use_reports[] = {{kFooMccMnc, num_iterations - 1}, |
398 {"mccmnc_bar", num_iterations}, | 443 {kBarMccMnc, num_iterations}, |
399 {"mccmnc_baz", num_iterations}}; | 444 {kBazMccMnc, num_iterations}}; |
400 | 445 |
401 for (const auto& expected_report : expected_data_use_reports) { | 446 for (const auto& expected_report : expected_data_use_reports) { |
402 const ExternalDataUseObserver::DataUseReportKey key( | 447 const ExternalDataUseObserver::DataUseReportKey key( |
403 kLabel, net::NetworkChangeNotifier::CONNECTION_UNKNOWN, | 448 kDefaultLabel, net::NetworkChangeNotifier::CONNECTION_UNKNOWN, |
404 expected_report.mcc_mnc); | 449 expected_report.mcc_mnc); |
405 | 450 |
406 EXPECT_NE(buffered_data_reports().end(), buffered_data_reports().find(key)); | 451 EXPECT_NE(buffered_data_reports().end(), buffered_data_reports().find(key)); |
407 EXPECT_EQ(static_cast<int64_t>(expected_report.number_of_merged_reports), | 452 EXPECT_EQ(static_cast<int64_t>(expected_report.number_of_merged_reports) * |
| 453 (default_download_bytes()), |
408 buffered_data_reports().find(key)->second.bytes_downloaded); | 454 buffered_data_reports().find(key)->second.bytes_downloaded); |
409 EXPECT_EQ(static_cast<int64_t>( | 455 EXPECT_EQ(static_cast<int64_t>(expected_report.number_of_merged_reports * |
410 expected_report.number_of_merged_reports * | 456 (default_upload_bytes())), |
411 external_data_use_observer()->data_use_report_min_bytes_), | |
412 buffered_data_reports().find(key)->second.bytes_uploaded); | 457 buffered_data_reports().find(key)->second.bytes_uploaded); |
413 } | 458 } |
414 } | 459 } |
415 | 460 |
416 // Tests that timestamps of merged reports is correct. | 461 // Tests that timestamps of merged reports is correct. |
417 TEST_F(ExternalDataUseObserverTest, TimestampsMergedCorrectly) { | 462 TEST_F(ExternalDataUseObserverTest, TimestampsMergedCorrectly) { |
418 const char kLabel[] = "label"; | 463 AddDefaultMatchingRule(); |
419 | |
420 std::vector<std::string> url_regexes; | |
421 url_regexes.push_back( | |
422 "http://www[.]google[.]com/#q=.*|https://www[.]google[.]com/#q=.*"); | |
423 | |
424 FetchMatchingRulesDone( | |
425 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | |
426 std::vector<std::string>(url_regexes.size(), kLabel)); | |
427 | 464 |
428 const size_t num_iterations = ExternalDataUseObserver::kMaxBufferSize * 2; | 465 const size_t num_iterations = ExternalDataUseObserver::kMaxBufferSize * 2; |
429 | 466 |
430 base::Time start_timestamp = base::Time::UnixEpoch(); | 467 base::Time start_timestamp = base::Time::UnixEpoch(); |
431 base::Time end_timestamp = start_timestamp + base::TimeDelta::FromSeconds(1); | 468 base::Time end_timestamp = start_timestamp + base::TimeDelta::FromSeconds(1); |
432 for (size_t i = 0; i < num_iterations; ++i) { | 469 for (size_t i = 0; i < num_iterations; ++i) { |
433 data_usage::DataUse data_use_foo( | |
434 GURL("http://www.google.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | |
435 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_foo", 1, 2); | |
436 external_data_use_observer()->BufferDataUseReport( | 470 external_data_use_observer()->BufferDataUseReport( |
437 data_use_foo, kLabel, start_timestamp, end_timestamp); | 471 default_data_use(), kDefaultLabel, start_timestamp, end_timestamp); |
438 | 472 |
439 start_timestamp += base::TimeDelta::FromSeconds(1); | 473 start_timestamp += base::TimeDelta::FromSeconds(1); |
440 end_timestamp += base::TimeDelta::FromSeconds(1); | 474 end_timestamp += base::TimeDelta::FromSeconds(1); |
441 } | 475 } |
442 | 476 |
443 EXPECT_EQ(1U, buffered_data_reports().size()); | 477 EXPECT_EQ(1U, buffered_data_reports().size()); |
444 EXPECT_EQ(0, buffered_data_reports().begin()->second.start_time.ToJavaTime()); | 478 EXPECT_EQ(0, buffered_data_reports().begin()->second.start_time.ToJavaTime()); |
445 // Convert from seconds to milliseconds. | 479 // Convert from seconds to milliseconds. |
446 EXPECT_EQ(static_cast<int64_t>(num_iterations * 1000), | 480 EXPECT_EQ(static_cast<int64_t>(num_iterations * 1000), |
447 buffered_data_reports().begin()->second.end_time.ToJavaTime()); | 481 buffered_data_reports().begin()->second.end_time.ToJavaTime()); |
448 } | 482 } |
449 | 483 |
450 // Tests the behavior when multiple matching rules are available for URL and | 484 // Tests the behavior when multiple matching rules are available for URL and |
451 // package name matching. | 485 // package name matching. |
452 TEST_F(ExternalDataUseObserverTest, MultipleMatchingRules) { | 486 TEST_F(ExternalDataUseObserverTest, MultipleMatchingRules) { |
453 std::vector<std::string> url_regexes; | 487 std::vector<std::string> url_regexes; |
454 url_regexes.push_back( | 488 url_regexes.push_back( |
455 "http://www[.]foo[.]com/#q=.*|https://www[.]foo[.]com/#q=.*"); | 489 "http://www[.]foo[.]com/#q=.*|https://www[.]foo[.]com/#q=.*"); |
456 url_regexes.push_back( | 490 url_regexes.push_back( |
457 "http://www[.]bar[.]com/#q=.*|https://www[.]bar[.]com/#q=.*"); | 491 "http://www[.]bar[.]com/#q=.*|https://www[.]bar[.]com/#q=.*"); |
458 | 492 |
459 std::vector<std::string> labels; | 493 std::vector<std::string> labels; |
460 const char kLabelFoo[] = "label_foo"; | 494 labels.push_back(kFooLabel); |
461 const char kLabelBar[] = "label_bar"; | 495 labels.push_back(kBarLabel); |
462 labels.push_back(kLabelFoo); | |
463 labels.push_back(kLabelBar); | |
464 | 496 |
465 std::vector<std::string> app_package_names; | 497 std::vector<std::string> app_package_names; |
466 const char kAppFoo[] = "com.example.foo"; | 498 const char kAppFoo[] = "com.example.foo"; |
467 const char kAppBar[] = "com.example.bar"; | 499 const char kAppBar[] = "com.example.bar"; |
468 app_package_names.push_back(kAppFoo); | 500 app_package_names.push_back(kAppFoo); |
469 app_package_names.push_back(kAppBar); | 501 app_package_names.push_back(kAppBar); |
470 | 502 |
471 FetchMatchingRulesDone(app_package_names, url_regexes, labels); | 503 FetchMatchingRulesDone(app_package_names, url_regexes, labels); |
472 EXPECT_EQ(0U, external_data_use_observer()->buffered_data_reports_.size()); | 504 EXPECT_EQ(0U, external_data_use_observer()->buffered_data_reports_.size()); |
473 EXPECT_FALSE(external_data_use_observer()->submit_data_report_pending_); | 505 EXPECT_TRUE(external_data_use_observer() |
| 506 ->last_data_report_submitted_ticks_.is_null()); |
474 EXPECT_FALSE(external_data_use_observer()->matching_rules_fetch_pending_); | 507 EXPECT_FALSE(external_data_use_observer()->matching_rules_fetch_pending_); |
475 | 508 |
476 // Check |kLabelFoo| matching rule. | 509 // Check |kLabelFoo| matching rule. |
477 data_usage::DataUse data_foo_1( | 510 data_usage::DataUse data_foo = default_data_use(); |
478 GURL("http://www.foo.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | 511 data_foo.url = GURL("http://www.foo.com/#q=abc"); |
479 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_1", | 512 data_foo.mcc_mnc = kFooMccMnc; |
480 external_data_use_observer()->data_use_report_min_bytes_, 0); | 513 external_data_use_observer()->OnDataUse(data_foo); |
481 external_data_use_observer()->OnDataUse(data_foo_1); | |
482 data_usage::DataUse data_foo_2( | |
483 GURL("http://www.foo.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | |
484 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_2", | |
485 external_data_use_observer()->data_use_report_min_bytes_, 0); | |
486 external_data_use_observer()->OnDataUse(data_foo_2); | |
487 | |
488 // The foo1 report should have been submitted. | |
489 EXPECT_TRUE(external_data_use_observer()->submit_data_report_pending_); | |
490 | |
491 // Only the foo2 report should be present. | |
492 EXPECT_EQ(1U, buffered_data_reports().size()); | |
493 const ExternalDataUseObserver::DataUseReportKey key_foo_2( | |
494 kLabelFoo, net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_2"); | |
495 EXPECT_NE(buffered_data_reports().end(), | |
496 buffered_data_reports().find(key_foo_2)); | |
497 | 514 |
498 // Check |kLabelBar| matching rule. | 515 // Check |kLabelBar| matching rule. |
499 data_usage::DataUse data_bar( | 516 data_usage::DataUse data_bar = default_data_use(); |
500 GURL("http://www.bar.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | 517 data_bar.url = GURL("http://www.bar.com/#q=abc"); |
501 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc", 0, 0); | 518 data_bar.mcc_mnc = kBarMccMnc; |
502 external_data_use_observer()->OnDataUse(data_bar); | 519 external_data_use_observer()->OnDataUse(data_bar); |
503 | 520 |
504 // Both the foo2 and bar reports should be present. | 521 // bar report should be present. |
505 EXPECT_EQ(2U, buffered_data_reports().size()); | 522 EXPECT_EQ(1U, buffered_data_reports().size()); |
506 EXPECT_NE(buffered_data_reports().end(), | |
507 buffered_data_reports().find(key_foo_2)); | |
508 | 523 |
509 const ExternalDataUseObserver::DataUseReportKey key_bar( | 524 const ExternalDataUseObserver::DataUseReportKey key_bar( |
510 kLabelBar, net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc"); | 525 kBarLabel, net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kBarMccMnc); |
511 EXPECT_NE(buffered_data_reports().end(), | 526 EXPECT_NE(buffered_data_reports().end(), |
512 buffered_data_reports().find(key_bar)); | 527 buffered_data_reports().find(key_bar)); |
513 | 528 |
514 // Test if labels are matched properly for app package names. | 529 // Test if labels are matched properly for app package names. |
515 std::string got_label; | 530 std::string got_label; |
516 EXPECT_TRUE( | 531 EXPECT_TRUE( |
517 external_data_use_observer()->MatchesAppPackageName(kAppFoo, &got_label)); | 532 external_data_use_observer()->MatchesAppPackageName(kAppFoo, &got_label)); |
518 EXPECT_EQ(kLabelFoo, got_label); | 533 EXPECT_EQ(kFooLabel, got_label); |
519 | 534 |
520 got_label = ""; | 535 got_label = ""; |
521 EXPECT_TRUE( | 536 EXPECT_TRUE( |
522 external_data_use_observer()->MatchesAppPackageName(kAppBar, &got_label)); | 537 external_data_use_observer()->MatchesAppPackageName(kAppBar, &got_label)); |
523 EXPECT_EQ(kLabelBar, got_label); | 538 EXPECT_EQ(kBarLabel, got_label); |
524 | 539 |
525 got_label = ""; | 540 got_label = ""; |
526 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( | 541 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( |
527 "com.example.unmatched", &got_label)); | 542 "com.example.unmatched", &got_label)); |
528 EXPECT_EQ(std::string(), got_label); | 543 EXPECT_EQ(std::string(), got_label); |
529 | 544 |
530 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( | 545 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( |
531 std::string(), &got_label)); | 546 std::string(), &got_label)); |
532 EXPECT_EQ(std::string(), got_label); | 547 EXPECT_EQ(std::string(), got_label); |
533 } | 548 } |
534 | 549 |
535 // Tests that hash function reports distinct values. This test may fail if there | 550 // Tests that hash function reports distinct values. This test may fail if there |
536 // is a hash collision, however the chances of that happening are very low. | 551 // is a hash collision, however the chances of that happening are very low. |
537 TEST_F(ExternalDataUseObserverTest, HashFunction) { | 552 TEST_F(ExternalDataUseObserverTest, HashFunction) { |
538 ExternalDataUseObserver::DataUseReportKeyHash hash; | 553 ExternalDataUseObserver::DataUseReportKeyHash hash; |
539 | 554 |
540 ExternalDataUseObserver::DataUseReportKey foo( | 555 ExternalDataUseObserver::DataUseReportKey foo( |
541 "foo_label", net::NetworkChangeNotifier::CONNECTION_UNKNOWN, | 556 kFooLabel, net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kFooMccMnc); |
542 "foo_mcc_mnc"); | |
543 ExternalDataUseObserver::DataUseReportKey bar_label( | 557 ExternalDataUseObserver::DataUseReportKey bar_label( |
544 "bar_label", net::NetworkChangeNotifier::CONNECTION_UNKNOWN, | 558 kBarLabel, net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kFooMccMnc); |
545 "foo_mcc_mnc"); | |
546 ExternalDataUseObserver::DataUseReportKey bar_network_type( | 559 ExternalDataUseObserver::DataUseReportKey bar_network_type( |
547 "foo_label", net::NetworkChangeNotifier::CONNECTION_WIFI, "foo_mcc_mnc"); | 560 kFooLabel, net::NetworkChangeNotifier::CONNECTION_WIFI, kFooMccMnc); |
548 ExternalDataUseObserver::DataUseReportKey bar_mcc_mnc( | 561 ExternalDataUseObserver::DataUseReportKey bar_mcc_mnc( |
549 "foo_label", net::NetworkChangeNotifier::CONNECTION_UNKNOWN, | 562 kFooLabel, net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kBarMccMnc); |
550 "bar_mcc_mnc"); | |
551 | 563 |
552 EXPECT_NE(hash(foo), hash(bar_label)); | 564 EXPECT_NE(hash(foo), hash(bar_label)); |
553 EXPECT_NE(hash(foo), hash(bar_label)); | 565 EXPECT_NE(hash(foo), hash(bar_label)); |
554 EXPECT_NE(hash(foo), hash(bar_network_type)); | 566 EXPECT_NE(hash(foo), hash(bar_network_type)); |
555 EXPECT_NE(hash(foo), hash(bar_mcc_mnc)); | 567 EXPECT_NE(hash(foo), hash(bar_mcc_mnc)); |
556 } | 568 } |
557 | 569 |
558 // Tests if matching rules are fetched periodically. | 570 // Tests if matching rules are fetched periodically. |
559 TEST_F(ExternalDataUseObserverTest, PeriodicFetchMatchingRules) { | 571 TEST_F(ExternalDataUseObserverTest, PeriodicFetchMatchingRules) { |
560 const char kLabel[] = "label"; | 572 AddDefaultMatchingRule(); |
561 | |
562 std::vector<std::string> url_regexes; | |
563 url_regexes.push_back( | |
564 "http://www[.]google[.]com/#q=.*|https://www[.]google[.]com/#q=.*"); | |
565 | |
566 FetchMatchingRulesDone( | |
567 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | |
568 std::vector<std::string>(url_regexes.size(), kLabel)); | |
569 | 573 |
570 EXPECT_FALSE(external_data_use_observer()->matching_rules_fetch_pending_); | 574 EXPECT_FALSE(external_data_use_observer()->matching_rules_fetch_pending_); |
571 EXPECT_FALSE( | 575 EXPECT_FALSE( |
572 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); | 576 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); |
573 | 577 |
574 // Change the time when the fetching rules were fetched. | 578 // Change the time when the fetching rules were fetched. |
575 external_data_use_observer()->last_matching_rules_fetch_time_ = | 579 external_data_use_observer()->last_matching_rules_fetch_time_ = |
576 base::TimeTicks::Now() - | 580 base::TimeTicks::Now() - |
577 external_data_use_observer()->fetch_matching_rules_duration_; | 581 external_data_use_observer()->fetch_matching_rules_duration_; |
578 // Matching rules should be expired. | 582 // Matching rules should be expired. |
579 EXPECT_GE(base::TimeTicks::Now() - | 583 EXPECT_GE(base::TimeTicks::Now() - |
580 external_data_use_observer()->last_matching_rules_fetch_time_, | 584 external_data_use_observer()->last_matching_rules_fetch_time_, |
581 external_data_use_observer()->fetch_matching_rules_duration_); | 585 external_data_use_observer()->fetch_matching_rules_duration_); |
582 | 586 |
583 // OnDataUse should trigger fetching of matching rules. | 587 // OnDataUse should trigger fetching of matching rules. |
584 data_usage::DataUse data_use_foo( | 588 external_data_use_observer()->OnDataUse(default_data_use()); |
585 GURL("http://www.google.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | |
586 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_foo", | |
587 external_data_use_observer()->data_use_report_min_bytes_, 1); | |
588 external_data_use_observer()->OnDataUse(data_use_foo); | |
589 | 589 |
590 // Matching rules should not be expired. | 590 // Matching rules should not be expired. |
591 EXPECT_LT(base::TimeTicks::Now() - | 591 EXPECT_LT(base::TimeTicks::Now() - |
592 external_data_use_observer()->last_matching_rules_fetch_time_, | 592 external_data_use_observer()->last_matching_rules_fetch_time_, |
593 external_data_use_observer()->fetch_matching_rules_duration_); | 593 external_data_use_observer()->fetch_matching_rules_duration_); |
594 } | 594 } |
595 | 595 |
596 // Tests if data use reports are sent only after the total bytes send/received | 596 // Tests if data use reports are sent only after the total bytes send/received |
597 // across all buffered reports have reached the specified threshold. | 597 // across all buffered reports have reached the specified threshold. |
598 TEST_F(ExternalDataUseObserverTest, BufferDataUseReports) { | 598 TEST_F(ExternalDataUseObserverTest, BufferDataUseReports) { |
599 const char kLabel[] = "label"; | 599 AddDefaultMatchingRule(); |
600 | 600 TriggerTabTrackingOnDefaultTab(); |
601 std::vector<std::string> url_regexes; | |
602 url_regexes.push_back( | |
603 "http://www[.]google[.]com/#q=.*|https://www[.]google[.]com/#q=.*"); | |
604 | |
605 FetchMatchingRulesDone( | |
606 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | |
607 std::vector<std::string>(url_regexes.size(), kLabel)); | |
608 | 601 |
609 // This tests reports 1024 bytes in each loop iteration. For the test to work | 602 // This tests reports 1024 bytes in each loop iteration. For the test to work |
610 // properly, |data_use_report_min_bytes_| should be a multiple of 1024. | 603 // properly, |data_use_report_min_bytes_| should be a multiple of 1024. |
611 ASSERT_EQ(0, external_data_use_observer()->data_use_report_min_bytes_ % 1024); | 604 ASSERT_EQ(0, external_data_use_observer()->data_use_report_min_bytes_ % 1024); |
612 | 605 |
613 const size_t num_iterations = | 606 const size_t num_iterations = |
614 external_data_use_observer()->data_use_report_min_bytes_ / 1024; | 607 external_data_use_observer()->data_use_report_min_bytes_ / 1024; |
615 | 608 |
616 for (size_t i = 0; i < num_iterations; ++i) { | 609 for (size_t i = 0; i < num_iterations; ++i) { |
617 data_usage::DataUse data_use_foo( | 610 data_usage::DataUse data_use = default_data_use(); |
618 GURL("http://www.google.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | 611 data_use.tx_bytes = 1024; |
619 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_foo", 1024, 0); | 612 data_use.rx_bytes = 0; |
620 external_data_use_observer()->OnDataUse(data_use_foo); | 613 external_data_use_observer()->OnDataUse(data_use); |
621 | 614 |
622 if (i != num_iterations - 1) { | 615 if (i != num_iterations - 1) { |
623 // Total buffered bytes is less than the minimum threshold. Data use | 616 // Total buffered bytes is less than the minimum threshold. Data use |
624 // report should not be send. | 617 // report should not be send. |
625 EXPECT_FALSE(external_data_use_observer()->submit_data_report_pending_); | 618 EXPECT_TRUE(external_data_use_observer() |
| 619 ->last_data_report_submitted_ticks_.is_null()); |
626 EXPECT_EQ(static_cast<int64_t>(i + 1), | 620 EXPECT_EQ(static_cast<int64_t>(i + 1), |
627 external_data_use_observer()->total_bytes_buffered_ / 1024); | 621 external_data_use_observer()->total_bytes_buffered_ / 1024); |
| 622 EXPECT_EQ(0, external_data_use_observer()->pending_report_bytes_); |
628 | 623 |
629 } else { | 624 } else { |
630 // Total bytes is at least the minimum threshold. This should trigger | 625 // Total bytes is at least the minimum threshold. This should trigger |
631 // submitting of the buffered data use report. | 626 // submitting of the buffered data use report. |
632 EXPECT_TRUE(external_data_use_observer()->submit_data_report_pending_); | 627 EXPECT_FALSE(external_data_use_observer() |
| 628 ->last_data_report_submitted_ticks_.is_null()); |
633 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); | 629 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); |
634 } | 630 } |
635 } | 631 } |
636 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); | 632 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); |
| 633 EXPECT_EQ(static_cast<int64_t>(num_iterations), |
| 634 external_data_use_observer()->pending_report_bytes_ / 1024); |
| 635 |
| 636 base::HistogramTester histogram_tester; |
| 637 external_data_use_observer()->OnReportDataUseDone(true); |
| 638 |
| 639 // Verify that metrics were updated correctly for the report that was |
| 640 // successfully submitted. |
| 641 histogram_tester.ExpectUniqueSample( |
| 642 "DataUsage.ReportSubmissionResult", |
| 643 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL, 1); |
| 644 histogram_tester.ExpectUniqueSample( |
| 645 "DataUsage.ReportSubmission.Successful.Bytes", |
| 646 external_data_use_observer()->data_use_report_min_bytes_, 1); |
| 647 |
| 648 // Verify that metrics were updated correctly for the report that was not |
| 649 // successfully submitted. |
| 650 external_data_use_observer()->OnDataUse(default_data_use()); |
| 651 external_data_use_observer()->OnReportDataUseDone(false); |
| 652 histogram_tester.ExpectTotalCount("DataUsage.ReportSubmissionResult", 2); |
| 653 histogram_tester.ExpectBucketCount( |
| 654 "DataUsage.ReportSubmissionResult", |
| 655 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_FAILED, 1); |
| 656 histogram_tester.ExpectUniqueSample( |
| 657 "DataUsage.ReportSubmission.Failed.Bytes", |
| 658 external_data_use_observer()->data_use_report_min_bytes_, 1); |
637 } | 659 } |
638 | 660 |
639 // Tests if the parameters from the field trial are populated correctly. | 661 // Tests if the parameters from the field trial are populated correctly. |
640 TEST_F(ExternalDataUseObserverTest, Variations) { | 662 TEST_F(ExternalDataUseObserverTest, Variations) { |
641 EXPECT_EQ(base::TimeDelta::FromSeconds(60 * 15), | |
642 external_data_use_observer()->fetch_matching_rules_duration_); | |
643 EXPECT_EQ(100 * 1024, | |
644 external_data_use_observer()->data_use_report_min_bytes_); | |
645 | |
646 variations::testing::ClearAllVariationParams(); | |
647 std::map<std::string, std::string> variation_params; | 663 std::map<std::string, std::string> variation_params; |
648 | 664 |
649 const int kFetchMatchingRulesDurationSeconds = 10000; | 665 const int kFetchMatchingRulesDurationSeconds = 10000; |
| 666 const int kDefaultMaxDataReportSubmitWaitMsec = 20000; |
650 const int64_t kDataUseReportMinBytes = 5000; | 667 const int64_t kDataUseReportMinBytes = 5000; |
651 variation_params["fetch_matching_rules_duration_seconds"] = | 668 variation_params["fetch_matching_rules_duration_seconds"] = |
652 base::Int64ToString(kFetchMatchingRulesDurationSeconds); | 669 base::Int64ToString(kFetchMatchingRulesDurationSeconds); |
| 670 variation_params["data_report_submit_timeout_msec"] = |
| 671 base::Int64ToString(kDefaultMaxDataReportSubmitWaitMsec); |
653 variation_params["data_use_report_min_bytes"] = | 672 variation_params["data_use_report_min_bytes"] = |
654 base::Int64ToString(kDataUseReportMinBytes); | 673 base::Int64ToString(kDataUseReportMinBytes); |
655 | 674 |
656 ASSERT_TRUE(variations::AssociateVariationParams( | 675 // Create another ExternalDataUseObserver object. |
657 ExternalDataUseObserver::kExternalDataUseObserverFieldTrial, "Enabled", | 676 ReplaceExternalDataUseObserver(variation_params); |
658 variation_params)); | 677 EXPECT_EQ(base::TimeDelta::FromSeconds(kFetchMatchingRulesDurationSeconds), |
| 678 external_data_use_observer()->fetch_matching_rules_duration_); |
| 679 EXPECT_EQ( |
| 680 base::TimeDelta::FromMilliseconds(kDefaultMaxDataReportSubmitWaitMsec), |
| 681 external_data_use_observer()->data_report_submit_timeout_); |
| 682 EXPECT_EQ(kDataUseReportMinBytes, |
| 683 external_data_use_observer()->data_use_report_min_bytes_); |
| 684 } |
659 | 685 |
660 base::FieldTrialList field_trial_list(nullptr); | 686 // Tests if the metrics are recorded correctly. |
| 687 TEST_F(ExternalDataUseObserverTest, DataUseReportTimedOut) { |
| 688 std::map<std::string, std::string> variation_params; |
| 689 variation_params["data_report_submit_timeout_msec"] = "0"; |
| 690 variation_params["data_use_report_min_bytes"] = "0"; |
661 | 691 |
662 base::FieldTrialList::CreateFieldTrial( | 692 // Create another ExternalDataUseObserver object. |
663 ExternalDataUseObserver::kExternalDataUseObserverFieldTrial, "Enabled"); | 693 ReplaceExternalDataUseObserver(variation_params); |
| 694 AddDefaultMatchingRule(); |
664 | 695 |
665 // Create another ExternalDataUseObserver object. This would fetch variation | 696 base::HistogramTester histogram_tester; |
666 // params. | 697 TriggerTabTrackingOnDefaultTab(); |
667 scoped_ptr<ExternalDataUseObserver> | 698 external_data_use_observer()->OnDataUse(default_data_use()); |
668 external_data_use_obsever_with_variations = Create(); | 699 external_data_use_observer()->OnDataUse(default_data_use()); |
669 EXPECT_EQ(base::TimeDelta::FromSeconds(kFetchMatchingRulesDurationSeconds), | 700 // First data use report should be marked as timed out. |
670 external_data_use_obsever_with_variations | 701 histogram_tester.ExpectUniqueSample( |
671 ->fetch_matching_rules_duration_); | 702 "DataUsage.ReportSubmissionResult", |
672 EXPECT_EQ( | 703 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); |
673 kDataUseReportMinBytes, | 704 histogram_tester.ExpectUniqueSample( |
674 external_data_use_obsever_with_variations->data_use_report_min_bytes_); | 705 "DataUsage.ReportSubmission.TimedOut.Bytes", |
| 706 default_upload_bytes() + default_download_bytes(), 1); |
675 } | 707 } |
676 | 708 |
677 } // namespace android | 709 } // namespace android |
678 | 710 |
679 } // namespace chrome | 711 } // namespace chrome |
OLD | NEW |