Commit 05b557b0 authored by Steven Bennetts's avatar Steven Bennetts Committed by Commit Bot

AutoConnectNotifier: Improve logging and tighten logic

This also fixes a bug in AutoConnectNotifierTest, which has been "flaky"
but failing consistently for quite a while now.

For trivial removal of extra include in ash_test_helper.cc:
TBR=jamescook@chromium.org

Bug: 1015493
Change-Id: I50ed1f11c39c7385888ae4b310d686fbe4f38a63
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1864626
Commit-Queue: Steven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708418}
parent fa0403b2
...@@ -13,8 +13,10 @@ ...@@ -13,8 +13,10 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "chromeos/network/network_connection_handler.h" #include "chromeos/network/network_connection_handler.h"
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_state.h" #include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h" #include "chromeos/network/network_state_handler.h"
#include "chromeos/network/network_type_pattern.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/canvas_image_source.h" #include "ui/gfx/image/canvas_image_source.h"
...@@ -79,18 +81,35 @@ void AutoConnectNotifier::ConnectToNetworkRequested( ...@@ -79,18 +81,35 @@ void AutoConnectNotifier::ConnectToNetworkRequested(
void AutoConnectNotifier::NetworkConnectionStateChanged( void AutoConnectNotifier::NetworkConnectionStateChanged(
const chromeos::NetworkState* network) { const chromeos::NetworkState* network) {
// No notification should be shown unless an auto-connection is underway. // Ignore non WiFi networks completely.
if (!timer_->IsRunning()) if (!network->Matches(chromeos::NetworkTypePattern::WiFi()))
return; return;
// The notification is only shown when a connection has succeeded; if // The notification is only shown when a connection has succeeded; if
// |network| is not connected, there is nothing to do. // |network| is not connected, there is nothing to do.
if (!network->IsConnectedState()) if (!network->IsConnectedState()) {
// Clear the tracked network if it is no longer connected or connecting.
if (!network->IsConnectingState() &&
network->guid() == connected_network_guid_) {
connected_network_guid_.clear();
}
return;
}
// No notification should be shown unless an auto-connection is underway.
if (!timer_->IsRunning()) {
// Track the currently connected network.
connected_network_guid_ = network->guid();
return;
}
// Ignore NetworkConnectionStateChanged for a previously connected network.
if (network->guid() == connected_network_guid_)
return; return;
// An auto-connected network has connected successfully. Display a // An auto-connected network has connected successfully. Display a
// notification alerting the user that this has occurred. // notification alerting the user that this has occurred.
DisplayNotification(); DisplayNotification(network);
has_user_explicitly_requested_connection_ = false; has_user_explicitly_requested_connection_ = false;
} }
...@@ -125,7 +144,9 @@ void AutoConnectNotifier::OnAutoConnectedInitiated(int auto_connect_reasons) { ...@@ -125,7 +144,9 @@ void AutoConnectNotifier::OnAutoConnectedInitiated(int auto_connect_reasons) {
timer_->Start(FROM_HERE, kNetworkConnectionTimeout, base::DoNothing()); timer_->Start(FROM_HERE, kNetworkConnectionTimeout, base::DoNothing());
} }
void AutoConnectNotifier::DisplayNotification() { void AutoConnectNotifier::DisplayNotification(
const chromeos::NetworkState* network) {
NET_LOG(EVENT) << "Show AutoConnect Notification for: " << network->name();
auto notification = ash::CreateSystemNotification( auto notification = ash::CreateSystemNotification(
message_center::NotificationType::NOTIFICATION_TYPE_SIMPLE, message_center::NotificationType::NOTIFICATION_TYPE_SIMPLE,
kAutoConnectNotificationId, kAutoConnectNotificationId,
......
...@@ -48,9 +48,10 @@ class ASH_EXPORT AutoConnectNotifier ...@@ -48,9 +48,10 @@ class ASH_EXPORT AutoConnectNotifier
static const char kAutoConnectNotificationId[]; static const char kAutoConnectNotificationId[];
private: private:
void DisplayNotification(); void DisplayNotification(const chromeos::NetworkState* network);
bool has_user_explicitly_requested_connection_ = false; bool has_user_explicitly_requested_connection_ = false;
std::string connected_network_guid_;
std::unique_ptr<base::OneShotTimer> timer_; std::unique_ptr<base::OneShotTimer> timer_;
DISALLOW_COPY_AND_ASSIGN(AutoConnectNotifier); DISALLOW_COPY_AND_ASSIGN(AutoConnectNotifier);
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "chromeos/network/auto_connect_handler.h" #include "chromeos/network/auto_connect_handler.h"
#include "chromeos/network/network_cert_loader.h" #include "chromeos/network/network_cert_loader.h"
#include "chromeos/network/network_handler.h" #include "chromeos/network/network_handler.h"
#include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
#include "dbus/object_path.h" #include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h" #include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
#include "ui/message_center/message_center.h" #include "ui/message_center/message_center.h"
...@@ -44,6 +45,9 @@ class AutoConnectNotifierTest : public AshTestBase { ...@@ -44,6 +45,9 @@ class AutoConnectNotifierTest : public AshTestBase {
chromeos::shill_clients::InitializeFakes(); chromeos::shill_clients::InitializeFakes();
chromeos::NetworkHandler::Initialize(); chromeos::NetworkHandler::Initialize();
CHECK(chromeos::NetworkHandler::Get()->auto_connect_handler()); CHECK(chromeos::NetworkHandler::Get()->auto_connect_handler());
network_config_helper_ = std::make_unique<
chromeos::network_config::CrosNetworkConfigTestHelper>();
AshTestBase::SetUp(); AshTestBase::SetUp();
mock_notification_timer_ = new base::MockOneShotTimer(); mock_notification_timer_ = new base::MockOneShotTimer();
...@@ -54,13 +58,14 @@ class AutoConnectNotifierTest : public AshTestBase { ...@@ -54,13 +58,14 @@ class AutoConnectNotifierTest : public AshTestBase {
chromeos::ShillServiceClient::Get()->GetTestInterface()->AddService( chromeos::ShillServiceClient::Get()->GetTestInterface()->AddService(
kTestServicePath, kTestServiceGuid, kTestServiceName, shill::kTypeWifi, kTestServicePath, kTestServiceGuid, kTestServiceName, shill::kTypeWifi,
shill::kStateOnline, true /* visible*/); shill::kStateIdle, true /* visible*/);
// Ensure fake DBus service initialization completes. // Ensure fake DBus service initialization completes.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
void TearDown() override { void TearDown() override {
AshTestBase::TearDown(); AshTestBase::TearDown();
network_config_helper_.reset();
chromeos::NetworkHandler::Shutdown(); chromeos::NetworkHandler::Shutdown();
chromeos::shill_clients::Shutdown(); chromeos::shill_clients::Shutdown();
chromeos::NetworkCertLoader::Shutdown(); chromeos::NetworkCertLoader::Shutdown();
...@@ -88,6 +93,9 @@ class AutoConnectNotifierTest : public AshTestBase { ...@@ -88,6 +93,9 @@ class AutoConnectNotifierTest : public AshTestBase {
base::MockOneShotTimer* mock_notification_timer_; base::MockOneShotTimer* mock_notification_timer_;
private: private:
std::unique_ptr<chromeos::network_config::CrosNetworkConfigTestHelper>
network_config_helper_;
DISALLOW_COPY_AND_ASSIGN(AutoConnectNotifierTest); DISALLOW_COPY_AND_ASSIGN(AutoConnectNotifierTest);
}; };
...@@ -139,6 +147,22 @@ TEST_F(AutoConnectNotifierTest, NoConnectionBeforeTimerExpires) { ...@@ -139,6 +147,22 @@ TEST_F(AutoConnectNotifierTest, NoConnectionBeforeTimerExpires) {
EXPECT_FALSE(notification); EXPECT_FALSE(notification);
} }
TEST_F(AutoConnectNotifierTest, ConnectToConnectedNetwork) {
SuccessfullyJoinWifiNetwork();
NotifyConnectToNetworkRequested();
chromeos::NetworkHandler::Get()
->auto_connect_handler()
->NotifyAutoConnectInitiatedForTest(
chromeos::AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED);
SuccessfullyJoinWifiNetwork();
message_center::Notification* notification =
message_center::MessageCenter::Get()->FindVisibleNotificationById(
GetNotificationId());
ASSERT_FALSE(notification);
}
TEST_F(AutoConnectNotifierTest, NotificationDisplayed) { TEST_F(AutoConnectNotifierTest, NotificationDisplayed) {
NotifyConnectToNetworkRequested(); NotifyConnectToNetworkRequested();
chromeos::NetworkHandler::Get() chromeos::NetworkHandler::Get()
......
...@@ -41,7 +41,6 @@ ...@@ -41,7 +41,6 @@
#include "chromeos/audio/cras_audio_handler.h" #include "chromeos/audio/cras_audio_handler.h"
#include "chromeos/dbus/audio/cras_audio_client.h" #include "chromeos/dbus/audio/cras_audio_client.h"
#include "chromeos/dbus/power/power_policy_controller.h" #include "chromeos/dbus/power/power_policy_controller.h"
#include "chromeos/network/network_handler.h"
#include "chromeos/system/fake_statistics_provider.h" #include "chromeos/system/fake_statistics_provider.h"
#include "components/discardable_memory/public/mojom/discardable_shared_memory_manager.mojom.h" #include "components/discardable_memory/public/mojom/discardable_shared_memory_manager.mojom.h"
#include "components/prefs/testing_pref_service.h" #include "components/prefs/testing_pref_service.h"
......
...@@ -258,6 +258,8 @@ void AutoConnectHandler::NotifyAutoConnectInitiatedForTest( ...@@ -258,6 +258,8 @@ void AutoConnectHandler::NotifyAutoConnectInitiatedForTest(
} }
void AutoConnectHandler::NotifyAutoConnectInitiated(int auto_connect_reasons) { void AutoConnectHandler::NotifyAutoConnectInitiated(int auto_connect_reasons) {
NET_LOG(EVENT) << "AutoConnectInitiated ["
<< AutoConnectReasonsToString(auto_connect_reasons_) << "]";
for (auto& observer : observer_list_) for (auto& observer : observer_list_)
observer.OnAutoConnectedInitiated(auto_connect_reasons); observer.OnAutoConnectedInitiated(auto_connect_reasons);
} }
......
...@@ -226,7 +226,7 @@ void NetworkConnectionHandlerImpl::ConnectToNetwork( ...@@ -226,7 +226,7 @@ void NetworkConnectionHandlerImpl::ConnectToNetwork(
const network_handler::ErrorCallback& error_callback, const network_handler::ErrorCallback& error_callback,
bool check_error_state, bool check_error_state,
ConnectCallbackMode mode) { ConnectCallbackMode mode) {
NET_LOG_USER("ConnectToNetwork", service_path); NET_LOG_USER("ConnectToNetworkRequested", service_path);
for (auto& observer : observers_) for (auto& observer : observers_)
observer.ConnectToNetworkRequested(service_path); observer.ConnectToNetworkRequested(service_path);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h" #include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
#include "chromeos/network/network_device_handler.h" #include "chromeos/network/network_device_handler.h"
#include "chromeos/network/network_handler.h"
#include "chromeos/services/network_config/cros_network_config.h" #include "chromeos/services/network_config/cros_network_config.h"
#include "chromeos/services/network_config/in_process_instance.h" #include "chromeos/services/network_config/in_process_instance.h"
...@@ -12,16 +13,21 @@ namespace chromeos { ...@@ -12,16 +13,21 @@ namespace chromeos {
namespace network_config { namespace network_config {
CrosNetworkConfigTestHelper::CrosNetworkConfigTestHelper() { CrosNetworkConfigTestHelper::CrosNetworkConfigTestHelper() {
network_device_handler_ = if (NetworkHandler::IsInitialized()) {
chromeos::NetworkDeviceHandler::InitializeForTesting( cros_network_config_impl_ = std::make_unique<CrosNetworkConfig>();
network_state_helper_.network_state_handler()); } else {
cros_network_config_impl_ = network_state_helper_ = std::make_unique<NetworkStateTestHelper>(
std::make_unique<chromeos::network_config::CrosNetworkConfig>( false /* use_default_devices_and_services */);
network_state_helper_.network_state_handler(), network_device_handler_ =
network_device_handler_.get(), chromeos::NetworkDeviceHandler::InitializeForTesting(
/*network_configuration_handler=*/nullptr, network_state_helper_->network_state_handler());
/*network_connection_handler=*/nullptr, cros_network_config_impl_ = std::make_unique<CrosNetworkConfig>(
/*network_certificate_handler=*/nullptr); network_state_helper_->network_state_handler(),
network_device_handler_.get(),
/*network_configuration_handler=*/nullptr,
/*network_connection_handler=*/nullptr,
/*network_certificate_handler=*/nullptr);
}
OverrideInProcessInstanceForTesting(cros_network_config_impl_.get()); OverrideInProcessInstanceForTesting(cros_network_config_impl_.get());
} }
......
...@@ -28,12 +28,11 @@ class CrosNetworkConfigTestHelper { ...@@ -28,12 +28,11 @@ class CrosNetworkConfigTestHelper {
~CrosNetworkConfigTestHelper(); ~CrosNetworkConfigTestHelper();
NetworkStateTestHelper& network_state_helper() { NetworkStateTestHelper& network_state_helper() {
return network_state_helper_; return *network_state_helper_;
} }
private: private:
NetworkStateTestHelper network_state_helper_{ std::unique_ptr<NetworkStateTestHelper> network_state_helper_;
false /* use_default_devices_and_services */};
std::unique_ptr<NetworkDeviceHandler> network_device_handler_; std::unique_ptr<NetworkDeviceHandler> network_device_handler_;
std::unique_ptr<CrosNetworkConfig> cros_network_config_impl_; std::unique_ptr<CrosNetworkConfig> cros_network_config_impl_;
......
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