OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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 "base/bind.h" | |
6 #include "base/bind_helpers.h" | |
7 #include "base/logging.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/run_loop.h" | |
10 #include "chrome/utility/wifi/wifi_service.h" | |
11 #include "components/onc/onc_constants.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace wifi { | |
15 | |
16 class WiFiServiceTest : public testing::Test { | |
17 public: | |
18 void LogError(const std::string& error_name, | |
19 scoped_ptr<base::DictionaryValue> error_data) { | |
20 LOG(ERROR) << "WiFi Error: " << error_name; | |
21 QuitRunLoop(); | |
22 } | |
23 | |
24 void TestError(const std::string& error_name, | |
25 scoped_ptr<base::DictionaryValue> error_data) { | |
26 LOG(ERROR) << "WiFi Error: " << error_name; | |
27 FAIL(); | |
28 } | |
29 | |
30 void OnDictionaryResult(const std::string& network_guid, | |
31 const base::DictionaryValue& dictionary) {} | |
32 | |
33 void OnNetworkProperties(const std::string& network_guid, | |
34 const WiFiService::NetworkProperties& properties) { | |
35 LOG(INFO) << "OnNetworkProperties" << *properties.ToValue(false).release(); | |
36 } | |
37 | |
38 void OnNetworksChangedEventWaitingForConnect( | |
39 const WiFiService::NetworkGuidList& network_guid_list) { | |
40 LOG(INFO) << "Networks Changed: " << network_guid_list.front(); | |
41 // Check that network is now connected. | |
42 wifi_service_->GetProperties( | |
43 connected_network_guid_, | |
44 base::Bind(&WiFiServiceTest::WaitForConnect, base::Unretained(this)), | |
45 base::Bind(&WiFiServiceTest::TestError, base::Unretained(this))); | |
46 } | |
47 | |
48 void OnNetworkConnectStarted(const std::string& network_guid) { | |
49 LOG(INFO) << "Started Network Connect:" << network_guid; | |
50 wifi_service_->SetNetworksChangedObserver( | |
51 base::Bind(&WiFiServiceTest::OnNetworksChangedEventWaitingForConnect, | |
52 base::Unretained(this))); | |
53 base::MessageLoop::current()->PostDelayedTask( | |
54 FROM_HERE, | |
55 base::MessageLoop::QuitClosure(), | |
56 base::TimeDelta::FromSeconds(10)); | |
57 } | |
58 | |
59 void WaitForConnect(const std::string& network_guid, | |
60 const WiFiService::NetworkProperties& properties) { | |
61 LOG(INFO) << "WaitForConnect" << *properties.ToValue(false).release(); | |
62 if (properties.connection_state == onc::connection_state::kConnected) | |
63 QuitRunLoop(); | |
64 } | |
65 | |
66 void WaitForDisconnect(const std::string& network_guid, | |
67 const WiFiService::NetworkProperties& properties) { | |
68 LOG(INFO) << "WaitForDisconnect" << *properties.ToValue(false).release(); | |
69 EXPECT_TRUE(properties.connection_state == | |
70 onc::connection_state::kNotConnected); | |
71 } | |
72 | |
73 void OnNetworkDisconnectStarted(const std::string& network_guid) { | |
74 LOG(INFO) << "Started Network Disconnect:" << network_guid; | |
75 // Check that Network state has changed to 'Not Connected'. | |
76 wifi_service_->GetProperties( | |
77 connected_network_guid_, | |
78 base::Bind(&WiFiServiceTest::WaitForDisconnect, base::Unretained(this)), | |
79 base::Bind(&WiFiServiceTest::TestError, base::Unretained(this))); | |
80 // Start connect back to the same network. | |
81 wifi_service_->StartConnect( | |
82 connected_network_guid_, | |
83 base::Bind(&WiFiServiceTest::OnNetworkConnectStarted, | |
84 base::Unretained(this)), | |
85 base::Bind(&WiFiServiceTest::TestError, base::Unretained(this))); | |
86 } | |
87 | |
88 void OnVisibleNetworks(const WiFiService::NetworkList& network_list) { | |
89 LOG(INFO) << "Visible WiFi Networks: " << network_list.size(); | |
90 } | |
91 | |
92 void FindConnectedNetwork(const WiFiService::NetworkList& network_list) { | |
93 for (WiFiService::NetworkList::const_iterator net = network_list.begin(); | |
94 net != network_list.end(); | |
95 ++net) { | |
96 if (net->connection_state == onc::connection_state::kConnected) { | |
97 connected_network_guid_ = net->guid; | |
98 LOG(INFO) << "Connected Network:\n" << *(net->ToValue(false).release()); | |
99 } | |
100 } | |
101 | |
102 if (!connected_network_guid_.empty()) { | |
103 wifi_service_->StartDisconnect( | |
104 connected_network_guid_, | |
105 base::Bind(&WiFiServiceTest::OnNetworkDisconnectStarted, | |
106 base::Unretained(this)), | |
107 base::Bind(&WiFiServiceTest::TestError, base::Unretained(this))); | |
108 } else { | |
109 LOG(INFO) << "No Connected Networks, skipping disconnect."; | |
110 QuitRunLoop(); | |
111 } | |
112 } | |
113 | |
114 void QuitRunLoop() { | |
115 base::MessageLoop::current()->PostTask(FROM_HERE, | |
116 base::MessageLoop::QuitClosure()); | |
117 } | |
118 | |
119 protected: | |
120 virtual void SetUp() { wifi_service_.reset(WiFiService::CreateService()); } | |
121 virtual void TearDown() {} | |
122 | |
123 std::string connected_network_guid_; | |
124 scoped_ptr<WiFiService> wifi_service_; | |
125 base::MessageLoop loop_; | |
126 }; | |
127 | |
128 // Test getting properties of network with invalid network guid. | |
129 TEST_F(WiFiServiceTest, GetProperties) { | |
130 wifi_service_.reset(WiFiService::CreateServiceMock()); | |
cbentzel
2013/10/16 12:51:36
I don't think we should be testing mock implementa
| |
131 wifi_service_->GetProperties( | |
132 "invalid network_guid", | |
133 base::Bind(&WiFiServiceTest::OnNetworkProperties, base::Unretained(this)), | |
134 base::Bind(&WiFiServiceTest::LogError, base::Unretained(this))); | |
135 } | |
136 | |
137 // Test getting list of visible networks. | |
138 TEST_F(WiFiServiceTest, GetVisibleNetworks) { | |
139 wifi_service_->GetVisibleNetworks( | |
cbentzel
2013/10/16 12:51:36
Will this work? It doesn't instantiate wifi_servic
| |
140 base::Bind(&WiFiServiceTest::OnVisibleNetworks, base::Unretained(this)), | |
141 base::Bind(&WiFiServiceTest::LogError, base::Unretained(this))); | |
142 } | |
143 | |
144 // Test that connected WiFi network can be disconnected and reconnected. | |
145 // Disabled to avoid network connection interruption unless enabled explicitly. | |
146 TEST_F(WiFiServiceTest, DISABLED_Reconnect) { | |
147 base::RunLoop run_loop; | |
148 wifi_service_->GetVisibleNetworks( | |
149 base::Bind(&WiFiServiceTest::FindConnectedNetwork, | |
150 base::Unretained(this)), | |
151 base::Bind(&WiFiServiceTest::LogError, base::Unretained(this))); | |
152 run_loop.Run(); | |
153 } | |
154 | |
155 } // namespace wifi | |
OLD | NEW |