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 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 | 368 |
369 // OnDataUse should trigger fetching of matching rules. | 369 // OnDataUse should trigger fetching of matching rules. |
370 OnDataUse(default_data_use()); | 370 OnDataUse(default_data_use()); |
371 | 371 |
372 // Matching rules should not be expired. | 372 // Matching rules should not be expired. |
373 EXPECT_LT(base::TimeTicks::Now() - | 373 EXPECT_LT(base::TimeTicks::Now() - |
374 external_data_use_observer()->last_matching_rules_fetch_time_, | 374 external_data_use_observer()->last_matching_rules_fetch_time_, |
375 external_data_use_observer()->fetch_matching_rules_duration_); | 375 external_data_use_observer()->fetch_matching_rules_duration_); |
376 } | 376 } |
377 | 377 |
378 // Tests if data use reports are sent only after the total bytes send/received | 378 // Tests if data use reports are sent only after the total bytes sent/received |
379 // across all buffered reports have reached the specified threshold. | 379 // across all buffered reports have reached the specified threshold. |
380 TEST_F(ExternalDataUseObserverTest, BufferDataUseReports) { | 380 TEST_F(ExternalDataUseObserverTest, BufferDataUseReports) { |
381 AddDefaultMatchingRule(); | 381 AddDefaultMatchingRule(); |
382 TriggerTabTrackingOnDefaultTab(); | 382 TriggerTabTrackingOnDefaultTab(); |
383 | 383 |
384 // This tests reports 1024 bytes in each loop iteration. For the test to work | 384 // This tests reports 1024 bytes in each loop iteration. For the test to work |
385 // properly, |data_use_report_min_bytes_| should be a multiple of 1024. | 385 // properly, |data_use_report_min_bytes_| should be a multiple of 1024. |
386 ASSERT_EQ(0, external_data_use_observer()->data_use_report_min_bytes_ % 1024); | 386 ASSERT_EQ(0, external_data_use_observer()->data_use_report_min_bytes_ % 1024); |
387 | 387 |
388 const size_t num_iterations = | 388 const size_t num_iterations = |
389 external_data_use_observer()->data_use_report_min_bytes_ / 1024; | 389 external_data_use_observer()->data_use_report_min_bytes_ / 1024; |
390 | 390 |
391 for (size_t i = 0; i < num_iterations; ++i) { | 391 for (size_t i = 0; i < num_iterations; ++i) { |
392 data_usage::DataUse data_use = default_data_use(); | 392 data_usage::DataUse data_use = default_data_use(); |
393 data_use.tx_bytes = 1024; | 393 data_use.tx_bytes = 1024; |
394 data_use.rx_bytes = 0; | 394 data_use.rx_bytes = 0; |
395 OnDataUse(data_use); | 395 OnDataUse(data_use); |
396 | 396 |
397 if (i != num_iterations - 1) { | 397 if (i != num_iterations - 1) { |
398 // Total buffered bytes is less than the minimum threshold. Data use | 398 // Total buffered bytes is less than the minimum threshold. Data use |
399 // report should not be send. | 399 // report should not be sent. |
400 EXPECT_TRUE(external_data_use_observer() | 400 EXPECT_TRUE(external_data_use_observer() |
401 ->last_data_report_submitted_ticks_.is_null()); | 401 ->last_data_report_submitted_ticks_.is_null()); |
402 EXPECT_EQ(static_cast<int64_t>(i + 1), | 402 EXPECT_EQ(static_cast<int64_t>(i + 1), |
403 external_data_use_observer()->total_bytes_buffered_ / 1024); | 403 external_data_use_observer()->total_bytes_buffered_ / 1024); |
404 EXPECT_EQ(0, external_data_use_observer()->pending_report_bytes_); | 404 EXPECT_EQ(0, external_data_use_observer()->pending_report_bytes_); |
405 | 405 |
406 } else { | 406 } else { |
407 // Total bytes is at least the minimum threshold. This should trigger | 407 // Total bytes is at least the minimum threshold. This should trigger |
408 // submitting of the buffered data use report. | 408 // submitting of the buffered data use report. |
409 EXPECT_FALSE(external_data_use_observer() | 409 EXPECT_FALSE(external_data_use_observer() |
(...skipping 23 matching lines...) Expand all Loading... |
433 external_data_use_observer()->OnReportDataUseDone(false); | 433 external_data_use_observer()->OnReportDataUseDone(false); |
434 histogram_tester.ExpectTotalCount("DataUsage.ReportSubmissionResult", 2); | 434 histogram_tester.ExpectTotalCount("DataUsage.ReportSubmissionResult", 2); |
435 histogram_tester.ExpectBucketCount( | 435 histogram_tester.ExpectBucketCount( |
436 "DataUsage.ReportSubmissionResult", | 436 "DataUsage.ReportSubmissionResult", |
437 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_FAILED, 1); | 437 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_FAILED, 1); |
438 histogram_tester.ExpectUniqueSample( | 438 histogram_tester.ExpectUniqueSample( |
439 "DataUsage.ReportSubmission.Bytes.Failed", | 439 "DataUsage.ReportSubmission.Bytes.Failed", |
440 external_data_use_observer()->data_use_report_min_bytes_, 1); | 440 external_data_use_observer()->data_use_report_min_bytes_, 1); |
441 } | 441 } |
442 | 442 |
| 443 #if defined(OS_ANDROID) |
| 444 // Tests data use report submission when application status callback is called. |
| 445 // Report should be submitted even if the number of bytes is less than the |
| 446 // threshold. Report should not be submitted if there is a pending report. |
| 447 TEST_F(ExternalDataUseObserverTest, DataUseReportingOnApplicationStatusChange) { |
| 448 AddDefaultMatchingRule(); |
| 449 TriggerTabTrackingOnDefaultTab(); |
| 450 |
| 451 // Report with less than threshold bytes should be reported, on application |
| 452 // state change to background. |
| 453 data_usage::DataUse data_use = default_data_use(); |
| 454 data_use.tx_bytes = 1; |
| 455 data_use.rx_bytes = 1; |
| 456 OnDataUse(data_use); |
| 457 EXPECT_TRUE(external_data_use_observer() |
| 458 ->last_data_report_submitted_ticks_.is_null()); |
| 459 EXPECT_EQ(2, external_data_use_observer()->total_bytes_buffered_); |
| 460 EXPECT_EQ(0, external_data_use_observer()->pending_report_bytes_); |
| 461 |
| 462 external_data_use_observer()->OnApplicationStateChange( |
| 463 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); |
| 464 EXPECT_FALSE(external_data_use_observer() |
| 465 ->last_data_report_submitted_ticks_.is_null()); |
| 466 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); |
| 467 EXPECT_EQ(2, external_data_use_observer()->pending_report_bytes_); |
| 468 external_data_use_observer()->OnReportDataUseDone(true); |
| 469 |
| 470 // Create pending report. |
| 471 OnDataUse(default_data_use()); |
| 472 EXPECT_FALSE(external_data_use_observer() |
| 473 ->last_data_report_submitted_ticks_.is_null()); |
| 474 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); |
| 475 EXPECT_EQ(default_upload_bytes() + default_download_bytes(), |
| 476 external_data_use_observer()->pending_report_bytes_); |
| 477 |
| 478 // Application state change should not submit if there is a pending report. |
| 479 data_use.tx_bytes = 1; |
| 480 data_use.rx_bytes = 1; |
| 481 OnDataUse(data_use); |
| 482 external_data_use_observer()->OnApplicationStateChange( |
| 483 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); |
| 484 EXPECT_FALSE(external_data_use_observer() |
| 485 ->last_data_report_submitted_ticks_.is_null()); |
| 486 EXPECT_EQ(2, external_data_use_observer()->total_bytes_buffered_); |
| 487 EXPECT_EQ(default_upload_bytes() + default_download_bytes(), |
| 488 external_data_use_observer()->pending_report_bytes_); |
| 489 |
| 490 // Once pending report submission done callback was received, report should be |
| 491 // submitted on next application state change. |
| 492 external_data_use_observer()->OnReportDataUseDone(true); |
| 493 external_data_use_observer()->OnApplicationStateChange( |
| 494 base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES); |
| 495 EXPECT_EQ(0, external_data_use_observer()->total_bytes_buffered_); |
| 496 EXPECT_EQ(2, external_data_use_observer()->pending_report_bytes_); |
| 497 } |
| 498 #endif // OS_ANDROID |
| 499 |
443 // Tests if the parameters from the field trial are populated correctly. | 500 // Tests if the parameters from the field trial are populated correctly. |
444 TEST_F(ExternalDataUseObserverTest, Variations) { | 501 TEST_F(ExternalDataUseObserverTest, Variations) { |
445 std::map<std::string, std::string> variation_params; | 502 std::map<std::string, std::string> variation_params; |
446 | 503 |
447 const int kFetchMatchingRulesDurationSeconds = 10000; | 504 const int kFetchMatchingRulesDurationSeconds = 10000; |
448 const int kDefaultMaxDataReportSubmitWaitMsec = 20000; | 505 const int kDefaultMaxDataReportSubmitWaitMsec = 20000; |
449 const int64_t kDataUseReportMinBytes = 5000; | 506 const int64_t kDataUseReportMinBytes = 5000; |
450 variation_params["fetch_matching_rules_duration_seconds"] = | 507 variation_params["fetch_matching_rules_duration_seconds"] = |
451 base::Int64ToString(kFetchMatchingRulesDurationSeconds); | 508 base::Int64ToString(kFetchMatchingRulesDurationSeconds); |
452 variation_params["data_report_submit_timeout_msec"] = | 509 variation_params["data_report_submit_timeout_msec"] = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 "DataUsage.ReportSubmissionResult", | 541 "DataUsage.ReportSubmissionResult", |
485 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); | 542 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, 1); |
486 histogram_tester.ExpectUniqueSample( | 543 histogram_tester.ExpectUniqueSample( |
487 "DataUsage.ReportSubmission.Bytes.TimedOut", | 544 "DataUsage.ReportSubmission.Bytes.TimedOut", |
488 default_upload_bytes() + default_download_bytes(), 1); | 545 default_upload_bytes() + default_download_bytes(), 1); |
489 } | 546 } |
490 | 547 |
491 } // namespace android | 548 } // namespace android |
492 | 549 |
493 } // namespace chrome | 550 } // namespace chrome |
OLD | NEW |