OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "net/nqe/network_qualities_prefs_manager.h" | 5 #include "net/nqe/network_qualities_prefs_manager.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/threading/thread_checker.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "net/base/network_change_notifier.h" | 14 #include "net/base/network_change_notifier.h" |
14 #include "net/nqe/effective_connection_type.h" | 15 #include "net/nqe/effective_connection_type.h" |
15 #include "net/nqe/network_quality_estimator_test_util.h" | 16 #include "net/nqe/network_quality_estimator_test_util.h" |
16 #include "net/nqe/network_quality_store.h" | 17 #include "net/nqe/network_quality_store.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 class MockPrefDelegate : public NetworkQualitiesPrefsManager::PrefDelegate { | 24 class TestPrefDelegate : public NetworkQualitiesPrefsManager::PrefDelegate { |
24 public: | 25 public: |
25 MockPrefDelegate() : write_count_(0) {} | 26 TestPrefDelegate() |
26 ~MockPrefDelegate() {} | 27 : write_count_(0), read_count_(0), value_(new base::DictionaryValue) {} |
| 28 |
| 29 ~TestPrefDelegate() override { |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); |
| 31 value_->Clear(); |
| 32 EXPECT_EQ(0U, value_->size()); |
| 33 } |
27 | 34 |
28 void SetDictionaryValue(const base::DictionaryValue& value) override { | 35 void SetDictionaryValue(const base::DictionaryValue& value) override { |
| 36 DCHECK(thread_checker_.CalledOnValidThread()); |
| 37 |
29 write_count_++; | 38 write_count_++; |
| 39 value_.reset(value.DeepCopy()); |
| 40 ASSERT_EQ(value.size(), value_->size()); |
30 } | 41 } |
31 | 42 |
32 size_t write_count() const { return write_count_; } | 43 const base::DictionaryValue& GetDictionaryValue() override { |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 |
| 46 read_count_++; |
| 47 return *(value_.get()); |
| 48 } |
| 49 |
| 50 size_t write_count() const { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); |
| 52 return write_count_; |
| 53 } |
| 54 |
| 55 size_t read_count() const { |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 57 return read_count_; |
| 58 } |
33 | 59 |
34 private: | 60 private: |
35 // Number of times prefs were written. | 61 // Number of times prefs were written and read, respectively.. |
36 mutable size_t write_count_; | 62 size_t write_count_; |
| 63 size_t read_count_; |
37 | 64 |
38 DISALLOW_COPY_AND_ASSIGN(MockPrefDelegate); | 65 // Current value of the prefs. |
| 66 std::unique_ptr<base::DictionaryValue> value_; |
| 67 |
| 68 base::ThreadChecker thread_checker_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(TestPrefDelegate); |
39 }; | 71 }; |
40 | 72 |
41 TEST(NetworkQualitiesPrefManager, Write) { | 73 TEST(NetworkQualitiesPrefManager, Write) { |
42 std::map<std::string, std::string> variation_params; | 74 std::map<std::string, std::string> variation_params; |
43 TestNetworkQualityEstimator estimator(variation_params, nullptr); | 75 TestNetworkQualityEstimator estimator(variation_params, nullptr); |
44 | 76 |
45 std::unique_ptr<MockPrefDelegate> prefs_delegate(new MockPrefDelegate()); | 77 std::unique_ptr<TestPrefDelegate> prefs_delegate(new TestPrefDelegate()); |
46 MockPrefDelegate* prefs_delegate_ptr = prefs_delegate.get(); | 78 TestPrefDelegate* prefs_delegate_ptr = prefs_delegate.get(); |
47 | 79 |
48 NetworkQualitiesPrefsManager manager(std::move(prefs_delegate)); | 80 NetworkQualitiesPrefsManager manager(std::move(prefs_delegate)); |
49 manager.InitializeOnNetworkThread(&estimator); | 81 manager.InitializeOnNetworkThread(&estimator); |
50 base::RunLoop().RunUntilIdle(); | 82 base::RunLoop().RunUntilIdle(); |
51 | 83 |
| 84 // Prefs must be read at when NetworkQualitiesPrefsManager is constructed. |
| 85 EXPECT_EQ(1u, prefs_delegate_ptr->read_count()); |
| 86 |
52 estimator.SimulateNetworkChange( | 87 estimator.SimulateNetworkChange( |
53 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test"); | 88 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test"); |
54 EXPECT_EQ(0u, prefs_delegate_ptr->write_count()); | 89 EXPECT_EQ(0u, prefs_delegate_ptr->write_count()); |
55 | 90 |
56 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); | 91 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); |
57 // Run a request so that effective connection type is recomputed, and | 92 // Run a request so that effective connection type is recomputed, and |
58 // observers are notified of change in the network quality. | 93 // observers are notified of change in the network quality. |
59 estimator.RunOneRequest(); | 94 estimator.RunOneRequest(); |
60 base::RunLoop().RunUntilIdle(); | 95 base::RunLoop().RunUntilIdle(); |
61 EXPECT_EQ(1u, prefs_delegate_ptr->write_count()); | 96 EXPECT_EQ(1u, prefs_delegate_ptr->write_count()); |
62 | 97 |
63 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | 98 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
64 // Run a request so that effective connection type is recomputed, and | 99 // Run a request so that effective connection type is recomputed, and |
65 // observers are notified of change in the network quality.. | 100 // observers are notified of change in the network quality.. |
66 estimator.RunOneRequest(); | 101 estimator.RunOneRequest(); |
67 base::RunLoop().RunUntilIdle(); | 102 base::RunLoop().RunUntilIdle(); |
68 EXPECT_EQ(2u, prefs_delegate_ptr->write_count()); | 103 EXPECT_EQ(2u, prefs_delegate_ptr->write_count()); |
69 | 104 |
| 105 // Prefs should not be read again. |
| 106 EXPECT_EQ(1u, prefs_delegate_ptr->read_count()); |
| 107 |
70 manager.ShutdownOnPrefThread(); | 108 manager.ShutdownOnPrefThread(); |
71 } | 109 } |
72 | 110 |
| 111 // Verify that the pref is not written if the network ID contains a period. |
| 112 TEST(NetworkQualitiesPrefManager, WriteWithPeriodInNetworkID) { |
| 113 std::map<std::string, std::string> variation_params; |
| 114 TestNetworkQualityEstimator estimator(variation_params, nullptr); |
| 115 |
| 116 std::unique_ptr<TestPrefDelegate> prefs_delegate(new TestPrefDelegate()); |
| 117 TestPrefDelegate* prefs_delegate_ptr = prefs_delegate.get(); |
| 118 |
| 119 NetworkQualitiesPrefsManager manager(std::move(prefs_delegate)); |
| 120 manager.InitializeOnNetworkThread(&estimator); |
| 121 base::RunLoop().RunUntilIdle(); |
| 122 |
| 123 EXPECT_EQ(1u, prefs_delegate_ptr->read_count()); |
| 124 |
| 125 estimator.SimulateNetworkChange( |
| 126 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "te.st"); |
| 127 EXPECT_EQ(0u, prefs_delegate_ptr->write_count()); |
| 128 |
| 129 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); |
| 130 // Run a request so that effective connection type is recomputed, and |
| 131 // observers are notified of change in the network quality. |
| 132 estimator.RunOneRequest(); |
| 133 base::RunLoop().RunUntilIdle(); |
| 134 EXPECT_EQ(0u, prefs_delegate_ptr->write_count()); |
| 135 |
| 136 manager.ShutdownOnPrefThread(); |
| 137 } |
| 138 |
| 139 TEST(NetworkQualitiesPrefManager, WriteAndReadWithMultipleNetworkIDs) { |
| 140 std::map<std::string, std::string> variation_params; |
| 141 TestNetworkQualityEstimator estimator(variation_params, nullptr); |
| 142 |
| 143 std::unique_ptr<TestPrefDelegate> prefs_delegate(new TestPrefDelegate()); |
| 144 |
| 145 NetworkQualitiesPrefsManager manager(std::move(prefs_delegate)); |
| 146 manager.InitializeOnNetworkThread(&estimator); |
| 147 base::RunLoop().RunUntilIdle(); |
| 148 |
| 149 estimator.SimulateNetworkChange( |
| 150 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test"); |
| 151 |
| 152 EXPECT_EQ(0u, manager.ForceReadPrefsForTesting().size()); |
| 153 |
| 154 estimator.set_recent_effective_connection_type( |
| 155 EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 156 // Run a request so that effective connection type is recomputed, and |
| 157 // observers are notified of change in the network quality. |
| 158 estimator.RunOneRequest(); |
| 159 base::RunLoop().RunUntilIdle(); |
| 160 // Verify that the observer was notified, and the updated network quality was |
| 161 // written to the prefs. |
| 162 EXPECT_EQ(1u, manager.ForceReadPrefsForTesting().size()); |
| 163 |
| 164 // Chnage the network ID. |
| 165 estimator.SimulateNetworkChange( |
| 166 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); |
| 167 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); |
| 168 estimator.RunOneRequest(); |
| 169 base::RunLoop().RunUntilIdle(); |
| 170 EXPECT_EQ(2u, manager.ForceReadPrefsForTesting().size()); |
| 171 |
| 172 estimator.SimulateNetworkChange( |
| 173 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test"); |
| 174 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
| 175 estimator.RunOneRequest(); |
| 176 base::RunLoop().RunUntilIdle(); |
| 177 EXPECT_EQ(3u, manager.ForceReadPrefsForTesting().size()); |
| 178 |
| 179 estimator.SimulateNetworkChange( |
| 180 NetworkChangeNotifier::ConnectionType::CONNECTION_4G, "test"); |
| 181 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_4G); |
| 182 estimator.RunOneRequest(); |
| 183 base::RunLoop().RunUntilIdle(); |
| 184 // Size of prefs must not exceed 3. |
| 185 EXPECT_EQ(3u, manager.ForceReadPrefsForTesting().size()); |
| 186 |
| 187 estimator.SimulateNetworkChange( |
| 188 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test"); |
| 189 estimator.set_recent_effective_connection_type( |
| 190 EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 191 estimator.RunOneRequest(); |
| 192 base::RunLoop().RunUntilIdle(); |
| 193 std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality> |
| 194 read_prefs = manager.ForceReadPrefsForTesting(); |
| 195 EXPECT_EQ(3u, read_prefs.size()); |
| 196 |
| 197 // Verify the contents of the prefs. |
| 198 for (std::map<nqe::internal::NetworkID, |
| 199 nqe::internal::CachedNetworkQuality>::const_iterator it = |
| 200 read_prefs.begin(); |
| 201 it != read_prefs.end(); ++it) { |
| 202 EXPECT_EQ("test", it->first.id); |
| 203 switch (it->first.type) { |
| 204 case NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN: |
| 205 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_SLOW_2G, |
| 206 it->second.effective_connection_type()); |
| 207 break; |
| 208 case NetworkChangeNotifier::ConnectionType::CONNECTION_2G: |
| 209 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_2G, |
| 210 it->second.effective_connection_type()); |
| 211 break; |
| 212 case NetworkChangeNotifier::ConnectionType::CONNECTION_3G: |
| 213 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_3G, |
| 214 it->second.effective_connection_type()); |
| 215 break; |
| 216 case NetworkChangeNotifier::ConnectionType::CONNECTION_4G: |
| 217 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_4G, |
| 218 it->second.effective_connection_type()); |
| 219 break; |
| 220 case NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI: |
| 221 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_SLOW_2G, |
| 222 it->second.effective_connection_type()); |
| 223 break; |
| 224 default: |
| 225 NOTREACHED(); |
| 226 } |
| 227 } |
| 228 |
| 229 manager.ShutdownOnPrefThread(); |
| 230 } |
| 231 |
73 } // namespace | 232 } // namespace |
74 | 233 |
75 } // namespace net | 234 } // namespace net |
OLD | NEW |