| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_reporter.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/metrics/field_trial.h" | 15 #include "base/metrics/field_trial.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/test/histogram_tester.h" | 19 #include "base/test/histogram_tester.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "chrome/browser/android/data_usage/data_use_tab_model.h" | 21 #include "chrome/browser/android/data_usage/data_use_tab_model.h" |
| 22 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
| 22 #include "components/data_usage/core/data_use.h" | 23 #include "components/data_usage/core/data_use.h" |
| 23 #include "components/data_usage/core/data_use_aggregator.h" | 24 #include "components/data_usage/core/data_use_aggregator.h" |
| 24 #include "components/data_usage/core/data_use_amortizer.h" | |
| 25 #include "components/data_usage/core/data_use_annotator.h" | |
| 26 #include "components/sessions/core/session_id.h" | 25 #include "components/sessions/core/session_id.h" |
| 27 #include "components/variations/variations_associated_data.h" | 26 #include "components/variations/variations_associated_data.h" |
| 28 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/test/test_browser_thread_bundle.h" | 28 #include "content/public/test/test_browser_thread_bundle.h" |
| 30 #include "net/base/network_change_notifier.h" | 29 #include "net/base/network_change_notifier.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 33 | 32 |
| 34 namespace { | 33 namespace { |
| 35 | 34 |
| 36 const char kUMAMatchingRuleFirstFetchDurationHistogram[] = | 35 const char kUMAMatchingRuleFirstFetchDurationHistogram[] = |
| 37 "DataUsage.Perf.MatchingRuleFirstFetchDuration"; | 36 "DataUsage.Perf.MatchingRuleFirstFetchDuration"; |
| 38 const char kUMAReportSubmissionDurationHistogram[] = | 37 const char kUMAReportSubmissionDurationHistogram[] = |
| 39 "DataUsage.Perf.ReportSubmissionDuration"; | 38 "DataUsage.Perf.ReportSubmissionDuration"; |
| 40 | 39 |
| 41 const char kDefaultLabel[] = "label"; | 40 const char kDefaultLabel[] = "label"; |
| 42 const SessionID::id_type kDefaultTabId = 0; | 41 const SessionID::id_type kDefaultTabId = 0; |
| 43 const char kDefaultURL[] = "http://www.google.com/#q=abc"; | 42 const char kDefaultURL[] = "http://www.google.com/#q=abc"; |
| 43 |
| 44 const char kFooMccMnc[] = "foo_mccmnc"; | 44 const char kFooMccMnc[] = "foo_mccmnc"; |
| 45 const char kBarMccMnc[] = "bar_mccmnc"; | 45 const char kBarMccMnc[] = "bar_mccmnc"; |
| 46 const char kBazMccMnc[] = "baz_mccmnc"; | 46 const char kBazMccMnc[] = "baz_mccmnc"; |
| 47 const char kFooLabel[] = "foo_label"; | 47 const char kFooLabel[] = "foo_label"; |
| 48 const char kBarLabel[] = "bar_label"; | 48 const char kBarLabel[] = "bar_label"; |
| 49 | 49 |
| 50 } // namespace | 50 } // namespace |
| 51 | 51 |
| 52 namespace chrome { | 52 namespace chrome { |
| 53 | 53 |
| 54 namespace android { | 54 namespace android { |
| 55 | 55 |
| 56 class ExternalDataUseObserverTest : public testing::Test { | 56 class ExternalDataUseReporterTest : public testing::Test { |
| 57 public: | 57 public: |
| 58 void SetUp() override { | 58 void SetUp() override { |
| 59 thread_bundle_.reset(new content::TestBrowserThreadBundle( | 59 thread_bundle_.reset(new content::TestBrowserThreadBundle( |
| 60 content::TestBrowserThreadBundle::IO_MAINLOOP)); | 60 content::TestBrowserThreadBundle::IO_MAINLOOP)); |
| 61 io_task_runner_ = content::BrowserThread::GetTaskRunnerForThread( | 61 io_task_runner_ = content::BrowserThread::GetTaskRunnerForThread( |
| 62 content::BrowserThread::IO); | 62 content::BrowserThread::IO); |
| 63 ui_task_runner_ = content::BrowserThread::GetTaskRunnerForThread( | 63 ui_task_runner_ = content::BrowserThread::GetTaskRunnerForThread( |
| 64 content::BrowserThread::UI); | 64 content::BrowserThread::UI); |
| 65 data_use_aggregator_.reset( | 65 data_use_aggregator_.reset( |
| 66 new data_usage::DataUseAggregator(nullptr, nullptr)); | 66 new data_usage::DataUseAggregator(nullptr, nullptr)); |
| 67 external_data_use_observer_.reset(new ExternalDataUseObserver( | 67 external_data_use_observer_.reset(new ExternalDataUseObserver( |
| 68 data_use_aggregator_.get(), io_task_runner_.get(), | 68 data_use_aggregator_.get(), io_task_runner_.get(), |
| 69 ui_task_runner_.get())); | 69 ui_task_runner_.get())); |
| 70 // Wait for |external_data_use_observer_| to create the Java object. | 70 // Wait for |external_data_use_observer_| to create the Java object. |
| 71 base::RunLoop().RunUntilIdle(); | 71 base::RunLoop().RunUntilIdle(); |
| 72 external_data_use_observer() | 72 data_use_tab_model()->is_control_app_installed_ = true; |
| 73 ->data_use_tab_model_->is_control_app_installed_ = true; | |
| 74 } | 73 } |
| 75 | 74 |
| 76 // Replaces |external_data_use_observer_| with a new ExternalDataUseObserver. | 75 // Replaces |external_data_use_observer_| with a new ExternalDataUseObserver. |
| 77 void ReplaceExternalDataUseObserver( | 76 void ReplaceExternalDataUseObserver( |
| 78 std::map<std::string, std::string> variation_params) { | 77 std::map<std::string, std::string> variation_params) { |
| 79 variations::testing::ClearAllVariationParams(); | 78 variations::testing::ClearAllVariationParams(); |
| 80 ASSERT_TRUE(variations::AssociateVariationParams( | 79 ASSERT_TRUE(variations::AssociateVariationParams( |
| 81 ExternalDataUseObserver::kExternalDataUseObserverFieldTrial, "Enabled", | 80 ExternalDataUseObserver::kExternalDataUseObserverFieldTrial, "Enabled", |
| 82 variation_params)); | 81 variation_params)); |
| 83 | 82 |
| 84 base::FieldTrialList field_trial_list(nullptr); | 83 base::FieldTrialList field_trial_list(nullptr); |
| 85 | 84 |
| 86 base::FieldTrialList::CreateFieldTrial( | 85 base::FieldTrialList::CreateFieldTrial( |
| 87 ExternalDataUseObserver::kExternalDataUseObserverFieldTrial, "Enabled"); | 86 ExternalDataUseObserver::kExternalDataUseObserverFieldTrial, "Enabled"); |
| 88 | 87 |
| 89 external_data_use_observer_.reset(new ExternalDataUseObserver( | 88 external_data_use_observer_.reset(new ExternalDataUseObserver( |
| 90 data_use_aggregator_.get(), io_task_runner_.get(), | 89 data_use_aggregator_.get(), io_task_runner_.get(), |
| 91 ui_task_runner_.get())); | 90 ui_task_runner_.get())); |
| 92 // Wait for |external_data_use_observer_| to create the Java object. | 91 // Wait for |external_data_use_observer_| to create the Java object. |
| 93 base::RunLoop().RunUntilIdle(); | 92 base::RunLoop().RunUntilIdle(); |
| 94 } | 93 } |
| 95 | 94 |
| 96 void FetchMatchingRulesDone(const std::vector<std::string>& app_package_name, | 95 void FetchMatchingRulesDone(const std::vector<std::string>& app_package_name, |
| 97 const std::vector<std::string>& domain_path_regex, | 96 const std::vector<std::string>& domain_path_regex, |
| 98 const std::vector<std::string>& label) { | 97 const std::vector<std::string>& label) { |
| 99 external_data_use_observer_->GetDataUseTabModel()->RegisterURLRegexes( | 98 data_use_tab_model()->RegisterURLRegexes(app_package_name, |
| 100 app_package_name, domain_path_regex, label); | 99 domain_path_regex, label); |
| 101 base::RunLoop().RunUntilIdle(); | 100 base::RunLoop().RunUntilIdle(); |
| 102 } | 101 } |
| 103 | 102 |
| 104 // Adds a default matching rule to |external_data_use_observer_|. | 103 // Adds a default matching rule to |external_data_use_observer_|. |
| 105 void AddDefaultMatchingRule() { | 104 void AddDefaultMatchingRule() { |
| 106 std::vector<std::string> url_regexes; | 105 std::vector<std::string> url_regexes; |
| 107 url_regexes.push_back( | 106 url_regexes.push_back( |
| 108 "http://www[.]google[.]com/#q=.*|https://www[.]google[.]com/#q=.*"); | 107 "http://www[.]google[.]com/#q=.*|https://www[.]google[.]com/#q=.*"); |
| 109 FetchMatchingRulesDone( | 108 FetchMatchingRulesDone( |
| 110 std::vector<std::string>(url_regexes.size(), std::string()), | 109 std::vector<std::string>(url_regexes.size(), std::string()), |
| 111 url_regexes, | 110 url_regexes, |
| 112 std::vector<std::string>(url_regexes.size(), kDefaultLabel)); | 111 std::vector<std::string>(url_regexes.size(), kDefaultLabel)); |
| 113 } | 112 } |
| 114 | 113 |
| 115 // Notifies DataUseTabModel that tab tracking has started on kDefaultTabId. | 114 // Notifies DataUseTabModel that tab tracking has started on kDefaultTabId. |
| 116 void TriggerTabTrackingOnDefaultTab() { | 115 void TriggerTabTrackingOnDefaultTab() { |
| 117 external_data_use_observer_->GetDataUseTabModel()->OnNavigationEvent( | 116 data_use_tab_model()->OnNavigationEvent( |
| 118 kDefaultTabId, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, | 117 kDefaultTabId, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, |
| 119 GURL(kDefaultURL), std::string(), nullptr); | 118 GURL(kDefaultURL), std::string(), nullptr); |
| 120 } | 119 } |
| 121 | 120 |
| 122 // Returns a default data_usage::DataUse object. | 121 // Returns a default data_usage::DataUse object. |
| 123 data_usage::DataUse default_data_use() { | 122 data_usage::DataUse default_data_use() { |
| 124 return data_usage::DataUse( | 123 return data_usage::DataUse( |
| 125 GURL(kDefaultURL), base::TimeTicks::Now(), GURL(), kDefaultTabId, | 124 GURL(kDefaultURL), base::TimeTicks::Now(), GURL(), kDefaultTabId, |
| 126 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "", | 125 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "", |
| 127 default_upload_bytes(), default_download_bytes()); | 126 default_upload_bytes(), default_download_bytes()); |
| 128 } | 127 } |
| 129 | 128 |
| 130 void OnDataUse(const data_usage::DataUse& data_use) { | 129 void OnDataUse(const data_usage::DataUse& data_use) { |
| 131 external_data_use_observer_->OnDataUse(data_use); | 130 external_data_use_reporter()->OnDataUse(data_use); |
| 132 base::RunLoop().RunUntilIdle(); | |
| 133 } | 131 } |
| 134 | 132 |
| 135 ExternalDataUseObserver* external_data_use_observer() const { | 133 ExternalDataUseObserver* external_data_use_observer() const { |
| 136 return external_data_use_observer_.get(); | 134 return external_data_use_observer_.get(); |
| 137 } | 135 } |
| 138 | 136 |
| 139 const ExternalDataUseObserver::DataUseReports& buffered_data_reports() const { | 137 ExternalDataUseReporter* external_data_use_reporter() const { |
| 140 return external_data_use_observer_->buffered_data_reports_; | 138 return external_data_use_observer_->GetExternalDataUseReporterForTesting(); |
| 139 } |
| 140 |
| 141 DataUseTabModel* data_use_tab_model() const { |
| 142 return external_data_use_reporter()->data_use_tab_model_; |
| 143 } |
| 144 |
| 145 const ExternalDataUseReporter::DataUseReports& buffered_data_reports() const { |
| 146 return external_data_use_reporter()->buffered_data_reports_; |
| 141 } | 147 } |
| 142 | 148 |
| 143 int64_t default_upload_bytes() const { return 1; } | 149 int64_t default_upload_bytes() const { return 1; } |
| 144 | 150 |
| 145 int64_t default_download_bytes() const { | 151 int64_t default_download_bytes() const { |
| 146 return external_data_use_observer_->data_use_report_min_bytes_; | 152 return external_data_use_reporter()->data_use_report_min_bytes_; |
| 147 } | 153 } |
| 148 | 154 |
| 149 private: | 155 private: |
| 150 std::unique_ptr<content::TestBrowserThreadBundle> thread_bundle_; | 156 std::unique_ptr<content::TestBrowserThreadBundle> thread_bundle_; |
| 151 std::unique_ptr<data_usage::DataUseAggregator> data_use_aggregator_; | 157 std::unique_ptr<data_usage::DataUseAggregator> data_use_aggregator_; |
| 152 std::unique_ptr<ExternalDataUseObserver> external_data_use_observer_; | 158 std::unique_ptr<ExternalDataUseObserver> external_data_use_observer_; |
| 153 | 159 |
| 154 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 160 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 155 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 161 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 156 }; | 162 }; |
| 157 | 163 |
| 158 // Verifies that the external data use observer is registered as an observer | |
| 159 // only when at least one matching rule is present. | |
| 160 TEST_F(ExternalDataUseObserverTest, RegisteredAsDataUseObserver) { | |
| 161 EXPECT_FALSE(external_data_use_observer()->registered_as_data_use_observer_); | |
| 162 | |
| 163 AddDefaultMatchingRule(); | |
| 164 EXPECT_TRUE(external_data_use_observer()->registered_as_data_use_observer_); | |
| 165 | |
| 166 // Push an empty vector. Since no matching rules are present, | |
| 167 // |external_data_use_observer| should no longer be registered as a data use | |
| 168 // observer. | |
| 169 FetchMatchingRulesDone(std::vector<std::string>(), std::vector<std::string>(), | |
| 170 std::vector<std::string>()); | |
| 171 base::RunLoop().RunUntilIdle(); | |
| 172 EXPECT_FALSE(external_data_use_observer()->registered_as_data_use_observer_); | |
| 173 } | |
| 174 | |
| 175 // Verifies that buffer size does not exceed the specified limit. | 164 // Verifies that buffer size does not exceed the specified limit. |
| 176 TEST_F(ExternalDataUseObserverTest, BufferSize) { | 165 TEST_F(ExternalDataUseReporterTest, BufferSize) { |
| 177 base::HistogramTester histogram_tester; | 166 base::HistogramTester histogram_tester; |
| 178 | 167 |
| 179 AddDefaultMatchingRule(); | 168 AddDefaultMatchingRule(); |
| 180 TriggerTabTrackingOnDefaultTab(); | 169 TriggerTabTrackingOnDefaultTab(); |
| 181 | 170 |
| 182 // Push more entries than the buffer size. Buffer size should not be exceeded. | 171 // Push more entries than the buffer size. Buffer size should not be exceeded. |
| 183 for (size_t i = 0; i < ExternalDataUseObserver::kMaxBufferSize * 2; ++i) { | 172 for (size_t i = 0; i < ExternalDataUseReporter::kMaxBufferSize * 2; ++i) { |
| 184 data_usage::DataUse data_use = default_data_use(); | 173 data_usage::DataUse data_use = default_data_use(); |
| 185 data_use.mcc_mnc = "mccmnc" + base::Int64ToString(i); | 174 data_use.mcc_mnc = "mccmnc" + base::Int64ToString(i); |
| 186 OnDataUse(data_use); | 175 OnDataUse(data_use); |
| 187 } | 176 } |
| 188 | 177 |
| 189 EXPECT_LE(0, external_data_use_observer()->total_bytes_buffered_); | 178 EXPECT_LE(0, external_data_use_reporter()->total_bytes_buffered_); |
| 190 | 179 |
| 191 // Verify that total buffered bytes is computed correctly. | 180 // Verify that total buffered bytes is computed correctly. |
| 192 EXPECT_EQ( | 181 EXPECT_EQ( |
| 193 static_cast<int64_t>(ExternalDataUseObserver::kMaxBufferSize * | 182 static_cast<int64_t>(ExternalDataUseReporter::kMaxBufferSize * |
| 194 (default_upload_bytes() + default_download_bytes())), | 183 (default_upload_bytes() + default_download_bytes())), |
| 195 external_data_use_observer()->total_bytes_buffered_); | 184 external_data_use_reporter()->total_bytes_buffered_); |
| 196 EXPECT_EQ(ExternalDataUseObserver::kMaxBufferSize, | 185 EXPECT_EQ(ExternalDataUseReporter::kMaxBufferSize, |
| 197 buffered_data_reports().size()); | 186 buffered_data_reports().size()); |
| 198 | 187 |
| 199 // Verify the label of the data use report. | 188 // Verify the label of the data use report. |
| 200 for (const auto& it : buffered_data_reports()) | 189 for (const auto& it : buffered_data_reports()) |
| 201 EXPECT_EQ(kDefaultLabel, it.first.label); | 190 EXPECT_EQ(kDefaultLabel, it.first.label); |
| 202 | 191 |
| 203 // Verify that metrics were updated correctly for the lost reports. | 192 // Verify that metrics were updated correctly for the lost reports. |
| 204 histogram_tester.ExpectUniqueSample( | 193 histogram_tester.ExpectUniqueSample( |
| 205 "DataUsage.ReportSubmissionResult", | 194 "DataUsage.ReportSubmissionResult", |
| 206 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_LOST, | 195 ExternalDataUseReporter::DATAUSAGE_REPORT_SUBMISSION_LOST, |
| 207 ExternalDataUseObserver::kMaxBufferSize - 1); | 196 ExternalDataUseReporter::kMaxBufferSize - 1); |
| 208 histogram_tester.ExpectUniqueSample( | 197 histogram_tester.ExpectUniqueSample( |
| 209 "DataUsage.ReportSubmission.Bytes.Lost", | 198 "DataUsage.ReportSubmission.Bytes.Lost", |
| 210 default_upload_bytes() + default_download_bytes(), | 199 default_upload_bytes() + default_download_bytes(), |
| 211 ExternalDataUseObserver::kMaxBufferSize - 1); | 200 ExternalDataUseReporter::kMaxBufferSize - 1); |
| 212 } | 201 } |
| 213 | 202 |
| 214 // Tests that buffered data use reports are merged correctly. | 203 // Tests that buffered data use reports are merged correctly. |
| 215 TEST_F(ExternalDataUseObserverTest, ReportsMergedCorrectly) { | 204 TEST_F(ExternalDataUseReporterTest, ReportsMergedCorrectly) { |
| 216 AddDefaultMatchingRule(); | 205 AddDefaultMatchingRule(); |
| 217 TriggerTabTrackingOnDefaultTab(); | 206 TriggerTabTrackingOnDefaultTab(); |
| 218 | 207 |
| 219 const size_t num_iterations = ExternalDataUseObserver::kMaxBufferSize * 2; | 208 const size_t num_iterations = ExternalDataUseReporter::kMaxBufferSize * 2; |
| 220 | 209 |
| 221 for (size_t i = 0; i < num_iterations; ++i) { | 210 for (size_t i = 0; i < num_iterations; ++i) { |
| 222 data_usage::DataUse data_use_foo = default_data_use(); | 211 data_usage::DataUse data_use_foo = default_data_use(); |
| 223 data_use_foo.mcc_mnc = kFooMccMnc; | 212 data_use_foo.mcc_mnc = kFooMccMnc; |
| 224 OnDataUse(data_use_foo); | 213 OnDataUse(data_use_foo); |
| 225 | 214 |
| 226 data_usage::DataUse data_use_bar = default_data_use(); | 215 data_usage::DataUse data_use_bar = default_data_use(); |
| 227 data_use_bar.mcc_mnc = kBarMccMnc; | 216 data_use_bar.mcc_mnc = kBarMccMnc; |
| 228 OnDataUse(data_use_bar); | 217 OnDataUse(data_use_bar); |
| 229 | 218 |
| 230 data_usage::DataUse data_use_baz = default_data_use(); | 219 data_usage::DataUse data_use_baz = default_data_use(); |
| 231 data_use_baz.mcc_mnc = kBazMccMnc; | 220 data_use_baz.mcc_mnc = kBazMccMnc; |
| 232 OnDataUse(data_use_baz); | 221 OnDataUse(data_use_baz); |
| 233 } | 222 } |
| 234 | 223 |
| 235 ASSERT_EQ(3U, buffered_data_reports().size()); | 224 ASSERT_EQ(3U, buffered_data_reports().size()); |
| 236 | 225 |
| 237 // One of the foo reports should have been submitted, and all the other foo | 226 // One of the foo reports should have been submitted, and all the other foo |
| 238 // reports should have been merged together. All of the bar and baz reports | 227 // reports should have been merged together. All of the bar and baz reports |
| 239 // should have been merged together respectively. | 228 // should have been merged together respectively. |
| 240 const struct { | 229 const struct { |
| 241 std::string mcc_mnc; | 230 std::string mcc_mnc; |
| 242 size_t number_of_merged_reports; | 231 size_t number_of_merged_reports; |
| 243 } expected_data_use_reports[] = {{kFooMccMnc, num_iterations - 1}, | 232 } expected_data_use_reports[] = {{kFooMccMnc, num_iterations - 1}, |
| 244 {kBarMccMnc, num_iterations}, | 233 {kBarMccMnc, num_iterations}, |
| 245 {kBazMccMnc, num_iterations}}; | 234 {kBazMccMnc, num_iterations}}; |
| 246 | 235 |
| 247 for (const auto& expected_report : expected_data_use_reports) { | 236 for (const auto& expected_report : expected_data_use_reports) { |
| 248 const ExternalDataUseObserver::DataUseReportKey key( | 237 const ExternalDataUseReporter::DataUseReportKey key( |
| 249 kDefaultLabel, DataUseTabModel::kDefaultTag, | 238 kDefaultLabel, DataUseTabModel::kDefaultTag, |
| 250 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, | 239 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, |
| 251 expected_report.mcc_mnc); | 240 expected_report.mcc_mnc); |
| 252 | 241 |
| 253 EXPECT_NE(buffered_data_reports().end(), buffered_data_reports().find(key)); | 242 EXPECT_NE(buffered_data_reports().end(), buffered_data_reports().find(key)); |
| 254 EXPECT_EQ(static_cast<int64_t>(expected_report.number_of_merged_reports) * | 243 EXPECT_EQ(static_cast<int64_t>(expected_report.number_of_merged_reports) * |
| 255 (default_download_bytes()), | 244 (default_download_bytes()), |
| 256 buffered_data_reports().find(key)->second.bytes_downloaded); | 245 buffered_data_reports().find(key)->second.bytes_downloaded); |
| 257 EXPECT_EQ(static_cast<int64_t>(expected_report.number_of_merged_reports * | 246 EXPECT_EQ(static_cast<int64_t>(expected_report.number_of_merged_reports * |
| 258 (default_upload_bytes())), | 247 (default_upload_bytes())), |
| 259 buffered_data_reports().find(key)->second.bytes_uploaded); | 248 buffered_data_reports().find(key)->second.bytes_uploaded); |
| 260 } | 249 } |
| 261 } | 250 } |
| 262 | 251 |
| 263 // Tests that timestamps of merged reports is correct. | 252 // Tests that timestamps of merged reports is correct. |
| 264 TEST_F(ExternalDataUseObserverTest, TimestampsMergedCorrectly) { | 253 TEST_F(ExternalDataUseReporterTest, TimestampsMergedCorrectly) { |
| 265 AddDefaultMatchingRule(); | 254 AddDefaultMatchingRule(); |
| 266 | 255 |
| 267 const size_t num_iterations = ExternalDataUseObserver::kMaxBufferSize * 2; | 256 const size_t num_iterations = ExternalDataUseReporter::kMaxBufferSize * 2; |
| 268 | 257 |
| 269 base::Time start_timestamp = base::Time::UnixEpoch(); | 258 base::Time start_timestamp = base::Time::UnixEpoch(); |
| 270 base::Time end_timestamp = start_timestamp + base::TimeDelta::FromSeconds(1); | 259 base::Time end_timestamp = start_timestamp + base::TimeDelta::FromSeconds(1); |
| 271 for (size_t i = 0; i < num_iterations; ++i) { | 260 for (size_t i = 0; i < num_iterations; ++i) { |
| 272 external_data_use_observer()->BufferDataUseReport( | 261 external_data_use_reporter()->BufferDataUseReport( |
| 273 default_data_use(), kDefaultLabel, DataUseTabModel::kDefaultTag, | 262 default_data_use(), kDefaultLabel, DataUseTabModel::kDefaultTag, |
| 274 start_timestamp, end_timestamp); | 263 start_timestamp, end_timestamp); |
| 275 | 264 |
| 276 start_timestamp += base::TimeDelta::FromSeconds(1); | 265 start_timestamp += base::TimeDelta::FromSeconds(1); |
| 277 end_timestamp += base::TimeDelta::FromSeconds(1); | 266 end_timestamp += base::TimeDelta::FromSeconds(1); |
| 278 } | 267 } |
| 279 | 268 |
| 280 EXPECT_EQ(1U, buffered_data_reports().size()); | 269 EXPECT_EQ(1U, buffered_data_reports().size()); |
| 281 EXPECT_EQ(0, buffered_data_reports().begin()->second.start_time.ToJavaTime()); | 270 EXPECT_EQ(0, buffered_data_reports().begin()->second.start_time.ToJavaTime()); |
| 282 // Convert from seconds to milliseconds. | 271 // Convert from seconds to milliseconds. |
| 283 EXPECT_EQ(static_cast<int64_t>(num_iterations * 1000), | 272 EXPECT_EQ(static_cast<int64_t>(num_iterations * 1000), |
| 284 buffered_data_reports().begin()->second.end_time.ToJavaTime()); | 273 buffered_data_reports().begin()->second.end_time.ToJavaTime()); |
| 285 } | 274 } |
| 286 | 275 |
| 287 // Tests the behavior when multiple matching rules are available for URL and | 276 // Tests the behavior when multiple matching rules are available for URL and |
| 288 // package name matching. | 277 // package name matching. |
| 289 TEST_F(ExternalDataUseObserverTest, MultipleMatchingRules) { | 278 TEST_F(ExternalDataUseReporterTest, MultipleMatchingRules) { |
| 290 std::vector<std::string> url_regexes; | 279 std::vector<std::string> url_regexes; |
| 291 url_regexes.push_back( | 280 url_regexes.push_back( |
| 292 "http://www[.]foo[.]com/#q=.*|https://www[.]foo[.]com/#q=.*"); | 281 "http://www[.]foo[.]com/#q=.*|https://www[.]foo[.]com/#q=.*"); |
| 293 url_regexes.push_back( | 282 url_regexes.push_back( |
| 294 "http://www[.]bar[.]com/#q=.*|https://www[.]bar[.]com/#q=.*"); | 283 "http://www[.]bar[.]com/#q=.*|https://www[.]bar[.]com/#q=.*"); |
| 295 | 284 |
| 296 std::vector<std::string> labels; | 285 std::vector<std::string> labels; |
| 297 labels.push_back(kFooLabel); | 286 labels.push_back(kFooLabel); |
| 298 labels.push_back(kBarLabel); | 287 labels.push_back(kBarLabel); |
| 299 | 288 |
| 300 std::vector<std::string> app_package_names; | 289 std::vector<std::string> app_package_names; |
| 301 const char kAppFoo[] = "com.example.foo"; | 290 const char kAppFoo[] = "com.example.foo"; |
| 302 const char kAppBar[] = "com.example.bar"; | 291 const char kAppBar[] = "com.example.bar"; |
| 303 app_package_names.push_back(kAppFoo); | 292 app_package_names.push_back(kAppFoo); |
| 304 app_package_names.push_back(kAppBar); | 293 app_package_names.push_back(kAppBar); |
| 305 | 294 |
| 306 FetchMatchingRulesDone(app_package_names, url_regexes, labels); | 295 FetchMatchingRulesDone(app_package_names, url_regexes, labels); |
| 307 | 296 |
| 308 external_data_use_observer()->GetDataUseTabModel()->OnNavigationEvent( | 297 data_use_tab_model()->OnNavigationEvent( |
| 309 kDefaultTabId, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, | 298 kDefaultTabId, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, |
| 310 GURL("http://www.foo.com/#q=abc"), std::string(), nullptr); | 299 GURL("http://www.foo.com/#q=abc"), std::string(), nullptr); |
| 311 | 300 |
| 312 external_data_use_observer()->GetDataUseTabModel()->OnNavigationEvent( | 301 data_use_tab_model()->OnNavigationEvent( |
| 313 kDefaultTabId + 1, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, | 302 kDefaultTabId + 1, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, |
| 314 GURL("http://www.bar.com/#q=abc"), std::string(), nullptr); | 303 GURL("http://www.bar.com/#q=abc"), std::string(), nullptr); |
| 315 | 304 |
| 316 EXPECT_EQ(0U, external_data_use_observer()->buffered_data_reports_.size()); | 305 EXPECT_EQ(0U, external_data_use_reporter()->buffered_data_reports_.size()); |
| 317 EXPECT_TRUE(external_data_use_observer() | 306 EXPECT_TRUE(external_data_use_reporter() |
| 318 ->last_data_report_submitted_ticks_.is_null()); | 307 ->last_data_report_submitted_ticks_.is_null()); |
| 319 | 308 |
| 320 // Check |kLabelFoo| matching rule. | 309 // Check |kLabelFoo| matching rule. |
| 321 data_usage::DataUse data_foo = default_data_use(); | 310 data_usage::DataUse data_foo = default_data_use(); |
| 322 data_foo.url = GURL("http://www.foo.com/#q=abc"); | 311 data_foo.url = GURL("http://www.foo.com/#q=abc"); |
| 323 data_foo.mcc_mnc = kFooMccMnc; | 312 data_foo.mcc_mnc = kFooMccMnc; |
| 324 OnDataUse(data_foo); | 313 OnDataUse(data_foo); |
| 325 | 314 |
| 326 // Check |kLabelBar| matching rule. | 315 // Check |kLabelBar| matching rule. |
| 327 data_usage::DataUse data_bar = default_data_use(); | 316 data_usage::DataUse data_bar = default_data_use(); |
| 328 data_bar.tab_id = kDefaultTabId + 1; | 317 data_bar.tab_id = kDefaultTabId + 1; |
| 329 data_bar.url = GURL("http://www.bar.com/#q=abc"); | 318 data_bar.url = GURL("http://www.bar.com/#q=abc"); |
| 330 data_bar.mcc_mnc = kBarMccMnc; | 319 data_bar.mcc_mnc = kBarMccMnc; |
| 331 OnDataUse(data_bar); | 320 OnDataUse(data_bar); |
| 332 | 321 |
| 333 // bar report should be present. | 322 // bar report should be present. |
| 334 EXPECT_EQ(1U, buffered_data_reports().size()); | 323 EXPECT_EQ(1U, buffered_data_reports().size()); |
| 335 | 324 |
| 336 const ExternalDataUseObserver::DataUseReportKey key_bar( | 325 const ExternalDataUseReporter::DataUseReportKey key_bar( |
| 337 kBarLabel, DataUseTabModel::kDefaultTag, | 326 kBarLabel, DataUseTabModel::kDefaultTag, |
| 338 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kBarMccMnc); | 327 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kBarMccMnc); |
| 339 EXPECT_NE(buffered_data_reports().end(), | 328 EXPECT_NE(buffered_data_reports().end(), |
| 340 buffered_data_reports().find(key_bar)); | 329 buffered_data_reports().find(key_bar)); |
| 341 } | 330 } |
| 342 | 331 |
| 343 // Tests that hash function reports distinct values. This test may fail if there | 332 // Tests that hash function reports distinct values. This test may fail if there |
| 344 // is a hash collision, however the chances of that happening are very low. | 333 // is a hash collision, however the chances of that happening are very low. |
| 345 TEST_F(ExternalDataUseObserverTest, HashFunction) { | 334 TEST_F(ExternalDataUseReporterTest, HashFunction) { |
| 346 ExternalDataUseObserver::DataUseReportKeyHash hash; | 335 ExternalDataUseReporter::DataUseReportKeyHash hash; |
| 347 | 336 |
| 348 ExternalDataUseObserver::DataUseReportKey foo( | 337 ExternalDataUseReporter::DataUseReportKey foo( |
| 349 kFooLabel, DataUseTabModel::kDefaultTag, | 338 kFooLabel, DataUseTabModel::kDefaultTag, |
| 350 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kFooMccMnc); | 339 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kFooMccMnc); |
| 351 ExternalDataUseObserver::DataUseReportKey bar_label( | 340 ExternalDataUseReporter::DataUseReportKey bar_label( |
| 352 kBarLabel, DataUseTabModel::kDefaultTag, | 341 kBarLabel, DataUseTabModel::kDefaultTag, |
| 353 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kFooMccMnc); | 342 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kFooMccMnc); |
| 354 ExternalDataUseObserver::DataUseReportKey bar_custom_tab_tag( | 343 ExternalDataUseReporter::DataUseReportKey bar_custom_tab_tag( |
| 355 kBarLabel, DataUseTabModel::kCustomTabTag, | 344 kBarLabel, DataUseTabModel::kCustomTabTag, |
| 356 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kFooMccMnc); | 345 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kFooMccMnc); |
| 357 ExternalDataUseObserver::DataUseReportKey bar_network_type( | 346 ExternalDataUseReporter::DataUseReportKey bar_network_type( |
| 358 kFooLabel, DataUseTabModel::kDefaultTag, | 347 kFooLabel, DataUseTabModel::kDefaultTag, |
| 359 net::NetworkChangeNotifier::CONNECTION_WIFI, kFooMccMnc); | 348 net::NetworkChangeNotifier::CONNECTION_WIFI, kFooMccMnc); |
| 360 ExternalDataUseObserver::DataUseReportKey bar_mcc_mnc( | 349 ExternalDataUseReporter::DataUseReportKey bar_mcc_mnc( |
| 361 kFooLabel, DataUseTabModel::kDefaultTag, | 350 kFooLabel, DataUseTabModel::kDefaultTag, |
| 362 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kBarMccMnc); | 351 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, kBarMccMnc); |
| 363 | 352 |
| 364 EXPECT_NE(hash(foo), hash(bar_label)); | 353 EXPECT_NE(hash(foo), hash(bar_label)); |
| 365 EXPECT_NE(hash(foo), hash(bar_custom_tab_tag)); | 354 EXPECT_NE(hash(foo), hash(bar_custom_tab_tag)); |
| 366 EXPECT_NE(hash(foo), hash(bar_network_type)); | 355 EXPECT_NE(hash(foo), hash(bar_network_type)); |
| 367 EXPECT_NE(hash(foo), hash(bar_mcc_mnc)); | 356 EXPECT_NE(hash(foo), hash(bar_mcc_mnc)); |
| 368 } | 357 } |
| 369 | 358 |
| 370 // Tests if matching rules are fetched periodically. | |
| 371 TEST_F(ExternalDataUseObserverTest, PeriodicFetchMatchingRules) { | |
| 372 AddDefaultMatchingRule(); | |
| 373 | |
| 374 EXPECT_FALSE( | |
| 375 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); | |
| 376 | |
| 377 // Change the time when the fetching rules were fetched. | |
| 378 external_data_use_observer()->last_matching_rules_fetch_time_ = | |
| 379 base::TimeTicks::Now() - | |
| 380 external_data_use_observer()->fetch_matching_rules_duration_; | |
| 381 // Matching rules should be expired. | |
| 382 EXPECT_GE(base::TimeTicks::Now() - | |
| 383 external_data_use_observer()->last_matching_rules_fetch_time_, | |
| 384 external_data_use_observer()->fetch_matching_rules_duration_); | |
| 385 | |
| 386 // OnDataUse should trigger fetching of matching rules. | |
| 387 OnDataUse(default_data_use()); | |
| 388 | |
| 389 // Matching rules should not be expired. | |
| 390 EXPECT_LT(base::TimeTicks::Now() - | |
| 391 external_data_use_observer()->last_matching_rules_fetch_time_, | |
| 392 external_data_use_observer()->fetch_matching_rules_duration_); | |
| 393 } | |
| 394 | |
| 395 // Tests the matching rule fetch behavior when the external control app is | |
| 396 // installed and not installed. Matching rules should be fetched when control | |
| 397 // app gets installed. If control app is installed and no valid rules are found, | |
| 398 // matching rules are fetched on every navigation. Rules are not fetched if | |
| 399 // control app is not installed or if more than zero valid rules have been | |
| 400 // fetched. | |
| 401 TEST_F(ExternalDataUseObserverTest, MatchingRuleFetchOnControlAppInstall) { | |
| 402 { | |
| 403 // Matching rules not fetched on navigation if control app is not installed, | |
| 404 // and navigation events will be buffered. | |
| 405 external_data_use_observer()->last_matching_rules_fetch_time_ = | |
| 406 base::TimeTicks(); | |
| 407 external_data_use_observer() | |
| 408 ->data_use_tab_model_->is_control_app_installed_ = false; | |
| 409 base::HistogramTester histogram_tester; | |
| 410 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( | |
| 411 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), | |
| 412 std::string(), nullptr); | |
| 413 base::RunLoop().RunUntilIdle(); | |
| 414 histogram_tester.ExpectTotalCount("DataUsage.MatchingRulesCount.Valid", 0); | |
| 415 } | |
| 416 | |
| 417 { | |
| 418 // Matching rules are fetched when control app is installed. | |
| 419 base::HistogramTester histogram_tester; | |
| 420 external_data_use_observer() | |
| 421 ->data_use_tab_model_->OnControlAppInstallStateChange(true); | |
| 422 base::RunLoop().RunUntilIdle(); | |
| 423 histogram_tester.ExpectTotalCount("DataUsage.MatchingRulesCount.Valid", 1); | |
| 424 } | |
| 425 | |
| 426 { | |
| 427 // Matching rules fetched on every navigation if control app is installed | |
| 428 // and zero rules are available. | |
| 429 external_data_use_observer()->last_matching_rules_fetch_time_ = | |
| 430 base::TimeTicks(); | |
| 431 base::HistogramTester histogram_tester; | |
| 432 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( | |
| 433 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), | |
| 434 std::string(), nullptr); | |
| 435 base::RunLoop().RunUntilIdle(); | |
| 436 histogram_tester.ExpectTotalCount("DataUsage.MatchingRulesCount.Valid", 1); | |
| 437 } | |
| 438 | |
| 439 { | |
| 440 // Matching rules not fetched on navigation if control app is installed and | |
| 441 // more than zero rules are available. | |
| 442 AddDefaultMatchingRule(); | |
| 443 external_data_use_observer()->last_matching_rules_fetch_time_ = | |
| 444 base::TimeTicks(); | |
| 445 EXPECT_TRUE(external_data_use_observer() | |
| 446 ->last_matching_rules_fetch_time_.is_null()); | |
| 447 base::HistogramTester histogram_tester; | |
| 448 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( | |
| 449 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), | |
| 450 std::string(), nullptr); | |
| 451 base::RunLoop().RunUntilIdle(); | |
| 452 histogram_tester.ExpectTotalCount("DataUsage.MatchingRulesCount.Valid", 0); | |
| 453 } | |
| 454 } | |
| 455 | |
| 456 // Tests if data use reports are sent only after the total bytes sent/received | 359 // Tests if data use reports are sent only after the total bytes sent/received |
| 457 // across all buffered reports have reached the specified threshold. | 360 // across all buffered reports have reached the specified threshold. |
| 458 TEST_F(ExternalDataUseObserverTest, BufferDataUseReports) { | 361 TEST_F(ExternalDataUseReporterTest, BufferDataUseReports) { |
| 459 AddDefaultMatchingRule(); | 362 AddDefaultMatchingRule(); |
| 460 TriggerTabTrackingOnDefaultTab(); | 363 TriggerTabTrackingOnDefaultTab(); |
| 461 | 364 |
| 462 // This tests reports 1024 bytes in each loop iteration. For the test to work | 365 // This tests reports 1024 bytes in each loop iteration. For the test to work |
| 463 // properly, |data_use_report_min_bytes_| should be a multiple of 1024. | 366 // properly, |data_use_report_min_bytes_| should be a multiple of 1024. |
| 464 ASSERT_EQ(0, external_data_use_observer()->data_use_report_min_bytes_ % 1024); | 367 ASSERT_EQ(0, external_data_use_reporter()->data_use_report_min_bytes_ % 1024); |
| 465 | 368 |
| 466 const size_t num_iterations = | 369 const size_t num_iterations = |
| 467 external_data_use_observer()->data_use_report_min_bytes_ / 1024; | 370 external_data_use_reporter()->data_use_report_min_bytes_ / 1024; |
| 468 | 371 |
| 469 for (size_t i = 0; i < num_iterations; ++i) { | 372 for (size_t i = 0; i < num_iterations; ++i) { |
| 470 data_usage::DataUse data_use = default_data_use(); | 373 data_usage::DataUse data_use = default_data_use(); |
| 471 data_use.tx_bytes = 1024; | 374 data_use.tx_bytes = 1024; |
| 472 data_use.rx_bytes = 0; | 375 data_use.rx_bytes = 0; |
| 473 OnDataUse(data_use); | 376 OnDataUse(data_use); |
| 474 | 377 |
| 475 if (i != num_iterations - 1) { | 378 if (i != num_iterations - 1) { |
| 476 // Total buffered bytes is less than the minimum threshold. Data use | 379 // Total buffered bytes is less than the minimum threshold. Data use |
| 477 // report should not be sent. | 380 // report should not be sent. |
| 478 EXPECT_TRUE(external_data_use_observer() | 381 EXPECT_TRUE(external_data_use_reporter() |
| 479 ->last_data_report_submitted_ticks_.is_null()); | 382 ->last_data_report_submitted_ticks_.is_null()); |
| 480 EXPECT_EQ(static_cast<int64_t>(i + 1), | 383 EXPECT_EQ(static_cast<int64_t>(i + 1), |
| 481 external_data_use_observer()->total_bytes_buffered_ / 1024); | 384 external_data_use_reporter()->total_bytes_buffered_ / 1024); |
| 482 EXPECT_EQ(0, external_data_use_observer()->pending_report_bytes_); | 385 EXPECT_EQ(0, external_data_use_reporter()->pending_report_bytes_); |
| 483 | 386 |
| 484 } else { | 387 } else { |
| 485 // Total bytes is at least the minimum threshold. This should trigger | 388 // Total bytes is at least the minimum threshold. This should trigger |
| 486 // submitting of the buffered data use report. | 389 // submitting of the buffered data use report. |
| 487 EXPECT_FALSE(external_data_use_observer() | 390 EXPECT_FALSE(external_data_use_reporter() |
| 488 ->last_data_report_submitted_ticks_.is_null()); | 391 ->last_data_report_submitted_ticks_.is_null()); |
| 489 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); | 392 EXPECT_EQ(0, external_data_use_reporter()->total_bytes_buffered_); |
| 490 } | 393 } |
| 491 } | 394 } |
| 492 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); | 395 EXPECT_EQ(0, external_data_use_reporter()->total_bytes_buffered_); |
| 493 EXPECT_EQ(static_cast<int64_t>(num_iterations), | 396 EXPECT_EQ(static_cast<int64_t>(num_iterations), |
| 494 external_data_use_observer()->pending_report_bytes_ / 1024); | 397 external_data_use_reporter()->pending_report_bytes_ / 1024); |
| 495 | 398 |
| 496 base::HistogramTester histogram_tester; | 399 base::HistogramTester histogram_tester; |
| 497 external_data_use_observer()->OnReportDataUseDone(true); | 400 external_data_use_reporter()->OnReportDataUseDone(true); |
| 498 | 401 |
| 499 // Verify that metrics were updated correctly for the report that was | 402 // Verify that metrics were updated correctly for the report that was |
| 500 // successfully submitted. | 403 // successfully submitted. |
| 501 histogram_tester.ExpectUniqueSample( | 404 histogram_tester.ExpectUniqueSample( |
| 502 "DataUsage.ReportSubmissionResult", | 405 "DataUsage.ReportSubmissionResult", |
| 503 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL, 1); | 406 ExternalDataUseReporter::DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL, 1); |
| 504 histogram_tester.ExpectUniqueSample( | 407 histogram_tester.ExpectUniqueSample( |
| 505 "DataUsage.ReportSubmission.Bytes.Successful", | 408 "DataUsage.ReportSubmission.Bytes.Successful", |
| 506 external_data_use_observer()->data_use_report_min_bytes_, 1); | 409 external_data_use_reporter()->data_use_report_min_bytes_, 1); |
| 507 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 1); | 410 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 1); |
| 508 | 411 |
| 509 // Verify that metrics were updated correctly for the report that was not | 412 // Verify that metrics were updated correctly for the report that was not |
| 510 // successfully submitted. | 413 // successfully submitted. |
| 511 OnDataUse(default_data_use()); | 414 OnDataUse(default_data_use()); |
| 512 external_data_use_observer()->OnReportDataUseDone(false); | 415 external_data_use_reporter()->OnReportDataUseDone(false); |
| 513 histogram_tester.ExpectTotalCount("DataUsage.ReportSubmissionResult", 2); | 416 histogram_tester.ExpectTotalCount("DataUsage.ReportSubmissionResult", 2); |
| 514 histogram_tester.ExpectBucketCount( | 417 histogram_tester.ExpectBucketCount( |
| 515 "DataUsage.ReportSubmissionResult", | 418 "DataUsage.ReportSubmissionResult", |
| 516 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_FAILED, 1); | 419 ExternalDataUseReporter::DATAUSAGE_REPORT_SUBMISSION_FAILED, 1); |
| 517 histogram_tester.ExpectUniqueSample( | 420 histogram_tester.ExpectUniqueSample( |
| 518 "DataUsage.ReportSubmission.Bytes.Failed", | 421 "DataUsage.ReportSubmission.Bytes.Failed", |
| 519 external_data_use_observer()->data_use_report_min_bytes_, 1); | 422 external_data_use_reporter()->data_use_report_min_bytes_, 1); |
| 520 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 2); | 423 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 2); |
| 521 } | 424 } |
| 522 | 425 |
| 523 #if defined(OS_ANDROID) | 426 #if defined(OS_ANDROID) |
| 524 // Tests data use report submission when application status callback is called. | 427 // Tests data use report submission when application status callback is called. |
| 525 // Report should be submitted even if the number of bytes is less than the | 428 // Report should be submitted even if the number of bytes is less than the |
| 526 // threshold. Report should not be submitted if there is a pending report. | 429 // threshold. Report should not be submitted if there is a pending report. |
| 527 TEST_F(ExternalDataUseObserverTest, DataUseReportingOnApplicationStatusChange) { | 430 TEST_F(ExternalDataUseReporterTest, DataUseReportingOnApplicationStatusChange) { |
| 528 base::HistogramTester histogram_tester; | 431 base::HistogramTester histogram_tester; |
| 529 AddDefaultMatchingRule(); | 432 AddDefaultMatchingRule(); |
| 530 TriggerTabTrackingOnDefaultTab(); | 433 TriggerTabTrackingOnDefaultTab(); |
| 531 | 434 |
| 532 // Report with less than threshold bytes should be reported, on application | 435 // Report with less than threshold bytes should be reported, on application |
| 533 // state change to background. | 436 // state change to background. |
| 534 data_usage::DataUse data_use = default_data_use(); | 437 data_usage::DataUse data_use = default_data_use(); |
| 535 data_use.tx_bytes = 1; | 438 data_use.tx_bytes = 1; |
| 536 data_use.rx_bytes = 1; | 439 data_use.rx_bytes = 1; |
| 537 OnDataUse(data_use); | 440 OnDataUse(data_use); |
| 538 EXPECT_TRUE(external_data_use_observer() | 441 EXPECT_TRUE(external_data_use_reporter() |
| 539 ->last_data_report_submitted_ticks_.is_null()); | 442 ->last_data_report_submitted_ticks_.is_null()); |
| 540 EXPECT_EQ(2, external_data_use_observer()->total_bytes_buffered_); | 443 EXPECT_EQ(2, external_data_use_reporter()->total_bytes_buffered_); |
| 541 EXPECT_EQ(0, external_data_use_observer()->pending_report_bytes_); | 444 EXPECT_EQ(0, external_data_use_reporter()->pending_report_bytes_); |
| 542 | 445 |
| 543 external_data_use_observer()->OnApplicationStateChange( | 446 external_data_use_reporter()->OnApplicationStateChange( |
| 544 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); | 447 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); |
| 545 EXPECT_FALSE(external_data_use_observer() | 448 EXPECT_FALSE(external_data_use_reporter() |
| 546 ->last_data_report_submitted_ticks_.is_null()); | 449 ->last_data_report_submitted_ticks_.is_null()); |
| 547 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); | 450 EXPECT_EQ(0, external_data_use_reporter()->total_bytes_buffered_); |
| 548 EXPECT_EQ(2, external_data_use_observer()->pending_report_bytes_); | 451 EXPECT_EQ(2, external_data_use_reporter()->pending_report_bytes_); |
| 549 external_data_use_observer()->OnReportDataUseDone(true); | 452 external_data_use_reporter()->OnReportDataUseDone(true); |
| 550 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 1); | 453 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 1); |
| 551 | 454 |
| 552 // Create pending report. | 455 // Create pending report. |
| 553 OnDataUse(default_data_use()); | 456 OnDataUse(default_data_use()); |
| 554 EXPECT_FALSE(external_data_use_observer() | 457 EXPECT_FALSE(external_data_use_reporter() |
| 555 ->last_data_report_submitted_ticks_.is_null()); | 458 ->last_data_report_submitted_ticks_.is_null()); |
| 556 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); | 459 EXPECT_EQ(0, external_data_use_reporter()->total_bytes_buffered_); |
| 557 EXPECT_EQ(default_upload_bytes() + default_download_bytes(), | 460 EXPECT_EQ(default_upload_bytes() + default_download_bytes(), |
| 558 external_data_use_observer()->pending_report_bytes_); | 461 external_data_use_reporter()->pending_report_bytes_); |
| 559 | 462 |
| 560 // Application state change should not submit if there is a pending report. | 463 // Application state change should not submit if there is a pending report. |
| 561 data_use.tx_bytes = 1; | 464 data_use.tx_bytes = 1; |
| 562 data_use.rx_bytes = 1; | 465 data_use.rx_bytes = 1; |
| 563 OnDataUse(data_use); | 466 OnDataUse(data_use); |
| 564 external_data_use_observer()->OnApplicationStateChange( | 467 external_data_use_reporter()->OnApplicationStateChange( |
| 565 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); | 468 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); |
| 566 EXPECT_FALSE(external_data_use_observer() | 469 EXPECT_FALSE(external_data_use_reporter() |
| 567 ->last_data_report_submitted_ticks_.is_null()); | 470 ->last_data_report_submitted_ticks_.is_null()); |
| 568 EXPECT_EQ(2, external_data_use_observer()->total_bytes_buffered_); | 471 EXPECT_EQ(2, external_data_use_reporter()->total_bytes_buffered_); |
| 569 EXPECT_EQ(default_upload_bytes() + default_download_bytes(), | 472 EXPECT_EQ(default_upload_bytes() + default_download_bytes(), |
| 570 external_data_use_observer()->pending_report_bytes_); | 473 external_data_use_reporter()->pending_report_bytes_); |
| 571 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 1); | 474 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 1); |
| 572 | 475 |
| 573 // Once pending report submission done callback was received, report should be | 476 // Once pending report submission done callback was received, report should be |
| 574 // submitted on next application state change. | 477 // submitted on next application state change. |
| 575 external_data_use_observer()->OnReportDataUseDone(true); | 478 external_data_use_reporter()->OnReportDataUseDone(true); |
| 576 external_data_use_observer()->OnApplicationStateChange( | 479 external_data_use_reporter()->OnApplicationStateChange( |
| 577 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); | 480 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); |
| 578 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); | 481 EXPECT_EQ(0, external_data_use_reporter()->total_bytes_buffered_); |
| 579 EXPECT_EQ(2, external_data_use_observer()->pending_report_bytes_); | 482 EXPECT_EQ(2, external_data_use_reporter()->pending_report_bytes_); |
| 580 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 2); | 483 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 2); |
| 581 } | 484 } |
| 582 #endif // OS_ANDROID | 485 #endif // OS_ANDROID |
| 583 | |
| 584 // Tests if the parameters from the field trial are populated correctly. | |
| 585 TEST_F(ExternalDataUseObserverTest, Variations) { | |
| 586 std::map<std::string, std::string> variation_params; | |
| 587 | |
| 588 const int kFetchMatchingRulesDurationSeconds = 10000; | |
| 589 const int kDefaultMaxDataReportSubmitWaitMsec = 20000; | |
| 590 const int64_t kDataUseReportMinBytes = 5000; | |
| 591 variation_params["fetch_matching_rules_duration_seconds"] = | |
| 592 base::Int64ToString(kFetchMatchingRulesDurationSeconds); | |
| 593 variation_params["data_report_submit_timeout_msec"] = | |
| 594 base::Int64ToString(kDefaultMaxDataReportSubmitWaitMsec); | |
| 595 variation_params["data_use_report_min_bytes"] = | |
| 596 base::Int64ToString(kDataUseReportMinBytes); | |
| 597 | |
| 598 // Create another ExternalDataUseObserver object. | |
| 599 ReplaceExternalDataUseObserver(variation_params); | |
| 600 EXPECT_EQ(base::TimeDelta::FromSeconds(kFetchMatchingRulesDurationSeconds), | |
| 601 external_data_use_observer()->fetch_matching_rules_duration_); | |
| 602 EXPECT_EQ( | |
| 603 base::TimeDelta::FromMilliseconds(kDefaultMaxDataReportSubmitWaitMsec), | |
| 604 external_data_use_observer()->data_report_submit_timeout_); | |
| 605 EXPECT_EQ(kDataUseReportMinBytes, | |
| 606 external_data_use_observer()->data_use_report_min_bytes_); | |
| 607 } | |
| 608 | 486 |
| 609 // Tests if the metrics are recorded correctly. | 487 // Tests if the metrics are recorded correctly. |
| 610 TEST_F(ExternalDataUseObserverTest, DataUseReportTimedOut) { | 488 TEST_F(ExternalDataUseReporterTest, DataUseReportTimedOut) { |
| 611 base::HistogramTester histogram_tester; | 489 base::HistogramTester histogram_tester; |
| 612 std::map<std::string, std::string> variation_params; | 490 std::map<std::string, std::string> variation_params; |
| 613 variation_params["data_report_submit_timeout_msec"] = "0"; | 491 variation_params["data_report_submit_timeout_msec"] = "0"; |
| 614 variation_params["data_use_report_min_bytes"] = "0"; | 492 variation_params["data_use_report_min_bytes"] = "0"; |
| 615 | 493 |
| 616 // Create another ExternalDataUseObserver object. | 494 // Create another ExternalDataUseObserver object. |
| 617 ReplaceExternalDataUseObserver(variation_params); | 495 ReplaceExternalDataUseObserver(variation_params); |
| 618 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, | 496 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, |
| 619 0); | 497 0); |
| 620 | 498 |
| 621 // Trigger the control app install, and matching rules will be fetched. | 499 // Trigger the control app install, and matching rules will be fetched. |
| 622 external_data_use_observer() | 500 data_use_tab_model()->OnControlAppInstallStateChange(true); |
| 623 ->GetDataUseTabModel() | |
| 624 ->OnControlAppInstallStateChange(true); | |
| 625 base::RunLoop().RunUntilIdle(); | 501 base::RunLoop().RunUntilIdle(); |
| 626 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, | 502 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, |
| 627 1); | 503 1); |
| 628 | 504 |
| 629 // Verify that matching rules are fetched on every navigation after the | 505 // Verify that matching rules are fetched on every navigation after the |
| 630 // control app is installed, since there are no valid rules yet. | 506 // control app is installed, since there are no valid rules yet. |
| 631 external_data_use_observer()->GetDataUseTabModel()->OnNavigationEvent( | 507 data_use_tab_model()->OnNavigationEvent( |
| 632 kDefaultTabId, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, | 508 kDefaultTabId, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, |
| 633 GURL(kDefaultURL), std::string(), nullptr); | 509 GURL(kDefaultURL), std::string(), nullptr); |
| 634 base::RunLoop().RunUntilIdle(); | 510 base::RunLoop().RunUntilIdle(); |
| 635 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, | 511 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, |
| 636 1); | 512 1); |
| 637 | 513 |
| 638 AddDefaultMatchingRule(); | 514 AddDefaultMatchingRule(); |
| 639 TriggerTabTrackingOnDefaultTab(); | 515 TriggerTabTrackingOnDefaultTab(); |
| 640 OnDataUse(default_data_use()); | 516 OnDataUse(default_data_use()); |
| 641 OnDataUse(default_data_use()); | 517 OnDataUse(default_data_use()); |
| 642 // First data use report should be marked as timed out. | 518 // First data use report should be marked as timed out. |
| 643 histogram_tester.ExpectUniqueSample( | 519 histogram_tester.ExpectUniqueSample( |
| 644 "DataUsage.ReportSubmissionResult", | 520 "DataUsage.ReportSubmissionResult", |
| 645 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); | 521 ExternalDataUseReporter::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); |
| 646 histogram_tester.ExpectUniqueSample( | 522 histogram_tester.ExpectUniqueSample( |
| 647 "DataUsage.ReportSubmission.Bytes.TimedOut", | 523 "DataUsage.ReportSubmission.Bytes.TimedOut", |
| 648 default_upload_bytes() + default_download_bytes(), 1); | 524 default_upload_bytes() + default_download_bytes(), 1); |
| 649 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 0); | 525 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 0); |
| 650 } | 526 } |
| 651 | 527 |
| 528 // Tests if the parameters from the field trial are populated correctly. |
| 529 TEST_F(ExternalDataUseReporterTest, Variations) { |
| 530 std::map<std::string, std::string> variation_params; |
| 531 |
| 532 const int kFetchMatchingRulesDurationSeconds = 10000; |
| 533 const int kDefaultMaxDataReportSubmitWaitMsec = 20000; |
| 534 const int64_t kDataUseReportMinBytes = 5000; |
| 535 variation_params["fetch_matching_rules_duration_seconds"] = |
| 536 base::Int64ToString(kFetchMatchingRulesDurationSeconds); |
| 537 variation_params["data_report_submit_timeout_msec"] = |
| 538 base::Int64ToString(kDefaultMaxDataReportSubmitWaitMsec); |
| 539 variation_params["data_use_report_min_bytes"] = |
| 540 base::Int64ToString(kDataUseReportMinBytes); |
| 541 |
| 542 // Create another ExternalDataUseObserver object. |
| 543 ReplaceExternalDataUseObserver(variation_params); |
| 544 EXPECT_EQ( |
| 545 base::TimeDelta::FromMilliseconds(kDefaultMaxDataReportSubmitWaitMsec), |
| 546 external_data_use_reporter()->data_report_submit_timeout_); |
| 547 EXPECT_EQ(kDataUseReportMinBytes, |
| 548 external_data_use_reporter()->data_use_report_min_bytes_); |
| 549 } |
| 550 |
| 652 } // namespace android | 551 } // namespace android |
| 653 | 552 |
| 654 } // namespace chrome | 553 } // namespace chrome |
| OLD | NEW |