| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 // OnDataUse should trigger fetching of matching rules. | 376 // OnDataUse should trigger fetching of matching rules. |
| 377 OnDataUse(default_data_use()); | 377 OnDataUse(default_data_use()); |
| 378 | 378 |
| 379 // Matching rules should not be expired. | 379 // Matching rules should not be expired. |
| 380 EXPECT_LT(base::TimeTicks::Now() - | 380 EXPECT_LT(base::TimeTicks::Now() - |
| 381 external_data_use_observer()->last_matching_rules_fetch_time_, | 381 external_data_use_observer()->last_matching_rules_fetch_time_, |
| 382 external_data_use_observer()->fetch_matching_rules_duration_); | 382 external_data_use_observer()->fetch_matching_rules_duration_); |
| 383 } | 383 } |
| 384 | 384 |
| 385 // Tests the matching rule fetch behavior when the external control app is | 385 // Tests the matching rule fetch behavior when the external control app is |
| 386 // installed and not installed. If control app is installed and no valid rules | 386 // installed and not installed. Matching rules should be fetched when control |
| 387 // are found, matching rules are fetched on every navigation. Rules are not | 387 // app gets installed. If control app is installed and no valid rules are found, |
| 388 // fetched if control app is not installed or if more than zero valid rules | 388 // matching rules are fetched on every navigation. Rules are not fetched if |
| 389 // have been fetched. | 389 // control app is not installed or if more than zero valid rules have been |
| 390 // fetched. |
| 390 TEST_F(ExternalDataUseObserverTest, MatchingRuleFetchOnControlAppInstall) { | 391 TEST_F(ExternalDataUseObserverTest, MatchingRuleFetchOnControlAppInstall) { |
| 391 // Matching rules not fetched on navigation if control app is not installed. | 392 // Matching rules not fetched on navigation if control app is not installed. |
| 392 external_data_use_observer()->last_matching_rules_fetch_time_ = | 393 external_data_use_observer()->last_matching_rules_fetch_time_ = |
| 393 base::TimeTicks(); | 394 base::TimeTicks(); |
| 394 external_data_use_observer()->data_use_tab_model_->is_control_app_installed_ = | 395 external_data_use_observer()->data_use_tab_model_->is_control_app_installed_ = |
| 395 false; | 396 false; |
| 396 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( | 397 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( |
| 397 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), | 398 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), |
| 398 std::string()); | 399 std::string()); |
| 399 EXPECT_TRUE( | 400 EXPECT_TRUE( |
| 400 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); | 401 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); |
| 401 | 402 |
| 403 // Matching rules are fetched when control app is installed. |
| 404 external_data_use_observer() |
| 405 ->data_use_tab_model_->OnControlAppInstallStateChange(true); |
| 406 base::RunLoop().RunUntilIdle(); |
| 407 EXPECT_FALSE( |
| 408 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); |
| 409 |
| 402 // Matching rules fetched on every navigation if control app is installed and | 410 // Matching rules fetched on every navigation if control app is installed and |
| 403 // zero rules are available. | 411 // zero rules are available. |
| 404 external_data_use_observer() | 412 external_data_use_observer()->last_matching_rules_fetch_time_ = |
| 405 ->data_use_tab_model_->OnControlAppInstallStateChange(true); | 413 base::TimeTicks(); |
| 406 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( | 414 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( |
| 407 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), | 415 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), |
| 408 std::string()); | 416 std::string()); |
| 409 base::RunLoop().RunUntilIdle(); | 417 base::RunLoop().RunUntilIdle(); |
| 410 EXPECT_FALSE( | 418 EXPECT_FALSE( |
| 411 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); | 419 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); |
| 412 | 420 |
| 413 // Matching rules not fetched on navigation if control app is installed and | 421 // Matching rules not fetched on navigation if control app is installed and |
| 414 // more than zero rules are available. | 422 // more than zero rules are available. |
| 415 AddDefaultMatchingRule(); | 423 AddDefaultMatchingRule(); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 // Tests if the metrics are recorded correctly. | 588 // Tests if the metrics are recorded correctly. |
| 581 TEST_F(ExternalDataUseObserverTest, DataUseReportTimedOut) { | 589 TEST_F(ExternalDataUseObserverTest, DataUseReportTimedOut) { |
| 582 base::HistogramTester histogram_tester; | 590 base::HistogramTester histogram_tester; |
| 583 std::map<std::string, std::string> variation_params; | 591 std::map<std::string, std::string> variation_params; |
| 584 variation_params["data_report_submit_timeout_msec"] = "0"; | 592 variation_params["data_report_submit_timeout_msec"] = "0"; |
| 585 variation_params["data_use_report_min_bytes"] = "0"; | 593 variation_params["data_use_report_min_bytes"] = "0"; |
| 586 | 594 |
| 587 // Create another ExternalDataUseObserver object. | 595 // Create another ExternalDataUseObserver object. |
| 588 ReplaceExternalDataUseObserver(variation_params); | 596 ReplaceExternalDataUseObserver(variation_params); |
| 589 | 597 |
| 590 // Trigger the control app install. | 598 // Trigger the control app install, and matching rules will be fetched. |
| 591 external_data_use_observer() | 599 external_data_use_observer() |
| 592 ->GetDataUseTabModel() | 600 ->GetDataUseTabModel() |
| 593 ->OnControlAppInstallStateChange(true); | 601 ->OnControlAppInstallStateChange(true); |
| 602 base::RunLoop().RunUntilIdle(); |
| 594 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, | 603 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, |
| 595 0); | 604 1); |
| 596 | 605 |
| 597 // Verify that matching rules are fetched on the first navigation after the | 606 // Verify that matching rules are fetched on every navigation after the |
| 598 // control app is installed. | 607 // control app is installed, since there are no valid rules yet. |
| 599 external_data_use_observer()->GetDataUseTabModel()->OnNavigationEvent( | 608 external_data_use_observer()->GetDataUseTabModel()->OnNavigationEvent( |
| 600 kDefaultTabId, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, | 609 kDefaultTabId, DataUseTabModel::TRANSITION_OMNIBOX_SEARCH, |
| 601 GURL(kDefaultURL), std::string()); | 610 GURL(kDefaultURL), std::string()); |
| 602 base::RunLoop().RunUntilIdle(); | 611 base::RunLoop().RunUntilIdle(); |
| 603 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, | 612 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, |
| 604 1); | 613 1); |
| 605 | 614 |
| 606 AddDefaultMatchingRule(); | 615 AddDefaultMatchingRule(); |
| 607 TriggerTabTrackingOnDefaultTab(); | 616 TriggerTabTrackingOnDefaultTab(); |
| 608 OnDataUse(default_data_use()); | 617 OnDataUse(default_data_use()); |
| 609 OnDataUse(default_data_use()); | 618 OnDataUse(default_data_use()); |
| 610 // First data use report should be marked as timed out. | 619 // First data use report should be marked as timed out. |
| 611 histogram_tester.ExpectUniqueSample( | 620 histogram_tester.ExpectUniqueSample( |
| 612 "DataUsage.ReportSubmissionResult", | 621 "DataUsage.ReportSubmissionResult", |
| 613 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); | 622 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); |
| 614 histogram_tester.ExpectUniqueSample( | 623 histogram_tester.ExpectUniqueSample( |
| 615 "DataUsage.ReportSubmission.Bytes.TimedOut", | 624 "DataUsage.ReportSubmission.Bytes.TimedOut", |
| 616 default_upload_bytes() + default_download_bytes(), 1); | 625 default_upload_bytes() + default_download_bytes(), 1); |
| 617 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 0); | 626 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 0); |
| 618 } | 627 } |
| 619 | 628 |
| 620 } // namespace android | 629 } // namespace android |
| 621 | 630 |
| 622 } // namespace chrome | 631 } // namespace chrome |
| OLD | NEW |