| 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 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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. Matching rules should be fetched when control | 386 // installed and not installed. Matching rules should be fetched when control |
| 387 // app gets installed. If control app is installed and no valid rules are found, | 387 // app gets installed. If control app is installed and no valid rules are found, |
| 388 // matching rules are fetched on every navigation. Rules are not fetched if | 388 // matching rules are fetched on every navigation. Rules are not fetched if |
| 389 // control app is not installed or if more than zero valid rules have been | 389 // control app is not installed or if more than zero valid rules have been |
| 390 // fetched. | 390 // fetched. |
| 391 TEST_F(ExternalDataUseObserverTest, MatchingRuleFetchOnControlAppInstall) { | 391 TEST_F(ExternalDataUseObserverTest, MatchingRuleFetchOnControlAppInstall) { |
| 392 // Matching rules not fetched on navigation if control app is not installed. | 392 { |
| 393 external_data_use_observer()->last_matching_rules_fetch_time_ = | 393 // Matching rules not fetched on navigation if control app is not installed. |
| 394 base::TimeTicks(); | 394 external_data_use_observer()->last_matching_rules_fetch_time_ = |
| 395 external_data_use_observer()->data_use_tab_model_->is_control_app_installed_ = | 395 base::TimeTicks(); |
| 396 false; | 396 external_data_use_observer() |
| 397 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( | 397 ->data_use_tab_model_->is_control_app_installed_ = false; |
| 398 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), | 398 base::HistogramTester histogram_tester; |
| 399 std::string()); | 399 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( |
| 400 EXPECT_TRUE( | 400 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), |
| 401 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); | 401 std::string()); |
| 402 base::RunLoop().RunUntilIdle(); |
| 403 histogram_tester.ExpectTotalCount("DataUsage.MatchingRulesCount.Valid", 0); |
| 404 } |
| 402 | 405 |
| 403 // Matching rules are fetched when control app is installed. | 406 { |
| 404 external_data_use_observer() | 407 // Matching rules are fetched when control app is installed. |
| 405 ->data_use_tab_model_->OnControlAppInstallStateChange(true); | 408 base::HistogramTester histogram_tester; |
| 406 base::RunLoop().RunUntilIdle(); | 409 external_data_use_observer() |
| 407 EXPECT_FALSE( | 410 ->data_use_tab_model_->OnControlAppInstallStateChange(true); |
| 408 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); | 411 base::RunLoop().RunUntilIdle(); |
| 412 histogram_tester.ExpectTotalCount("DataUsage.MatchingRulesCount.Valid", 1); |
| 413 } |
| 409 | 414 |
| 410 // Matching rules fetched on every navigation if control app is installed and | 415 { |
| 411 // zero rules are available. | 416 // Matching rules fetched on every navigation if control app is installed |
| 412 external_data_use_observer()->last_matching_rules_fetch_time_ = | 417 // and zero rules are available. |
| 413 base::TimeTicks(); | 418 external_data_use_observer()->last_matching_rules_fetch_time_ = |
| 414 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( | 419 base::TimeTicks(); |
| 415 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), | 420 base::HistogramTester histogram_tester; |
| 416 std::string()); | 421 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( |
| 417 base::RunLoop().RunUntilIdle(); | 422 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), |
| 418 EXPECT_FALSE( | 423 std::string()); |
| 419 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); | 424 base::RunLoop().RunUntilIdle(); |
| 425 histogram_tester.ExpectTotalCount("DataUsage.MatchingRulesCount.Valid", 1); |
| 426 } |
| 420 | 427 |
| 421 // Matching rules not fetched on navigation if control app is installed and | 428 { |
| 422 // more than zero rules are available. | 429 // Matching rules not fetched on navigation if control app is installed and |
| 423 AddDefaultMatchingRule(); | 430 // more than zero rules are available. |
| 424 external_data_use_observer()->last_matching_rules_fetch_time_ = | 431 AddDefaultMatchingRule(); |
| 425 base::TimeTicks(); | 432 external_data_use_observer()->last_matching_rules_fetch_time_ = |
| 426 EXPECT_TRUE( | 433 base::TimeTicks(); |
| 427 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); | 434 EXPECT_TRUE(external_data_use_observer() |
| 428 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( | 435 ->last_matching_rules_fetch_time_.is_null()); |
| 429 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), | 436 base::HistogramTester histogram_tester; |
| 430 std::string()); | 437 external_data_use_observer()->data_use_tab_model_->OnNavigationEvent( |
| 431 EXPECT_TRUE( | 438 kDefaultTabId, DataUseTabModel::TRANSITION_LINK, GURL(kDefaultURL), |
| 432 external_data_use_observer()->last_matching_rules_fetch_time_.is_null()); | 439 std::string()); |
| 440 base::RunLoop().RunUntilIdle(); |
| 441 histogram_tester.ExpectTotalCount("DataUsage.MatchingRulesCount.Valid", 0); |
| 442 } |
| 433 } | 443 } |
| 434 | 444 |
| 435 // Tests if data use reports are sent only after the total bytes sent/received | 445 // Tests if data use reports are sent only after the total bytes sent/received |
| 436 // across all buffered reports have reached the specified threshold. | 446 // across all buffered reports have reached the specified threshold. |
| 437 TEST_F(ExternalDataUseObserverTest, BufferDataUseReports) { | 447 TEST_F(ExternalDataUseObserverTest, BufferDataUseReports) { |
| 438 AddDefaultMatchingRule(); | 448 AddDefaultMatchingRule(); |
| 439 TriggerTabTrackingOnDefaultTab(); | 449 TriggerTabTrackingOnDefaultTab(); |
| 440 | 450 |
| 441 // This tests reports 1024 bytes in each loop iteration. For the test to work | 451 // This tests reports 1024 bytes in each loop iteration. For the test to work |
| 442 // properly, |data_use_report_min_bytes_| should be a multiple of 1024. | 452 // properly, |data_use_report_min_bytes_| should be a multiple of 1024. |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 | 597 |
| 588 // Tests if the metrics are recorded correctly. | 598 // Tests if the metrics are recorded correctly. |
| 589 TEST_F(ExternalDataUseObserverTest, DataUseReportTimedOut) { | 599 TEST_F(ExternalDataUseObserverTest, DataUseReportTimedOut) { |
| 590 base::HistogramTester histogram_tester; | 600 base::HistogramTester histogram_tester; |
| 591 std::map<std::string, std::string> variation_params; | 601 std::map<std::string, std::string> variation_params; |
| 592 variation_params["data_report_submit_timeout_msec"] = "0"; | 602 variation_params["data_report_submit_timeout_msec"] = "0"; |
| 593 variation_params["data_use_report_min_bytes"] = "0"; | 603 variation_params["data_use_report_min_bytes"] = "0"; |
| 594 | 604 |
| 595 // Create another ExternalDataUseObserver object. | 605 // Create another ExternalDataUseObserver object. |
| 596 ReplaceExternalDataUseObserver(variation_params); | 606 ReplaceExternalDataUseObserver(variation_params); |
| 607 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, |
| 608 0); |
| 597 | 609 |
| 598 // Trigger the control app install, and matching rules will be fetched. | 610 // Trigger the control app install, and matching rules will be fetched. |
| 599 external_data_use_observer() | 611 external_data_use_observer() |
| 600 ->GetDataUseTabModel() | 612 ->GetDataUseTabModel() |
| 601 ->OnControlAppInstallStateChange(true); | 613 ->OnControlAppInstallStateChange(true); |
| 602 base::RunLoop().RunUntilIdle(); | 614 base::RunLoop().RunUntilIdle(); |
| 603 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, | 615 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, |
| 604 1); | 616 1); |
| 605 | 617 |
| 606 // Verify that matching rules are fetched on every navigation after the | 618 // Verify that matching rules are fetched on every navigation after the |
| (...skipping 15 matching lines...) Expand all Loading... |
| 622 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); | 634 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); |
| 623 histogram_tester.ExpectUniqueSample( | 635 histogram_tester.ExpectUniqueSample( |
| 624 "DataUsage.ReportSubmission.Bytes.TimedOut", | 636 "DataUsage.ReportSubmission.Bytes.TimedOut", |
| 625 default_upload_bytes() + default_download_bytes(), 1); | 637 default_upload_bytes() + default_download_bytes(), 1); |
| 626 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 0); | 638 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 0); |
| 627 } | 639 } |
| 628 | 640 |
| 629 } // namespace android | 641 } // namespace android |
| 630 | 642 |
| 631 } // namespace chrome | 643 } // namespace chrome |
| OLD | NEW |