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
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <memory>
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/variations/variations_associated_data.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_test.h"
#include "net/base/features.h"
#include "net/nqe/network_quality_estimator.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/network_quality_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace {
class TestNetworkQualityObserver
: public network::NetworkQualityTracker::EffectiveConnectionTypeObserver {
public:
explicit TestNetworkQualityObserver(network::NetworkQualityTracker* tracker)
: run_loop_wait_effective_connection_type_(
net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
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);
effective_connection_type_ = type;
if (effective_connection_type_ != run_loop_wait_effective_connection_type_)
return;
if (run_loop_)
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_ = std::make_unique<base::RunLoop>();
run_loop_wait_effective_connection_type_ =
run_loop_wait_effective_connection_type;
run_loop_->Run();
run_loop_.reset();
}
private:
net::EffectiveConnectionType run_loop_wait_effective_connection_type_;
std::unique_ptr<base::RunLoop> run_loop_;
raw_ptr<network::NetworkQualityTracker> tracker_;
net::EffectiveConnectionType effective_connection_type_;
};
void CheckEffectiveConnectionType(net::EffectiveConnectionType expected) {
TestNetworkQualityObserver network_quality_observer(
g_browser_process->network_quality_tracker());
network_quality_observer.WaitForNotification(expected);
}
class NetworkQualityEstimatorBrowserTest : public InProcessBrowserTest {
public:
NetworkQualityEstimatorBrowserTest() = default;
~NetworkQualityEstimatorBrowserTest() override = default;
void SetUp() override {
// Must start listening (And get a port for the proxy) before calling
// SetUp(). Use two phase EmbeddedTestServer setup for proxy tests.
ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
InProcessBrowserTest::SetUp();
}
void SetUpOnMainThread() override {
embedded_test_server()->StartAcceptingConnections();
}
void TearDown() override {
// Need to stop this before |connection_listener_| is destroyed.
EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
InProcessBrowserTest::TearDown();
}
};
class NetworkQualityEstimatorEctCommandLineBrowserTest
: public NetworkQualityEstimatorBrowserTest {
public:
NetworkQualityEstimatorEctCommandLineBrowserTest() = default;
~NetworkQualityEstimatorEctCommandLineBrowserTest() override = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchASCII("--force-effective-connection-type",
"Slow-2G");
}
};
IN_PROC_BROWSER_TEST_F(NetworkQualityEstimatorEctCommandLineBrowserTest,
ForceECTFromCommandLine) {
CheckEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
}
class NetworkQualityEstimatorEctFieldTrialBrowserTest
: public NetworkQualityEstimatorBrowserTest {
public:
NetworkQualityEstimatorEctFieldTrialBrowserTest() = default;
~NetworkQualityEstimatorEctFieldTrialBrowserTest() override = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
variations::testing::ClearAllVariationParams();
std::map<std::string, std::string> variation_params;
variation_params["force_effective_connection_type"] = "2G";
scoped_feature_list_.InitAndEnableFeatureWithParameters(
net::features::kNetworkQualityEstimator, variation_params);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(NetworkQualityEstimatorEctFieldTrialBrowserTest,
ForceECTUsingFieldTrial) {
CheckEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_2G);
}
class NetworkQualityEstimatorEctFieldTrialAndCommandLineBrowserTest
: public NetworkQualityEstimatorEctFieldTrialBrowserTest {
public:
NetworkQualityEstimatorEctFieldTrialAndCommandLineBrowserTest() = default;
~NetworkQualityEstimatorEctFieldTrialAndCommandLineBrowserTest() override =
default;
void SetUpCommandLine(base::CommandLine* command_line) override {
NetworkQualityEstimatorEctFieldTrialBrowserTest::SetUpCommandLine(
command_line);
command_line->AppendSwitchASCII("--force-effective-connection-type",
"Slow-2G");
}
};
IN_PROC_BROWSER_TEST_F(
NetworkQualityEstimatorEctFieldTrialAndCommandLineBrowserTest,
ECTFromCommandLineOverridesFieldTrial) {
CheckEffectiveConnectionType(net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
}
} // namespace
|