| 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 "sync/internal_api/public/util/weak_handle.h" | 5 #include "sync/internal_api/public/util/weak_handle.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace syncer { | 17 namespace syncer { |
| 17 | 18 |
| 18 using ::testing::_; | 19 using ::testing::_; |
| 19 using ::testing::SaveArg; | 20 using ::testing::SaveArg; |
| 20 using ::testing::StrictMock; | 21 using ::testing::StrictMock; |
| 21 | 22 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 } | 54 } |
| 54 | 55 |
| 55 void PumpLoop() { | 56 void PumpLoop() { |
| 56 base::RunLoop().RunUntilIdle(); | 57 base::RunLoop().RunUntilIdle(); |
| 57 } | 58 } |
| 58 | 59 |
| 59 static void CallTestFromOtherThread(tracked_objects::Location from_here, | 60 static void CallTestFromOtherThread(tracked_objects::Location from_here, |
| 60 const WeakHandle<Base>& h) { | 61 const WeakHandle<Base>& h) { |
| 61 base::Thread t("Test thread"); | 62 base::Thread t("Test thread"); |
| 62 ASSERT_TRUE(t.Start()); | 63 ASSERT_TRUE(t.Start()); |
| 63 t.message_loop()->PostTask( | 64 t.task_runner()->PostTask( |
| 64 from_here, base::Bind(&WeakHandleTest::CallTest, from_here, h)); | 65 from_here, base::Bind(&WeakHandleTest::CallTest, from_here, h)); |
| 65 } | 66 } |
| 66 | 67 |
| 67 private: | 68 private: |
| 68 static void CallTest(tracked_objects::Location from_here, | 69 static void CallTest(tracked_objects::Location from_here, |
| 69 const WeakHandle<Base>& h) { | 70 const WeakHandle<Base>& h) { |
| 70 h.Call(from_here, &Base::Test); | 71 h.Call(from_here, &Base::Test); |
| 71 } | 72 } |
| 72 | 73 |
| 73 base::MessageLoop message_loop_; | 74 base::MessageLoop message_loop_; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 194 |
| 194 TEST_F(WeakHandleTest, DeleteOnOtherThread) { | 195 TEST_F(WeakHandleTest, DeleteOnOtherThread) { |
| 195 StrictMock<Base> b; | 196 StrictMock<Base> b; |
| 196 EXPECT_CALL(b, Test()).Times(0); | 197 EXPECT_CALL(b, Test()).Times(0); |
| 197 | 198 |
| 198 WeakHandle<Base>* h = new WeakHandle<Base>(b.AsWeakHandle()); | 199 WeakHandle<Base>* h = new WeakHandle<Base>(b.AsWeakHandle()); |
| 199 | 200 |
| 200 { | 201 { |
| 201 base::Thread t("Test thread"); | 202 base::Thread t("Test thread"); |
| 202 ASSERT_TRUE(t.Start()); | 203 ASSERT_TRUE(t.Start()); |
| 203 t.message_loop()->DeleteSoon(FROM_HERE, h); | 204 t.task_runner()->DeleteSoon(FROM_HERE, h); |
| 204 } | 205 } |
| 205 | 206 |
| 206 PumpLoop(); | 207 PumpLoop(); |
| 207 } | 208 } |
| 208 | 209 |
| 209 void CallTestWithSelf(const WeakHandle<Base>& b1) { | 210 void CallTestWithSelf(const WeakHandle<Base>& b1) { |
| 210 StrictMock<Base> b2; | 211 StrictMock<Base> b2; |
| 211 b1.Call(FROM_HERE, &Base::TestWithSelf, b2.AsWeakHandle()); | 212 b1.Call(FROM_HERE, &Base::TestWithSelf, b2.AsWeakHandle()); |
| 212 } | 213 } |
| 213 | 214 |
| 214 TEST_F(WeakHandleTest, WithDestroyedThread) { | 215 TEST_F(WeakHandleTest, WithDestroyedThread) { |
| 215 StrictMock<Base> b1; | 216 StrictMock<Base> b1; |
| 216 WeakHandle<Base> b2; | 217 WeakHandle<Base> b2; |
| 217 EXPECT_CALL(b1, TestWithSelf(_)).WillOnce(SaveArg<0>(&b2)); | 218 EXPECT_CALL(b1, TestWithSelf(_)).WillOnce(SaveArg<0>(&b2)); |
| 218 | 219 |
| 219 { | 220 { |
| 220 base::Thread t("Test thread"); | 221 base::Thread t("Test thread"); |
| 221 ASSERT_TRUE(t.Start()); | 222 ASSERT_TRUE(t.Start()); |
| 222 t.message_loop()->PostTask(FROM_HERE, | 223 t.task_runner()->PostTask(FROM_HERE, |
| 223 base::Bind(&CallTestWithSelf, | 224 base::Bind(&CallTestWithSelf, b1.AsWeakHandle())); |
| 224 b1.AsWeakHandle())); | |
| 225 } | 225 } |
| 226 | 226 |
| 227 // Calls b1.TestWithSelf(). | 227 // Calls b1.TestWithSelf(). |
| 228 PumpLoop(); | 228 PumpLoop(); |
| 229 | 229 |
| 230 // Shouldn't do anything, since the thread is gone. | 230 // Shouldn't do anything, since the thread is gone. |
| 231 b2.Call(FROM_HERE, &Base::Test); | 231 b2.Call(FROM_HERE, &Base::Test); |
| 232 | 232 |
| 233 // |b2| shouldn't leak when it's destroyed, even if the original | 233 // |b2| shouldn't leak when it's destroyed, even if the original |
| 234 // thread is gone. | 234 // thread is gone. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 EXPECT_FALSE(base_weak_handle.IsInitialized()); | 317 EXPECT_FALSE(base_weak_handle.IsInitialized()); |
| 318 } | 318 } |
| 319 | 319 |
| 320 TEST_F(WeakHandleTest, TypeConversionConstructorUninitializedAssignment) { | 320 TEST_F(WeakHandleTest, TypeConversionConstructorUninitializedAssignment) { |
| 321 WeakHandle<Base> base_weak_handle; | 321 WeakHandle<Base> base_weak_handle; |
| 322 base_weak_handle = WeakHandle<Derived>(); | 322 base_weak_handle = WeakHandle<Derived>(); |
| 323 EXPECT_FALSE(base_weak_handle.IsInitialized()); | 323 EXPECT_FALSE(base_weak_handle.IsInitialized()); |
| 324 } | 324 } |
| 325 | 325 |
| 326 } // namespace syncer | 326 } // namespace syncer |
| OLD | NEW |