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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/mac/scoped_nsautorelease_pool.h" | 8 #include "base/mac/scoped_nsautorelease_pool.h" |
9 #include "base/memory/scoped_nsobject.h" | 9 #include "base/mac/scoped_nsobject.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 TEST(ScopedNSObjectTest, ScopedNSObject) { | 14 TEST(ScopedNSObjectTest, ScopedNSObject) { |
15 scoped_nsobject<NSObject> p1([[NSObject alloc] init]); | 15 base::scoped_nsobject<NSObject> p1([[NSObject alloc] init]); |
16 ASSERT_TRUE(p1.get()); | 16 ASSERT_TRUE(p1.get()); |
17 ASSERT_EQ(1u, [p1 retainCount]); | 17 ASSERT_EQ(1u, [p1 retainCount]); |
18 scoped_nsobject<NSObject> p2(p1); | 18 base::scoped_nsobject<NSObject> p2(p1); |
19 ASSERT_EQ(p1.get(), p2.get()); | 19 ASSERT_EQ(p1.get(), p2.get()); |
20 ASSERT_EQ(2u, [p1 retainCount]); | 20 ASSERT_EQ(2u, [p1 retainCount]); |
21 p2.reset(); | 21 p2.reset(); |
22 ASSERT_EQ(nil, p2.get()); | 22 ASSERT_EQ(nil, p2.get()); |
23 ASSERT_EQ(1u, [p1 retainCount]); | 23 ASSERT_EQ(1u, [p1 retainCount]); |
24 { | 24 { |
25 scoped_nsobject<NSObject> p3 = p1; | 25 base::scoped_nsobject<NSObject> p3 = p1; |
26 ASSERT_EQ(p1.get(), p3.get()); | 26 ASSERT_EQ(p1.get(), p3.get()); |
27 ASSERT_EQ(2u, [p1 retainCount]); | 27 ASSERT_EQ(2u, [p1 retainCount]); |
28 p3 = p1; | 28 p3 = p1; |
29 ASSERT_EQ(p1.get(), p3.get()); | 29 ASSERT_EQ(p1.get(), p3.get()); |
30 ASSERT_EQ(2u, [p1 retainCount]); | 30 ASSERT_EQ(2u, [p1 retainCount]); |
31 } | 31 } |
32 ASSERT_EQ(1u, [p1 retainCount]); | 32 ASSERT_EQ(1u, [p1 retainCount]); |
33 scoped_nsobject<NSObject> p4([p1.get() retain]); | 33 base::scoped_nsobject<NSObject> p4([p1.get() retain]); |
34 ASSERT_EQ(2u, [p1 retainCount]); | 34 ASSERT_EQ(2u, [p1 retainCount]); |
35 ASSERT_TRUE(p1 == p1.get()); | 35 ASSERT_TRUE(p1 == p1.get()); |
36 ASSERT_TRUE(p1 == p1); | 36 ASSERT_TRUE(p1 == p1); |
37 ASSERT_FALSE(p1 != p1); | 37 ASSERT_FALSE(p1 != p1); |
38 ASSERT_FALSE(p1 != p1.get()); | 38 ASSERT_FALSE(p1 != p1.get()); |
39 scoped_nsobject<NSObject> p5([[NSObject alloc] init]); | 39 base::scoped_nsobject<NSObject> p5([[NSObject alloc] init]); |
40 ASSERT_TRUE(p1 != p5); | 40 ASSERT_TRUE(p1 != p5); |
41 ASSERT_TRUE(p1 != p5.get()); | 41 ASSERT_TRUE(p1 != p5.get()); |
42 ASSERT_FALSE(p1 == p5); | 42 ASSERT_FALSE(p1 == p5); |
43 ASSERT_FALSE(p1 == p5.get()); | 43 ASSERT_FALSE(p1 == p5.get()); |
44 | 44 |
45 scoped_nsobject<NSObject> p6 = p1; | 45 base::scoped_nsobject<NSObject> p6 = p1; |
46 ASSERT_EQ(3u, [p6 retainCount]); | 46 ASSERT_EQ(3u, [p6 retainCount]); |
47 { | 47 { |
48 base::mac::ScopedNSAutoreleasePool pool; | 48 base::mac::ScopedNSAutoreleasePool pool; |
49 p6.autorelease(); | 49 p6.autorelease(); |
50 ASSERT_EQ(nil, p6.get()); | 50 ASSERT_EQ(nil, p6.get()); |
51 ASSERT_EQ(3u, [p1 retainCount]); | 51 ASSERT_EQ(3u, [p1 retainCount]); |
52 } | 52 } |
53 ASSERT_EQ(2u, [p1 retainCount]); | 53 ASSERT_EQ(2u, [p1 retainCount]); |
54 } | 54 } |
55 | 55 |
56 TEST(ScopedNSObjectTest, ScopedNSObjectInContainer) { | 56 TEST(ScopedNSObjectTest, ScopedNSObjectInContainer) { |
57 scoped_nsobject<id> p([[NSObject alloc] init]); | 57 base::scoped_nsobject<id> p([[NSObject alloc] init]); |
58 ASSERT_TRUE(p.get()); | 58 ASSERT_TRUE(p.get()); |
59 ASSERT_EQ(1u, [p retainCount]); | 59 ASSERT_EQ(1u, [p retainCount]); |
60 { | 60 { |
61 std::vector<scoped_nsobject<id> > objects; | 61 std::vector<base::scoped_nsobject<id>> objects; |
62 objects.push_back(p); | 62 objects.push_back(p); |
63 ASSERT_EQ(2u, [p retainCount]); | 63 ASSERT_EQ(2u, [p retainCount]); |
64 ASSERT_EQ(p.get(), objects[0].get()); | 64 ASSERT_EQ(p.get(), objects[0].get()); |
65 objects.push_back(scoped_nsobject<id>([[NSObject alloc] init])); | 65 objects.push_back(base::scoped_nsobject<id>([[NSObject alloc] init])); |
66 ASSERT_TRUE(objects[1].get()); | 66 ASSERT_TRUE(objects[1].get()); |
67 ASSERT_EQ(1u, [objects[1] retainCount]); | 67 ASSERT_EQ(1u, [objects[1] retainCount]); |
68 } | 68 } |
69 ASSERT_EQ(1u, [p retainCount]); | 69 ASSERT_EQ(1u, [p retainCount]); |
70 } | 70 } |
71 | 71 |
72 TEST(ScopedNSObjectTest, ScopedNSObjectFreeFunctions) { | 72 TEST(ScopedNSObjectTest, ScopedNSObjectFreeFunctions) { |
73 scoped_nsobject<id> p1([[NSObject alloc] init]); | 73 base::scoped_nsobject<id> p1([[NSObject alloc] init]); |
74 id o1 = p1.get(); | 74 id o1 = p1.get(); |
75 ASSERT_TRUE(o1 == p1); | 75 ASSERT_TRUE(o1 == p1); |
76 ASSERT_FALSE(o1 != p1); | 76 ASSERT_FALSE(o1 != p1); |
77 scoped_nsobject<id> p2([[NSObject alloc] init]); | 77 base::scoped_nsobject<id> p2([[NSObject alloc] init]); |
78 ASSERT_TRUE(o1 != p2); | 78 ASSERT_TRUE(o1 != p2); |
79 ASSERT_FALSE(o1 == p2); | 79 ASSERT_FALSE(o1 == p2); |
80 id o2 = p2.get(); | 80 id o2 = p2.get(); |
81 swap(p1, p2); | 81 swap(p1, p2); |
82 ASSERT_EQ(o2, p1.get()); | 82 ASSERT_EQ(o2, p1.get()); |
83 ASSERT_EQ(o1, p2.get()); | 83 ASSERT_EQ(o1, p2.get()); |
84 } | 84 } |
85 | 85 |
86 } // namespace | 86 } // namespace |
OLD | NEW |