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 <algorithm> |
7 #include <map> | 8 #include <map> |
8 #include <memory> | 9 #include <memory> |
9 | 10 |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/string_number_conversions.h" |
12 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
13 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
14 #include "base/values.h" | 16 #include "base/values.h" |
15 #include "net/base/network_change_notifier.h" | 17 #include "net/base/network_change_notifier.h" |
16 #include "net/nqe/effective_connection_type.h" | 18 #include "net/nqe/effective_connection_type.h" |
17 #include "net/nqe/network_quality_estimator_test_util.h" | 19 #include "net/nqe/network_quality_estimator_test_util.h" |
18 #include "net/nqe/network_quality_store.h" | 20 #include "net/nqe/network_quality_store.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 | 22 |
21 namespace net { | 23 namespace net { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 // Run a request so that effective connection type is recomputed, and | 131 // Run a request so that effective connection type is recomputed, and |
130 // observers are notified of change in the network quality. | 132 // observers are notified of change in the network quality. |
131 estimator.RunOneRequest(); | 133 estimator.RunOneRequest(); |
132 base::RunLoop().RunUntilIdle(); | 134 base::RunLoop().RunUntilIdle(); |
133 EXPECT_EQ(0u, prefs_delegate_ptr->write_count()); | 135 EXPECT_EQ(0u, prefs_delegate_ptr->write_count()); |
134 | 136 |
135 manager.ShutdownOnPrefThread(); | 137 manager.ShutdownOnPrefThread(); |
136 } | 138 } |
137 | 139 |
138 TEST(NetworkQualitiesPrefManager, WriteAndReadWithMultipleNetworkIDs) { | 140 TEST(NetworkQualitiesPrefManager, WriteAndReadWithMultipleNetworkIDs) { |
| 141 static const size_t kMaxCacheSize = 10u; |
139 TestNetworkQualityEstimator estimator; | 142 TestNetworkQualityEstimator estimator; |
140 | 143 |
141 std::unique_ptr<TestPrefDelegate> prefs_delegate(new TestPrefDelegate()); | 144 std::unique_ptr<TestPrefDelegate> prefs_delegate(new TestPrefDelegate()); |
142 | 145 |
143 NetworkQualitiesPrefsManager manager(std::move(prefs_delegate)); | 146 NetworkQualitiesPrefsManager manager(std::move(prefs_delegate)); |
144 manager.InitializeOnNetworkThread(&estimator); | 147 manager.InitializeOnNetworkThread(&estimator); |
145 base::RunLoop().RunUntilIdle(); | 148 base::RunLoop().RunUntilIdle(); |
146 | 149 |
147 estimator.SimulateNetworkChange( | 150 estimator.SimulateNetworkChange( |
148 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test"); | 151 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); |
149 | 152 |
150 EXPECT_EQ(0u, manager.ForceReadPrefsForTesting().size()); | 153 EXPECT_EQ(0u, manager.ForceReadPrefsForTesting().size()); |
151 | 154 |
152 estimator.set_recent_effective_connection_type( | 155 estimator.set_recent_effective_connection_type( |
153 EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | 156 EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
154 // Run a request so that effective connection type is recomputed, and | 157 // Run a request so that effective connection type is recomputed, and |
155 // observers are notified of change in the network quality. | 158 // observers are notified of change in the network quality. |
156 estimator.RunOneRequest(); | 159 estimator.RunOneRequest(); |
157 base::RunLoop().RunUntilIdle(); | 160 base::RunLoop().RunUntilIdle(); |
158 // Verify that the observer was notified, and the updated network quality was | 161 // Verify that the observer was notified, and the updated network quality was |
159 // written to the prefs. | 162 // written to the prefs. |
160 EXPECT_EQ(1u, manager.ForceReadPrefsForTesting().size()); | 163 EXPECT_EQ(1u, manager.ForceReadPrefsForTesting().size()); |
161 | 164 |
162 // Chnage the network ID. | 165 // Change the network ID. |
163 estimator.SimulateNetworkChange( | 166 for (size_t i = 0; i < kMaxCacheSize; ++i) { |
164 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); | 167 estimator.SimulateNetworkChange( |
165 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); | 168 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, |
166 estimator.RunOneRequest(); | 169 "test" + base::IntToString(i)); |
167 base::RunLoop().RunUntilIdle(); | |
168 EXPECT_EQ(2u, manager.ForceReadPrefsForTesting().size()); | |
169 | 170 |
170 estimator.SimulateNetworkChange( | 171 estimator.RunOneRequest(); |
171 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test"); | 172 base::RunLoop().RunUntilIdle(); |
172 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | |
173 estimator.RunOneRequest(); | |
174 base::RunLoop().RunUntilIdle(); | |
175 EXPECT_EQ(3u, manager.ForceReadPrefsForTesting().size()); | |
176 | 173 |
177 estimator.SimulateNetworkChange( | 174 EXPECT_EQ(std::min(i + 2, kMaxCacheSize), |
178 NetworkChangeNotifier::ConnectionType::CONNECTION_4G, "test"); | 175 manager.ForceReadPrefsForTesting().size()); |
179 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_4G); | 176 } |
180 estimator.RunOneRequest(); | |
181 base::RunLoop().RunUntilIdle(); | |
182 // Size of prefs must not exceed 3. | |
183 EXPECT_EQ(3u, manager.ForceReadPrefsForTesting().size()); | |
184 | 177 |
185 estimator.SimulateNetworkChange( | |
186 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test"); | |
187 estimator.set_recent_effective_connection_type( | |
188 EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | |
189 estimator.RunOneRequest(); | |
190 base::RunLoop().RunUntilIdle(); | |
191 std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality> | 178 std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality> |
192 read_prefs = manager.ForceReadPrefsForTesting(); | 179 read_prefs = manager.ForceReadPrefsForTesting(); |
193 EXPECT_EQ(3u, read_prefs.size()); | |
194 | 180 |
195 // Verify the contents of the prefs. | 181 // Verify the contents of the prefs. |
196 for (std::map<nqe::internal::NetworkID, | 182 for (std::map<nqe::internal::NetworkID, |
197 nqe::internal::CachedNetworkQuality>::const_iterator it = | 183 nqe::internal::CachedNetworkQuality>::const_iterator it = |
198 read_prefs.begin(); | 184 read_prefs.begin(); |
199 it != read_prefs.end(); ++it) { | 185 it != read_prefs.end(); ++it) { |
200 EXPECT_EQ("test", it->first.id); | 186 EXPECT_EQ(0u, it->first.id.find("test", 0u)); |
201 switch (it->first.type) { | 187 EXPECT_EQ(NetworkChangeNotifier::ConnectionType::CONNECTION_2G, |
202 case NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN: | 188 it->first.type); |
203 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_SLOW_2G, | 189 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_SLOW_2G, |
204 it->second.effective_connection_type()); | 190 it->second.effective_connection_type()); |
205 break; | |
206 case NetworkChangeNotifier::ConnectionType::CONNECTION_2G: | |
207 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_2G, | |
208 it->second.effective_connection_type()); | |
209 break; | |
210 case NetworkChangeNotifier::ConnectionType::CONNECTION_3G: | |
211 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_3G, | |
212 it->second.effective_connection_type()); | |
213 break; | |
214 case NetworkChangeNotifier::ConnectionType::CONNECTION_4G: | |
215 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_4G, | |
216 it->second.effective_connection_type()); | |
217 break; | |
218 case NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI: | |
219 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_SLOW_2G, | |
220 it->second.effective_connection_type()); | |
221 break; | |
222 default: | |
223 NOTREACHED(); | |
224 } | |
225 } | 191 } |
226 | 192 |
227 base::HistogramTester histogram_tester; | 193 base::HistogramTester histogram_tester; |
228 estimator.OnPrefsRead(read_prefs); | 194 estimator.OnPrefsRead(read_prefs); |
229 histogram_tester.ExpectUniqueSample("NQE.Prefs.ReadSize", 3, 1); | 195 histogram_tester.ExpectUniqueSample("NQE.Prefs.ReadSize", kMaxCacheSize, 1); |
230 | 196 |
231 manager.ShutdownOnPrefThread(); | 197 manager.ShutdownOnPrefThread(); |
232 } | 198 } |
233 | 199 |
234 // Verifies that the prefs are cleared correctly. | 200 // Verifies that the prefs are cleared correctly. |
235 TEST(NetworkQualitiesPrefManager, ClearPrefs) { | 201 TEST(NetworkQualitiesPrefManager, ClearPrefs) { |
236 TestNetworkQualityEstimator estimator; | 202 TestNetworkQualityEstimator estimator; |
237 | 203 |
238 std::unique_ptr<TestPrefDelegate> prefs_delegate(new TestPrefDelegate()); | 204 std::unique_ptr<TestPrefDelegate> prefs_delegate(new TestPrefDelegate()); |
239 | 205 |
(...skipping 26 matching lines...) Expand all Loading... |
266 base::RunLoop().RunUntilIdle(); | 232 base::RunLoop().RunUntilIdle(); |
267 // Verify that the observer was notified, and the updated network quality was | 233 // Verify that the observer was notified, and the updated network quality was |
268 // written to the prefs. | 234 // written to the prefs. |
269 EXPECT_EQ(1u, manager.ForceReadPrefsForTesting().size()); | 235 EXPECT_EQ(1u, manager.ForceReadPrefsForTesting().size()); |
270 manager.ShutdownOnPrefThread(); | 236 manager.ShutdownOnPrefThread(); |
271 } | 237 } |
272 | 238 |
273 } // namespace | 239 } // namespace |
274 | 240 |
275 } // namespace net | 241 } // namespace net |
OLD | NEW |