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 "chrome/browser/sync/util/weak_handle.h" | 5 #include "chrome/browser/sync/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/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace browser_sync { | 16 namespace browser_sync { |
17 namespace { | |
18 | 17 |
19 using ::testing::_; | 18 using ::testing::_; |
20 using ::testing::SaveArg; | 19 using ::testing::SaveArg; |
21 using ::testing::StrictMock; | 20 using ::testing::StrictMock; |
22 | 21 |
23 class Base { | 22 class Base { |
24 public: | 23 public: |
25 Base() : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | 24 Base() : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} |
26 | 25 |
27 WeakHandle<Base> AsWeakHandle() { | 26 WeakHandle<Base> AsWeakHandle() { |
28 return MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()); | 27 return MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()); |
29 } | 28 } |
30 | 29 |
31 void Kill() { | 30 void Kill() { |
32 weak_ptr_factory_.InvalidateWeakPtrs(); | 31 weak_ptr_factory_.InvalidateWeakPtrs(); |
33 } | 32 } |
34 | 33 |
35 MOCK_METHOD0(Test, void()); | 34 MOCK_METHOD0(Test, void()); |
36 MOCK_METHOD1(Test1, void(const int&)); | 35 MOCK_METHOD1(Test1, void(const int&)); |
37 MOCK_METHOD2(Test2, void(const int&, Base*)); | 36 MOCK_METHOD2(Test2, void(const int&, Base*)); |
38 MOCK_METHOD3(Test3, void(const int&, Base*, float)); | 37 MOCK_METHOD3(Test3, void(const int&, Base*, float)); |
39 MOCK_METHOD4(Test4, void(const int&, Base*, float, const char*)); | 38 MOCK_METHOD4(Test4, void(const int&, Base*, float, const char*)); |
40 | 39 |
41 MOCK_METHOD1(TestWithSelf, void(const WeakHandle<Base>&)); | 40 MOCK_METHOD1(TestWithSelf, void(const WeakHandle<Base>&)); |
42 | 41 |
43 private: | 42 private: |
44 base::WeakPtrFactory<Base> weak_ptr_factory_; | 43 base::WeakPtrFactory<Base> weak_ptr_factory_; |
45 }; | 44 }; |
46 | 45 |
| 46 class Derived : public Base, public base::SupportsWeakPtr<Derived> {}; |
| 47 |
47 class WeakHandleTest : public ::testing::Test { | 48 class WeakHandleTest : public ::testing::Test { |
48 protected: | 49 protected: |
49 virtual void TearDown() { | 50 virtual void TearDown() { |
50 // Process any last-minute posted tasks. | 51 // Process any last-minute posted tasks. |
51 PumpLoop(); | 52 PumpLoop(); |
52 } | 53 } |
53 | 54 |
54 void PumpLoop() { | 55 void PumpLoop() { |
55 message_loop_.RunAllPending(); | 56 message_loop_.RunAllPending(); |
56 } | 57 } |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 h = b.AsWeakHandle(); | 254 h = b.AsWeakHandle(); |
254 EXPECT_TRUE(h.IsInitialized()); | 255 EXPECT_TRUE(h.IsInitialized()); |
255 h.Call(FROM_HERE, &Base::Test); | 256 h.Call(FROM_HERE, &Base::Test); |
256 h.Reset(); | 257 h.Reset(); |
257 EXPECT_FALSE(h.IsInitialized()); | 258 EXPECT_FALSE(h.IsInitialized()); |
258 } | 259 } |
259 | 260 |
260 PumpLoop(); | 261 PumpLoop(); |
261 } | 262 } |
262 | 263 |
263 } // namespace | 264 TEST_F(WeakHandleTest, TypeConversionConstructor) { |
| 265 StrictMock<Derived> d; |
| 266 EXPECT_CALL(d, Test()).Times(2); |
| 267 |
| 268 const WeakHandle<Derived> weak_handle = MakeWeakHandle(d.AsWeakPtr()); |
| 269 |
| 270 // Should trigger type conversion constructor. |
| 271 const WeakHandle<Base> base_weak_handle(weak_handle); |
| 272 // Should trigger regular copy constructor. |
| 273 const WeakHandle<Derived> derived_weak_handle(weak_handle); |
| 274 |
| 275 EXPECT_TRUE(base_weak_handle.IsInitialized()); |
| 276 base_weak_handle.Call(FROM_HERE, &Base::Test); |
| 277 |
| 278 EXPECT_TRUE(derived_weak_handle.IsInitialized()); |
| 279 // Copy constructor shouldn't construct a new |core_|. |
| 280 EXPECT_EQ(weak_handle.core_.get(), derived_weak_handle.core_.get()); |
| 281 derived_weak_handle.Call(FROM_HERE, &Base::Test); |
| 282 |
| 283 PumpLoop(); |
| 284 } |
| 285 |
| 286 TEST_F(WeakHandleTest, TypeConversionConstructorMakeWeakHandle) { |
| 287 const base::WeakPtr<Derived> weak_ptr; |
| 288 |
| 289 // Should trigger type conversion constructor after MakeWeakHandle. |
| 290 WeakHandle<Base> base_weak_handle(MakeWeakHandle(weak_ptr)); |
| 291 // Should trigger regular copy constructor after MakeWeakHandle. |
| 292 const WeakHandle<Derived> derived_weak_handle(MakeWeakHandle(weak_ptr)); |
| 293 |
| 294 EXPECT_TRUE(base_weak_handle.IsInitialized()); |
| 295 EXPECT_TRUE(derived_weak_handle.IsInitialized()); |
| 296 } |
| 297 |
| 298 TEST_F(WeakHandleTest, TypeConversionConstructorAssignment) { |
| 299 const WeakHandle<Derived> weak_handle = |
| 300 MakeWeakHandle(Derived().AsWeakPtr()); |
| 301 |
| 302 // Should trigger type conversion constructor before the assignment. |
| 303 WeakHandle<Base> base_weak_handle; |
| 304 base_weak_handle = weak_handle; |
| 305 // Should trigger regular copy constructor before the assignment. |
| 306 WeakHandle<Derived> derived_weak_handle; |
| 307 derived_weak_handle = weak_handle; |
| 308 |
| 309 EXPECT_TRUE(base_weak_handle.IsInitialized()); |
| 310 EXPECT_TRUE(derived_weak_handle.IsInitialized()); |
| 311 // Copy constructor shouldn't construct a new |core_|. |
| 312 EXPECT_EQ(weak_handle.core_.get(), derived_weak_handle.core_.get()); |
| 313 } |
| 314 |
| 315 TEST_F(WeakHandleTest, TypeConversionConstructorUninitialized) { |
| 316 const WeakHandle<Base> base_weak_handle = WeakHandle<Derived>(); |
| 317 EXPECT_FALSE(base_weak_handle.IsInitialized()); |
| 318 } |
| 319 |
| 320 TEST_F(WeakHandleTest, TypeConversionConstructorUninitializedAssignment) { |
| 321 WeakHandle<Base> base_weak_handle; |
| 322 base_weak_handle = WeakHandle<Derived>(); |
| 323 EXPECT_FALSE(base_weak_handle.IsInitialized()); |
| 324 } |
| 325 |
264 } // namespace browser_sync | 326 } // namespace browser_sync |
OLD | NEW |