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_checker.h" |
| 6 |
5 #include <memory> | 7 #include <memory> |
6 | 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
9 #include "base/macros.h" | 11 #include "base/macros.h" |
10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
11 #include "base/sequence_token.h" | 13 #include "base/sequence_token.h" |
| 14 #include "base/test/gtest_util.h" |
12 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
13 #include "base/threading/simple_thread.h" | 16 #include "base/threading/simple_thread.h" |
14 #include "base/threading/thread_checker_impl.h" | |
15 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 | 19 |
18 namespace base { | 20 namespace base { |
19 namespace { | 21 namespace { |
20 | 22 |
21 // A thread that runs a callback. | 23 // A thread that runs a callback. |
22 class RunCallbackThread : public SimpleThread { | 24 class RunCallbackThread : public SimpleThread { |
23 public: | 25 public: |
24 explicit RunCallbackThread(const Closure& callback) | 26 explicit RunCallbackThread(const Closure& callback) |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 thread_checker.DetachFromThread(); | 187 thread_checker.DetachFromThread(); |
186 | 188 |
187 // Verify that CalledOnValidThread() returns true when called on a different | 189 // Verify that CalledOnValidThread() returns true when called on a different |
188 // thread after a call to DetachFromThread(). | 190 // thread after a call to DetachFromThread(). |
189 RunCallbackOnNewThreadSynchronously( | 191 RunCallbackOnNewThreadSynchronously( |
190 Bind(&ExpectCalledOnValidThread, Unretained(&thread_checker))); | 192 Bind(&ExpectCalledOnValidThread, Unretained(&thread_checker))); |
191 | 193 |
192 EXPECT_FALSE(thread_checker.CalledOnValidThread()); | 194 EXPECT_FALSE(thread_checker.CalledOnValidThread()); |
193 } | 195 } |
194 | 196 |
| 197 TEST(ThreadCheckerTest, Macros) { |
| 198 THREAD_CHECKER(my_thread_checker); |
| 199 |
| 200 RunCallbackOnNewThreadSynchronously(Bind([](ThreadChecker* thread_checker) { |
| 201 #if DCHECK_IS_ON() |
| 202 EXPECT_DCHECK_DEATH({ DCHECK_CALLED_ON_VALID_THREAD(*thread_checker); }); |
| 203 #else |
| 204 // Happily no-ops on non-dcheck builds. |
| 205 DCHECK_CALLED_ON_VALID_THREAD(*thread_checker); |
| 206 #endif |
| 207 }, Unretained(&my_thread_checker))); |
| 208 |
| 209 DETACH_FROM_THREAD(my_thread_checker); |
| 210 |
| 211 RunCallbackOnNewThreadSynchronously(Bind([](ThreadChecker* thread_checker) { |
| 212 // Happily passes after detaching from original thread. |
| 213 DCHECK_CALLED_ON_VALID_THREAD(*thread_checker); |
| 214 }, Unretained(&my_thread_checker))); |
| 215 } |
| 216 |
195 } // namespace base | 217 } // namespace base |
OLD | NEW |