| Index: chromeos/network/network_state_manager_unittest.cc
|
| diff --git a/chromeos/network/network_state_manager_unittest.cc b/chromeos/network/network_state_manager_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..030cbeba2a59c68cd2ccdfc688e4605d3fcb801a
|
| --- /dev/null
|
| +++ b/chromeos/network/network_state_manager_unittest.cc
|
| @@ -0,0 +1,182 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chromeos/network/network_state_manager.h"
|
| +
|
| +#include <map>
|
| +#include <set>
|
| +#include <string>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/message_loop.h"
|
| +#include "base/values.h"
|
| +#include "chromeos/dbus/dbus_thread_manager.h"
|
| +#include "chromeos/dbus/shill_service_client.h"
|
| +#include "chromeos/network/network_state.h"
|
| +#include "dbus/object_path.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace chromeos {
|
| +
|
| +namespace {
|
| +
|
| +void ErrorCallbackFunction(const std::string& error_name,
|
| + const std::string& error_message) {
|
| + LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
|
| +}
|
| +
|
| +class TestObserver : public NetworkStateManager::Observer {
|
| + public:
|
| + TestObserver()
|
| + : manager_changed_count_(0),
|
| + network_count_(0) {
|
| + }
|
| +
|
| + virtual ~TestObserver() {}
|
| +
|
| + virtual void NetworkManagerChanged(const std::string& property) {
|
| + ++manager_changed_count_;
|
| + }
|
| +
|
| + virtual void NetworkListChanged(
|
| + const NetworkStateManager::NetworkStateList& networks) {
|
| + network_count_ = networks.size();
|
| + }
|
| +
|
| + virtual void ActiveNetworkChanged(const NetworkState* network) {
|
| + active_network_ = network ? network->path() : "";
|
| + active_network_state_ = network ? network->state() : "";
|
| + }
|
| +
|
| + virtual void ActiveNetworkStateChanged(const NetworkState* network) {
|
| + active_network_state_ = network ? network->state() : "";
|
| + }
|
| +
|
| + virtual void NetworkServicePropertyChanged(const NetworkState* network,
|
| + const std::string& property) {
|
| + if (!network)
|
| + return;
|
| + changed_properties_[network->path()].insert(property);
|
| + }
|
| +
|
| + int manager_changed_count() { return manager_changed_count_; }
|
| + int network_count() { return network_count_; }
|
| + std::string active_network() { return active_network_; }
|
| + std::string active_network_state() { return active_network_state_; }
|
| +
|
| + bool PropertyChangedForService(const std::string& service_path,
|
| + const std::string& property) {
|
| + std::map<std::string, std::set<std::string> >::iterator iter =
|
| + changed_properties_.find(service_path);
|
| + if (iter == changed_properties_.end())
|
| + return false;
|
| + if (iter->second.find(property) == iter->second.end())
|
| + return false;
|
| + return true;
|
| + }
|
| +
|
| + private:
|
| + int manager_changed_count_;
|
| + int network_count_;
|
| + std::string active_network_;
|
| + std::string active_network_state_;
|
| + std::map<std::string, std::set<std::string> > changed_properties_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(TestObserver);
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +class NetworkStateManagerTest : public testing::Test {
|
| + public:
|
| + NetworkStateManagerTest() {}
|
| + virtual ~NetworkStateManagerTest() {}
|
| +
|
| + virtual void SetUp() OVERRIDE {
|
| + // Initialize DBusThreadManager with a stub implementation.
|
| + DBusThreadManager::InitializeWithStub();
|
| + }
|
| +
|
| + virtual void TearDown() OVERRIDE {
|
| + DBusThreadManager::Shutdown();
|
| + }
|
| +
|
| + protected:
|
| + MessageLoopForUI message_loop_;
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(NetworkStateManagerTest);
|
| +};
|
| +
|
| +TEST_F(NetworkStateManagerTest, NetworkStateManagerStub) {
|
| + // This relies on the stub dbus implementations for ShillManagerClient,
|
| + scoped_ptr<NetworkStateManager> state_manager(new NetworkStateManager());
|
| + scoped_ptr<TestObserver> test_observer(new TestObserver());
|
| + state_manager->AddObserver(test_observer.get());
|
| + state_manager->Init();
|
| + message_loop_.RunAllPending();
|
| + EXPECT_EQ(test_observer->manager_changed_count(), 1);
|
| + // ShillManagerClient stub entries are in shill_manager_client.cc.
|
| + const int kNumShillManagerClientStubImplServices = 4;
|
| + EXPECT_EQ(kNumShillManagerClientStubImplServices,
|
| + test_observer->network_count());
|
| + const int kNumShillManagerClientStubImplOnlineServices = 2;
|
| + EXPECT_EQ(kNumShillManagerClientStubImplOnlineServices,
|
| + state_manager->NumObservedNetworksForTest());
|
| + const std::string kShillManagerClientStubActiveService = "stub_ethernet";
|
| + EXPECT_EQ(kShillManagerClientStubActiveService,
|
| + test_observer->active_network());
|
| + EXPECT_EQ(kShillManagerClientStubActiveService,
|
| + state_manager->ActiveNetwork()->path());
|
| + EXPECT_EQ(kShillManagerClientStubActiveService,
|
| + state_manager->ConnectedNetworkByType(
|
| + flimflam::kTypeEthernet)->path());
|
| + // ShillServiceClient stub entries are in shill_service_client.cc.
|
| + const std::string kShillManagerClientStubActiveServiceState =
|
| + flimflam::kStateOnline;
|
| + EXPECT_EQ(kShillManagerClientStubActiveServiceState,
|
| + test_observer->active_network_state());
|
| +}
|
| +
|
| +TEST_F(NetworkStateManagerTest, NetworkStateManagerTechnologyChanged) {
|
| + // This relies on the stub dbus implementations for ShillManagerClient,
|
| + scoped_ptr<NetworkStateManager> state_manager(new NetworkStateManager());
|
| + scoped_ptr<TestObserver> test_observer(new TestObserver());
|
| + state_manager->AddObserver(test_observer.get());
|
| + state_manager->Init();
|
| + message_loop_.RunAllPending();
|
| + EXPECT_EQ(1, test_observer->manager_changed_count());
|
| + // Enable a technology.
|
| + EXPECT_FALSE(state_manager->TechnologyEnabled(flimflam::kTypeWimax));
|
| + state_manager->SetTechnologyEnabled(flimflam::kTypeWimax, true);
|
| + message_loop_.RunAllPending();
|
| + // Ensure we get a manager changed callback when we change a property.
|
| + EXPECT_EQ(2, test_observer->manager_changed_count());
|
| + EXPECT_TRUE(state_manager->TechnologyEnabled(flimflam::kTypeWimax));
|
| +}
|
| +
|
| +TEST_F(NetworkStateManagerTest, NetworkStateManagerServicePropertyChanged) {
|
| + // This relies on the stub dbus implementations for ShillManagerClient,
|
| + scoped_ptr<NetworkStateManager> state_manager(new NetworkStateManager());
|
| + scoped_ptr<TestObserver> test_observer(new TestObserver());
|
| + state_manager->AddObserver(test_observer.get());
|
| + state_manager->Init();
|
| + message_loop_.RunAllPending();
|
| + // Set a service property.
|
| + const std::string eth0 = "stub_ethernet";
|
| + EXPECT_EQ("none", state_manager->GetNetworkState(eth0)->security());
|
| + EXPECT_FALSE(test_observer->PropertyChangedForService(
|
| + eth0, flimflam::kSecurityProperty));
|
| + DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
|
| + dbus::ObjectPath(eth0),
|
| + flimflam::kSecurityProperty, base::StringValue("TestSecurity"),
|
| + base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
|
| + message_loop_.RunAllPending();
|
| + EXPECT_EQ("TestSecurity", state_manager->GetNetworkState(eth0)->security());
|
| + EXPECT_TRUE(test_observer->PropertyChangedForService(
|
| + eth0, flimflam::kSecurityProperty));
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|