Chromium Code Reviews| 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_network_change_observer.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "base/test/simple_test_tick_clock.h" | |
| 11 #include "base/values.h" | |
| 12 #include "net/base/network_change_notifier.h" | |
| 13 #include "net/reporting/reporting_cache.h" | |
| 14 #include "net/reporting/reporting_client.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 ReportingNetworkChangeObserverTest : public ReportingTestBase { | |
| 23 protected: | |
| 24 void SimulateNetworkChange() { | |
| 25 // TODO: Need to SetTestNotificationsOnly(true) to keep things from flaking, | |
| 26 // but have to figure out how to do that before NCN is created or how to | |
| 27 // recreate NCN. | |
| 28 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | |
| 29 NetworkChangeNotifier::CONNECTION_NONE); | |
| 30 base::RunLoop().RunUntilIdle(); | |
| 31 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | |
| 32 NetworkChangeNotifier::CONNECTION_WIFI); | |
| 33 base::RunLoop().RunUntilIdle(); | |
| 34 } | |
| 35 | |
| 36 size_t report_count() { | |
| 37 std::vector<const ReportingReport*> reports; | |
| 38 cache()->GetReports(&reports); | |
| 39 return reports.size(); | |
| 40 } | |
| 41 | |
| 42 size_t client_count() { | |
| 43 std::vector<const ReportingClient*> clients; | |
| 44 cache()->GetClients(&clients); | |
| 45 return clients.size(); | |
| 46 } | |
| 47 | |
| 48 const GURL kUrl_ = GURL("https://origin/path"); | |
| 49 const url::Origin kOrigin_ = url::Origin(kUrl_); | |
| 50 const GURL kEndpoint_ = GURL("https://endpoint/"); | |
| 51 const std::string kGroup_ = "group"; | |
| 52 const std::string kType_ = "default"; | |
| 53 }; | |
| 54 | |
| 55 TEST_F(ReportingNetworkChangeObserverTest, ClearNothing) { | |
| 56 ReportingPolicy new_policy = policy(); | |
| 57 new_policy.clear_reports_on_network_changes = false; | |
| 58 new_policy.clear_clients_on_network_changes = false; | |
| 59 UsePolicy(new_policy); | |
| 60 | |
| 61 cache()->AddReport(kUrl_, kGroup_, kType_, | |
| 62 base::MakeUnique<base::DictionaryValue>(), | |
| 63 tick_clock()->NowTicks(), 0); | |
| 64 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | |
| 65 kGroup_, | |
| 66 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); | |
| 67 ASSERT_EQ(1u, report_count()); | |
| 68 ASSERT_EQ(1u, client_count()); | |
| 69 | |
| 70 SimulateNetworkChange(); | |
| 71 | |
| 72 EXPECT_EQ(1u, report_count()); | |
| 73 EXPECT_EQ(1u, client_count()); | |
| 74 } | |
| 75 | |
| 76 TEST_F(ReportingNetworkChangeObserverTest, ClearReports) { | |
| 77 ReportingPolicy new_policy = policy(); | |
| 78 new_policy.clear_reports_on_network_changes = true; | |
| 79 new_policy.clear_clients_on_network_changes = false; | |
| 80 UsePolicy(new_policy); | |
| 81 | |
| 82 cache()->AddReport(kUrl_, kGroup_, kType_, | |
| 83 base::MakeUnique<base::DictionaryValue>(), | |
| 84 tick_clock()->NowTicks(), 0); | |
| 85 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | |
| 86 kGroup_, | |
| 87 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); | |
| 88 | |
|
shivanisha
2017/04/20 17:39:37
ASSERT_EQ(1u, report_count());
ASSERT_EQ(1u, clien
Julia Tuttle
2017/04/20 17:53:05
Done.
| |
| 89 SimulateNetworkChange(); | |
| 90 | |
| 91 EXPECT_EQ(0u, report_count()); | |
| 92 EXPECT_EQ(1u, client_count()); | |
| 93 } | |
| 94 | |
| 95 TEST_F(ReportingNetworkChangeObserverTest, ClearClients) { | |
| 96 ReportingPolicy new_policy = policy(); | |
| 97 new_policy.clear_reports_on_network_changes = false; | |
| 98 new_policy.clear_clients_on_network_changes = true; | |
| 99 UsePolicy(new_policy); | |
| 100 | |
| 101 cache()->AddReport(kUrl_, kGroup_, kType_, | |
| 102 base::MakeUnique<base::DictionaryValue>(), | |
| 103 tick_clock()->NowTicks(), 0); | |
| 104 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | |
| 105 kGroup_, | |
| 106 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); | |
| 107 | |
|
shivanisha
2017/04/20 17:39:37
ASSERT_EQ(1u, report_count());
ASSERT_EQ(1u, clien
Julia Tuttle
2017/04/20 17:53:05
Done.
| |
| 108 SimulateNetworkChange(); | |
| 109 | |
| 110 EXPECT_EQ(1u, report_count()); | |
| 111 EXPECT_EQ(0u, client_count()); | |
| 112 } | |
| 113 | |
| 114 TEST_F(ReportingNetworkChangeObserverTest, ClearReportsAndClients) { | |
| 115 ReportingPolicy new_policy = policy(); | |
| 116 new_policy.clear_reports_on_network_changes = true; | |
| 117 new_policy.clear_clients_on_network_changes = true; | |
| 118 UsePolicy(new_policy); | |
| 119 | |
| 120 cache()->AddReport(kUrl_, kGroup_, kType_, | |
| 121 base::MakeUnique<base::DictionaryValue>(), | |
| 122 tick_clock()->NowTicks(), 0); | |
| 123 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | |
| 124 kGroup_, | |
| 125 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); | |
| 126 | |
|
shivanisha
2017/04/20 17:39:37
ASSERT_EQ(1u, report_count());
ASSERT_EQ(1u, clien
Julia Tuttle
2017/04/20 17:53:05
Done.
| |
| 127 SimulateNetworkChange(); | |
| 128 | |
| 129 EXPECT_EQ(0u, report_count()); | |
| 130 EXPECT_EQ(0u, client_count()); | |
| 131 } | |
| 132 | |
| 133 } // namespace | |
| 134 } // namespace net | |
| OLD | NEW |