1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/nqe/network_qualities_prefs_manager.h"
#include <algorithm>
#include <map>
#include <memory>
#include "base/run_loop.h"
#include "base/sequence_checker.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/values.h"
#include "net/base/network_change_notifier.h"
#include "net/nqe/effective_connection_type.h"
#include "net/nqe/network_id.h"
#include "net/nqe/network_quality_estimator_test_util.h"
#include "net/nqe/network_quality_store.h"
#include "net/test/test_with_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
class TestPrefDelegate : public NetworkQualitiesPrefsManager::PrefDelegate {
public:
TestPrefDelegate() = default;
TestPrefDelegate(const TestPrefDelegate&) = delete;
TestPrefDelegate& operator=(const TestPrefDelegate&) = delete;
~TestPrefDelegate() override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
void SetDictionaryValue(const base::Value::Dict& dict) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
write_count_++;
value_ = dict.Clone();
ASSERT_EQ(dict.size(), value_.size());
}
base::Value::Dict GetDictionaryValue() override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
read_count_++;
return value_.Clone();
}
size_t write_count() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return write_count_;
}
size_t read_count() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return read_count_;
}
private:
// Number of times prefs were written and read, respectively..
size_t write_count_ = 0;
size_t read_count_ = 0;
// Current value of the prefs.
base::Value::Dict value_;
SEQUENCE_CHECKER(sequence_checker_);
};
using NetworkQualitiesPrefManager = TestWithTaskEnvironment;
TEST_F(NetworkQualitiesPrefManager, Write) {
// Force set the ECT to Slow 2G so that the ECT does not match the default
// ECT for the current connection type. This forces the prefs to be written
// for the current connection.
std::map<std::string, std::string> variation_params;
variation_params["force_effective_connection_type"] = "Slow-2G";
TestNetworkQualityEstimator estimator(variation_params);
auto prefs_delegate = std::make_unique<TestPrefDelegate>();
TestPrefDelegate* prefs_delegate_ptr = prefs_delegate.get();
NetworkQualitiesPrefsManager manager(std::move(prefs_delegate));
manager.InitializeOnNetworkThread(&estimator);
base::RunLoop().RunUntilIdle();
// Prefs must be read at when NetworkQualitiesPrefsManager is constructed.
EXPECT_EQ(2u, prefs_delegate_ptr->read_count());
estimator.SimulateNetworkChange(
NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test");
EXPECT_EQ(3u, prefs_delegate_ptr->write_count());
// Network quality generated from the default observation must be written.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(3u, prefs_delegate_ptr->write_count());
estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G);
// Run a request so that effective connection type is recomputed, and
// observers are notified of change in the network quality.
estimator.RunOneRequest();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(4u, prefs_delegate_ptr->write_count());
estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G);
// Run a request so that effective connection type is recomputed, and
// observers are notified of change in the network quality..
estimator.RunOneRequest();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(5u, prefs_delegate_ptr->write_count());
// Prefs should not be read again.
EXPECT_EQ(2u, prefs_delegate_ptr->read_count());
manager.ShutdownOnPrefSequence();
}
TEST_F(NetworkQualitiesPrefManager, WriteWhenMatchingExpectedECT) {
// Force set the ECT to Slow 2G so that the ECT does not match the default
// ECT for the current connection type. This forces the prefs to be written
// for the current connection.
std::map<std::string, std::string> variation_params;
variation_params["force_effective_connection_type"] = "Slow-2G";
TestNetworkQualityEstimator estimator(variation_params);
auto prefs_delegate = std::make_unique<TestPrefDelegate>();
TestPrefDelegate* prefs_delegate_ptr = prefs_delegate.get();
NetworkQualitiesPrefsManager manager(std::move(prefs_delegate));
manager.InitializeOnNetworkThread(&estimator);
base::RunLoop().RunUntilIdle();
// Prefs must be read at when NetworkQualitiesPrefsManager is constructed.
EXPECT_EQ(2u, prefs_delegate_ptr->read_count());
const nqe::internal::NetworkID network_id(
NetworkChangeNotifier::ConnectionType::CONNECTION_4G, "test", INT32_MIN);
estimator.SimulateNetworkChange(network_id.type, network_id.id);
EXPECT_EQ(3u, prefs_delegate_ptr->write_count());
// Network quality generated from the default observation must be written.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(3u, prefs_delegate_ptr->write_count());
estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G);
// Run a request so that effective connection type is recomputed, and
// observers are notified of change in the network quality.
estimator.RunOneRequest();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(4u, prefs_delegate_ptr->write_count());
estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G);
// Run a request so that effective connection type is recomputed, and
// observers are notified of change in the network quality..
estimator.RunOneRequest();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(5u, prefs_delegate_ptr->write_count());
// Prefs should not be read again.
EXPECT_EQ(2u, prefs_delegate_ptr->read_count());
EXPECT_EQ(2u, manager.ForceReadPrefsForTesting().size());
EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_3G,
manager.ForceReadPrefsForTesting()
.find(network_id)
->second.effective_connection_type());
estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_4G);
estimator.RunOneRequest();
base::RunLoop().RunUntilIdle();
// Network Quality should be persisted to disk even if it matches the typical
// quality of the network. See crbug.com/890859.
EXPECT_EQ(2u, manager.ForceReadPrefsForTesting().size());
EXPECT_EQ(1u, manager.ForceReadPrefsForTesting().count(network_id));
EXPECT_EQ(6u, prefs_delegate_ptr->write_count());
manager.ShutdownOnPrefSequence();
}
TEST_F(NetworkQualitiesPrefManager, WriteAndReadWithMultipleNetworkIDs) {
static const size_t kMaxCacheSize = 20u;
// Force set the ECT to Slow 2G so that the ECT does not match the default
// ECT for the current connection type. This forces the prefs to be written
// for the current connection.
std::map<std::string, std::string> variation_params;
variation_params["force_effective_connection_type"] = "Slow-2G";
TestNetworkQualityEstimator estimator(variation_params);
auto prefs_delegate = std::make_unique<TestPrefDelegate>();
NetworkQualitiesPrefsManager manager(std::move(prefs_delegate));
manager.InitializeOnNetworkThread(&estimator);
base::RunLoop().RunUntilIdle();
estimator.SimulateNetworkChange(
NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test");
EXPECT_EQ(2u, manager.ForceReadPrefsForTesting().size());
estimator.set_recent_effective_connection_type(
EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
// Run a request so that effective connection type is recomputed, and
// observers are notified of change in the network quality.
estimator.RunOneRequest();
base::RunLoop().RunUntilIdle();
// Verify that the observer was notified, and the updated network quality was
// written to the prefs.
EXPECT_EQ(2u, manager.ForceReadPrefsForTesting().size());
// Change the network ID.
for (size_t i = 0; i < kMaxCacheSize; ++i) {
estimator.SimulateNetworkChange(
NetworkChangeNotifier::ConnectionType::CONNECTION_2G,
"test" + base::NumberToString(i));
estimator.RunOneRequest();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(std::min(i + 3, kMaxCacheSize),
manager.ForceReadPrefsForTesting().size());
}
std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality>
read_prefs = manager.ForceReadPrefsForTesting();
// Verify the contents of the prefs.
size_t count_2g_entries = 0;
for (std::map<nqe::internal::NetworkID,
nqe::internal::CachedNetworkQuality>::const_iterator it =
read_prefs.begin();
it != read_prefs.end(); ++it) {
if (it->first.type ==
NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN) {
continue;
}
EXPECT_EQ(0u, it->first.id.find("test", 0u));
EXPECT_EQ(NetworkChangeNotifier::ConnectionType::CONNECTION_2G,
it->first.type);
EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
it->second.effective_connection_type());
++count_2g_entries;
}
// At most one entry should be for the network with connection type
// NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN.
EXPECT_LE(kMaxCacheSize - 1, count_2g_entries);
estimator.OnPrefsRead(read_prefs);
manager.ShutdownOnPrefSequence();
}
// Verifies that the prefs are cleared correctly.
TEST_F(NetworkQualitiesPrefManager, ClearPrefs) {
// Force set the ECT to Slow 2G so that the ECT does not match the default
// ECT for the current connection type. This forces the prefs to be written
// for the current connection.
std::map<std::string, std::string> variation_params;
variation_params["force_effective_connection_type"] = "Slow-2G";
TestNetworkQualityEstimator estimator(variation_params);
auto prefs_delegate = std::make_unique<TestPrefDelegate>();
NetworkQualitiesPrefsManager manager(std::move(prefs_delegate));
manager.InitializeOnNetworkThread(&estimator);
base::RunLoop().RunUntilIdle();
estimator.SimulateNetworkChange(
NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test");
EXPECT_EQ(2u, manager.ForceReadPrefsForTesting().size());
estimator.set_recent_effective_connection_type(
EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
// Run a request so that effective connection type is recomputed, and
// observers are notified of change in the network quality.
estimator.RunOneRequest();
base::RunLoop().RunUntilIdle();
// Verify that the observer was notified, and the updated network quality was
// written to the prefs.
EXPECT_EQ(2u, manager.ForceReadPrefsForTesting().size());
// Prefs must be completely cleared.
manager.ClearPrefs();
EXPECT_EQ(0u, manager.ForceReadPrefsForTesting().size());
estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G);
// Run a request so that effective connection type is recomputed, and
// observers are notified of change in the network quality.
estimator.RunOneRequest();
base::RunLoop().RunUntilIdle();
// Verify that the observer was notified, and the updated network quality was
// written to the prefs.
EXPECT_EQ(1u, manager.ForceReadPrefsForTesting().size());
manager.ShutdownOnPrefSequence();
}
} // namespace
} // namespace net
|