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
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/deferred_sequenced_task_runner.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_impl.h"
#include "chrome/browser/chrome_content_browser_client.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/network_service_util.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h"
#include "content/public/test/browser_test_utils.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#include "net/nqe/effective_connection_type.h"
#include "net/nqe/network_quality_estimator.h"
#include "services/network/network_service.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/network_quality_tracker.h"
#include "services/network/public/mojom/network_service_test.mojom.h"
namespace network {
namespace {
// Simulates a network quality change. This is only called when network service
// is running in the browser process, in which case, the network quality
// estimator lives on the network thread.
void SimulateNetworkQualityChangeOnNetworkThread(
net::EffectiveConnectionType type) {
network::NetworkService::GetNetworkServiceForTesting()
->network_quality_estimator()
->SimulateNetworkQualityChangeForTesting(type);
base::RunLoop().RunUntilIdle();
}
class TestNetworkQualityObserver
: public NetworkQualityTracker::EffectiveConnectionTypeObserver {
public:
explicit TestNetworkQualityObserver(NetworkQualityTracker* tracker)
: num_notifications_(0),
run_loop_wait_effective_connection_type_(
net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
run_loop_(std::make_unique<base::RunLoop>()),
tracker_(tracker),
effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {
tracker_->AddEffectiveConnectionTypeObserver(this);
}
TestNetworkQualityObserver(const TestNetworkQualityObserver&) = delete;
TestNetworkQualityObserver& operator=(const TestNetworkQualityObserver&) =
delete;
~TestNetworkQualityObserver() override {
tracker_->RemoveEffectiveConnectionTypeObserver(this);
}
// NetworkQualityTracker::EffectiveConnectionTypeObserver implementation:
void OnEffectiveConnectionTypeChanged(
net::EffectiveConnectionType type) override {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
net::EffectiveConnectionType queried_type =
tracker_->GetEffectiveConnectionType();
EXPECT_EQ(type, queried_type);
num_notifications_++;
effective_connection_type_ = type;
if (effective_connection_type_ != run_loop_wait_effective_connection_type_)
return;
run_loop_->Quit();
}
void WaitForNotification(
net::EffectiveConnectionType run_loop_wait_effective_connection_type) {
if (effective_connection_type_ == run_loop_wait_effective_connection_type)
return;
ASSERT_NE(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
run_loop_wait_effective_connection_type);
run_loop_wait_effective_connection_type_ =
run_loop_wait_effective_connection_type;
run_loop_->Run();
run_loop_ = std::make_unique<base::RunLoop>();
}
size_t num_notifications() const { return num_notifications_; }
net::EffectiveConnectionType effective_connection_type() const {
return effective_connection_type_;
}
base::TimeDelta http_rtt() const { return tracker_->GetHttpRTT(); }
base::TimeDelta transport_rtt() const { return tracker_->GetTransportRTT(); }
int32_t downlink_bandwidth_kbps() const {
return tracker_->GetDownstreamThroughputKbps();
}
private:
size_t num_notifications_;
net::EffectiveConnectionType run_loop_wait_effective_connection_type_;
std::unique_ptr<base::RunLoop> run_loop_;
raw_ptr<NetworkQualityTracker> tracker_;
net::EffectiveConnectionType effective_connection_type_;
};
} // namespace
class NetworkQualityTrackerBrowserTest : public InProcessBrowserTest {
public:
NetworkQualityTrackerBrowserTest() = default;
~NetworkQualityTrackerBrowserTest() override = default;
// Simulates a network quality change.
void SimulateNetworkQualityChange(net::EffectiveConnectionType type) {
if (!content::IsOutOfProcessNetworkService()) {
scoped_refptr<base::SequencedTaskRunner> task_runner =
content::GetIOThreadTaskRunner({});
if (content::IsInProcessNetworkService())
task_runner = content::GetNetworkTaskRunner();
task_runner->PostTask(
FROM_HERE,
base::BindOnce(&SimulateNetworkQualityChangeOnNetworkThread, type));
return;
}
mojo::ScopedAllowSyncCallForTesting allow_sync_call;
content::StoragePartition* partition =
browser()->profile()->GetDefaultStoragePartition();
DCHECK(partition->GetNetworkContext());
DCHECK(content::GetNetworkService());
mojo::Remote<network::mojom::NetworkServiceTest> network_service_test;
content::GetNetworkService()->BindTestInterfaceForTesting(
network_service_test.BindNewPipeAndPassReceiver());
base::RunLoop run_loop;
network_service_test->SimulateNetworkQualityChange(
type, base::BindOnce([](base::RunLoop* run_loop) { run_loop->Quit(); },
base::Unretained(&run_loop)));
run_loop.Run();
}
};
// Basic test to make sure NetworkQualityTracker is set up, and observers are
// notified.
IN_PROC_BROWSER_TEST_F(NetworkQualityTrackerBrowserTest,
NetworkQualityTracker) {
// Change the network quality to UNKNOWN to prevent any spurious
// notifications.
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
base::RunLoop().RunUntilIdle();
NetworkQualityTracker* tracker = g_browser_process->network_quality_tracker();
EXPECT_NE(nullptr, tracker);
base::RunLoop run_loop;
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_4G);
TestNetworkQualityObserver network_quality_observer(tracker);
network_quality_observer.WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_4G);
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_4G,
network_quality_observer.effective_connection_type());
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_3G);
network_quality_observer.WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_3G);
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_3G,
network_quality_observer.effective_connection_type());
base::RunLoop().RunUntilIdle();
// Verify that not too many effective connection type observations are
// received. Note that setting the effective connection type to UNKNOWN above,
// and adding the observer is racy. In some cases, the observer may receiver
// the notification about effective connection type being UNKNOWN, followed
// by other notifications.
EXPECT_LE(1u, network_quality_observer.num_notifications());
EXPECT_GE(5u, network_quality_observer.num_notifications());
// Typical RTT and downlink values when effective connection type is 3G. Taken
// from net::NetworkQualityEstimatorParams.
EXPECT_EQ(base::Milliseconds(450), network_quality_observer.http_rtt());
EXPECT_EQ(base::Milliseconds(400), network_quality_observer.transport_rtt());
EXPECT_EQ(400, network_quality_observer.downlink_bandwidth_kbps());
}
// Basic test to make sure NetworkQualityTracker is set up, and clients are
// notified as soon as they request notifications from the
// NetworkQualityEstimatorManager.
IN_PROC_BROWSER_TEST_F(NetworkQualityTrackerBrowserTest,
NetworkQualityTrackerNotifiedOnInitialization) {
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_2G);
base::RunLoop().RunUntilIdle();
NetworkQualityTracker* tracker = g_browser_process->network_quality_tracker();
EXPECT_NE(nullptr, tracker);
base::RunLoop run_loop;
TestNetworkQualityObserver network_quality_observer(tracker);
network_quality_observer.WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_2G);
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G,
network_quality_observer.effective_connection_type());
// Typical RTT and downlink values when effective connection type is 2G. Taken
// from net::NetworkQualityEstimatorParams.
EXPECT_EQ(base::Milliseconds(1800), network_quality_observer.http_rtt());
EXPECT_EQ(base::Milliseconds(1500), network_quality_observer.transport_rtt());
EXPECT_EQ(75, network_quality_observer.downlink_bandwidth_kbps());
}
// Simulates a network service crash, and ensures that network quality estimate
// manager binds to the restarted network service.
IN_PROC_BROWSER_TEST_F(NetworkQualityTrackerBrowserTest,
SimulateNetworkServiceCrash) {
// Network service is not running out of process, so cannot be crashed.
if (!content::IsOutOfProcessNetworkService())
return;
// Change the network quality to UNKNOWN to prevent any spurious
// notifications.
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
base::RunLoop().RunUntilIdle();
NetworkQualityTracker* tracker = g_browser_process->network_quality_tracker();
EXPECT_NE(nullptr, tracker);
base::RunLoop run_loop;
TestNetworkQualityObserver network_quality_observer(tracker);
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
network_quality_observer.effective_connection_type());
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_3G);
network_quality_observer.WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_3G);
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_3G,
network_quality_observer.effective_connection_type());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, network_quality_observer.num_notifications());
// Typical RTT and downlink values when effective connection type is 3G. Taken
// from net::NetworkQualityEstimatorParams.
EXPECT_EQ(base::Milliseconds(450), network_quality_observer.http_rtt());
EXPECT_EQ(base::Milliseconds(400), network_quality_observer.transport_rtt());
EXPECT_EQ(400, network_quality_observer.downlink_bandwidth_kbps());
SimulateNetworkServiceCrash();
// Flush the network interface to make sure it notices the crash.
browser()
->profile()
->GetDefaultStoragePartition()
->FlushNetworkInterfaceForTesting();
base::RunLoop().RunUntilIdle();
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_2G);
network_quality_observer.WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_2G);
EXPECT_LE(2u, network_quality_observer.num_notifications());
EXPECT_EQ(base::Milliseconds(1800), network_quality_observer.http_rtt());
EXPECT_EQ(base::Milliseconds(1500), network_quality_observer.transport_rtt());
EXPECT_EQ(75, network_quality_observer.downlink_bandwidth_kbps());
}
} // namespace network
|