OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 virtual void CleanUp() OVERRIDE { | 81 virtual void CleanUp() OVERRIDE { |
82 event_list_->push_back(THREAD_EVENT_CLEANUP); | 82 event_list_->push_back(THREAD_EVENT_CLEANUP); |
83 } | 83 } |
84 | 84 |
85 private: | 85 private: |
86 EventList* event_list_; | 86 EventList* event_list_; |
87 }; | 87 }; |
88 | 88 |
89 // Observer that writes a value into |event_list| when a message loop has been | 89 // Observer that writes a value into |event_list| when a message loop has been |
90 // destroyed. | 90 // destroyed. |
91 class CapturingDestructionObserver : public MessageLoop::DestructionObserver { | 91 class CapturingDestructionObserver |
| 92 : public base::MessageLoop::DestructionObserver { |
92 public: | 93 public: |
93 // |event_list| must remain valid throughout the observer's lifetime. | 94 // |event_list| must remain valid throughout the observer's lifetime. |
94 explicit CapturingDestructionObserver(EventList* event_list) | 95 explicit CapturingDestructionObserver(EventList* event_list) |
95 : event_list_(event_list) { | 96 : event_list_(event_list) { |
96 } | 97 } |
97 | 98 |
98 // DestructionObserver implementation: | 99 // DestructionObserver implementation: |
99 virtual void WillDestroyCurrentMessageLoop() OVERRIDE { | 100 virtual void WillDestroyCurrentMessageLoop() OVERRIDE { |
100 event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED); | 101 event_list_->push_back(THREAD_EVENT_MESSAGE_LOOP_DESTROYED); |
101 event_list_ = NULL; | 102 event_list_ = NULL; |
102 } | 103 } |
103 | 104 |
104 private: | 105 private: |
105 EventList* event_list_; | 106 EventList* event_list_; |
106 }; | 107 }; |
107 | 108 |
108 // Task that adds a destruction observer to the current message loop. | 109 // Task that adds a destruction observer to the current message loop. |
109 void RegisterDestructionObserver(MessageLoop::DestructionObserver* observer) { | 110 void RegisterDestructionObserver( |
110 MessageLoop::current()->AddDestructionObserver(observer); | 111 base::MessageLoop::DestructionObserver* observer) { |
| 112 base::MessageLoop::current()->AddDestructionObserver(observer); |
111 } | 113 } |
112 | 114 |
113 } // namespace | 115 } // namespace |
114 | 116 |
115 TEST_F(ThreadTest, Restart) { | 117 TEST_F(ThreadTest, Restart) { |
116 Thread a("Restart"); | 118 Thread a("Restart"); |
117 a.Stop(); | 119 a.Stop(); |
118 EXPECT_FALSE(a.message_loop()); | 120 EXPECT_FALSE(a.message_loop()); |
119 EXPECT_FALSE(a.IsRunning()); | 121 EXPECT_FALSE(a.IsRunning()); |
120 EXPECT_TRUE(a.Start()); | 122 EXPECT_TRUE(a.Start()); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 | 230 |
229 // Upon leaving this scope, the thread is deleted. | 231 // Upon leaving this scope, the thread is deleted. |
230 } | 232 } |
231 | 233 |
232 // Check the order of events during shutdown. | 234 // Check the order of events during shutdown. |
233 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); | 235 ASSERT_EQ(static_cast<size_t>(THREAD_NUM_EVENTS), captured_events.size()); |
234 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); | 236 EXPECT_EQ(THREAD_EVENT_INIT, captured_events[0]); |
235 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); | 237 EXPECT_EQ(THREAD_EVENT_CLEANUP, captured_events[1]); |
236 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); | 238 EXPECT_EQ(THREAD_EVENT_MESSAGE_LOOP_DESTROYED, captured_events[2]); |
237 } | 239 } |
OLD | NEW |