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/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
346 TEST(WeakPtrTest, Dereference) { | 346 TEST(WeakPtrTest, Dereference) { |
347 Base data; | 347 Base data; |
348 data.member = "123456"; | 348 data.member = "123456"; |
349 WeakPtrFactory<Base> factory(&data); | 349 WeakPtrFactory<Base> factory(&data); |
350 WeakPtr<Base> ptr = factory.GetWeakPtr(); | 350 WeakPtr<Base> ptr = factory.GetWeakPtr(); |
351 EXPECT_EQ(&data, ptr.get()); | 351 EXPECT_EQ(&data, ptr.get()); |
352 EXPECT_EQ(data.member, (*ptr).member); | 352 EXPECT_EQ(data.member, (*ptr).member); |
353 EXPECT_EQ(data.member, ptr->member); | 353 EXPECT_EQ(data.member, ptr->member); |
354 } | 354 } |
355 | 355 |
356 struct WithRefCounting : RefCounted<WithRefCounting>, | |
dominich
2012/06/18 15:32:44
I don't understand this. The WeakPtr is already re
| |
357 SupportsWeakPtr<WithRefCounting> { | |
358 protected: | |
359 friend class base::RefCounted<WithRefCounting>; | |
360 ~WithRefCounting() {} | |
361 }; | |
362 | |
363 TEST(WeakPtrTest, WithRefCounting) { | |
364 scoped_refptr<WithRefCounting> ref_ptr(new WithRefCounting); | |
365 WeakPtr<WithRefCounting> weak_ptr = ref_ptr->AsWeakPtr(); | |
366 EXPECT_EQ(ref_ptr.get(), weak_ptr.get()); | |
367 ref_ptr = NULL; | |
368 EXPECT_FALSE(weak_ptr); | |
369 } | |
370 | |
371 struct SubtleWithRefCounting : WithRefCounting { | |
372 SubtleWithRefCounting() : self(base::AsWeakPtr(this)) {} | |
373 private: | |
374 ~SubtleWithRefCounting() { | |
375 EXPECT_EQ(this, self); | |
376 } | |
377 | |
378 WeakPtr<SubtleWithRefCounting> self; | |
379 }; | |
380 | |
381 TEST(WeakPtrTest, SubtleWithRefCounting) { | |
382 scoped_refptr<SubtleWithRefCounting> ref_ptr(new SubtleWithRefCounting); | |
383 } | |
384 | |
356 } // namespace base | 385 } // namespace base |
OLD | NEW |