1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
7 #include "base/ios/weak_nsobject.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 TEST(WeakNSObjectTest, WeakNSObject) {
18 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
19 WeakNSObject<NSObject> w1(p1);
25 TEST(WeakNSObjectTest, MultipleWeakNSObject) {
26 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
27 WeakNSObject<NSObject> w1(p1);
28 WeakNSObject<NSObject> w2(w1);
31 EXPECT_TRUE(w1.get() == w2.get());
37 TEST(WeakNSObjectTest, WeakNSObjectDies) {
38 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
40 WeakNSObject<NSObject> w1(p1);
45 TEST(WeakNSObjectTest, WeakNSObjectReset) {
46 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
47 WeakNSObject<NSObject> w1(p1);
52 EXPECT_TRUE([p1 description]);
55 TEST(WeakNSObjectTest, WeakNSObjectResetWithObject) {
56 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
57 scoped_nsobject<NSObject> p2([[NSObject alloc] init]);
58 WeakNSObject<NSObject> w1(p1);
62 EXPECT_TRUE([p1 description]);
63 EXPECT_TRUE([p2 description]);
66 TEST(WeakNSObjectTest, WeakNSObjectEmpty) {
67 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
68 WeakNSObject<NSObject> w1;
76 TEST(WeakNSObjectTest, WeakNSObjectCopy) {
77 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
78 WeakNSObject<NSObject> w1(p1);
79 WeakNSObject<NSObject> w2(w1);
87 TEST(WeakNSObjectTest, WeakNSObjectAssignment) {
88 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
89 WeakNSObject<NSObject> w1(p1);
90 WeakNSObject<NSObject> w2;
100 // Touches |weak_data| by increasing its length by 1. Used to check that the
101 // weak object can be dereferenced.
102 void TouchWeakData(const WeakNSObject<NSMutableData>& weak_data) {
105 [weak_data increaseLengthBy:1];
108 // Makes a copy of |weak_object| on the current thread and posts a task to touch
109 // the weak object on its original thread.
110 void CopyWeakNSObjectAndPost(const WeakNSObject<NSMutableData>& weak_object,
111 scoped_refptr<SingleThreadTaskRunner> runner) {
112 // Copy using constructor.
113 WeakNSObject<NSMutableData> weak_copy1(weak_object);
114 runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy1));
115 // Copy using assignment operator.
116 WeakNSObject<NSMutableData> weak_copy2 = weak_object;
117 runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy2));
120 // Tests that the weak object can be copied on a different thread.
121 TEST(WeakNSObjectTest, WeakNSObjectCopyOnOtherThread) {
123 Thread other_thread("WeakNSObjectCopyOnOtherThread");
124 other_thread.Start();
126 scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]);
127 WeakNSObject<NSMutableData> weak(data);
129 scoped_refptr<SingleThreadTaskRunner> runner = loop.task_runner();
130 other_thread.task_runner()->PostTask(
131 FROM_HERE, Bind(&CopyWeakNSObjectAndPost, weak, runner));
135 // Check that TouchWeakData was called and the object touched twice.
136 EXPECT_EQ(2u, [data length]);