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