Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: base/ios/weak_nsobject_unittest.mm

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/ios/weak_nsobject.mm ('k') | base/json/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/basictypes.h"
6 #include "base/bind.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"
13
14 namespace base {
15 namespace {
16
17 TEST(WeakNSObjectTest, WeakNSObject) {
18 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
19 WeakNSObject<NSObject> w1(p1);
20 EXPECT_TRUE(w1);
21 p1.reset();
22 EXPECT_FALSE(w1);
23 }
24
25 TEST(WeakNSObjectTest, MultipleWeakNSObject) {
26 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
27 WeakNSObject<NSObject> w1(p1);
28 WeakNSObject<NSObject> w2(w1);
29 EXPECT_TRUE(w1);
30 EXPECT_TRUE(w2);
31 EXPECT_TRUE(w1.get() == w2.get());
32 p1.reset();
33 EXPECT_FALSE(w1);
34 EXPECT_FALSE(w2);
35 }
36
37 TEST(WeakNSObjectTest, WeakNSObjectDies) {
38 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
39 {
40 WeakNSObject<NSObject> w1(p1);
41 EXPECT_TRUE(w1);
42 }
43 }
44
45 TEST(WeakNSObjectTest, WeakNSObjectReset) {
46 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
47 WeakNSObject<NSObject> w1(p1);
48 EXPECT_TRUE(w1);
49 w1.reset();
50 EXPECT_FALSE(w1);
51 EXPECT_TRUE(p1);
52 EXPECT_TRUE([p1 description]);
53 }
54
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);
59 EXPECT_TRUE(w1);
60 w1.reset(p2);
61 EXPECT_TRUE(w1);
62 EXPECT_TRUE([p1 description]);
63 EXPECT_TRUE([p2 description]);
64 }
65
66 TEST(WeakNSObjectTest, WeakNSObjectEmpty) {
67 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
68 WeakNSObject<NSObject> w1;
69 EXPECT_FALSE(w1);
70 w1.reset(p1);
71 EXPECT_TRUE(w1);
72 p1.reset();
73 EXPECT_FALSE(w1);
74 }
75
76 TEST(WeakNSObjectTest, WeakNSObjectCopy) {
77 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
78 WeakNSObject<NSObject> w1(p1);
79 WeakNSObject<NSObject> w2(w1);
80 EXPECT_TRUE(w1);
81 EXPECT_TRUE(w2);
82 p1.reset();
83 EXPECT_FALSE(w1);
84 EXPECT_FALSE(w2);
85 }
86
87 TEST(WeakNSObjectTest, WeakNSObjectAssignment) {
88 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
89 WeakNSObject<NSObject> w1(p1);
90 WeakNSObject<NSObject> w2;
91 EXPECT_FALSE(w2);
92 w2 = w1;
93 EXPECT_TRUE(w1);
94 EXPECT_TRUE(w2);
95 p1.reset();
96 EXPECT_FALSE(w1);
97 EXPECT_FALSE(w2);
98 }
99
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) {
103 if (!weak_data)
104 return;
105 [weak_data increaseLengthBy:1];
106 }
107
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));
118 }
119
120 // Tests that the weak object can be copied on a different thread.
121 TEST(WeakNSObjectTest, WeakNSObjectCopyOnOtherThread) {
122 MessageLoop loop;
123 Thread other_thread("WeakNSObjectCopyOnOtherThread");
124 other_thread.Start();
125
126 scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]);
127 WeakNSObject<NSMutableData> weak(data);
128
129 scoped_refptr<SingleThreadTaskRunner> runner = loop.task_runner();
130 other_thread.task_runner()->PostTask(
131 FROM_HERE, Bind(&CopyWeakNSObjectAndPost, weak, runner));
132 other_thread.Stop();
133 loop.RunUntilIdle();
134
135 // Check that TouchWeakData was called and the object touched twice.
136 EXPECT_EQ(2u, [data length]);
137 }
138
139 } // namespace
140 } // namespace base
OLDNEW
« no previous file with comments | « base/ios/weak_nsobject.mm ('k') | base/json/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698