OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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/service/net/service_network_change_notifier_thread.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/scoped_ptr.h" | |
10 #include "base/task.h" | |
11 #include "base/thread.h" | |
12 #include "chrome/common/net/thread_blocker.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace { | |
16 | |
17 class ServiceNetworkChangeNotifierThreadTest : public testing::Test { | |
18 protected: | |
19 ServiceNetworkChangeNotifierThreadTest() | |
20 : io_thread_("ServiceNetworkChangeNotifierThreadTest_IO") { | |
21 } | |
22 | |
23 virtual ~ServiceNetworkChangeNotifierThreadTest() {} | |
24 | |
25 virtual void SetUp() { | |
26 // We need to set the message loop type explicitly because | |
27 // IOThread doesn't do it for us and the Linux | |
28 // NetworkChangeNotifier expects it. | |
29 base::Thread::Options options; | |
30 options.message_loop_type = MessageLoop::TYPE_IO; | |
31 EXPECT_TRUE(io_thread_.StartWithOptions(options)); | |
32 thread_blocker_.reset(new chrome_common_net::ThreadBlocker(&io_thread_)); | |
33 thread_blocker_->Block(); | |
34 } | |
35 | |
36 virtual void TearDown() { | |
37 // Nothing should be posted on |io_thread_| at this point. | |
38 thread_blocker_->Unblock(); | |
39 thread_blocker_.reset(); | |
40 io_thread_.Stop(); | |
41 } | |
42 | |
43 base::Thread io_thread_; | |
44 scoped_ptr<chrome_common_net::ThreadBlocker> thread_blocker_; | |
45 | |
46 private: | |
47 DISALLOW_COPY_AND_ASSIGN(ServiceNetworkChangeNotifierThreadTest); | |
48 }; | |
49 | |
50 void CheckNonNullNetworkChangeNotifier( | |
51 ServiceNetworkChangeNotifierThread* network_change_notifier_thread) { | |
52 EXPECT_EQ(network_change_notifier_thread->GetMessageLoop(), | |
53 MessageLoop::current()); | |
54 EXPECT_TRUE( | |
55 network_change_notifier_thread->GetNetworkChangeNotifier() != NULL); | |
56 } | |
57 | |
58 void CheckNullNetworkChangeNotifier( | |
59 ServiceNetworkChangeNotifierThread* network_change_notifier_thread) { | |
60 EXPECT_EQ(network_change_notifier_thread->GetMessageLoop(), | |
61 MessageLoop::current()); | |
62 EXPECT_TRUE( | |
63 network_change_notifier_thread->GetNetworkChangeNotifier() == NULL); | |
64 } | |
65 | |
66 TEST_F(ServiceNetworkChangeNotifierThreadTest, Basic) { | |
67 scoped_refptr<ServiceNetworkChangeNotifierThread> change_notifier_thread = | |
68 new ServiceNetworkChangeNotifierThread(io_thread_.message_loop()); | |
69 EXPECT_EQ(io_thread_.message_loop(), | |
70 change_notifier_thread->GetMessageLoop()); | |
71 change_notifier_thread->Initialize(); | |
72 io_thread_.message_loop()->PostTask( | |
73 FROM_HERE, | |
74 NewRunnableFunction(&CheckNonNullNetworkChangeNotifier, | |
75 change_notifier_thread.get())); | |
76 // Pump the thread to make sure the task we just posted is run | |
77 // before this test ends. | |
78 thread_blocker_->Unblock(); | |
79 thread_blocker_->Block(); | |
80 } | |
81 | |
82 TEST_F(ServiceNetworkChangeNotifierThreadTest, Uninitialized) { | |
83 scoped_refptr<ServiceNetworkChangeNotifierThread> change_notifier_thread = | |
84 new ServiceNetworkChangeNotifierThread(io_thread_.message_loop()); | |
85 EXPECT_EQ(io_thread_.message_loop(), | |
86 change_notifier_thread->GetMessageLoop()); | |
87 // We have not called Initialize. Expect the GetNetworkChangeNotifier() call | |
88 // to return NULL. | |
89 io_thread_.message_loop()->PostTask( | |
90 FROM_HERE, | |
91 NewRunnableFunction(&CheckNullNetworkChangeNotifier, | |
92 change_notifier_thread.get())); | |
93 // Pump the thread to make sure the task we just posted is run | |
94 // before this test ends. | |
95 thread_blocker_->Unblock(); | |
96 thread_blocker_->Block(); | |
97 } | |
98 | |
99 } // namespace | |
100 | |
OLD | NEW |