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