forked from facebookarchive/AsyncDisplayKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASBridgedPropertiesTests.mm
More file actions
230 lines (203 loc) · 6.42 KB
/
Copy pathASBridgedPropertiesTests.mm
File metadata and controls
230 lines (203 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// ASBridgedPropertiesTests.m
// AsyncDisplayKit
//
// Created by Adlai Holler on 1/7/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "ASPendingStateController.h"
#import "ASDisplayNode.h"
#import "ASThread.h"
#import "ASDisplayNodeInternal.h"
#import "_ASPendingState.h"
#import "ASCellNode.h"
@interface ASPendingStateController (Testing)
- (BOOL)test_isFlushScheduled;
@end
@interface ASBridgedPropertiesTestView : UIView
@property (nonatomic, readonly) BOOL receivedSetNeedsLayout;
@end
@implementation ASBridgedPropertiesTestView
- (void)setNeedsLayout
{
_receivedSetNeedsLayout = YES;
[super setNeedsLayout];
}
@end
@interface ASBridgedPropertiesTestNode : ASDisplayNode
@property (nullable, nonatomic, copy) dispatch_block_t onDealloc;
@end
@implementation ASBridgedPropertiesTestNode
- (void)dealloc {
_onDealloc();
}
@end
@interface ASBridgedPropertiesTests : XCTestCase
@end
/// Dispatches the given block synchronously onto a different thread.
/// This is useful for testing non-main-thread behavior because `dispatch_sync`
/// will often use the current thread.
static inline void ASDispatchSyncOnOtherThread(dispatch_block_t block) {
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_enter(group);
dispatch_async(q, ^{
ASDisplayNodeCAssertNotMainThread();
block();
dispatch_group_leave(group);
});
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
}
@implementation ASBridgedPropertiesTests
- (void)testTheresASharedInstance
{
XCTAssertNotNil([ASPendingStateController sharedInstance]);
}
/// FIXME: This test is unreliable for an as-yet unknown reason
/// but that being intermittent, and this test being so strict, it's
/// reasonable to assume for now the failures don't reflect a framework bug.
/// See https://github.com/facebook/AsyncDisplayKit/pull/1048
- (void)DISABLED_testThatDirtyNodesAreNotRetained
{
ASPendingStateController *ctrl = [ASPendingStateController sharedInstance];
__block BOOL didDealloc = NO;
@autoreleasepool {
__attribute__((objc_precise_lifetime)) ASBridgedPropertiesTestNode *node = [ASBridgedPropertiesTestNode new];
node.onDealloc = ^{
didDealloc = YES;
};
[node view];
XCTAssertEqual(node.alpha, 1);
ASDispatchSyncOnOtherThread(^{
node.alpha = 0;
});
XCTAssertEqual(node.alpha, 1);
XCTAssert(ctrl.test_isFlushScheduled);
}
XCTAssertTrue(didDealloc);
}
- (void)testThatSettingABridgedViewPropertyInBackgroundGetsFlushedOnNextRunLoop
{
ASDisplayNode *node = [ASDisplayNode new];
[node view];
XCTAssertEqual(node.alpha, 1);
ASDispatchSyncOnOtherThread(^{
node.alpha = 0;
});
XCTAssertEqual(node.alpha, 1);
[self waitForMainDispatchQueueToFlush];
XCTAssertEqual(node.alpha, 0);
}
- (void)testThatSettingABridgedLayerPropertyInBackgroundGetsFlushedOnNextRunLoop
{
ASDisplayNode *node = [ASDisplayNode new];
[node view];
XCTAssertEqual(node.shadowOpacity, 0);
ASDispatchSyncOnOtherThread(^{
node.shadowOpacity = 1;
});
XCTAssertEqual(node.shadowOpacity, 0);
[self waitForMainDispatchQueueToFlush];
XCTAssertEqual(node.shadowOpacity, 1);
}
- (void)testThatReadingABridgedViewPropertyInBackgroundThrowsAnException
{
ASDisplayNode *node = [ASDisplayNode new];
[node view];
ASDispatchSyncOnOtherThread(^{
XCTAssertThrows(node.alpha);
});
}
- (void)testThatReadingABridgedLayerPropertyInBackgroundThrowsAnException
{
ASDisplayNode *node = [ASDisplayNode new];
[node view];
ASDispatchSyncOnOtherThread(^{
XCTAssertThrows(node.contentsScale);
});
}
- (void)testThatManuallyFlushingTheSyncControllerImmediatelyAppliesChanges
{
ASPendingStateController *ctrl = [ASPendingStateController sharedInstance];
ASDisplayNode *node = [ASDisplayNode new];
[node view];
XCTAssertEqual(node.alpha, 1);
ASDispatchSyncOnOtherThread(^{
node.alpha = 0;
});
XCTAssertEqual(node.alpha, 1);
[ctrl flush];
XCTAssertEqual(node.alpha, 0);
XCTAssertFalse(ctrl.test_isFlushScheduled);
}
- (void)testThatFlushingTheControllerInBackgroundThrows
{
ASPendingStateController *ctrl = [ASPendingStateController sharedInstance];
ASDisplayNode *node = [ASDisplayNode new];
[node view];
XCTAssertEqual(node.alpha, 1);
ASDispatchSyncOnOtherThread(^{
node.alpha = 0;
XCTAssertThrows([ctrl flush]);
});
}
- (void)testThatSettingABridgedPropertyOnMainThreadPassesDirectlyToView
{
ASPendingStateController *ctrl = [ASPendingStateController sharedInstance];
ASDisplayNode *node = [ASDisplayNode new];
XCTAssertFalse(ASDisplayNodeGetPendingState(node).hasChanges);
[node view];
XCTAssertEqual(node.alpha, 1);
node.alpha = 0;
XCTAssertEqual(node.view.alpha, 0);
XCTAssertEqual(node.alpha, 0);
XCTAssertFalse(ASDisplayNodeGetPendingState(node).hasChanges);
XCTAssertFalse(ctrl.test_isFlushScheduled);
}
- (void)testThatCallingSetNeedsLayoutFromBackgroundCausesItToHappenLater
{
ASDisplayNode *node = [[ASDisplayNode alloc] initWithViewClass:ASBridgedPropertiesTestView.class];
ASBridgedPropertiesTestView *view = (ASBridgedPropertiesTestView *)node.view;
XCTAssertFalse(view.receivedSetNeedsLayout);
ASDispatchSyncOnOtherThread(^{
XCTAssertNoThrow([node setNeedsLayout]);
});
XCTAssertFalse(view.receivedSetNeedsLayout);
[self waitForMainDispatchQueueToFlush];
XCTAssertTrue(view.receivedSetNeedsLayout);
}
- (void)testThatCallingSetNeedsLayoutOnACellNodeFromBackgroundIsSafe
{
ASCellNode *node = [ASCellNode new];
[node view];
ASDispatchSyncOnOtherThread(^{
XCTAssertNoThrow([node setNeedsLayout]);
});
}
- (void)testThatCallingSetNeedsDisplayFromBackgroundCausesItToHappenLater
{
ASDisplayNode *node = [ASDisplayNode new];
[node.layer displayIfNeeded];
XCTAssertFalse(node.layer.needsDisplay);
ASDispatchSyncOnOtherThread(^{
XCTAssertNoThrow([node setNeedsDisplay]);
});
XCTAssertFalse(node.layer.needsDisplay);
[self waitForMainDispatchQueueToFlush];
XCTAssertTrue(node.layer.needsDisplay);
}
/// [XCTExpectation expectationWithPredicate:] should handle this
/// but under Xcode 7.2.1 its polling interval is 1 second
/// which makes the tests really slow and I'm impatient.
- (void)waitForMainDispatchQueueToFlush
{
__block BOOL done = NO;
dispatch_async(dispatch_get_main_queue(), ^{
done = YES;
});
while (!done) {
[NSRunLoop.mainRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
@end