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 "chrome/browser/chromeos/network_state_notifier.h" 6 7 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" 8 #include "chrome/browser/chromeos/cros/mock_network_library.h" 9 #include "chrome/test/ui_test_utils.h" 10 #include "content/browser/browser_thread.h" 11 #include "content/common/notification_registrar.h" 12 #include "content/common/notification_service.h" 13 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace chromeos { 17 18 using ::testing::Return; 19 using ::testing::_; 20 21 class NetworkStateNotifierTest : public CrosInProcessBrowserTest, 22 public NotificationObserver { 23 public: 24 NetworkStateNotifierTest() : mock_network_library_(NULL) { 25 } 26 27 protected: 28 virtual void SetUpInProcessBrowserTestFixture() { 29 cros_mock_->InitStatusAreaMocks(); 30 cros_mock_->SetStatusAreaMocksExpectations(); 31 // Initialize network state notifier. 32 ASSERT_TRUE(CrosLibrary::Get()->EnsureLoaded()); 33 mock_network_library_ = cros_mock_->mock_network_library(); 34 ASSERT_TRUE(mock_network_library_); 35 EXPECT_CALL(*mock_network_library_, Connected()) 36 .Times(1) 37 .WillRepeatedly((Return(true))) 38 .RetiresOnSaturation(); 39 NetworkStateNotifier::GetInstance(); 40 } 41 42 // NotificationObserver overrides. 43 virtual void Observe(NotificationType type, 44 const NotificationSource& source, 45 const NotificationDetails& details) { 46 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); 47 EXPECT_TRUE(NotificationType::NETWORK_STATE_CHANGED == type); 48 chromeos::NetworkStateDetails* state_details = 49 Details<chromeos::NetworkStateDetails>(details).ptr(); 50 state_ = state_details->state(); 51 } 52 53 void WaitForNotification() { 54 ui_test_utils::WaitForNotification( 55 NotificationType::NETWORK_STATE_CHANGED); 56 } 57 58 protected: 59 MockNetworkLibrary *mock_network_library_; 60 NetworkStateDetails::State state_; 61 }; 62 63 IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestConnected) { 64 // NETWORK_STATE_CHAGNED has to be registered in UI thread. 65 NotificationRegistrar registrar; 66 registrar.Add(this, NotificationType::NETWORK_STATE_CHANGED, 67 NotificationService::AllSources()); 68 EXPECT_CALL(*mock_network_library_, Connected()) 69 .Times(1) 70 .WillRepeatedly((Return(true))) 71 .RetiresOnSaturation(); 72 NetworkStateNotifier* notifier = NetworkStateNotifier::GetInstance(); 73 notifier->OnNetworkManagerChanged(mock_network_library_); 74 WaitForNotification(); 75 EXPECT_EQ(chromeos::NetworkStateDetails::CONNECTED, state_); 76 } 77 78 IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestConnecting) { 79 NotificationRegistrar registrar; 80 registrar.Add(this, NotificationType::NETWORK_STATE_CHANGED, 81 NotificationService::AllSources()); 82 EXPECT_CALL(*mock_network_library_, Connected()) 83 .Times(1) 84 .WillOnce((Return(false))) 85 .RetiresOnSaturation(); 86 EXPECT_CALL(*mock_network_library_, Connecting()) 87 .Times(1) 88 .WillOnce((Return(true))) 89 .RetiresOnSaturation(); 90 NetworkStateNotifier* notifier = NetworkStateNotifier::GetInstance(); 91 notifier->OnNetworkManagerChanged(mock_network_library_); 92 WaitForNotification(); 93 EXPECT_EQ(chromeos::NetworkStateDetails::CONNECTING, state_); 94 } 95 96 IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestDisconnected) { 97 NotificationRegistrar registrar; 98 registrar.Add(this, NotificationType::NETWORK_STATE_CHANGED, 99 NotificationService::AllSources()); 100 EXPECT_CALL(*mock_network_library_, Connected()) 101 .Times(1) 102 .WillOnce((Return(false))) 103 .RetiresOnSaturation(); 104 EXPECT_CALL(*mock_network_library_, Connecting()) 105 .Times(1) 106 .WillOnce((Return(false))) 107 .RetiresOnSaturation(); 108 NetworkStateNotifier* notifier = NetworkStateNotifier::GetInstance(); 109 notifier->OnNetworkManagerChanged(mock_network_library_); 110 WaitForNotification(); 111 EXPECT_EQ(chromeos::NetworkStateDetails::DISCONNECTED, state_); 112 } 113 114 } // namespace chromeos 115