1 // Copyright (c) 2011 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 <string> 6 7 #include "base/memory/scoped_ptr.h" 8 #include "base/message_loop.h" 9 #include "base/string16.h" 10 #include "base/string_number_conversions.h" 11 #include "base/utf_string_conversions.h" 12 #include "chrome/browser/chromeos/cros/cros_library.h" 13 #include "chrome/browser/chromeos/cros/mock_login_library.h" 14 #include "chrome/browser/chromeos/cros/mock_network_library.h" 15 #include "chrome/browser/chromeos/cros/network_library.h" 16 #include "chrome/browser/chromeos/login/mock_screen_observer.h" 17 #include "chrome/browser/chromeos/login/network_screen.h" 18 #include "chrome/browser/chromeos/login/network_selection_view.h" 19 #include "chrome/browser/chromeos/login/view_screen.h" 20 #include "chrome/browser/chromeos/login/wizard_controller.h" 21 #include "chrome/browser/chromeos/login/wizard_in_process_browser_test.h" 22 #include "chrome/browser/chromeos/login/wizard_screen.h" 23 #include "chrome/test/ui_test_utils.h" 24 #include "grit/generated_resources.h" 25 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "ui/base/l10n/l10n_util.h" 28 #include "views/controls/button/text_button.h" 29 30 namespace chromeos { 31 using ::testing::AnyNumber; 32 using ::testing::InvokeWithoutArgs; 33 using ::testing::Return; 34 using ::testing::ReturnRef; 35 using ::testing::_; 36 using ::testing::A; 37 using views::Button; 38 39 class DummyButtonListener : public views::ButtonListener { 40 public: 41 virtual void ButtonPressed(views::Button* sender, 42 const views::Event& event) {} 43 }; 44 45 class NetworkScreenTest : public WizardInProcessBrowserTest { 46 public: 47 NetworkScreenTest(): WizardInProcessBrowserTest("network"), 48 mock_login_library_(NULL), 49 mock_network_library_(NULL) { 50 } 51 52 protected: 53 virtual void SetUpInProcessBrowserTestFixture() { 54 cros_mock_->InitStatusAreaMocks(); 55 mock_network_library_ = cros_mock_->mock_network_library(); 56 mock_login_library_ = new MockLoginLibrary(); 57 cellular_.reset(new NetworkDevice("cellular")); 58 cros_mock_->test_api()->SetLoginLibrary(mock_login_library_, true); 59 EXPECT_CALL(*mock_login_library_, EmitLoginPromptReady()) 60 .Times(1); 61 EXPECT_CALL(*mock_login_library_,RequestRetrieveProperty(_,_,_)) 62 .Times(AnyNumber()); 63 64 // Minimal set of expectations needed on NetworkScreen initialization. 65 // Status bar expectations are defined with RetiresOnSaturation() so 66 // these mocks will be active once status bar is initialized. 67 EXPECT_CALL(*mock_network_library_, ethernet_connected()) 68 .Times(2) 69 .WillRepeatedly(Return(false)); 70 EXPECT_CALL(*mock_network_library_, ethernet_connecting()) 71 .Times(2) 72 .WillRepeatedly(Return(false)); 73 EXPECT_CALL(*mock_network_library_, wifi_connected()) 74 .Times(1) 75 .WillRepeatedly(Return(false)); 76 EXPECT_CALL(*mock_network_library_, FindWifiDevice()) 77 .Times(AnyNumber()); 78 EXPECT_CALL(*mock_network_library_, FindEthernetDevice()) 79 .Times(AnyNumber()); 80 81 cros_mock_->SetStatusAreaMocksExpectations(); 82 83 // Override these return values, but do not set specific expectation: 84 EXPECT_CALL(*mock_network_library_, wifi_available()) 85 .Times(AnyNumber()) 86 .WillRepeatedly((Return(true))); 87 EXPECT_CALL(*mock_network_library_, wifi_enabled()) 88 .Times(AnyNumber()) 89 .WillRepeatedly((Return(true))); 90 EXPECT_CALL(*mock_network_library_, wifi_connecting()) 91 .Times(AnyNumber()) 92 .WillRepeatedly((Return(false))); 93 EXPECT_CALL(*mock_network_library_, wifi_scanning()) 94 .Times(AnyNumber()) 95 .WillRepeatedly((Return(false))); 96 EXPECT_CALL(*mock_network_library_, cellular_available()) 97 .Times(AnyNumber()) 98 .WillRepeatedly((Return(true))); 99 EXPECT_CALL(*mock_network_library_, cellular_enabled()) 100 .Times(AnyNumber()) 101 .WillRepeatedly((Return(true))); 102 EXPECT_CALL(*mock_network_library_, cellular_connecting()) 103 .Times(AnyNumber()) 104 .WillRepeatedly((Return(false))); 105 106 EXPECT_CALL(*mock_network_library_, FindCellularDevice()) 107 .Times(AnyNumber()) 108 .WillRepeatedly((Return(cellular_.get()))); 109 } 110 111 virtual void TearDownInProcessBrowserTestFixture() { 112 CrosInProcessBrowserTest::TearDownInProcessBrowserTestFixture(); 113 cros_mock_->test_api()->SetLoginLibrary(NULL, false); 114 } 115 116 void EmulateContinueButtonExit(NetworkScreen* network_screen) { 117 scoped_ptr<MockScreenObserver> 118 mock_screen_observer(new MockScreenObserver()); 119 EXPECT_CALL(*mock_screen_observer, 120 OnExit(ScreenObserver::NETWORK_CONNECTED)) 121 .Times(1); 122 EXPECT_CALL(*mock_network_library_, Connected()) 123 .WillOnce(Return(true)); 124 controller()->set_observer(mock_screen_observer.get()); 125 DummyButtonListener button_listener; 126 views::TextButton button(&button_listener, L"Button"); 127 views::MouseEvent event(ui::ET_MOUSE_RELEASED, 128 0, 0, 129 ui::EF_LEFT_BUTTON_DOWN); 130 network_screen->ButtonPressed(&button, event); 131 ui_test_utils::RunAllPendingInMessageLoop(); 132 controller()->set_observer(NULL); 133 } 134 135 MockLoginLibrary* mock_login_library_; 136 MockNetworkLibrary* mock_network_library_; 137 scoped_ptr<NetworkDevice> cellular_; 138 139 private: 140 DISALLOW_COPY_AND_ASSIGN(NetworkScreenTest); 141 }; 142 143 IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Ethernet) { 144 ASSERT_TRUE(controller()); 145 NetworkScreen* network_screen = controller()->GetNetworkScreen(); 146 ASSERT_TRUE(network_screen != NULL); 147 ASSERT_EQ(network_screen, controller()->current_screen()); 148 149 NetworkSelectionView* network_view = network_screen->view(); 150 ASSERT_TRUE(network_view != NULL); 151 152 EXPECT_CALL(*mock_network_library_, ethernet_connected()) 153 .WillOnce((Return(false))); 154 EXPECT_CALL(*mock_network_library_, wifi_connected()) 155 .WillOnce((Return(false))); 156 EXPECT_CALL(*mock_network_library_, cellular_connected()) 157 .WillOnce((Return(false))); 158 EXPECT_CALL(*mock_network_library_, ethernet_connecting()) 159 .WillOnce((Return(true))); 160 EXPECT_FALSE(network_view->IsContinueEnabled()); 161 network_screen->OnNetworkManagerChanged(mock_network_library_); 162 163 EXPECT_CALL(*mock_network_library_, ethernet_connected()) 164 .WillOnce(Return(true)); 165 EXPECT_CALL(*mock_network_library_, Connected()) 166 .Times(3) 167 .WillRepeatedly(Return(true)); 168 EXPECT_FALSE(network_view->IsContinueEnabled()); 169 EXPECT_FALSE(network_view->IsConnecting()); 170 network_screen->OnNetworkManagerChanged(mock_network_library_); 171 172 EXPECT_TRUE(network_view->IsContinueEnabled()); 173 EmulateContinueButtonExit(network_screen); 174 } 175 176 IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Wifi) { 177 ASSERT_TRUE(controller()); 178 NetworkScreen* network_screen = controller()->GetNetworkScreen(); 179 ASSERT_TRUE(network_screen != NULL); 180 ASSERT_EQ(network_screen, controller()->current_screen()); 181 182 NetworkSelectionView* network_view = network_screen->view(); 183 ASSERT_TRUE(network_view != NULL); 184 185 EXPECT_CALL(*mock_network_library_, ethernet_connected()) 186 .WillOnce((Return(false))); 187 EXPECT_CALL(*mock_network_library_, wifi_connected()) 188 .WillOnce((Return(false))); 189 EXPECT_CALL(*mock_network_library_, cellular_connected()) 190 .WillOnce((Return(false))); 191 EXPECT_CALL(*mock_network_library_, ethernet_connecting()) 192 .WillOnce((Return(false))); 193 EXPECT_CALL(*mock_network_library_, wifi_connecting()) 194 .WillOnce((Return(true))); 195 scoped_ptr<WifiNetwork> wifi(new WifiNetwork("wifi")); 196 WifiNetworkVector wifi_networks; 197 wifi_networks.push_back(wifi.get()); 198 EXPECT_CALL(*mock_network_library_, wifi_network()) 199 .WillRepeatedly(Return(wifi.get())); 200 EXPECT_CALL(*mock_network_library_, wifi_networks()) 201 .WillRepeatedly(ReturnRef(wifi_networks)); 202 EXPECT_FALSE(network_view->IsContinueEnabled()); 203 network_screen->OnNetworkManagerChanged(mock_network_library_); 204 205 EXPECT_CALL(*mock_network_library_, ethernet_connected()) 206 .WillOnce(Return(true)); 207 EXPECT_CALL(*mock_network_library_, Connected()) 208 .Times(3) 209 .WillRepeatedly(Return(true)); 210 EXPECT_FALSE(network_view->IsContinueEnabled()); 211 EXPECT_FALSE(network_view->IsConnecting()); 212 network_screen->OnNetworkManagerChanged(mock_network_library_); 213 214 EXPECT_TRUE(network_view->IsContinueEnabled()); 215 EmulateContinueButtonExit(network_screen); 216 } 217 218 IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Cellular) { 219 ASSERT_TRUE(controller()); 220 NetworkScreen* network_screen = controller()->GetNetworkScreen(); 221 ASSERT_TRUE(network_screen != NULL); 222 ASSERT_EQ(network_screen, controller()->current_screen()); 223 224 NetworkSelectionView* network_view = network_screen->view(); 225 ASSERT_TRUE(network_view != NULL); 226 227 EXPECT_CALL(*mock_network_library_, ethernet_connected()) 228 .WillOnce((Return(false))); 229 EXPECT_CALL(*mock_network_library_, wifi_connected()) 230 .WillOnce((Return(false))); 231 EXPECT_CALL(*mock_network_library_, cellular_connected()) 232 .WillOnce((Return(false))); 233 EXPECT_CALL(*mock_network_library_, ethernet_connecting()) 234 .WillOnce((Return(false))); 235 EXPECT_CALL(*mock_network_library_, wifi_connecting()) 236 .WillOnce((Return(false))); 237 EXPECT_CALL(*mock_network_library_, cellular_connecting()) 238 .WillOnce((Return(true))); 239 scoped_ptr<CellularNetwork> cellular(new CellularNetwork("cellular")); 240 EXPECT_CALL(*mock_network_library_, cellular_network()) 241 .WillOnce(Return(cellular.get())); 242 EXPECT_FALSE(network_view->IsContinueEnabled()); 243 network_screen->OnNetworkManagerChanged(mock_network_library_); 244 245 EXPECT_CALL(*mock_network_library_, ethernet_connected()) 246 .WillOnce(Return(true)); 247 EXPECT_CALL(*mock_network_library_, Connected()) 248 .Times(3) 249 .WillRepeatedly(Return(true)); 250 EXPECT_FALSE(network_view->IsContinueEnabled()); 251 EXPECT_FALSE(network_view->IsConnecting()); 252 network_screen->OnNetworkManagerChanged(mock_network_library_); 253 254 EXPECT_TRUE(network_view->IsContinueEnabled()); 255 EmulateContinueButtonExit(network_screen); 256 } 257 258 IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Timeout) { 259 ASSERT_TRUE(controller()); 260 NetworkScreen* network_screen = controller()->GetNetworkScreen(); 261 ASSERT_TRUE(network_screen != NULL); 262 ASSERT_EQ(network_screen, controller()->current_screen()); 263 264 NetworkSelectionView* network_view = network_screen->view(); 265 ASSERT_TRUE(network_view != NULL); 266 267 EXPECT_CALL(*mock_network_library_, ethernet_connected()) 268 .WillOnce((Return(false))); 269 EXPECT_CALL(*mock_network_library_, wifi_connected()) 270 .WillOnce((Return(false))); 271 EXPECT_CALL(*mock_network_library_, cellular_connected()) 272 .WillOnce((Return(false))); 273 EXPECT_CALL(*mock_network_library_, ethernet_connecting()) 274 .WillOnce((Return(false))); 275 EXPECT_CALL(*mock_network_library_, wifi_connecting()) 276 .WillOnce((Return(true))); 277 scoped_ptr<WifiNetwork> wifi(new WifiNetwork("wifi")); 278 EXPECT_CALL(*mock_network_library_, wifi_network()) 279 .WillOnce(Return(wifi.get())); 280 EXPECT_FALSE(network_view->IsContinueEnabled()); 281 network_screen->OnNetworkManagerChanged(mock_network_library_); 282 283 EXPECT_CALL(*mock_network_library_, Connected()) 284 .Times(2) 285 .WillRepeatedly(Return(false)); 286 EXPECT_FALSE(network_view->IsContinueEnabled()); 287 EXPECT_FALSE(network_view->IsConnecting()); 288 network_screen->OnConnectionTimeout(); 289 290 // Close infobubble with error message - it makes the test stable. 291 EXPECT_FALSE(network_view->IsContinueEnabled()); 292 EXPECT_FALSE(network_view->IsConnecting()); 293 network_screen->ClearErrors(); 294 } 295 296 } // namespace chromeos 297