Chromium Code Reviews| Index: net/base/network_quality_estimator_unittest.cc |
| diff --git a/net/base/network_quality_estimator_unittest.cc b/net/base/network_quality_estimator_unittest.cc |
| index 5ec81ab440f4055e4b83d3b283343382c4c5e34f..ceca74654da262b534c28488dfe0dbe0d58d5cf4 100644 |
| --- a/net/base/network_quality_estimator_unittest.cc |
| +++ b/net/base/network_quality_estimator_unittest.cc |
| @@ -4,9 +4,9 @@ |
| #include "net/base/network_quality_estimator.h" |
| -#include "base/basictypes.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/run_loop.h" |
| +#include "base/strings/safe_sprintf.h" |
| #include "base/test/histogram_tester.h" |
| #include "base/threading/platform_thread.h" |
| #include "base/time/time.h" |
| @@ -72,7 +72,7 @@ TEST(NetworkQualityEstimatorTest, DISABLED_TestPeakKbpsFastestRTTUpdates) { |
| EXPECT_GT(network_quality.fastest_rtt_confidence, 0); |
| EXPECT_GT(network_quality.peak_throughput_kbps_confidence, 0); |
| EXPECT_GE(network_quality.fastest_rtt, request_duration); |
| - EXPECT_GT(network_quality.peak_throughput_kbps, uint32(0)); |
| + EXPECT_GT(network_quality.peak_throughput_kbps, 0U); |
| EXPECT_LE( |
| network_quality.peak_throughput_kbps, |
| min_transfer_size_in_bytes * 8.0 / request_duration.InMilliseconds()); |
| @@ -98,4 +98,104 @@ TEST(NetworkQualityEstimatorTest, DISABLED_TestPeakKbpsFastestRTTUpdates) { |
| } |
| #endif // !defined(OS_IOS) |
| +// Helps in setting the current network name. |
| +class NetworkQualityEstimatorTest : public NetworkQualityEstimator { |
|
mmenke
2015/05/28 15:25:03
This name is weird: This is the same name as you
tbansal1
2015/05/29 02:27:24
Done.
|
| + public: |
| + NetworkQualityEstimatorTest() : NetworkQualityEstimator() {} |
|
mmenke
2015/05/28 15:25:03
NetworkQualityEstimator() is not needed.
tbansal1
2015/05/29 02:27:24
Done.
|
| + |
| + // Overwrite the current network name for testing. |
| + void SetCurrentNetworkName(std::string network_name) { |
| + current_network_name_ = network_name; |
| + } |
| + |
| + std::string GetCurrentNetworkName() const override { |
|
mmenke
2015/05/28 15:25:03
Should group the overridden methods with a comment
tbansal1
2015/05/29 02:27:24
Done.
|
| + return current_network_name_; |
| + } |
| + |
| + bool ReadCachedNetworkQualityEstimate() { |
| + return NetworkQualityEstimator::ReadCachedNetworkQualityEstimate(); |
| + } |
| + |
| + uint32_t GetCacheSize() const { return cached_network_quality_.size(); } |
| + |
| + void OnConnectionTypeChanged( |
| + NetworkChangeNotifier::ConnectionType connection_type) override { |
| + NetworkQualityEstimator::OnConnectionTypeChanged(connection_type); |
| + } |
| +}; |
| + |
| +// Test if the network estimates are cached. |
| +TEST(NetworkQualityEstimatorTest, TestCaching) { |
| + NetworkQualityEstimatorTest estimator; |
| + EXPECT_EQ(0U, estimator.GetCacheSize()); |
| + |
| + uint32_t cache_size = 0; |
| + estimator.OnConnectionTypeChanged( |
| + NetworkChangeNotifier::ConnectionType::CONNECTION_2G); |
| + EXPECT_EQ(0U, estimator.GetCacheSize()); |
| + for (size_t i = 1; i <= 10; ++i) { |
| + // Calling the loop multiple times should have no effect on cache size as |
| + // the same cache entry should get updated (because the network name does |
| + // not change with multiple iterations of the loop). |
| + estimator.SetCurrentNetworkName("test-1"); |
| + estimator.OnConnectionTypeChanged( |
| + NetworkChangeNotifier::ConnectionType::CONNECTION_2G); |
| + // Network should be added to the cache exactly once. |
| + if (i == 1) |
| + ++cache_size; |
| + EXPECT_EQ(cache_size, estimator.GetCacheSize()) << i; |
| + } |
| + |
| + estimator.SetCurrentNetworkName("test-2"); |
| + |
| + for (size_t i = 1; i <= 10; ++i) { |
| + // Calling the loop multiple times should have no effect on cache size as |
| + // the same cache entry should get updated (because the network name does |
| + // not change with multiple iterations of the loop). |
| + estimator.OnConnectionTypeChanged( |
| + NetworkChangeNotifier::ConnectionType::CONNECTION_2G); |
| + // Network should be added to the cache exactly once. |
| + if (i == 1) |
| + ++cache_size; |
| + EXPECT_EQ(cache_size, estimator.GetCacheSize()) << i; |
| + } |
| + |
| + EXPECT_EQ(true, estimator.ReadCachedNetworkQualityEstimate()); |
| + // Reading quality of a network that has been seen before, should return true. |
| + estimator.SetCurrentNetworkName("test-1"); |
| + EXPECT_EQ(true, estimator.ReadCachedNetworkQualityEstimate()); |
| + |
| + // Reading quality of a network that has never been seen should return false. |
| + estimator.SetCurrentNetworkName("test-3"); |
| + EXPECT_EQ(false, estimator.ReadCachedNetworkQualityEstimate()); |
| +} |
| + |
| +// Tests if the cache size remains bounded. |
| +TEST(NetworkQualityEstimatorTest, TestCacheMaximumSize) { |
| + NetworkQualityEstimatorTest estimator; |
| + estimator.OnConnectionTypeChanged( |
| + NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); |
| + EXPECT_EQ(0U, estimator.GetCacheSize()); |
| + |
| + char network_name[20]; |
| + for (size_t i = 1; i <= 10; ++i) { |
| + base::strings::SafeSPrintf(network_name, "%d", i); |
| + estimator.SetCurrentNetworkName(network_name); |
| + estimator.OnConnectionTypeChanged( |
| + NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); |
| + } |
| + EXPECT_LT(estimator.GetCacheSize(), 10U); |
| + EXPECT_GT(estimator.GetCacheSize(), 0U); |
| + |
| + // First network should be evicted from the cache. |
| + base::strings::SafeSPrintf(network_name, "%d", 1); |
| + estimator.SetCurrentNetworkName(network_name); |
| + EXPECT_EQ(false, estimator.ReadCachedNetworkQualityEstimate()); |
| + |
| + // Last network should not be evicted from the cache. |
| + base::strings::SafeSPrintf(network_name, "%d", 10); |
| + estimator.SetCurrentNetworkName(network_name); |
| + EXPECT_EQ(true, estimator.ReadCachedNetworkQualityEstimate()); |
| +} |
| + |
| } // namespace net |