Commit 249e7251 authored by Robbie McElrath's avatar Robbie McElrath Committed by Commit Bot

Deflake NetworkChangeManagerClientBrowserTest.ReconnectToNetworkService

This test times out when the network connection type is already at its
expected starting value before the test checks.

Change-Id: I034dc25c883247bbf1ee11fadf8c1c7f24b11941
Reviewed-on: https://chromium-review.googlesource.com/c/1481808
Commit-Queue: Robbie McElrath <rmcelrath@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#634442}
parent fb1c4b1b
...@@ -19,14 +19,17 @@ namespace { ...@@ -19,14 +19,17 @@ namespace {
class NetObserver : public net::NetworkChangeNotifier::NetworkChangeObserver { class NetObserver : public net::NetworkChangeNotifier::NetworkChangeObserver {
public: public:
NetObserver() { net::NetworkChangeNotifier::AddNetworkChangeObserver(this); } NetObserver() {
net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
last_connection_type_ = net::NetworkChangeNotifier::GetConnectionType();
}
~NetObserver() override { ~NetObserver() override {
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
} }
void WaitForConnectionType(net::NetworkChangeNotifier::ConnectionType type) { void WaitForConnectionType(net::NetworkChangeNotifier::ConnectionType type) {
while (last_connection_type != type) { while (last_connection_type_ != type) {
run_loop_.reset(new base::RunLoop()); run_loop_.reset(new base::RunLoop());
run_loop_->Run(); run_loop_->Run();
run_loop_.reset(); run_loop_.reset();
...@@ -36,15 +39,14 @@ class NetObserver : public net::NetworkChangeNotifier::NetworkChangeObserver { ...@@ -36,15 +39,14 @@ class NetObserver : public net::NetworkChangeNotifier::NetworkChangeObserver {
// net::NetworkChangeNotifier:NetworkChangeObserver: // net::NetworkChangeNotifier:NetworkChangeObserver:
void OnNetworkChanged( void OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) override { net::NetworkChangeNotifier::ConnectionType type) override {
change_count++; change_count_++;
last_connection_type = type; last_connection_type_ = type;
if (run_loop_) if (run_loop_)
run_loop_->Quit(); run_loop_->Quit();
} }
int change_count = 0; int change_count_ = 0;
net::NetworkChangeNotifier::ConnectionType last_connection_type = net::NetworkChangeNotifier::ConnectionType last_connection_type_;
net::NetworkChangeNotifier::CONNECTION_UNKNOWN;
private: private:
std::unique_ptr<base::RunLoop> run_loop_; std::unique_ptr<base::RunLoop> run_loop_;
...@@ -53,8 +55,12 @@ class NetObserver : public net::NetworkChangeNotifier::NetworkChangeObserver { ...@@ -53,8 +55,12 @@ class NetObserver : public net::NetworkChangeNotifier::NetworkChangeObserver {
class NetworkServiceObserver class NetworkServiceObserver
: public network::NetworkConnectionTracker::NetworkConnectionObserver { : public network::NetworkConnectionTracker::NetworkConnectionObserver {
public: public:
NetworkServiceObserver() { NetworkServiceObserver() : weak_factory_(this) {
content::GetNetworkConnectionTracker()->AddNetworkConnectionObserver(this); content::GetNetworkConnectionTracker()->AddNetworkConnectionObserver(this);
content::GetNetworkConnectionTracker()->GetConnectionType(
&last_connection_type_,
base::BindOnce(&NetworkServiceObserver::OnConnectionChanged,
weak_factory_.GetWeakPtr()));
} }
~NetworkServiceObserver() override { ~NetworkServiceObserver() override {
...@@ -63,7 +69,7 @@ class NetworkServiceObserver ...@@ -63,7 +69,7 @@ class NetworkServiceObserver
} }
void WaitForConnectionType(network::mojom::ConnectionType type) { void WaitForConnectionType(network::mojom::ConnectionType type) {
while (last_connection_type != type) { while (last_connection_type_ != type) {
run_loop_.reset(new base::RunLoop()); run_loop_.reset(new base::RunLoop());
run_loop_->Run(); run_loop_->Run();
run_loop_.reset(); run_loop_.reset();
...@@ -72,18 +78,18 @@ class NetworkServiceObserver ...@@ -72,18 +78,18 @@ class NetworkServiceObserver
// network::NetworkConnectionTracker::NetworkConnectionObserver: // network::NetworkConnectionTracker::NetworkConnectionObserver:
void OnConnectionChanged(network::mojom::ConnectionType type) override { void OnConnectionChanged(network::mojom::ConnectionType type) override {
change_count++; change_count_++;
last_connection_type = type; last_connection_type_ = type;
if (run_loop_) if (run_loop_)
run_loop_->Quit(); run_loop_->Quit();
} }
int change_count = 0; int change_count_ = 0;
network::mojom::ConnectionType last_connection_type = network::mojom::ConnectionType last_connection_type_;
network::mojom::ConnectionType::CONNECTION_UNKNOWN;
private: private:
std::unique_ptr<base::RunLoop> run_loop_; std::unique_ptr<base::RunLoop> run_loop_;
base::WeakPtrFactory<NetworkServiceObserver> weak_factory_;
}; };
} // namespace } // namespace
...@@ -119,15 +125,15 @@ IN_PROC_BROWSER_TEST_F(NetworkChangeManagerClientBrowserTest, ...@@ -119,15 +125,15 @@ IN_PROC_BROWSER_TEST_F(NetworkChangeManagerClientBrowserTest,
net::NetworkChangeNotifier::CONNECTION_WIFI); net::NetworkChangeNotifier::CONNECTION_WIFI);
// NetworkChangeNotifier will send a CONNECTION_NONE notification before // NetworkChangeNotifier will send a CONNECTION_NONE notification before
// the CONNECTION_WIFI one. // the CONNECTION_WIFI one.
EXPECT_EQ(2, net_observer.change_count); EXPECT_EQ(2, net_observer.change_count_);
EXPECT_EQ(net::NetworkChangeNotifier::CONNECTION_WIFI, EXPECT_EQ(net::NetworkChangeNotifier::CONNECTION_WIFI,
net_observer.last_connection_type); net_observer.last_connection_type_);
network_service_observer.WaitForConnectionType( network_service_observer.WaitForConnectionType(
network::mojom::ConnectionType::CONNECTION_WIFI); network::mojom::ConnectionType::CONNECTION_WIFI);
EXPECT_EQ(2, network_service_observer.change_count); EXPECT_EQ(2, network_service_observer.change_count_);
EXPECT_EQ(network::mojom::ConnectionType::CONNECTION_WIFI, EXPECT_EQ(network::mojom::ConnectionType::CONNECTION_WIFI,
network_service_observer.last_connection_type); network_service_observer.last_connection_type_);
} }
// Tests that the NetworkChangeManagerClient reconnects to the network service // Tests that the NetworkChangeManagerClient reconnects to the network service
...@@ -158,15 +164,15 @@ IN_PROC_BROWSER_TEST_F(NetworkChangeManagerClientBrowserTest, ...@@ -158,15 +164,15 @@ IN_PROC_BROWSER_TEST_F(NetworkChangeManagerClientBrowserTest,
net::NetworkChangeNotifier::CONNECTION_WIFI); net::NetworkChangeNotifier::CONNECTION_WIFI);
// NetworkChangeNotifier will send a CONNECTION_NONE notification before // NetworkChangeNotifier will send a CONNECTION_NONE notification before
// the CONNECTION_WIFI one. // the CONNECTION_WIFI one.
EXPECT_EQ(2, net_observer.change_count); EXPECT_EQ(2, net_observer.change_count_);
EXPECT_EQ(net::NetworkChangeNotifier::CONNECTION_WIFI, EXPECT_EQ(net::NetworkChangeNotifier::CONNECTION_WIFI,
net_observer.last_connection_type); net_observer.last_connection_type_);
network_service_observer.WaitForConnectionType( network_service_observer.WaitForConnectionType(
network::mojom::ConnectionType::CONNECTION_WIFI); network::mojom::ConnectionType::CONNECTION_WIFI);
EXPECT_EQ(2, network_service_observer.change_count); EXPECT_EQ(2, network_service_observer.change_count_);
EXPECT_EQ(network::mojom::ConnectionType::CONNECTION_WIFI, EXPECT_EQ(network::mojom::ConnectionType::CONNECTION_WIFI,
network_service_observer.last_connection_type); network_service_observer.last_connection_type_);
} }
} // namespace chromeos } // namespace chromeos
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment