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_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace net { |
| 17 namespace { |
| 18 |
| 19 using Report = ReportingCache::Report; |
| 20 |
| 21 const GURL kUrl("https://origin/path"); |
| 22 const std::string kGroup("group"); |
| 23 const std::string kType("default"); |
| 24 |
| 25 class ReportingGarbageCollectorTest : public ::testing::Test { |
| 26 protected: |
| 27 ReportingGarbageCollectorTest() { |
| 28 policy_.report_max_age = base::TimeDelta::FromMinutes(15); |
| 29 policy_.report_max_attempts = 5; |
| 30 policy_.report_persist_across_network_changes = false; |
| 31 } |
| 32 |
| 33 void CollectGarbage(bool network_changed) { |
| 34 ReportingGarbageCollector::CollectGarbage( |
| 35 &cache_, policy_, clock_.NowTicks(), network_changed); |
| 36 } |
| 37 |
| 38 size_t report_count() { |
| 39 std::vector<const Report*> reports; |
| 40 cache_.GetReports(&reports); |
| 41 return reports.size(); |
| 42 } |
| 43 |
| 44 base::SimpleTestTickClock clock_; |
| 45 ReportingCache cache_; |
| 46 ReportingGarbageCollector::Policy policy_; |
| 47 }; |
| 48 |
| 49 TEST_F(ReportingGarbageCollectorTest, Report) { |
| 50 cache_.AddReport(kUrl, kGroup, kType, |
| 51 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), |
| 52 0); |
| 53 |
| 54 CollectGarbage(/* network_changed= */ false); |
| 55 EXPECT_EQ(1u, report_count()); |
| 56 } |
| 57 |
| 58 TEST_F(ReportingGarbageCollectorTest, ExpiredReport) { |
| 59 cache_.AddReport(kUrl, kGroup, kType, |
| 60 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), |
| 61 0); |
| 62 |
| 63 clock_.Advance(2 * policy_.report_max_age); |
| 64 |
| 65 CollectGarbage(/* network_changed= */ false); |
| 66 EXPECT_EQ(0u, report_count()); |
| 67 } |
| 68 |
| 69 TEST_F(ReportingGarbageCollectorTest, FailedReport) { |
| 70 cache_.AddReport(kUrl, kGroup, kType, |
| 71 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), |
| 72 0); |
| 73 |
| 74 std::vector<const Report*> reports; |
| 75 cache_.GetReports(&reports); |
| 76 for (int i = 0; i < policy_.report_max_attempts; i++) { |
| 77 cache_.IncrementReportsAttempts(reports); |
| 78 } |
| 79 |
| 80 CollectGarbage(/* network_changed= */ false); |
| 81 EXPECT_EQ(0u, report_count()); |
| 82 } |
| 83 |
| 84 TEST_F(ReportingGarbageCollectorTest, NetworkChangePersist) { |
| 85 policy_.report_persist_across_network_changes = true; |
| 86 |
| 87 cache_.AddReport(kUrl, kGroup, kType, |
| 88 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), |
| 89 0); |
| 90 |
| 91 CollectGarbage(/* network_changed= */ true); |
| 92 EXPECT_EQ(1u, report_count()); |
| 93 } |
| 94 |
| 95 TEST_F(ReportingGarbageCollectorTest, NetworkChangeClear) { |
| 96 policy_.report_persist_across_network_changes = false; |
| 97 |
| 98 cache_.AddReport(kUrl, kGroup, kType, |
| 99 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), |
| 100 0); |
| 101 |
| 102 CollectGarbage(/* network_changed= */ true); |
| 103 EXPECT_EQ(0u, report_count()); |
| 104 } |
| 105 |
| 106 } // namespace |
| 107 } // namespace net |
OLD | NEW |