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 15 matching lines...) Expand all Loading... |
26 #include "components/sessions/core/session_id.h" | 26 #include "components/sessions/core/session_id.h" |
27 #include "components/variations/variations_associated_data.h" | 27 #include "components/variations/variations_associated_data.h" |
28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
29 #include "content/public/test/test_browser_thread_bundle.h" | 29 #include "content/public/test/test_browser_thread_bundle.h" |
30 #include "net/base/network_change_notifier.h" | 30 #include "net/base/network_change_notifier.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
32 #include "url/gurl.h" | 32 #include "url/gurl.h" |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
| 36 const char kUMAMatchingRuleFirstFetchDurationHistogram[] = |
| 37 "DataUsage.Perf.MatchingRuleFirstFetchDuration"; |
| 38 const char kUMAReportSubmissionDurationHistogram[] = |
| 39 "DataUsage.Perf.ReportSubmissionDuration"; |
| 40 |
36 const char kDefaultLabel[] = "label"; | 41 const char kDefaultLabel[] = "label"; |
37 const SessionID::id_type kDefaultTabId = 0; | 42 const SessionID::id_type kDefaultTabId = 0; |
38 const char kDefaultURL[] = "http://www.google.com/#q=abc"; | 43 const char kDefaultURL[] = "http://www.google.com/#q=abc"; |
39 const char kFooMccMnc[] = "foo_mccmnc"; | 44 const char kFooMccMnc[] = "foo_mccmnc"; |
40 const char kBarMccMnc[] = "bar_mccmnc"; | 45 const char kBarMccMnc[] = "bar_mccmnc"; |
41 const char kBazMccMnc[] = "baz_mccmnc"; | 46 const char kBazMccMnc[] = "baz_mccmnc"; |
42 const char kFooLabel[] = "foo_label"; | 47 const char kFooLabel[] = "foo_label"; |
43 const char kBarLabel[] = "bar_label"; | 48 const char kBarLabel[] = "bar_label"; |
44 | 49 |
45 } // namespace | 50 } // namespace |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 external_data_use_observer()->OnReportDataUseDone(true); | 465 external_data_use_observer()->OnReportDataUseDone(true); |
461 | 466 |
462 // Verify that metrics were updated correctly for the report that was | 467 // Verify that metrics were updated correctly for the report that was |
463 // successfully submitted. | 468 // successfully submitted. |
464 histogram_tester.ExpectUniqueSample( | 469 histogram_tester.ExpectUniqueSample( |
465 "DataUsage.ReportSubmissionResult", | 470 "DataUsage.ReportSubmissionResult", |
466 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL, 1); | 471 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL, 1); |
467 histogram_tester.ExpectUniqueSample( | 472 histogram_tester.ExpectUniqueSample( |
468 "DataUsage.ReportSubmission.Bytes.Successful", | 473 "DataUsage.ReportSubmission.Bytes.Successful", |
469 external_data_use_observer()->data_use_report_min_bytes_, 1); | 474 external_data_use_observer()->data_use_report_min_bytes_, 1); |
| 475 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 1); |
470 | 476 |
471 // Verify that metrics were updated correctly for the report that was not | 477 // Verify that metrics were updated correctly for the report that was not |
472 // successfully submitted. | 478 // successfully submitted. |
473 OnDataUse(default_data_use()); | 479 OnDataUse(default_data_use()); |
474 external_data_use_observer()->OnReportDataUseDone(false); | 480 external_data_use_observer()->OnReportDataUseDone(false); |
475 histogram_tester.ExpectTotalCount("DataUsage.ReportSubmissionResult", 2); | 481 histogram_tester.ExpectTotalCount("DataUsage.ReportSubmissionResult", 2); |
476 histogram_tester.ExpectBucketCount( | 482 histogram_tester.ExpectBucketCount( |
477 "DataUsage.ReportSubmissionResult", | 483 "DataUsage.ReportSubmissionResult", |
478 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_FAILED, 1); | 484 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_FAILED, 1); |
479 histogram_tester.ExpectUniqueSample( | 485 histogram_tester.ExpectUniqueSample( |
480 "DataUsage.ReportSubmission.Bytes.Failed", | 486 "DataUsage.ReportSubmission.Bytes.Failed", |
481 external_data_use_observer()->data_use_report_min_bytes_, 1); | 487 external_data_use_observer()->data_use_report_min_bytes_, 1); |
| 488 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 2); |
482 } | 489 } |
483 | 490 |
484 #if defined(OS_ANDROID) | 491 #if defined(OS_ANDROID) |
485 // Tests data use report submission when application status callback is called. | 492 // Tests data use report submission when application status callback is called. |
486 // Report should be submitted even if the number of bytes is less than the | 493 // Report should be submitted even if the number of bytes is less than the |
487 // threshold. Report should not be submitted if there is a pending report. | 494 // threshold. Report should not be submitted if there is a pending report. |
488 TEST_F(ExternalDataUseObserverTest, DataUseReportingOnApplicationStatusChange) { | 495 TEST_F(ExternalDataUseObserverTest, DataUseReportingOnApplicationStatusChange) { |
| 496 base::HistogramTester histogram_tester; |
489 AddDefaultMatchingRule(); | 497 AddDefaultMatchingRule(); |
490 TriggerTabTrackingOnDefaultTab(); | 498 TriggerTabTrackingOnDefaultTab(); |
491 | 499 |
492 // Report with less than threshold bytes should be reported, on application | 500 // Report with less than threshold bytes should be reported, on application |
493 // state change to background. | 501 // state change to background. |
494 data_usage::DataUse data_use = default_data_use(); | 502 data_usage::DataUse data_use = default_data_use(); |
495 data_use.tx_bytes = 1; | 503 data_use.tx_bytes = 1; |
496 data_use.rx_bytes = 1; | 504 data_use.rx_bytes = 1; |
497 OnDataUse(data_use); | 505 OnDataUse(data_use); |
498 EXPECT_TRUE(external_data_use_observer() | 506 EXPECT_TRUE(external_data_use_observer() |
499 ->last_data_report_submitted_ticks_.is_null()); | 507 ->last_data_report_submitted_ticks_.is_null()); |
500 EXPECT_EQ(2, external_data_use_observer()->total_bytes_buffered_); | 508 EXPECT_EQ(2, external_data_use_observer()->total_bytes_buffered_); |
501 EXPECT_EQ(0, external_data_use_observer()->pending_report_bytes_); | 509 EXPECT_EQ(0, external_data_use_observer()->pending_report_bytes_); |
502 | 510 |
503 external_data_use_observer()->OnApplicationStateChange( | 511 external_data_use_observer()->OnApplicationStateChange( |
504 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); | 512 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); |
505 EXPECT_FALSE(external_data_use_observer() | 513 EXPECT_FALSE(external_data_use_observer() |
506 ->last_data_report_submitted_ticks_.is_null()); | 514 ->last_data_report_submitted_ticks_.is_null()); |
507 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); | 515 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); |
508 EXPECT_EQ(2, external_data_use_observer()->pending_report_bytes_); | 516 EXPECT_EQ(2, external_data_use_observer()->pending_report_bytes_); |
509 external_data_use_observer()->OnReportDataUseDone(true); | 517 external_data_use_observer()->OnReportDataUseDone(true); |
| 518 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 1); |
510 | 519 |
511 // Create pending report. | 520 // Create pending report. |
512 OnDataUse(default_data_use()); | 521 OnDataUse(default_data_use()); |
513 EXPECT_FALSE(external_data_use_observer() | 522 EXPECT_FALSE(external_data_use_observer() |
514 ->last_data_report_submitted_ticks_.is_null()); | 523 ->last_data_report_submitted_ticks_.is_null()); |
515 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); | 524 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); |
516 EXPECT_EQ(default_upload_bytes() + default_download_bytes(), | 525 EXPECT_EQ(default_upload_bytes() + default_download_bytes(), |
517 external_data_use_observer()->pending_report_bytes_); | 526 external_data_use_observer()->pending_report_bytes_); |
518 | 527 |
519 // Application state change should not submit if there is a pending report. | 528 // Application state change should not submit if there is a pending report. |
520 data_use.tx_bytes = 1; | 529 data_use.tx_bytes = 1; |
521 data_use.rx_bytes = 1; | 530 data_use.rx_bytes = 1; |
522 OnDataUse(data_use); | 531 OnDataUse(data_use); |
523 external_data_use_observer()->OnApplicationStateChange( | 532 external_data_use_observer()->OnApplicationStateChange( |
524 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); | 533 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); |
525 EXPECT_FALSE(external_data_use_observer() | 534 EXPECT_FALSE(external_data_use_observer() |
526 ->last_data_report_submitted_ticks_.is_null()); | 535 ->last_data_report_submitted_ticks_.is_null()); |
527 EXPECT_EQ(2, external_data_use_observer()->total_bytes_buffered_); | 536 EXPECT_EQ(2, external_data_use_observer()->total_bytes_buffered_); |
528 EXPECT_EQ(default_upload_bytes() + default_download_bytes(), | 537 EXPECT_EQ(default_upload_bytes() + default_download_bytes(), |
529 external_data_use_observer()->pending_report_bytes_); | 538 external_data_use_observer()->pending_report_bytes_); |
| 539 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 1); |
530 | 540 |
531 // Once pending report submission done callback was received, report should be | 541 // Once pending report submission done callback was received, report should be |
532 // submitted on next application state change. | 542 // submitted on next application state change. |
533 external_data_use_observer()->OnReportDataUseDone(true); | 543 external_data_use_observer()->OnReportDataUseDone(true); |
534 external_data_use_observer()->OnApplicationStateChange( | 544 external_data_use_observer()->OnApplicationStateChange( |
535 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); | 545 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); |
536 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); | 546 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); |
537 EXPECT_EQ(2, external_data_use_observer()->pending_report_bytes_); | 547 EXPECT_EQ(2, external_data_use_observer()->pending_report_bytes_); |
| 548 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 2); |
538 } | 549 } |
539 #endif // OS_ANDROID | 550 #endif // OS_ANDROID |
540 | 551 |
541 // Tests if the parameters from the field trial are populated correctly. | 552 // Tests if the parameters from the field trial are populated correctly. |
542 TEST_F(ExternalDataUseObserverTest, Variations) { | 553 TEST_F(ExternalDataUseObserverTest, Variations) { |
543 std::map<std::string, std::string> variation_params; | 554 std::map<std::string, std::string> variation_params; |
544 | 555 |
545 const int kFetchMatchingRulesDurationSeconds = 10000; | 556 const int kFetchMatchingRulesDurationSeconds = 10000; |
546 const int kDefaultMaxDataReportSubmitWaitMsec = 20000; | 557 const int kDefaultMaxDataReportSubmitWaitMsec = 20000; |
547 const int64_t kDataUseReportMinBytes = 5000; | 558 const int64_t kDataUseReportMinBytes = 5000; |
(...skipping 10 matching lines...) Expand all Loading... |
558 external_data_use_observer()->fetch_matching_rules_duration_); | 569 external_data_use_observer()->fetch_matching_rules_duration_); |
559 EXPECT_EQ( | 570 EXPECT_EQ( |
560 base::TimeDelta::FromMilliseconds(kDefaultMaxDataReportSubmitWaitMsec), | 571 base::TimeDelta::FromMilliseconds(kDefaultMaxDataReportSubmitWaitMsec), |
561 external_data_use_observer()->data_report_submit_timeout_); | 572 external_data_use_observer()->data_report_submit_timeout_); |
562 EXPECT_EQ(kDataUseReportMinBytes, | 573 EXPECT_EQ(kDataUseReportMinBytes, |
563 external_data_use_observer()->data_use_report_min_bytes_); | 574 external_data_use_observer()->data_use_report_min_bytes_); |
564 } | 575 } |
565 | 576 |
566 // Tests if the metrics are recorded correctly. | 577 // Tests if the metrics are recorded correctly. |
567 TEST_F(ExternalDataUseObserverTest, DataUseReportTimedOut) { | 578 TEST_F(ExternalDataUseObserverTest, DataUseReportTimedOut) { |
| 579 base::HistogramTester histogram_tester; |
568 std::map<std::string, std::string> variation_params; | 580 std::map<std::string, std::string> variation_params; |
569 variation_params["data_report_submit_timeout_msec"] = "0"; | 581 variation_params["data_report_submit_timeout_msec"] = "0"; |
570 variation_params["data_use_report_min_bytes"] = "0"; | 582 variation_params["data_use_report_min_bytes"] = "0"; |
571 | 583 |
572 // Create another ExternalDataUseObserver object. | 584 // Create another ExternalDataUseObserver object. |
573 ReplaceExternalDataUseObserver(variation_params); | 585 ReplaceExternalDataUseObserver(variation_params); |
| 586 histogram_tester.ExpectTotalCount(kUMAMatchingRuleFirstFetchDurationHistogram, |
| 587 1); |
| 588 |
574 AddDefaultMatchingRule(); | 589 AddDefaultMatchingRule(); |
575 | 590 |
576 base::HistogramTester histogram_tester; | |
577 TriggerTabTrackingOnDefaultTab(); | 591 TriggerTabTrackingOnDefaultTab(); |
578 OnDataUse(default_data_use()); | 592 OnDataUse(default_data_use()); |
579 OnDataUse(default_data_use()); | 593 OnDataUse(default_data_use()); |
580 // First data use report should be marked as timed out. | 594 // First data use report should be marked as timed out. |
581 histogram_tester.ExpectUniqueSample( | 595 histogram_tester.ExpectUniqueSample( |
582 "DataUsage.ReportSubmissionResult", | 596 "DataUsage.ReportSubmissionResult", |
583 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); | 597 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); |
584 histogram_tester.ExpectUniqueSample( | 598 histogram_tester.ExpectUniqueSample( |
585 "DataUsage.ReportSubmission.Bytes.TimedOut", | 599 "DataUsage.ReportSubmission.Bytes.TimedOut", |
586 default_upload_bytes() + default_download_bytes(), 1); | 600 default_upload_bytes() + default_download_bytes(), 1); |
| 601 histogram_tester.ExpectTotalCount(kUMAReportSubmissionDurationHistogram, 0); |
587 } | 602 } |
588 | 603 |
589 } // namespace android | 604 } // namespace android |
590 | 605 |
591 } // namespace chrome | 606 } // namespace chrome |
OLD | NEW |