OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "base/threading/thread.h" | 5 #include "base/threading/thread.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 virtual void WillDestroyCurrentMessageLoop() { | 98 virtual void WillDestroyCurrentMessageLoop() { |
99 event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED); | 99 event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED); |
100 event_list_ = NULL; | 100 event_list_ = NULL; |
101 } | 101 } |
102 | 102 |
103 private: | 103 private: |
104 EventList* event_list_; | 104 EventList* event_list_; |
105 }; | 105 }; |
106 | 106 |
107 // Task that adds a destruction observer to the current message loop. | 107 // Task that adds a destruction observer to the current message loop. |
108 class RegisterDestructionObserver : public Task { | 108 void RegisterDestructionObserver(MessageLoop::DestructionObserver* observer) { |
109 public: | 109 MessageLoop::current()->AddDestructionObserver(observer); |
110 explicit RegisterDestructionObserver( | 110 } |
111 MessageLoop::DestructionObserver* observer) | |
112 : observer_(observer) { | |
113 } | |
114 | |
115 virtual void Run() { | |
116 MessageLoop::current()->AddDestructionObserver(observer_); | |
117 observer_ = NULL; | |
118 } | |
119 | |
120 private: | |
121 MessageLoop::DestructionObserver* observer_; | |
122 }; | |
123 | 111 |
124 } // namespace | 112 } // namespace |
125 | 113 |
126 TEST_F(ThreadTest, Restart) { | 114 TEST_F(ThreadTest, Restart) { |
127 Thread a("Restart"); | 115 Thread a("Restart"); |
128 a.Stop(); | 116 a.Stop(); |
129 EXPECT_FALSE(a.message_loop()); | 117 EXPECT_FALSE(a.message_loop()); |
130 EXPECT_FALSE(a.IsRunning()); | 118 EXPECT_FALSE(a.IsRunning()); |
131 EXPECT_TRUE(a.Start()); | 119 EXPECT_TRUE(a.Start()); |
132 EXPECT_TRUE(a.message_loop()); | 120 EXPECT_TRUE(a.message_loop()); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 { | 215 { |
228 // Start a thread which writes its event into |captured_events|. | 216 // Start a thread which writes its event into |captured_events|. |
229 CaptureToEventList t(&captured_events); | 217 CaptureToEventList t(&captured_events); |
230 EXPECT_TRUE(t.Start()); | 218 EXPECT_TRUE(t.Start()); |
231 EXPECT_TRUE(t.message_loop()); | 219 EXPECT_TRUE(t.message_loop()); |
232 EXPECT_TRUE(t.IsRunning()); | 220 EXPECT_TRUE(t.IsRunning()); |
233 | 221 |
234 // Register an observer that writes into |captured_events| once the | 222 // Register an observer that writes into |captured_events| once the |
235 // thread's message loop is destroyed. | 223 // thread's message loop is destroyed. |
236 t.message_loop()->PostTask( | 224 t.message_loop()->PostTask( |
237 FROM_HERE, | 225 FROM_HERE, base::Bind(&RegisterDestructionObserver, |
238 new RegisterDestructionObserver(&loop_destruction_observer)); | 226 base::Unretained(&loop_destruction_observer))); |
239 | 227 |
240 // Upon leaving this scope, the thread is deleted. | 228 // Upon leaving this scope, the thread is deleted. |
241 } | 229 } |
242 | 230 |
243 // Check the order of events during shutdown. | 231 // Check the order of events during shutdown. |
244 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); | 232 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); |
245 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); | 233 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); |
246 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); | 234 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); |
247 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); | 235 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); |
248 } | 236 } |
OLD | NEW |