OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/reporting/reporting_garbage_collector.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/test/simple_test_tick_clock.h" |
| 11 #include "base/time/time.h" |
| 12 #include "net/reporting/reporting_cache.h" |
| 13 #include "net/reporting/reporting_report.h" |
| 14 #include "net/reporting/reporting_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace net { |
| 18 namespace { |
| 19 |
| 20 const GURL kUrl("https://origin/path"); |
| 21 const std::string kGroup("group"); |
| 22 const std::string kType("default"); |
| 23 |
| 24 class ReportingGarbageCollectorTest : public ::testing::Test { |
| 25 protected: |
| 26 ReportingGarbageCollectorTest() { |
| 27 policy_.report_max_age = base::TimeDelta::FromMinutes(15); |
| 28 policy_.report_max_attempts = 5; |
| 29 policy_.report_persist_across_network_changes = false; |
| 30 } |
| 31 |
| 32 void CollectGarbage(bool network_changed) { |
| 33 ReportingGarbageCollector::CollectGarbage( |
| 34 &cache_, policy_, clock_.NowTicks(), network_changed); |
| 35 } |
| 36 |
| 37 size_t report_count() { |
| 38 std::vector<const ReportingReport*> reports; |
| 39 cache_.GetReports(&reports); |
| 40 return reports.size(); |
| 41 } |
| 42 |
| 43 base::SimpleTestTickClock clock_; |
| 44 ReportingCache cache_; |
| 45 ReportingGarbageCollector::Policy policy_; |
| 46 }; |
| 47 |
| 48 TEST_F(ReportingGarbageCollectorTest, Report) { |
| 49 cache_.AddReport(kUrl, kGroup, kType, |
| 50 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), |
| 51 0); |
| 52 |
| 53 CollectGarbage(/* network_changed= */ false); |
| 54 EXPECT_EQ(1u, report_count()); |
| 55 } |
| 56 |
| 57 TEST_F(ReportingGarbageCollectorTest, ExpiredReport) { |
| 58 cache_.AddReport(kUrl, kGroup, kType, |
| 59 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), |
| 60 0); |
| 61 |
| 62 clock_.Advance(2 * policy_.report_max_age); |
| 63 |
| 64 CollectGarbage(/* network_changed= */ false); |
| 65 EXPECT_EQ(0u, report_count()); |
| 66 } |
| 67 |
| 68 TEST_F(ReportingGarbageCollectorTest, FailedReport) { |
| 69 cache_.AddReport(kUrl, kGroup, kType, |
| 70 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), |
| 71 0); |
| 72 |
| 73 std::vector<const ReportingReport*> reports; |
| 74 cache_.GetReports(&reports); |
| 75 for (int i = 0; i < policy_.report_max_attempts; i++) { |
| 76 cache_.IncrementReportsAttempts(reports); |
| 77 } |
| 78 |
| 79 CollectGarbage(/* network_changed= */ false); |
| 80 EXPECT_EQ(0u, report_count()); |
| 81 } |
| 82 |
| 83 TEST_F(ReportingGarbageCollectorTest, NetworkChangePersist) { |
| 84 policy_.report_persist_across_network_changes = true; |
| 85 |
| 86 cache_.AddReport(kUrl, kGroup, kType, |
| 87 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), |
| 88 0); |
| 89 |
| 90 CollectGarbage(/* network_changed= */ true); |
| 91 EXPECT_EQ(1u, report_count()); |
| 92 } |
| 93 |
| 94 TEST_F(ReportingGarbageCollectorTest, NetworkChangeClear) { |
| 95 policy_.report_persist_across_network_changes = false; |
| 96 |
| 97 cache_.AddReport(kUrl, kGroup, kType, |
| 98 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), |
| 99 0); |
| 100 |
| 101 CollectGarbage(/* network_changed= */ true); |
| 102 EXPECT_EQ(0u, report_count()); |
| 103 } |
| 104 |
| 105 } // namespace |
| 106 } // namespace net |
OLD | NEW |