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 "base/timer/mock_timer.h" |
| 13 #include "net/reporting/reporting_cache.h" |
| 14 #include "net/reporting/reporting_policy.h" |
| 15 #include "net/reporting/reporting_report.h" |
| 16 #include "net/reporting/reporting_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace net { |
| 20 namespace { |
| 21 |
| 22 class ReportingGarbageCollectorTest : public ReportingTestBase { |
| 23 protected: |
| 24 size_t report_count() { |
| 25 std::vector<const ReportingReport*> reports; |
| 26 cache()->GetReports(&reports); |
| 27 return reports.size(); |
| 28 } |
| 29 |
| 30 const GURL kUrl_ = GURL("https://origin/path"); |
| 31 const std::string kGroup_ = "group"; |
| 32 const std::string kType_ = "default"; |
| 33 }; |
| 34 |
| 35 // Make sure the garbage collector is actually present in the context. |
| 36 TEST_F(ReportingGarbageCollectorTest, Created) { |
| 37 EXPECT_NE(nullptr, garbage_collector()); |
| 38 } |
| 39 |
| 40 // Make sure that the garbage collection timer is started and stopped correctly. |
| 41 TEST_F(ReportingGarbageCollectorTest, Timer) { |
| 42 EXPECT_FALSE(garbage_collection_timer()->IsRunning()); |
| 43 |
| 44 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 45 base::MakeUnique<base::DictionaryValue>(), |
| 46 tick_clock()->NowTicks(), 0); |
| 47 |
| 48 EXPECT_TRUE(garbage_collection_timer()->IsRunning()); |
| 49 |
| 50 garbage_collection_timer()->Fire(); |
| 51 |
| 52 EXPECT_FALSE(garbage_collection_timer()->IsRunning()); |
| 53 } |
| 54 |
| 55 TEST_F(ReportingGarbageCollectorTest, Report) { |
| 56 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 57 base::MakeUnique<base::DictionaryValue>(), |
| 58 tick_clock()->NowTicks(), 0); |
| 59 garbage_collection_timer()->Fire(); |
| 60 |
| 61 EXPECT_EQ(1u, report_count()); |
| 62 } |
| 63 |
| 64 TEST_F(ReportingGarbageCollectorTest, ExpiredReport) { |
| 65 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 66 base::MakeUnique<base::DictionaryValue>(), |
| 67 tick_clock()->NowTicks(), 0); |
| 68 tick_clock()->Advance(2 * policy().max_report_age); |
| 69 garbage_collection_timer()->Fire(); |
| 70 |
| 71 EXPECT_EQ(0u, report_count()); |
| 72 } |
| 73 |
| 74 TEST_F(ReportingGarbageCollectorTest, FailedReport) { |
| 75 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 76 base::MakeUnique<base::DictionaryValue>(), |
| 77 tick_clock()->NowTicks(), 0); |
| 78 |
| 79 std::vector<const ReportingReport*> reports; |
| 80 cache()->GetReports(&reports); |
| 81 for (int i = 0; i < policy().max_report_attempts; i++) { |
| 82 cache()->IncrementReportsAttempts(reports); |
| 83 } |
| 84 |
| 85 garbage_collection_timer()->Fire(); |
| 86 |
| 87 EXPECT_EQ(0u, report_count()); |
| 88 } |
| 89 |
| 90 } // namespace |
| 91 } // namespace net |
OLD | NEW |