OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_quality_estimator.h" | 5 #include "net/nqe/network_quality_estimator.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 std::unique_ptr<test_server::HttpResponse> HandleRequest( | 109 std::unique_ptr<test_server::HttpResponse> HandleRequest( |
110 const test_server::HttpRequest& request) { | 110 const test_server::HttpRequest& request) { |
111 std::unique_ptr<test_server::BasicHttpResponse> http_response( | 111 std::unique_ptr<test_server::BasicHttpResponse> http_response( |
112 new test_server::BasicHttpResponse()); | 112 new test_server::BasicHttpResponse()); |
113 http_response->set_code(HTTP_OK); | 113 http_response->set_code(HTTP_OK); |
114 http_response->set_content("hello"); | 114 http_response->set_content("hello"); |
115 http_response->set_content_type("text/plain"); | 115 http_response->set_content_type("text/plain"); |
116 return std::move(http_response); | 116 return std::move(http_response); |
117 } | 117 } |
118 | 118 |
| 119 // Runs one URL request to completion. |
| 120 void RunOneRequest() { |
| 121 TestDelegate test_delegate; |
| 122 TestURLRequestContext context(true); |
| 123 context.set_network_quality_estimator(this); |
| 124 context.Init(); |
| 125 std::unique_ptr<URLRequest> request( |
| 126 context.CreateRequest(GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 127 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
| 128 request->Start(); |
| 129 base::RunLoop().Run(); |
| 130 } |
| 131 |
119 // Returns a GURL hosted at embedded test server. | 132 // Returns a GURL hosted at embedded test server. |
120 const GURL GetEchoURL() const { | 133 const GURL GetEchoURL() const { |
121 return embedded_test_server_.GetURL("/echo.html"); | 134 return embedded_test_server_.GetURL("/echo.html"); |
122 } | 135 } |
123 | 136 |
124 void set_effective_connection_type(EffectiveConnectionType type) { | 137 void set_effective_connection_type(EffectiveConnectionType type) { |
125 effective_connection_type_set_ = true; | 138 effective_connection_type_set_ = true; |
126 effective_connection_type_ = type; | 139 effective_connection_type_ = type; |
127 } | 140 } |
128 | 141 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 | 277 |
265 return NetworkQualityEstimator::GetAccuracyRecordingIntervals(); | 278 return NetworkQualityEstimator::GetAccuracyRecordingIntervals(); |
266 } | 279 } |
267 | 280 |
268 void set_rand_double(double rand_double) { rand_double_ = rand_double; } | 281 void set_rand_double(double rand_double) { rand_double_ = rand_double; } |
269 | 282 |
270 double RandDouble() const override { return rand_double_; } | 283 double RandDouble() const override { return rand_double_; } |
271 | 284 |
272 using NetworkQualityEstimator::SetTickClockForTesting; | 285 using NetworkQualityEstimator::SetTickClockForTesting; |
273 using NetworkQualityEstimator::OnConnectionTypeChanged; | 286 using NetworkQualityEstimator::OnConnectionTypeChanged; |
| 287 using NetworkQualityEstimator::NetworkQualityStoreForTesting; |
274 | 288 |
275 private: | 289 private: |
276 // NetworkQualityEstimator implementation that returns the overridden | 290 // NetworkQualityEstimator implementation that returns the overridden |
277 // network | 291 // network |
278 // id (instead of invoking platform APIs). | 292 // id (instead of invoking platform APIs). |
279 nqe::internal::NetworkID GetCurrentNetworkID() const override { | 293 nqe::internal::NetworkID GetCurrentNetworkID() const override { |
280 return nqe::internal::NetworkID(current_network_type_, current_network_id_); | 294 return nqe::internal::NetworkID(current_network_type_, current_network_id_); |
281 } | 295 } |
282 | 296 |
283 bool effective_connection_type_set_; | 297 bool effective_connection_type_set_; |
(...skipping 1926 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2210 (buckets.at(0).min >> kBitsPerMetric >> kBitsPerMetric) % 128); | 2224 (buckets.at(0).min >> kBitsPerMetric >> kBitsPerMetric) % 128); |
2211 | 2225 |
2212 // Get the bits at index 18-24 which contain the resource fetch time. | 2226 // Get the bits at index 18-24 which contain the resource fetch time. |
2213 EXPECT_LE(0, (buckets.at(0).min >> kBitsPerMetric) % 128); | 2227 EXPECT_LE(0, (buckets.at(0).min >> kBitsPerMetric) % 128); |
2214 | 2228 |
2215 // Get the bits at index 25-31 which contain the resource load size. | 2229 // Get the bits at index 25-31 which contain the resource load size. |
2216 EXPECT_LE(0, (buckets.at(0).min) % 128); | 2230 EXPECT_LE(0, (buckets.at(0).min) % 128); |
2217 } | 2231 } |
2218 } | 2232 } |
2219 | 2233 |
| 2234 class TestNetworkQualitiesCacheObserver |
| 2235 : public nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver { |
| 2236 public: |
| 2237 TestNetworkQualitiesCacheObserver() |
| 2238 : network_id_(net::NetworkChangeNotifier::CONNECTION_UNKNOWN, |
| 2239 std::string()), |
| 2240 notification_received_(0) {} |
| 2241 ~TestNetworkQualitiesCacheObserver() override {} |
| 2242 |
| 2243 void OnChangeInCachedNetworkQuality( |
| 2244 const nqe::internal::NetworkID& network_id, |
| 2245 const nqe::internal::CachedNetworkQuality& cached_network_quality) |
| 2246 override { |
| 2247 network_id_ = network_id; |
| 2248 notification_received_++; |
| 2249 } |
| 2250 |
| 2251 size_t get_notification_received_and_reset() { |
| 2252 size_t notification_received = notification_received_; |
| 2253 notification_received_ = 0; |
| 2254 return notification_received; |
| 2255 } |
| 2256 |
| 2257 nqe::internal::NetworkID network_id() const { return network_id_; } |
| 2258 |
| 2259 private: |
| 2260 nqe::internal::NetworkID network_id_; |
| 2261 size_t notification_received_; |
| 2262 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualitiesCacheObserver); |
| 2263 }; |
| 2264 |
| 2265 TEST(NetworkQualityEstimatorTest, CacheObserver) { |
| 2266 TestNetworkQualitiesCacheObserver observer; |
| 2267 std::map<std::string, std::string> variation_params; |
| 2268 TestNetworkQualityEstimator estimator(variation_params); |
| 2269 |
| 2270 // Add |observer| as a persistent caching observer. |
| 2271 estimator.NetworkQualityStoreForTesting()->AddNetworkQualitiesCacheObserver( |
| 2272 &observer); |
| 2273 |
| 2274 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
| 2275 estimator.SimulateNetworkChangeTo( |
| 2276 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test3g"); |
| 2277 estimator.RunOneRequest(); |
| 2278 EXPECT_EQ(1u, observer.get_notification_received_and_reset()); |
| 2279 EXPECT_EQ("test3g", observer.network_id().id); |
| 2280 |
| 2281 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); |
| 2282 estimator.SimulateNetworkChangeTo( |
| 2283 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test2g"); |
| 2284 // One notification should be received for the previous network |
| 2285 // ("test3g") right before the connection change event. The second |
| 2286 // notification should be received for the second network ("test2g"). |
| 2287 EXPECT_EQ(2u, observer.get_notification_received_and_reset()); |
| 2288 estimator.RunOneRequest(); |
| 2289 EXPECT_EQ("test2g", observer.network_id().id); |
| 2290 |
| 2291 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_4G); |
| 2292 // Start multiple requests, but there should be only one notification |
| 2293 // received, since the effective connection type does not change. |
| 2294 estimator.RunOneRequest(); |
| 2295 estimator.RunOneRequest(); |
| 2296 estimator.RunOneRequest(); |
| 2297 EXPECT_EQ(1u, observer.get_notification_received_and_reset()); |
| 2298 |
| 2299 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); |
| 2300 estimator.RunOneRequest(); |
| 2301 EXPECT_EQ(1u, observer.get_notification_received_and_reset()); |
| 2302 |
| 2303 // Remove |observer|, and it should not receive any notifications. |
| 2304 estimator.NetworkQualityStoreForTesting() |
| 2305 ->RemoveNetworkQualitiesCacheObserver(&observer); |
| 2306 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
| 2307 estimator.SimulateNetworkChangeTo( |
| 2308 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test2g"); |
| 2309 EXPECT_EQ(0u, observer.get_notification_received_and_reset()); |
| 2310 estimator.RunOneRequest(); |
| 2311 EXPECT_EQ(0u, observer.get_notification_received_and_reset()); |
| 2312 } |
| 2313 |
2220 } // namespace net | 2314 } // namespace net |
OLD | NEW |