| 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 "components/sync/base/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/single_thread_task_runner.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace syncer { | 17 namespace syncer { |
| 18 | 18 |
| 19 using ::testing::_; | 19 using ::testing::_; |
| 20 using ::testing::SaveArg; | 20 using ::testing::SaveArg; |
| 21 using ::testing::StrictMock; | 21 using ::testing::StrictMock; |
| 22 | 22 |
| 23 class Base { | 23 class Base { |
| 24 public: | 24 public: |
| 25 Base() : weak_ptr_factory_(this) {} | 25 Base() : weak_ptr_factory_(this) {} |
| 26 | 26 |
| 27 WeakHandle<Base> AsWeakHandle() { | 27 WeakHandle<Base> AsWeakHandle() { |
| 28 return MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()); | 28 return MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void Kill() { | 31 void Kill() { weak_ptr_factory_.InvalidateWeakPtrs(); } |
| 32 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 33 } | |
| 34 | 32 |
| 35 MOCK_METHOD0(Test, void()); | 33 MOCK_METHOD0(Test, void()); |
| 36 MOCK_METHOD1(Test1, void(const int&)); | 34 MOCK_METHOD1(Test1, void(const int&)); |
| 37 MOCK_METHOD2(Test2, void(const int&, Base*)); | 35 MOCK_METHOD2(Test2, void(const int&, Base*)); |
| 38 MOCK_METHOD3(Test3, void(const int&, Base*, float)); | 36 MOCK_METHOD3(Test3, void(const int&, Base*, float)); |
| 39 MOCK_METHOD4(Test4, void(const int&, Base*, float, const char*)); | 37 MOCK_METHOD4(Test4, void(const int&, Base*, float, const char*)); |
| 40 | 38 |
| 41 MOCK_METHOD1(TestWithSelf, void(const WeakHandle<Base>&)); | 39 MOCK_METHOD1(TestWithSelf, void(const WeakHandle<Base>&)); |
| 42 | 40 |
| 43 private: | 41 private: |
| 44 base::WeakPtrFactory<Base> weak_ptr_factory_; | 42 base::WeakPtrFactory<Base> weak_ptr_factory_; |
| 45 }; | 43 }; |
| 46 | 44 |
| 47 class Derived : public Base, public base::SupportsWeakPtr<Derived> {}; | 45 class Derived : public Base, public base::SupportsWeakPtr<Derived> {}; |
| 48 | 46 |
| 49 class WeakHandleTest : public ::testing::Test { | 47 class WeakHandleTest : public ::testing::Test { |
| 50 protected: | 48 protected: |
| 51 void TearDown() override { | 49 void TearDown() override { |
| 52 // Process any last-minute posted tasks. | 50 // Process any last-minute posted tasks. |
| 53 PumpLoop(); | 51 PumpLoop(); |
| 54 } | 52 } |
| 55 | 53 |
| 56 void PumpLoop() { | 54 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
| 57 base::RunLoop().RunUntilIdle(); | |
| 58 } | |
| 59 | 55 |
| 60 static void CallTestFromOtherThread(tracked_objects::Location from_here, | 56 static void CallTestFromOtherThread(tracked_objects::Location from_here, |
| 61 const WeakHandle<Base>& h) { | 57 const WeakHandle<Base>& h) { |
| 62 base::Thread t("Test thread"); | 58 base::Thread t("Test thread"); |
| 63 ASSERT_TRUE(t.Start()); | 59 ASSERT_TRUE(t.Start()); |
| 64 t.task_runner()->PostTask( | 60 t.task_runner()->PostTask( |
| 65 from_here, base::Bind(&WeakHandleTest::CallTest, from_here, h)); | 61 from_here, base::Bind(&WeakHandleTest::CallTest, from_here, h)); |
| 66 } | 62 } |
| 67 | 63 |
| 68 private: | 64 private: |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 // Should trigger type conversion constructor after MakeWeakHandle. | 285 // Should trigger type conversion constructor after MakeWeakHandle. |
| 290 WeakHandle<Base> base_weak_handle(MakeWeakHandle(weak_ptr)); | 286 WeakHandle<Base> base_weak_handle(MakeWeakHandle(weak_ptr)); |
| 291 // Should trigger regular copy constructor after MakeWeakHandle. | 287 // Should trigger regular copy constructor after MakeWeakHandle. |
| 292 const WeakHandle<Derived> derived_weak_handle(MakeWeakHandle(weak_ptr)); | 288 const WeakHandle<Derived> derived_weak_handle(MakeWeakHandle(weak_ptr)); |
| 293 | 289 |
| 294 EXPECT_TRUE(base_weak_handle.IsInitialized()); | 290 EXPECT_TRUE(base_weak_handle.IsInitialized()); |
| 295 EXPECT_TRUE(derived_weak_handle.IsInitialized()); | 291 EXPECT_TRUE(derived_weak_handle.IsInitialized()); |
| 296 } | 292 } |
| 297 | 293 |
| 298 TEST_F(WeakHandleTest, TypeConversionConstructorAssignment) { | 294 TEST_F(WeakHandleTest, TypeConversionConstructorAssignment) { |
| 299 const WeakHandle<Derived> weak_handle = | 295 const WeakHandle<Derived> weak_handle = MakeWeakHandle(Derived().AsWeakPtr()); |
| 300 MakeWeakHandle(Derived().AsWeakPtr()); | |
| 301 | 296 |
| 302 // Should trigger type conversion constructor before the assignment. | 297 // Should trigger type conversion constructor before the assignment. |
| 303 WeakHandle<Base> base_weak_handle; | 298 WeakHandle<Base> base_weak_handle; |
| 304 base_weak_handle = weak_handle; | 299 base_weak_handle = weak_handle; |
| 305 // Should trigger regular copy constructor before the assignment. | 300 // Should trigger regular copy constructor before the assignment. |
| 306 WeakHandle<Derived> derived_weak_handle; | 301 WeakHandle<Derived> derived_weak_handle; |
| 307 derived_weak_handle = weak_handle; | 302 derived_weak_handle = weak_handle; |
| 308 | 303 |
| 309 EXPECT_TRUE(base_weak_handle.IsInitialized()); | 304 EXPECT_TRUE(base_weak_handle.IsInitialized()); |
| 310 EXPECT_TRUE(derived_weak_handle.IsInitialized()); | 305 EXPECT_TRUE(derived_weak_handle.IsInitialized()); |
| 311 // Copy constructor shouldn't construct a new |core_|. | 306 // Copy constructor shouldn't construct a new |core_|. |
| 312 EXPECT_EQ(weak_handle.core_.get(), derived_weak_handle.core_.get()); | 307 EXPECT_EQ(weak_handle.core_.get(), derived_weak_handle.core_.get()); |
| 313 } | 308 } |
| 314 | 309 |
| 315 TEST_F(WeakHandleTest, TypeConversionConstructorUninitialized) { | 310 TEST_F(WeakHandleTest, TypeConversionConstructorUninitialized) { |
| 316 const WeakHandle<Base> base_weak_handle = WeakHandle<Derived>(); | 311 const WeakHandle<Base> base_weak_handle = WeakHandle<Derived>(); |
| 317 EXPECT_FALSE(base_weak_handle.IsInitialized()); | 312 EXPECT_FALSE(base_weak_handle.IsInitialized()); |
| 318 } | 313 } |
| 319 | 314 |
| 320 TEST_F(WeakHandleTest, TypeConversionConstructorUninitializedAssignment) { | 315 TEST_F(WeakHandleTest, TypeConversionConstructorUninitializedAssignment) { |
| 321 WeakHandle<Base> base_weak_handle; | 316 WeakHandle<Base> base_weak_handle; |
| 322 base_weak_handle = WeakHandle<Derived>(); | 317 base_weak_handle = WeakHandle<Derived>(); |
| 323 EXPECT_FALSE(base_weak_handle.IsInitialized()); | 318 EXPECT_FALSE(base_weak_handle.IsInitialized()); |
| 324 } | 319 } |
| 325 | 320 |
| 326 } // namespace syncer | 321 } // namespace syncer |
| OLD | NEW |