summaryrefslogtreecommitdiffstats
path: root/chromeos/network
diff options
context:
space:
mode:
authorstevenjb@chromium.org <stevenjb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-21 00:19:39 +0000
committerstevenjb@chromium.org <stevenjb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-21 00:19:39 +0000
commit18cbf7dc0de1442b722304cd67524e37ce86d025 (patch)
tree38a780f6ef5879f32e717e9b098d13b6b40a424b /chromeos/network
parent2082c45d97739ca558a2710ac8c7d0c277c03ce4 (diff)
downloadchromium_src-18cbf7dc0de1442b722304cd67524e37ce86d025.zip
chromium_src-18cbf7dc0de1442b722304cd67524e37ce86d025.tar.gz
chromium_src-18cbf7dc0de1442b722304cd67524e37ce86d025.tar.bz2
Improve NetworkStateHandler API
The UI needs more flexible accessors than "ActiveNetwork," which is also a somewhat unclear term. This replaces it with DefaultNetwork and provides better accessors for the UI. BUG=none For webui: TBR=jhawkins@chromium.org Review URL: https://codereview.chromium.org/11614035 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@174288 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chromeos/network')
-rw-r--r--chromeos/network/network_state.cc22
-rw-r--r--chromeos/network/network_state.h8
-rw-r--r--chromeos/network/network_state_handler.cc115
-rw-r--r--chromeos/network/network_state_handler.h63
-rw-r--r--chromeos/network/network_state_handler_observer.cc6
-rw-r--r--chromeos/network/network_state_handler_observer.h23
-rw-r--r--chromeos/network/network_state_handler_unittest.cc154
7 files changed, 247 insertions, 144 deletions
diff --git a/chromeos/network/network_state.cc b/chromeos/network/network_state.cc
index c87bdcb4..830e993 100644
--- a/chromeos/network/network_state.cc
+++ b/chromeos/network/network_state.cc
@@ -24,7 +24,7 @@ bool NetworkState::PropertyChanged(const std::string& key,
if (key == flimflam::kSignalStrengthProperty) {
return GetIntegerValue(key, value, &signal_strength_);
} else if (key == flimflam::kStateProperty) {
- return GetStringValue(key, value, &state_);
+ return GetStringValue(key, value, &connection_state_);
} else if (key == flimflam::kErrorProperty) {
return GetStringValue(key, value, &error_);
} else if (key == flimflam::kActivationStateProperty) {
@@ -42,25 +42,25 @@ bool NetworkState::PropertyChanged(const std::string& key,
}
bool NetworkState::IsConnectedState() const {
- return StateIsConnected(state_);
+ return StateIsConnected(connection_state_);
}
bool NetworkState::IsConnectingState() const {
- return StateIsConnecting(state_);
+ return StateIsConnecting(connection_state_);
}
// static
-bool NetworkState::StateIsConnected(const std::string& state) {
- return (state == flimflam::kStateReady ||
- state == flimflam::kStateOnline ||
- state == flimflam::kStatePortal);
+bool NetworkState::StateIsConnected(const std::string& connection_state) {
+ return (connection_state == flimflam::kStateReady ||
+ connection_state == flimflam::kStateOnline ||
+ connection_state == flimflam::kStatePortal);
}
// static
-bool NetworkState::StateIsConnecting(const std::string& state) {
- return (state == flimflam::kStateAssociation ||
- state == flimflam::kStateConfiguration ||
- state == flimflam::kStateCarrier);
+bool NetworkState::StateIsConnecting(const std::string& connection_state) {
+ return (connection_state == flimflam::kStateAssociation ||
+ connection_state == flimflam::kStateConfiguration ||
+ connection_state == flimflam::kStateCarrier);
}
} // namespace chromeos
diff --git a/chromeos/network/network_state.h b/chromeos/network/network_state.h
index b5f7376..c5fa66d 100644
--- a/chromeos/network/network_state.h
+++ b/chromeos/network/network_state.h
@@ -28,7 +28,7 @@ class CHROMEOS_EXPORT NetworkState : public ManagedState {
const std::string& technology() const { return technology_; }
const std::string& ip_address() const { return ip_address_; }
const std::string& device_path() const { return device_path_; }
- const std::string& state() const { return state_; }
+ const std::string& connection_state() const { return connection_state_; }
const std::string& error() const { return error_; }
const std::string& activation_state() const { return activation_state_; }
const std::string& roaming() const { return roaming_; }
@@ -38,8 +38,8 @@ class CHROMEOS_EXPORT NetworkState : public ManagedState {
bool IsConnectingState() const;
// Helpers (used e.g. when a state is cached)
- static bool StateIsConnected(const std::string& state);
- static bool StateIsConnecting(const std::string& state);
+ static bool StateIsConnected(const std::string& connection_state);
+ static bool StateIsConnecting(const std::string& connection_state);
private:
friend class NetworkStateHandler;
@@ -53,7 +53,7 @@ class CHROMEOS_EXPORT NetworkState : public ManagedState {
std::string security_;
std::string device_path_;
std::string ip_address_;
- std::string state_;
+ std::string connection_state_;
std::string error_;
// Wireless properties
int signal_strength_;
diff --git a/chromeos/network/network_state_handler.cc b/chromeos/network/network_state_handler.cc
index 7b0e0d9..0fc7ec1 100644
--- a/chromeos/network/network_state_handler.cc
+++ b/chromeos/network/network_state_handler.cc
@@ -18,11 +18,33 @@
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace {
+
const char kLogModule[] = "NetworkPropertyHandler";
+
+// Returns true if |network->type()| == |match_type|, or it matches one of the
+// following special match types:
+// * kMatchTypeDefault matches any network (i.e. the first instance)
+// * kMatchTypeNonVirtual matches non virtual networks
+// * kMatchTypeWireless matches wireless networks
+bool NetworkStateMatchesType(const chromeos::NetworkState* network,
+ const std::string& match_type) {
+ const std::string& type = network->type();
+ return ((match_type == chromeos::NetworkStateHandler::kMatchTypeDefault) ||
+ (match_type == type) ||
+ (match_type == chromeos::NetworkStateHandler::kMatchTypeNonVirtual &&
+ type != flimflam::kTypeVPN) ||
+ (match_type == chromeos::NetworkStateHandler::kMatchTypeWireless &&
+ type != flimflam::kTypeEthernet && type != flimflam::kTypeVPN));
}
+} // namespace
+
namespace chromeos {
+const char NetworkStateHandler::kMatchTypeDefault[] = "default";
+const char NetworkStateHandler::kMatchTypeWireless[] = "wireless";
+const char NetworkStateHandler::kMatchTypeNonVirtual[] = "non-virtual";
+
static NetworkStateHandler* g_network_state_handler = NULL;
NetworkStateHandler::NetworkStateHandler() {
@@ -108,7 +130,7 @@ const NetworkState* NetworkStateHandler::GetNetworkState(
return GetModifiableNetworkState(service_path);
}
-const NetworkState* NetworkStateHandler::ActiveNetwork() const {
+const NetworkState* NetworkStateHandler::DefaultNetwork() const {
if (network_list_.empty())
return NULL;
const NetworkState* network = network_list_.front()->AsNetworkState();
@@ -126,7 +148,7 @@ const NetworkState* NetworkStateHandler::ConnectedNetworkByType(
DCHECK(network);
if (!network->IsConnectedState())
break; // Connected networks are listed first.
- if (network->type() == type)
+ if (NetworkStateMatchesType(network, type))
return network;
}
return NULL;
@@ -142,10 +164,8 @@ const NetworkState* NetworkStateHandler::ConnectingNetworkByType(
continue;
if (!network->IsConnectingState())
break; // Connected and connecting networks are listed first.
- if (network->type() == type ||
- (type.empty() && type != flimflam::kTypeEthernet)) {
+ if (NetworkStateMatchesType(network, type))
return network;
- }
}
return NULL;
}
@@ -285,24 +305,24 @@ void NetworkStateHandler::UpdateManagedStateProperties(
LOG(ERROR) << "GetPropertiesCallback: " << path << " Not found!";
return;
}
- bool network_property_changed = false;
+ bool network_property_updated = false;
for (base::DictionaryValue::Iterator iter(properties);
iter.HasNext(); iter.Advance()) {
if (type == ManagedState::MANAGED_TYPE_NETWORK) {
if (ParseNetworkServiceProperty(
managed->AsNetworkState(), iter.key(), iter.value())) {
- network_property_changed = true;
+ network_property_updated = true;
}
} else {
managed->PropertyChanged(iter.key(), iter.value());
}
}
// Notify observers.
- if (network_property_changed) {
+ if (network_property_updated) {
NetworkState* network = managed->AsNetworkState();
DCHECK(network);
FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
- NetworkServiceChanged(network));
+ NetworkPropertiesUpdated(network));
}
network_event_log::AddEntry(
kLogModule, "PropertiesReceived",
@@ -316,15 +336,15 @@ void NetworkStateHandler::UpdateNetworkServiceProperty(
NetworkState* network = GetModifiableNetworkState(service_path);
if (!network)
return;
- if (ParseNetworkServiceProperty(network, key, value)) {
- std::string detail = network->name() + "." + key;
- std::string vstr;
- if (value.GetAsString(&vstr))
- detail += " = " + vstr;
- network_event_log::AddEntry(kLogModule, "NetworkPropertyChanged", detail);
- FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
- NetworkServiceChanged(network));
- }
+ if (!ParseNetworkServiceProperty(network, key, value))
+ return;
+ std::string detail = network->name() + "." + key;
+ std::string vstr;
+ if (value.GetAsString(&vstr))
+ detail += " = " + vstr;
+ network_event_log::AddEntry(kLogModule, "NetworkPropertiesUpdated", detail);
+ FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
+ NetworkPropertiesUpdated(network));
}
void NetworkStateHandler::UpdateNetworkServiceIPAddress(
@@ -338,7 +358,7 @@ void NetworkStateHandler::UpdateNetworkServiceIPAddress(
network->set_ip_address(ip_address);
FOR_EACH_OBSERVER(
NetworkStateHandlerObserver, observers_,
- NetworkServiceChanged(network));
+ NetworkPropertiesUpdated(network));
}
void NetworkStateHandler::ManagerPropertyChanged() {
@@ -357,19 +377,9 @@ void NetworkStateHandler::ManagedStateListChanged(
StringPrintf("Size: %"PRIuS, network_list_.size()));
FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
NetworkListChanged(network_list));
- // Update the active network and notify observers if it has changed.
- NetworkState* new_active_network =
- network_list_.empty() ? NULL : network_list_.front()->AsNetworkState();
- std::string new_active_network_path;
- if (new_active_network)
- new_active_network_path = new_active_network->path();
- if (new_active_network_path != active_network_path_) {
- network_event_log::AddEntry(
- kLogModule, "ActiveNetworkChanged", new_active_network_path);
- active_network_path_ = new_active_network_path;
- FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
- ActiveNetworkChanged(new_active_network));
- }
+ // The list order may have changed, so check if the default network changed.
+ if (CheckDefaultNetworkChanged())
+ OnDefaultNetworkChanged();
} else if (type == ManagedState::MANAGED_TYPE_DEVICE) {
network_event_log::AddEntry(
kLogModule, "DeviceListChanged",
@@ -430,14 +440,47 @@ bool NetworkStateHandler::ParseNetworkServiceProperty(
const std::string& key,
const base::Value& value) {
DCHECK(network);
+ std::string prev_connection_state = network->connection_state();
if (!network->PropertyChanged(key, value))
return false;
- if (network->path() == active_network_path_ &&
- key == flimflam::kStateProperty) {
- FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
- ActiveNetworkStateChanged(network));
+ if (key == flimflam::kStateProperty &&
+ network->connection_state() != prev_connection_state) {
+ OnNetworkConnectionStateChanged(network);
+ }
+ return true;
+}
+
+void NetworkStateHandler::OnNetworkConnectionStateChanged(
+ NetworkState* network) {
+ std::string desc = StringPrintf(
+ "%s: %s", network->path().c_str(), network->connection_state().c_str());
+ network_event_log::AddEntry(
+ kLogModule, "NetworkConnectionStateChanged", desc);
+ FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
+ NetworkConnectionStateChanged(network));
+ if (CheckDefaultNetworkChanged() ||
+ network->path() == default_network_path_) {
+ OnDefaultNetworkChanged();
}
+}
+
+bool NetworkStateHandler::CheckDefaultNetworkChanged() {
+ std::string new_default_network_path;
+ const NetworkState* new_default_network = DefaultNetwork();
+ if (new_default_network)
+ new_default_network_path = new_default_network->path();
+ if (new_default_network_path == default_network_path_)
+ return false;
+ default_network_path_ = new_default_network_path;
return true;
}
+void NetworkStateHandler::OnDefaultNetworkChanged() {
+ const NetworkState* default_network = DefaultNetwork();
+ network_event_log::AddEntry(
+ kLogModule, "DefaultNetworkChanged", default_network->path());
+ FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
+ DefaultNetworkChanged(default_network));
+}
+
} // namespace chromeos
diff --git a/chromeos/network/network_state_handler.h b/chromeos/network/network_state_handler.h
index 8c2d125..68f48a7 100644
--- a/chromeos/network/network_state_handler.h
+++ b/chromeos/network/network_state_handler.h
@@ -31,14 +31,14 @@ class NetworkState;
class NetworkStateHandlerObserver;
class NetworkStateHandlerTest;
-// Class for tracking the list of visible networks and their state.
+// Class for tracking the list of visible networks and their properties.
//
-// This class maps essential state from the connection manager (Shill) for
-// each visible network. It is not used to change the state of services or
-// devices, only global (manager) state.
+// This class maps essential properties from the connection manager (Shill) for
+// each visible network. It is not used to change the properties of services or
+// devices, only global (manager) properties.
//
-// All getters return the currently cached state. This class is expected to
-// keep states up to date by managing the appropriate Shill observers.
+// All getters return the currently cached properties. This class is expected to
+// keep properties up to date by managing the appropriate Shill observers.
// It will invoke its own more specific observer methods when the specified
// changes occur.
class CHROMEOS_EXPORT NetworkStateHandler
@@ -66,8 +66,8 @@ class CHROMEOS_EXPORT NetworkStateHandler
bool TechnologyAvailable(const std::string& technology) const;
bool TechnologyEnabled(const std::string& technology) const;
- // Asynchronously sets the enabled state for |technology|.
- // Note: Modifes Manager state. Calls |error_callback| on failure.
+ // Asynchronously sets the enabled property for |technology|.
+ // Note: Modifies Manager state. Calls |error_callback| on failure.
void SetTechnologyEnabled(
const std::string& technology,
bool enabled,
@@ -82,19 +82,23 @@ class CHROMEOS_EXPORT NetworkStateHandler
// Finds and returns a network state by |service_path| or NULL if not found.
// Note: NetworkState is frequently updated asynchronously, i.e. properties
// are not always updated all at once. This will contain the most recent
- // value for each state. To receive notifications when the state changes,
- // observer this class and implement NetworkServiceChanged().
+ // value for each property. To receive notifications when a property changes,
+ // observe this class and implement NetworkPropertyChanged().
const NetworkState* GetNetworkState(const std::string& service_path) const;
- // Returns the "active" network (first network in the list if connected),
- // NULL if none.
- const NetworkState* ActiveNetwork() const;
+ // Returns the default connected network (which includes VPNs) or NULL.
+ // This is equivalent to ConnectedNetworkByType(kMatchTypeDefault).
+ const NetworkState* DefaultNetwork() const;
- // Returns the first connected network of type |type|, otherwise NULL.
+ // Returns the primary connected network of matching |type|, otherwise NULL.
+ // |type| can be a type defined in service_constants.h, or one of the
+ // following special constants:
+ // * kMatchTypeDefault returns the default (active) network
+ // * kMatchTypeNonVirtual returns the primary connected non virtual network
+ // * kMatchTypeWireless returns the primary connected wireless network
const NetworkState* ConnectedNetworkByType(const std::string& type) const;
- // Returns the first connecting network of type |type|, otherwise NULL.
- // An empty type will return any connecting non-ethernet network.
+ // Like ConnectedNetworkByType() but returns a connecting network or NULL.
const NetworkState* ConnectingNetworkByType(const std::string& type) const;
// Returns the hardware (MAC) address for the first connected network
@@ -114,6 +118,10 @@ class CHROMEOS_EXPORT NetworkStateHandler
// Returns true if a scan was requested.
bool RequestWifiScan() const;
+ static const char kMatchTypeDefault[];
+ static const char kMatchTypeWireless[];
+ static const char kMatchTypeNonVirtual[];
+
protected:
NetworkStateHandler();
@@ -155,9 +163,9 @@ class CHROMEOS_EXPORT NetworkStateHandler
virtual void ManagerPropertyChanged() OVERRIDE;
// Called by |shill_property_handler_| when the service or device list has
- // changed and all entries have been updated. If |type| == TYPE_NETWORK,
- // this notifies observers that the network list has changed, and if the
- // active network has changed sends that notification also.
+ // changed and all entries have been updated. This updates the list and
+ // notifies observers. If |type| == TYPE_NETWORK this also calls
+ // CheckDefaultNetworkChanged().
virtual void ManagedStateListChanged(
ManagedState::ManagedType type) OVERRIDE;
@@ -181,11 +189,22 @@ class CHROMEOS_EXPORT NetworkStateHandler
// Gets the list specified by |type|.
ManagedStateList* GetManagedList(ManagedState::ManagedType type);
- // Helper function called to parse |network| properties.
+ // Helper function called to parse |network| properties. Also calls
+ // OnNetworkConnectionStateChanged() if the connection_state property changes.
bool ParseNetworkServiceProperty(NetworkState* network,
const std::string& key,
const base::Value& value);
+ // Helper function to notify observers. Calls CheckDefaultNetworkChanged().
+ void OnNetworkConnectionStateChanged(NetworkState* network);
+
+ // If the default network changed returns true and sets
+ // |default_network_path_|.
+ bool CheckDefaultNetworkChanged();
+
+ // Logs an event and notifies observers.
+ void OnDefaultNetworkChanged();
+
// Shill property handler instance, owned by this class.
scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
@@ -200,8 +219,8 @@ class CHROMEOS_EXPORT NetworkStateHandler
std::set<std::string> available_technologies_;
std::set<std::string> enabled_technologies_;
- // Keeps track of the active network for notifying observers when it changes.
- std::string active_network_path_;
+ // Keeps track of the default network for notifying observers when it changes.
+ std::string default_network_path_;
DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler);
};
diff --git a/chromeos/network/network_state_handler_observer.cc b/chromeos/network/network_state_handler_observer.cc
index e573683..08ee3e8 100644
--- a/chromeos/network/network_state_handler_observer.cc
+++ b/chromeos/network/network_state_handler_observer.cc
@@ -22,15 +22,15 @@ void NetworkStateHandlerObserver::NetworkListChanged(
void NetworkStateHandlerObserver::DeviceListChanged() {
}
-void NetworkStateHandlerObserver::ActiveNetworkChanged(
+void NetworkStateHandlerObserver::DefaultNetworkChanged(
const NetworkState* network) {
}
-void NetworkStateHandlerObserver::ActiveNetworkStateChanged(
+void NetworkStateHandlerObserver::NetworkConnectionStateChanged(
const NetworkState* network) {
}
-void NetworkStateHandlerObserver::NetworkServiceChanged(
+void NetworkStateHandlerObserver::NetworkPropertiesUpdated(
const NetworkState* network) {
}
diff --git a/chromeos/network/network_state_handler_observer.h b/chromeos/network/network_state_handler_observer.h
index 07a4a77..96f25ce 100644
--- a/chromeos/network/network_state_handler_observer.h
+++ b/chromeos/network/network_state_handler_observer.h
@@ -34,17 +34,18 @@ class CHROMEOS_EXPORT NetworkStateHandlerObserver {
// of devices, so they are not passed in the method.
virtual void DeviceListChanged();
- // The active network changed. |network| will be NULL if there is no longer
- // an active network.
- virtual void ActiveNetworkChanged(const NetworkState* network);
-
- // The state of the active network changed.
- virtual void ActiveNetworkStateChanged(const NetworkState* network);
-
- // One or more network service properties changed. Note: for the active
- // network, this will be called in *addition* to ActiveNetworkStateChanged()
- // if the state property changed.
- virtual void NetworkServiceChanged(const NetworkState* network);
+ // The default network changed (includes VPNs) or its connection state
+ // changed. |network| will be NULL if there is no longer a default network.
+ virtual void DefaultNetworkChanged(const NetworkState* network);
+
+ // The connection state of |network| changed.
+ virtual void NetworkConnectionStateChanged(const NetworkState* network);
+
+ // One or more properties of |network| have been updated. Note: this will get
+ // called in *addition* to NetworkConnectionStateChanged() when the
+ // connection state property changes. Use this to track properties like
+ // wifi strength.
+ virtual void NetworkPropertiesUpdated(const NetworkState* network);
private:
DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerObserver);
diff --git a/chromeos/network/network_state_handler_unittest.cc b/chromeos/network/network_state_handler_unittest.cc
index 448bf4c..20522eb 100644
--- a/chromeos/network/network_state_handler_unittest.cc
+++ b/chromeos/network/network_state_handler_unittest.cc
@@ -29,64 +29,78 @@ void ErrorCallbackFunction(const std::string& error_name,
}
using chromeos::NetworkState;
+using chromeos::NetworkStateHandler;
class TestObserver : public chromeos::NetworkStateHandlerObserver {
public:
- TestObserver()
- : manager_changed_count_(0),
+ explicit TestObserver(NetworkStateHandler* handler)
+ : handler_(handler),
+ manager_changed_count_(0),
network_count_(0) {
}
virtual ~TestObserver() {
}
- virtual void NetworkManagerChanged() {
+ virtual void NetworkManagerChanged() OVERRIDE {
++manager_changed_count_;
}
virtual void NetworkListChanged(
- const chromeos::NetworkStateHandler::NetworkStateList& networks) {
+ const NetworkStateHandler::NetworkStateList& networks) OVERRIDE {
network_count_ = networks.size();
+ if (network_count_ == 0) {
+ default_network_ = "";
+ default_network_connection_state_ = "";
+ }
}
- virtual void ActiveNetworkChanged(const NetworkState* network) {
- active_network_ = network ? network->path() : "";
- active_network_state_ = network ? network->state() : "";
+ virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE {
+ default_network_ = network ? network->path() : "";
+ default_network_connection_state_ =
+ network ? network->connection_state() : "";
}
- virtual void ActiveNetworkStateChanged(const NetworkState* network) {
- active_network_state_ = network ? network->state() : "";
+ virtual void NetworkConnectionStateChanged(
+ const NetworkState* network) OVERRIDE {
+ network_connection_state_[network->path()] = network->connection_state();
+ connection_state_changes_[network->path()]++;
}
- virtual void NetworkServiceChanged(const NetworkState* network) {
+ virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE {
DCHECK(network);
- std::map<std::string, int>::iterator iter =
- property_changes_.find(network->path());
- if (iter == property_changes_.end())
- property_changes_[network->path()] = 1;
- else
- iter->second++;
+ property_updates_[network->path()]++;
}
size_t manager_changed_count() { return manager_changed_count_; }
size_t network_count() { return network_count_; }
- std::string active_network() { return active_network_; }
- std::string active_network_state() { return active_network_state_; }
-
- int PropertyChangesForService(const std::string& service_path) {
- std::map<std::string, int>::iterator iter =
- property_changes_.find(service_path);
- if (iter == property_changes_.end())
- return 0;
- return iter->second;
+ std::string default_network() { return default_network_; }
+ std::string default_network_connection_state() {
+ return default_network_connection_state_;
+ }
+
+ int PropertyUpdatesForService(const std::string& service_path) {
+ return property_updates_[service_path];
+ }
+
+ int ConnectionStateChangesForService(const std::string& service_path) {
+ return connection_state_changes_[service_path];
+ }
+
+ std::string NetworkConnectionStateForService(
+ const std::string& service_path) {
+ return network_connection_state_[service_path];
}
private:
+ NetworkStateHandler* handler_;
size_t manager_changed_count_;
size_t network_count_;
- std::string active_network_;
- std::string active_network_state_;
- std::map<std::string, int> property_changes_;
+ std::string default_network_;
+ std::string default_network_connection_state_;
+ std::map<std::string, int> property_updates_;
+ std::map<std::string, int> connection_state_changes_;
+ std::map<std::string, std::string> network_connection_state_;
DISALLOW_COPY_AND_ASSIGN(TestObserver);
};
@@ -112,8 +126,8 @@ class NetworkStateHandlerTest : public testing::Test {
}
void SetupNetworkStateHandler() {
- test_observer_.reset(new TestObserver);
network_state_handler_.reset(new NetworkStateHandler);
+ test_observer_.reset(new TestObserver(network_state_handler_.get()));
network_state_handler_->AddObserver(test_observer_.get());
network_state_handler_->InitShillPropertyHandler();
}
@@ -136,19 +150,25 @@ TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) {
const size_t kNumShillManagerClientStubImplServices = 4;
EXPECT_EQ(kNumShillManagerClientStubImplServices,
test_observer_->network_count());
- // Ensure that the first stub network is the active network.
- const std::string kShillManagerClientStubActiveService = "stub_ethernet";
- EXPECT_EQ(kShillManagerClientStubActiveService,
- test_observer_->active_network());
- EXPECT_EQ(kShillManagerClientStubActiveService,
- network_state_handler_->ActiveNetwork()->path());
- EXPECT_EQ(kShillManagerClientStubActiveService,
+ // Ensure that the first stub network is the default network.
+ const std::string kShillManagerClientStubDefaultService = "stub_ethernet";
+ EXPECT_EQ(kShillManagerClientStubDefaultService,
+ test_observer_->default_network());
+ EXPECT_EQ(kShillManagerClientStubDefaultService,
+ network_state_handler_->ConnectedNetworkByType(
+ NetworkStateHandler::kMatchTypeDefault)->path());
+ EXPECT_EQ(kShillManagerClientStubDefaultService,
network_state_handler_->ConnectedNetworkByType(
flimflam::kTypeEthernet)->path());
- EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state());
+ const std::string kShillManagerClientStubDefaultWireless = "stub_wifi1";
+ EXPECT_EQ(kShillManagerClientStubDefaultWireless,
+ network_state_handler_->ConnectedNetworkByType(
+ NetworkStateHandler::kMatchTypeWireless)->path());
+ EXPECT_EQ(flimflam::kStateOnline,
+ test_observer_->default_network_connection_state());
}
-TEST_F(NetworkStateHandlerTest, NetworkStateHandlerTechnologyChanged) {
+TEST_F(NetworkStateHandlerTest, TechnologyChanged) {
// This relies on the stub dbus implementations for ShillManagerClient,
SetupNetworkStateHandler();
message_loop_.RunUntilIdle();
@@ -163,14 +183,14 @@ TEST_F(NetworkStateHandlerTest, NetworkStateHandlerTechnologyChanged) {
EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax));
}
-TEST_F(NetworkStateHandlerTest, NetworkStateHandlerServicePropertyChanged) {
+TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) {
// This relies on the stub dbus implementations for ShillManagerClient,
SetupNetworkStateHandler();
message_loop_.RunUntilIdle();
// Set a service property.
const std::string eth0 = "stub_ethernet";
EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security());
- EXPECT_EQ(1, test_observer_->PropertyChangesForService(eth0));
+ EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(eth0));
base::StringValue security_value("TestSecurity");
DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
dbus::ObjectPath(eth0),
@@ -179,36 +199,56 @@ TEST_F(NetworkStateHandlerTest, NetworkStateHandlerServicePropertyChanged) {
message_loop_.RunUntilIdle();
EXPECT_EQ("TestSecurity",
network_state_handler_->GetNetworkState(eth0)->security());
- EXPECT_EQ(2, test_observer_->PropertyChangesForService(eth0));
+ EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth0));
+}
+
+TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) {
+ // This relies on the stub dbus implementations for ShillManagerClient,
+ SetupNetworkStateHandler();
+ message_loop_.RunUntilIdle();
+ // Change a network state.
+ ShillServiceClient::TestInterface* service_test =
+ DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
+ const std::string eth0 = "stub_ethernet";
+ base::StringValue connection_state_idle_value(flimflam::kStateIdle);
+ service_test->SetServiceProperty(eth0, flimflam::kStateProperty,
+ connection_state_idle_value);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(flimflam::kStateIdle,
+ test_observer_->NetworkConnectionStateForService(eth0));
+ EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth0));
+ // Confirm that changing the connection state to the same value does *not*
+ // signal the observer.
+ service_test->SetServiceProperty(eth0, flimflam::kStateProperty,
+ connection_state_idle_value);
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth0));
}
-TEST_F(NetworkStateHandlerTest, NetworkStateHandlerActiveServiceChanged) {
+TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) {
// This relies on the stub dbus implementations for ShillManagerClient,
SetupNetworkStateHandler();
message_loop_.RunUntilIdle();
- // Change the active network by inserting wifi1 at the front of the list.
ShillManagerClient::TestInterface* manager_test =
DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
ASSERT_TRUE(manager_test);
- const std::string wifi1 = "stub_wifi1";
- manager_test->AddServiceAtIndex(wifi1, 0, true);
- message_loop_.RunUntilIdle();
- EXPECT_EQ(wifi1, test_observer_->active_network());
- EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state());
-
- // Change the state of wifi1, ensure that triggers the active state changed
- // observer.
ShillServiceClient::TestInterface* service_test =
DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
ASSERT_TRUE(service_test);
- base::StringValue state_value(flimflam::kStateConfiguration);
- service_test->SetServiceProperty(wifi1, flimflam::kStateProperty,
- state_value);
+
+ // Change the default network by inserting wifi1 at the front of the list
+ // and changing the state of stub_ethernet to Idle.
+ const std::string wifi1 = "stub_wifi1";
+ manager_test->AddServiceAtIndex(wifi1, 0, true);
+ const std::string eth0 = "stub_ethernet";
+ base::StringValue connection_state_idle_value(flimflam::kStateIdle);
+ service_test->SetServiceProperty(eth0, flimflam::kStateProperty,
+ connection_state_idle_value);
message_loop_.RunUntilIdle();
- EXPECT_EQ(wifi1, test_observer_->active_network());
- EXPECT_EQ(flimflam::kStateConfiguration,
- test_observer_->active_network_state());
+ EXPECT_EQ(wifi1, test_observer_->default_network());
+ EXPECT_EQ(flimflam::kStateOnline,
+ test_observer_->default_network_connection_state());
}
} // namespace chromeos