Chromium Code Reviews| 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 "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 namespace wifi { | |
|
stevenjb
2013/09/25 19:09:15
We should be consistent and either use 'namespace
mef
2013/10/08 21:46:26
Done.
| |
| 14 | |
| 15 class WiFiServiceTest : public testing::Test { | |
| 16 public: | |
| 17 void LogError(const std::string& error_name, | |
| 18 scoped_ptr<base::DictionaryValue> error_data) { | |
| 19 LOG(ERROR) << "WiFi Error: " << error_name; | |
| 20 QuitRunLoop(); | |
| 21 } | |
| 22 | |
| 23 void TestError(const std::string& error_name, | |
| 24 scoped_ptr<base::DictionaryValue> error_data) { | |
| 25 LOG(ERROR) << "WiFi Error: " << error_name; | |
| 26 FAIL(); | |
| 27 } | |
| 28 | |
| 29 void OnDictionaryResult(const std::string& network_guid, | |
| 30 const base::DictionaryValue& dictionary) {} | |
| 31 | |
| 32 void OnNetworkProperties(const std::string& network_guid, | |
| 33 const WiFiService::NetworkProperties& properties) { | |
| 34 LOG(INFO) << "OnNetworkProperties" << *properties.ToValue(false).release(); | |
| 35 } | |
| 36 | |
| 37 void OnNetworksChangedEventWaitingForConnect( | |
| 38 const WiFiService::NetworkGuidList& network_guid_list) { | |
| 39 LOG(INFO) << "Networks Changed: " << network_guid_list.front(); | |
| 40 // Check that network is now connected. | |
| 41 wifi_service_->GetProperties( | |
| 42 connected_network_guid_, | |
| 43 base::Bind(&WiFiServiceTest::WaitForConnect, base::Unretained(this)), | |
| 44 base::Bind(&WiFiServiceTest::TestError, base::Unretained(this))); | |
| 45 } | |
| 46 | |
| 47 void OnNetworkConnectStarted(const std::string& network_guid) { | |
| 48 LOG(INFO) << "Started Network Connect:" << network_guid; | |
| 49 wifi_service_->SetNetworksChangedObserver( | |
| 50 base::Bind(&WiFiServiceTest::OnNetworksChangedEventWaitingForConnect, | |
| 51 base::Unretained(this))); | |
| 52 base::MessageLoop::current()->PostDelayedTask( | |
| 53 FROM_HERE, | |
| 54 base::MessageLoop::QuitClosure(), | |
| 55 base::TimeDelta::FromSeconds(10)); | |
| 56 } | |
| 57 | |
| 58 void WaitForConnect(const std::string& network_guid, | |
| 59 const WiFiService::NetworkProperties& properties) { | |
| 60 LOG(INFO) << "WaitForConnect" << *properties.ToValue(false).release(); | |
| 61 if (WiFiService::kConnectionStateConnected == properties.connection_state) | |
| 62 QuitRunLoop(); | |
| 63 } | |
| 64 | |
| 65 void WaitForDisconnect(const std::string& network_guid, | |
| 66 const WiFiService::NetworkProperties& properties) { | |
| 67 LOG(INFO) << "WaitForDisconnect" << *properties.ToValue(false).release(); | |
| 68 EXPECT_EQ(WiFiService::kConnectionStateNotConnected, | |
| 69 properties.connection_state); | |
| 70 } | |
| 71 | |
| 72 void OnNetworkDisconnectStarted(const std::string& network_guid) { | |
| 73 LOG(INFO) << "Started Network Disconnect:" << network_guid; | |
| 74 // Check that Network state has changed to 'Not Connected'. | |
| 75 wifi_service_->GetProperties( | |
| 76 connected_network_guid_, | |
| 77 base::Bind(&WiFiServiceTest::WaitForDisconnect, base::Unretained(this)), | |
| 78 base::Bind(&WiFiServiceTest::TestError, base::Unretained(this))); | |
| 79 // Start connect back to the same network. | |
| 80 wifi_service_->StartConnect( | |
| 81 connected_network_guid_, | |
| 82 base::Bind(&WiFiServiceTest::OnNetworkConnectStarted, | |
| 83 base::Unretained(this)), | |
| 84 base::Bind(&WiFiServiceTest::TestError, base::Unretained(this))); | |
| 85 } | |
| 86 | |
| 87 void OnVisibleNetworks(const WiFiService::NetworkList& network_list) { | |
| 88 LOG(INFO) << "Visible WiFi Networks: " << network_list.size(); | |
| 89 } | |
| 90 | |
| 91 void FindConnectedNetwork(const WiFiService::NetworkList& network_list) { | |
| 92 for (WiFiService::NetworkList::const_iterator net = network_list.begin(); | |
| 93 net != network_list.end(); | |
| 94 ++net) { | |
| 95 if (net->connection_state == WiFiService::kConnectionStateConnected) { | |
| 96 connected_network_guid_ = net->guid; | |
| 97 LOG(INFO) << "Connected Network:\n" << *(net->ToValue(false).release()); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 if (!connected_network_guid_.empty()) { | |
| 102 wifi_service_->StartDisconnect( | |
| 103 connected_network_guid_, | |
| 104 base::Bind(&WiFiServiceTest::OnNetworkDisconnectStarted, | |
| 105 base::Unretained(this)), | |
| 106 base::Bind(&WiFiServiceTest::TestError, base::Unretained(this))); | |
| 107 } else { | |
| 108 LOG(INFO) << "No Connected Networks, skipping disconnect."; | |
| 109 QuitRunLoop(); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void QuitRunLoop() { | |
| 114 base::MessageLoop::current()->PostTask(FROM_HERE, | |
| 115 base::MessageLoop::QuitClosure()); | |
| 116 } | |
| 117 | |
| 118 private: | |
| 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()); | |
| 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( | |
| 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 |