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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 #include <dlfcn.h> | |
6 | 7 |
8 #include "base/logging.h" | |
7 #import "base/memory/scoped_nsobject.h" | 9 #import "base/memory/scoped_nsobject.h" |
8 #import "chrome/browser/ui/cocoa/objc_zombie.h" | 10 #import "chrome/browser/ui/cocoa/objc_zombie.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "testing/platform_test.h" | 12 #include "testing/platform_test.h" |
11 | 13 |
14 namespace { | |
15 | |
16 // Dynamically look up |objc_setAssociatedObject()|, which isn't | |
17 // available until the 10.6 SDK. | |
18 | |
19 typedef void objc_setAssociatedObjectFn(id object, void *key, id value, | |
20 int policy); | |
21 objc_setAssociatedObjectFn* LookupSetAssociatedObjectFn() { | |
22 return reinterpret_cast<objc_setAssociatedObjectFn*>( | |
23 dlsym(RTLD_DEFAULT, "objc_setAssociatedObject")); | |
24 } | |
25 | |
26 } // namespace | |
27 | |
12 @interface ZombieCxxDestructTest : NSObject | 28 @interface ZombieCxxDestructTest : NSObject |
13 { | 29 { |
14 scoped_nsobject<id> aRef_; | 30 scoped_nsobject<id> aRef_; |
15 } | 31 } |
16 - (id)initWith:(id)anObject; | 32 - (id)initWith:(id)anObject; |
17 @end | 33 @end |
18 | 34 |
19 @implementation ZombieCxxDestructTest | 35 @implementation ZombieCxxDestructTest |
20 - (id)initWith:(id)anObject { | 36 - (id)initWith:(id)anObject { |
21 self = [super init]; | 37 self = [super init]; |
22 if (self) { | 38 if (self) { |
23 aRef_.reset([anObject retain]); | 39 aRef_.reset([anObject retain]); |
24 } | 40 } |
25 return self; | 41 return self; |
26 } | 42 } |
27 @end | 43 @end |
28 | 44 |
45 @interface ZombieAssociatedObjectTest : NSObject | |
46 + (BOOL)supportsAssociatedObjects; | |
47 - (id)initWithAssociatedObject:(id)anObject; | |
48 @end | |
49 | |
50 @implementation ZombieAssociatedObjectTest | |
51 | |
52 + (BOOL)supportsAssociatedObjects { | |
53 if (LookupSetAssociatedObjectFn()) | |
54 return YES; | |
55 return NO; | |
56 } | |
57 | |
58 - (id)initWithAssociatedObject:(id)anObject { | |
Avi (use Gerrit)
2011/06/08 03:08:53
nice!
| |
59 self = [super init]; | |
60 if (self) { | |
61 objc_setAssociatedObjectFn* fn = LookupSetAssociatedObjectFn(); | |
62 if (fn) { | |
63 // Cribbed from 10.6 <objc/runtime.h>. | |
64 static const int kObjcAssociationRetain = 01401; | |
65 | |
66 // The address of the variable itself is the unique key, the | |
67 // contents don't matter. | |
68 static char kAssociatedObjectKey = 'x'; | |
69 | |
70 (*fn)(self, &kAssociatedObjectKey, anObject, kObjcAssociationRetain); | |
71 } | |
72 } | |
73 return self; | |
74 } | |
75 | |
76 @end | |
77 | |
29 namespace { | 78 namespace { |
30 | 79 |
31 // Verify that the C++ destructors run when the last reference to the | 80 // Verify that the C++ destructors run when the last reference to the |
32 // object is released. Unfortunately, testing the negative requires | 81 // object is released. |
33 // commenting out the |object_cxxDestruct()| call in | 82 // NOTE(shess): To test the negative, comment out the |g_objectDestruct()| |
34 // |ZombieDealloc()|. | 83 // call in |ZombieDealloc()|. |
35 | |
36 TEST(ObjcZombieTest, CxxDestructors) { | 84 TEST(ObjcZombieTest, CxxDestructors) { |
37 scoped_nsobject<id> anObject([[NSObject alloc] init]); | 85 scoped_nsobject<id> anObject([[NSObject alloc] init]); |
38 EXPECT_EQ(1u, [anObject retainCount]); | 86 EXPECT_EQ(1u, [anObject retainCount]); |
39 | 87 |
40 ASSERT_TRUE(ObjcEvilDoers::ZombieEnable(YES, 100)); | 88 ASSERT_TRUE(ObjcEvilDoers::ZombieEnable(YES, 100)); |
41 | 89 |
42 scoped_nsobject<ZombieCxxDestructTest> soonInfected( | 90 scoped_nsobject<ZombieCxxDestructTest> soonInfected( |
43 [[ZombieCxxDestructTest alloc] initWith:anObject]); | 91 [[ZombieCxxDestructTest alloc] initWith:anObject]); |
44 EXPECT_EQ(2u, [anObject retainCount]); | 92 EXPECT_EQ(2u, [anObject retainCount]); |
45 | 93 |
46 // When |soonInfected| becomes a zombie, the C++ destructors should | 94 // When |soonInfected| becomes a zombie, the C++ destructors should |
47 // run and release a reference to |anObject|. | 95 // run and release a reference to |anObject|. |
48 soonInfected.reset(); | 96 soonInfected.reset(); |
49 EXPECT_EQ(1u, [anObject retainCount]); | 97 EXPECT_EQ(1u, [anObject retainCount]); |
50 | 98 |
51 // The local reference should remain (C++ destructors aren't re-run). | 99 // The local reference should remain (C++ destructors aren't re-run). |
52 ObjcEvilDoers::ZombieDisable(); | 100 ObjcEvilDoers::ZombieDisable(); |
53 EXPECT_EQ(1u, [anObject retainCount]); | 101 EXPECT_EQ(1u, [anObject retainCount]); |
54 } | 102 } |
55 | 103 |
104 // Verify that the associated objects are released when the object is | |
105 // released. | |
106 // NOTE(shess): To test the negative, hardcode |g_objectDestruct| to | |
107 // the 10.5 version in |ZombieInit()|, and run this test on 10.6. | |
108 TEST(ObjcZombieTest, AssociatedObjectsReleased) { | |
109 if (![ZombieAssociatedObjectTest supportsAssociatedObjects]) { | |
110 LOG(ERROR) | |
111 << "ObjcZombieTest.AssociatedObjectsReleased not supported on 10.5"; | |
112 return; | |
113 } | |
114 | |
115 scoped_nsobject<id> anObject([[NSObject alloc] init]); | |
116 EXPECT_EQ(1u, [anObject retainCount]); | |
117 | |
118 ASSERT_TRUE(ObjcEvilDoers::ZombieEnable(YES, 100)); | |
119 | |
120 scoped_nsobject<ZombieAssociatedObjectTest> soonInfected( | |
121 [[ZombieAssociatedObjectTest alloc] initWithAssociatedObject:anObject]); | |
122 EXPECT_EQ(2u, [anObject retainCount]); | |
123 | |
124 // When |soonInfected| becomes a zombie, the associated object | |
125 // should be released. | |
126 soonInfected.reset(); | |
127 EXPECT_EQ(1u, [anObject retainCount]); | |
128 | |
129 // The local reference should remain (associated objects not re-released). | |
130 ObjcEvilDoers::ZombieDisable(); | |
131 EXPECT_EQ(1u, [anObject retainCount]); | |
132 } | |
133 | |
56 } // namespace | 134 } // namespace |
OLD | NEW |