Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(667)

Side by Side Diff: chrome/browser/chromeos/network_state_notifier_browsertest.cc

Issue 2943006: Use WaitForNotification instead of busy loop. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: " Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/network_state_notifier.h" 5 #include "chrome/browser/chromeos/network_state_notifier.h"
6 6
7 #include "chrome/browser/chrome_thread.h" 7 #include "chrome/browser/chrome_thread.h"
8 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" 8 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h"
9 #include "chrome/browser/chromeos/cros/mock_network_library.h" 9 #include "chrome/browser/chromeos/cros/mock_network_library.h"
10 #include "chrome/common/notification_service.h" 10 #include "chrome/common/notification_service.h"
11 #include "chrome/common/notification_registrar.h" 11 #include "chrome/common/notification_registrar.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "chrome/test/ui_test_utils.h" 14 #include "chrome/test/ui_test_utils.h"
15 15
16 namespace chromeos { 16 namespace chromeos {
17 17
18 using ::testing::Return; 18 using ::testing::Return;
19 using ::testing::_; 19 using ::testing::_;
20 20
21 class NetworkStateNotifierTest : public CrosInProcessBrowserTest, 21 class NetworkStateNotifierTest : public CrosInProcessBrowserTest,
22 public NotificationObserver { 22 public NotificationObserver {
23 public: 23 public:
24 NetworkStateNotifierTest() 24 NetworkStateNotifierTest() {
25 : notification_received_(false) {
26 } 25 }
27 26
28 protected: 27 protected:
29 virtual void SetUpInProcessBrowserTestFixture() { 28 virtual void SetUpInProcessBrowserTestFixture() {
30 InitStatusAreaMocks(); 29 InitStatusAreaMocks();
31 SetStatusAreaMocksExpectations(); 30 SetStatusAreaMocksExpectations();
32 // Initialize network state notifier. 31 // Initialize network state notifier.
33 ASSERT_TRUE(CrosLibrary::Get()->EnsureLoaded()); 32 ASSERT_TRUE(CrosLibrary::Get()->EnsureLoaded());
34 ASSERT_TRUE(mock_network_library_); 33 ASSERT_TRUE(mock_network_library_);
35 EXPECT_CALL(*mock_network_library_, Connected()) 34 EXPECT_CALL(*mock_network_library_, Connected())
36 .Times(1) 35 .Times(1)
37 .WillRepeatedly((Return(true))) 36 .WillRepeatedly((Return(true)))
38 .RetiresOnSaturation(); 37 .RetiresOnSaturation();
39 NetworkStateNotifier::Get(); 38 NetworkStateNotifier::Get();
40 } 39 }
41 40
42 // NotificationObserver overrides. 41 // NotificationObserver overrides.
43 virtual void Observe(NotificationType type, 42 virtual void Observe(NotificationType type,
44 const NotificationSource& source, 43 const NotificationSource& source,
45 const NotificationDetails& details) { 44 const NotificationDetails& details) {
46 EXPECT_TRUE(ChromeThread::CurrentlyOn(ChromeThread::UI)); 45 EXPECT_TRUE(ChromeThread::CurrentlyOn(ChromeThread::UI));
47 EXPECT_TRUE(NotificationType::NETWORK_STATE_CHANGED == type); 46 EXPECT_TRUE(NotificationType::NETWORK_STATE_CHANGED == type);
48 chromeos::NetworkStateDetails* state_details = 47 chromeos::NetworkStateDetails* state_details =
49 Details<chromeos::NetworkStateDetails>(details).ptr(); 48 Details<chromeos::NetworkStateDetails>(details).ptr();
50 state_ = state_details->state(); 49 state_ = state_details->state();
51 notification_received_ = true;
52 } 50 }
53 51
54 void WaitForNotification() { 52 void WaitForNotification() {
55 notification_received_ = false; 53 ui_test_utils::WaitForNotification(
56 while (!notification_received_) { 54 NotificationType::NETWORK_STATE_CHANGED);
57 ui_test_utils::RunAllPendingInMessageLoop();
58 }
59 } 55 }
60 56
61 protected: 57 protected:
62 NetworkStateDetails::State state_; 58 NetworkStateDetails::State state_;
63 bool notification_received_;
64 }; 59 };
65 60
66 IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestConnected) { 61 IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestConnected) {
67 // NETWORK_STATE_CHAGNED has to be registered in UI thread. 62 // NETWORK_STATE_CHAGNED has to be registered in UI thread.
68 NotificationRegistrar registrar; 63 NotificationRegistrar registrar;
69 registrar.Add(this, NotificationType::NETWORK_STATE_CHANGED, 64 registrar.Add(this, NotificationType::NETWORK_STATE_CHANGED,
70 NotificationService::AllSources()); 65 NotificationService::AllSources());
71 EXPECT_CALL(*mock_network_library_, Connected()) 66 EXPECT_CALL(*mock_network_library_, Connected())
72 .Times(1) 67 .Times(1)
73 .WillRepeatedly((Return(true))) 68 .WillRepeatedly((Return(true)))
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 .Times(1) 103 .Times(1)
109 .WillOnce((Return(false))) 104 .WillOnce((Return(false)))
110 .RetiresOnSaturation(); 105 .RetiresOnSaturation();
111 NetworkStateNotifier* notifier = NetworkStateNotifier::Get(); 106 NetworkStateNotifier* notifier = NetworkStateNotifier::Get();
112 notifier->NetworkChanged(mock_network_library_); 107 notifier->NetworkChanged(mock_network_library_);
113 WaitForNotification(); 108 WaitForNotification();
114 EXPECT_EQ(chromeos::NetworkStateDetails::DISCONNECTED, state_); 109 EXPECT_EQ(chromeos::NetworkStateDetails::DISCONNECTED, state_);
115 } 110 }
116 111
117 } // namespace chromeos 112 } // namespace chromeos
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698