Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Side by Side Diff: chromeos/network/network_state_handler_unittest.cc

Issue 11192024: Add chromeos::NetworkStateManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase with dbus tests in trunk Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chromeos/network/network_state_handler.h"
6
7 #include <map>
8 #include <set>
9 #include <string>
10
11 #include "base/bind.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h"
14 #include "base/values.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/dbus/shill_manager_client.h"
17 #include "chromeos/dbus/shill_service_client.h"
18 #include "chromeos/network/network_state.h"
19 #include "chromeos/network/network_state_handler_observer.h"
20 #include "dbus/object_path.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h"
23
24 namespace chromeos {
gauravsh 2012/11/06 01:56:59 Can this be moved to 95?
stevenjb 2012/11/06 03:17:03 Done.
25
26 namespace {
27
28 void ErrorCallbackFunction(const std::string& error_name,
29 const std::string& error_message) {
30 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
31 }
32
33 class TestObserver : public NetworkStateHandlerObserver {
34 public:
35 TestObserver()
36 : manager_changed_count_(0),
37 network_count_(0) {
38 }
39
40 virtual ~TestObserver() {
41 }
42
43 virtual void NetworkManagerChanged() {
44 ++manager_changed_count_;
45 }
46
47 virtual void NetworkListChanged(
48 const NetworkStateHandler::NetworkStateList& networks) {
49 network_count_ = networks.size();
50 }
51
52 virtual void ActiveNetworkChanged(const NetworkState* network) {
53 active_network_ = network ? network->path() : "";
54 active_network_state_ = network ? network->state() : "";
55 }
56
57 virtual void ActiveNetworkStateChanged(const NetworkState* network) {
58 active_network_state_ = network ? network->state() : "";
59 }
60
61 virtual void NetworkServiceChanged(const NetworkState* network) {
62 DCHECK(network);
63 std::map<std::string, int>::iterator iter =
64 property_changes_.find(network->path());
65 if (iter == property_changes_.end())
66 property_changes_[network->path()] = 1;
67 else
68 iter->second++;
69 }
70
71 size_t manager_changed_count() { return manager_changed_count_; }
72 size_t network_count() { return network_count_; }
73 std::string active_network() { return active_network_; }
74 std::string active_network_state() { return active_network_state_; }
75
76 int PropertyChangesForService(const std::string& service_path) {
77 std::map<std::string, int>::iterator iter =
78 property_changes_.find(service_path);
79 if (iter == property_changes_.end())
80 return 0;
81 return iter->second;
82 }
83
84 private:
85 size_t manager_changed_count_;
86 size_t network_count_;
87 std::string active_network_;
88 std::string active_network_state_;
89 std::map<std::string, int> property_changes_;
90
91 DISALLOW_COPY_AND_ASSIGN(TestObserver);
92 };
93
94 } // namespace
95
96 class NetworkStateHandlerTest : public testing::Test {
97 public:
98 NetworkStateHandlerTest() {}
99 virtual ~NetworkStateHandlerTest() {}
100
101 virtual void SetUp() OVERRIDE {
102 // Initialize DBusThreadManager with a stub implementation.
103 DBusThreadManager::InitializeWithStub();
104 }
105
106 virtual void TearDown() OVERRIDE {
107 network_state_handler_.reset();
108 test_observer_.reset();
109 DBusThreadManager::Shutdown();
110 }
111
112 void SetupNetworkStateHandler() {
113 test_observer_.reset(new TestObserver);
114 network_state_handler_.reset(new NetworkStateHandler);
115 network_state_handler_->AddObserver(test_observer_.get());
116 network_state_handler_->Init();
117 }
118
119 protected:
120 MessageLoopForUI message_loop_;
121 scoped_ptr<NetworkStateHandler> network_state_handler_;
122 scoped_ptr<TestObserver> test_observer_;
123
124 private:
125 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest);
126 };
127
128 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) {
129 // This relies on the stub dbus implementations for ShillManagerClient,
130 SetupNetworkStateHandler();
131 message_loop_.RunUntilIdle();
132 EXPECT_EQ(1u, test_observer_->manager_changed_count());
133 // Ensure that the network list is the expected size.
134 const size_t kNumShillManagerClientStubImplServices = 4;
135 EXPECT_EQ(kNumShillManagerClientStubImplServices,
136 test_observer_->network_count());
137 // Ensure that the first stub network is the active network.
138 const std::string kShillManagerClientStubActiveService = "stub_ethernet";
139 EXPECT_EQ(kShillManagerClientStubActiveService,
140 test_observer_->active_network());
141 EXPECT_EQ(kShillManagerClientStubActiveService,
142 network_state_handler_->ActiveNetwork()->path());
143 EXPECT_EQ(kShillManagerClientStubActiveService,
144 network_state_handler_->ConnectedNetworkByType(
145 flimflam::kTypeEthernet)->path());
146 EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state());
147 }
148
149 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerTechnologyChanged) {
150 // This relies on the stub dbus implementations for ShillManagerClient,
151 SetupNetworkStateHandler();
152 message_loop_.RunUntilIdle();
153 EXPECT_EQ(1u, test_observer_->manager_changed_count());
154 // Enable a technology.
155 EXPECT_FALSE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax));
156 network_state_handler_->SetTechnologyEnabled(flimflam::kTypeWimax, true);
157 message_loop_.RunUntilIdle();
158 // Ensure we get a manager changed callback when we change a property.
159 EXPECT_EQ(2u, test_observer_->manager_changed_count());
160 EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax));
161 }
162
163 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerServicePropertyChanged) {
164 // This relies on the stub dbus implementations for ShillManagerClient,
165 SetupNetworkStateHandler();
166 message_loop_.RunUntilIdle();
167 // Set a service property.
168 const std::string eth0 = "stub_ethernet";
169 EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security());
170 EXPECT_EQ(1, test_observer_->PropertyChangesForService(eth0));
171 base::StringValue security_value("TestSecurity");
172 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
173 dbus::ObjectPath(eth0),
174 flimflam::kSecurityProperty, security_value,
175 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
176 message_loop_.RunUntilIdle();
177 EXPECT_EQ("TestSecurity",
178 network_state_handler_->GetNetworkState(eth0)->security());
179 EXPECT_EQ(2, test_observer_->PropertyChangesForService(eth0));
180 }
181
182 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerActiveServiceChanged) {
183 // This relies on the stub dbus implementations for ShillManagerClient,
184 SetupNetworkStateHandler();
185 message_loop_.RunUntilIdle();
186
187 // Change the active network by inserting wifi1 at the front of the list.
188 ShillManagerClient::TestInterface* manager_test =
189 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
190 ASSERT_TRUE(manager_test);
191 const std::string wifi1 = "stub_wifi1";
192 manager_test->InsertService(wifi1, 0);
193 message_loop_.RunUntilIdle();
194 EXPECT_EQ(wifi1, test_observer_->active_network());
195 EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state());
196
197 // Change the state of wifi1, ensure that triggers the active state changed
198 // observer.
199 ShillServiceClient::TestInterface* service_test =
200 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
201 ASSERT_TRUE(service_test);
202 base::StringValue state_value(flimflam::kStateConfiguration);
203 service_test->SetServiceProperty(wifi1, flimflam::kStateProperty,
204 state_value);
205 message_loop_.RunUntilIdle();
206 EXPECT_EQ(wifi1, test_observer_->active_network());
207 EXPECT_EQ(flimflam::kStateConfiguration,
208 test_observer_->active_network_state());
209 }
210
211 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698