forked from facebookarchive/AsyncDisplayKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASStackLayoutSpecSnapshotTests.mm
More file actions
525 lines (424 loc) · 21.9 KB
/
Copy pathASStackLayoutSpecSnapshotTests.mm
File metadata and controls
525 lines (424 loc) · 21.9 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "ASLayoutSpecSnapshotTestsHelper.h"
#import "ASStackLayoutSpec.h"
#import "ASStackLayoutSpecUtilities.h"
#import "ASBackgroundLayoutSpec.h"
#import "ASRatioLayoutSpec.h"
#import "ASInsetLayoutSpec.h"
#import "ASLayoutOptions.h"
@interface ASStackLayoutSpecSnapshotTests : ASLayoutSpecSnapshotTestCase
@end
@implementation ASStackLayoutSpecSnapshotTests
- (void)setUp
{
[super setUp];
self.recordMode = NO;
}
static NSArray *defaultSubnodes()
{
return defaultSubnodesWithSameSize(CGSizeZero, NO);
}
static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
NSArray *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor redColor]),
ASDisplayNodeWithBackgroundColor([UIColor blueColor]),
ASDisplayNodeWithBackgroundColor([UIColor greenColor])
];
for (ASStaticSizeDisplayNode *subnode in subnodes) {
subnode.staticSize = subnodeSize;
subnode.layoutOptions.flexGrow = flex;
subnode.layoutOptions.flexShrink = flex;
}
return subnodes;
}
- (void)testStackLayoutSpecWithJustify:(ASStackLayoutJustifyContent)justify
flex:(BOOL)flex
sizeRange:(ASSizeRange)sizeRange
identifier:(NSString *)identifier
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionHorizontal,
.justifyContent = justify
};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, flex);
[self testStackLayoutSpecWithStyle:style sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}
- (void)testStackLayoutSpecWithStyle:(ASStackLayoutSpecStyle)style
sizeRange:(ASSizeRange)sizeRange
subnodes:(NSArray *)subnodes
identifier:(NSString *)identifier
{
[self testStackLayoutSpecWithStyle:style children:subnodes sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}
- (void)testStackLayoutSpecWithStyle:(ASStackLayoutSpecStyle)style
children:(NSArray *)children
sizeRange:(ASSizeRange)sizeRange
subnodes:(NSArray *)subnodes
identifier:(NSString *)identifier
{
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor whiteColor]);
ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:[ASStackLayoutSpec stackLayoutSpecWithDirection:style.direction spacing:style.spacing justifyContent:style.justifyContent alignItems:style.alignItems children:children]
background:backgroundNode];
NSMutableArray *newSubnodes = [NSMutableArray arrayWithObject:backgroundNode];
[newSubnodes addObjectsFromArray:subnodes];
[self testLayoutSpec:layoutSpec sizeRange:sizeRange subnodes:newSubnodes identifier:identifier];
}
- (void)testUnderflowBehaviors
{
// width 300px; height 0-300px
static ASSizeRange kSize = {{300, 0}, {300, 300}};
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentStart flex:NO sizeRange:kSize identifier:@"justifyStart"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentCenter flex:NO sizeRange:kSize identifier:@"justifyCenter"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentEnd flex:NO sizeRange:kSize identifier:@"justifyEnd"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentStart flex:YES sizeRange:kSize identifier:@"flex"];
}
- (void)testOverflowBehaviors
{
// width 110px; height 0-300px
static ASSizeRange kSize = {{110, 0}, {110, 300}};
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentStart flex:NO sizeRange:kSize identifier:@"justifyStart"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentCenter flex:NO sizeRange:kSize identifier:@"justifyCenter"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentEnd flex:NO sizeRange:kSize identifier:@"justifyEnd"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentStart flex:YES sizeRange:kSize identifier:@"flex"];
}
- (void)testOverflowBehaviorsWhenAllFlexShrinkChildrenHaveBeenClampedToZeroButViolationStillExists
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
((ASDisplayNode *)subnodes[1]).layoutOptions.flexShrink = YES;
// Width is 75px--that's less than the sum of the widths of the children, which is 100px.
static ASSizeRange kSize = {{75, 0}, {75, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testFlexWithUnequalIntrinsicSizes
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, YES);
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {150, 150};
// width 300px; height 0-150px.
static ASSizeRange kUnderflowSize = {{300, 0}, {300, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kUnderflowSize subnodes:subnodes identifier:@"underflow"];
// width 200px; height 0-150px.
static ASSizeRange kOverflowSize = {{200, 0}, {200, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kOverflowSize subnodes:subnodes identifier:@"overflow"];
}
- (void)testCrossAxisSizeBehaviors
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 50};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 50};
// width 0-300px; height 300px
static ASSizeRange kVariableHeight = {{0, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
// width 300px; height 300px
static ASSizeRange kFixedHeight = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kFixedHeight subnodes:subnodes identifier:@"fixedHeight"];
}
- (void)testStackSpacing
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.spacing = 10
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 50};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 50};
// width 0-300px; height 300px
static ASSizeRange kVariableHeight = {{0, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
}
- (void)testStackSpacingWithChildrenHavingNilObjects
{
// This should take a zero height since all children have a nil node. If it takes a height > 0, a blue background
// will show up, hence failing the test.
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASLayoutSpec *layoutSpec =
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:{10, 10, 10 ,10}
child:
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[]]
background:backgroundNode]];
// width 300px; height 0-300px
static ASSizeRange kVariableHeight = {{300, 0}, {300, 300}};
[self testLayoutSpec:layoutSpec sizeRange:kVariableHeight subnodes:@[backgroundNode] identifier:@"variableHeight"];
}
- (void)testChildSpacing
{
// width 0-INF; height 0-INF
static ASSizeRange kAnySize = {{0, 0}, {INFINITY, INFINITY}};
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingBefore = 10;
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.spacingBefore = 20;
[self testStackLayoutSpecWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingBefore"];
// Reset above spacing values
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingAfter = 10;
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.spacingAfter = 20;
[self testStackLayoutSpecWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingAfter"];
// Reset above spacing values
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingAfter = 0;
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.spacingAfter = 0;
style.spacing = 10;
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingBefore = -10;
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingAfter = -10;
[self testStackLayoutSpecWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingBalancedOut"];
}
- (void)testJustifiedCenterWithChildSpacing
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[0]).layoutOptions.spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.spacingBefore = 30;
// width 0-300px; height 300px
static ASSizeRange kVariableHeight = {{0, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
}
- (void)testChildThatChangesCrossSizeWhenMainSizeIsFlexed
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
ASStaticSizeDisplayNode * subnode1 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASStaticSizeDisplayNode * subnode2 = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
subnode2.staticSize = {50, 50};
ASRatioLayoutSpec *child1 = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.5 child:subnode1];
child1.layoutOptions.flexBasis = ASRelativeDimensionMakeWithPercent(1);
child1.layoutOptions.flexGrow = YES;
child1.layoutOptions.flexShrink = YES;
static ASSizeRange kFixedWidth = {{150, 0}, {150, INFINITY}};
[self testStackLayoutSpecWithStyle:style children:@[child1, subnode2] sizeRange:kFixedWidth subnodes:@[subnode1, subnode2] identifier:nil];
}
- (void)testAlignCenterWithFlexedMainDimension
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.alignItems = ASStackLayoutAlignItemsCenter
};
ASStaticSizeDisplayNode *subnode1 = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
subnode1.staticSize = {100, 100};
subnode1.layoutOptions.flexShrink = YES;
ASStaticSizeDisplayNode *subnode2 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
subnode2.staticSize = {50, 50};
subnode2.layoutOptions.flexShrink = YES;
NSArray *subnodes = @[subnode1, subnode2];
static ASSizeRange kFixedWidth = {{150, 0}, {150, 100}};
[self testStackLayoutSpecWithStyle:style sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
}
- (void)testAlignCenterWithIndefiniteCrossDimension
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
ASStaticSizeDisplayNode *subnode1 = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
subnode1.staticSize = {100, 100};
ASStaticSizeDisplayNode *subnode2 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
subnode2.staticSize = {50, 50};
subnode2.layoutOptions.alignSelf = ASStackLayoutAlignSelfCenter;
NSArray *subnodes = @[subnode1, subnode2];
static ASSizeRange kFixedWidth = {{150, 0}, {150, INFINITY}};
[self testStackLayoutSpecWithStyle:style sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
}
- (void)testAlignedStart
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter,
.alignItems = ASStackLayoutAlignItemsStart
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[0]).layoutOptions.spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.spacingBefore = 30;
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedEnd
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter,
.alignItems = ASStackLayoutAlignItemsEnd
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[0]).layoutOptions.spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.spacingBefore = 30;
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedCenter
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter,
.alignItems = ASStackLayoutAlignItemsCenter
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[0]).layoutOptions.spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.spacingBefore = 30;
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedStretchNoChildExceedsMin
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter,
.alignItems = ASStackLayoutAlignItemsStretch
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[0]).layoutOptions.spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.spacingBefore = 30;
static ASSizeRange kVariableSize = {{200, 200}, {300, 300}};
// all children should be 200px wide
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedStretchOneChildExceedsMin
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter,
.alignItems = ASStackLayoutAlignItemsStretch
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[0]).layoutOptions.spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.spacingBefore = 30;
static ASSizeRange kVariableSize = {{50, 50}, {300, 300}};
// all children should be 150px wide
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableSize subnodes:subnodes identifier:nil];
}
- (void)testEmptyStack
{
static ASSizeRange kVariableSize = {{50, 50}, {300, 300}};
[self testStackLayoutSpecWithStyle:{} sizeRange:kVariableSize subnodes:@[] identifier:nil];
}
- (void)testFixedFlexBasisAppliedWhenFlexingItems
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {150, 150};
for (ASStaticSizeDisplayNode *subnode in subnodes) {
subnode.layoutOptions.flexGrow = YES;
subnode.layoutOptions.flexBasis = ASRelativeDimensionMakeWithPoints(10);
}
// width 300px; height 0-150px.
static ASSizeRange kUnderflowSize = {{300, 0}, {300, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kUnderflowSize subnodes:subnodes identifier:@"underflow"];
// width 200px; height 0-150px.
static ASSizeRange kOverflowSize = {{200, 0}, {200, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kOverflowSize subnodes:subnodes identifier:@"overflow"];
}
- (void)testPercentageFlexBasisResolvesAgainstParentSize
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
for (ASStaticSizeDisplayNode *subnode in subnodes) {
subnode.layoutOptions.flexGrow = YES;
}
// This should override the intrinsic size of 50pts and instead compute to 50% = 100pts.
// The result should be that the red box is twice as wide as the blue and gree boxes after flexing.
((ASStaticSizeDisplayNode *)subnodes[0]).layoutOptions.flexBasis = ASRelativeDimensionMakeWithPercent(0.5);
static ASSizeRange kSize = {{200, 0}, {200, INFINITY}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testFixedFlexBasisOverridesIntrinsicSizeForNonFlexingChildren
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {150, 150};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {50, 50};
for (ASStaticSizeDisplayNode *subnode in subnodes) {
subnode.layoutOptions.flexBasis = ASRelativeDimensionMakeWithPoints(20);
}
static ASSizeRange kSize = {{300, 0}, {300, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testCrossAxisStretchingOccursAfterStackAxisFlexing
{
NSArray *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor]),
ASDisplayNodeWithBackgroundColor([UIColor blueColor]),
ASDisplayNodeWithBackgroundColor([UIColor redColor])
];
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {10, 0};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {3000, 3000};
ASRatioLayoutSpec *child2 = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.0 child:subnodes[2]];
child2.layoutOptions.flexGrow = YES;
child2.layoutOptions.flexShrink = YES;
// If cross axis stretching occurred *before* flexing, then the blue child would be stretched to 3000 points tall.
// Instead it should be stretched to 300 points tall, matching the red child and not overlapping the green inset.
ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:UIEdgeInsetsMake(10, 10, 10, 10)
child:
[ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[subnodes[1], child2,]]
]
background:subnodes[0]];
static ASSizeRange kSize = {{300, 0}, {300, INFINITY}};
[self testLayoutSpec:layoutSpec sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testViolationIsDistributedEquallyAmongFlexibleChildren
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {300, 50};
((ASStaticSizeDisplayNode *)subnodes[0]).layoutOptions.flexShrink = YES;
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).layoutOptions.flexShrink = NO;
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {200, 50};
((ASStaticSizeDisplayNode *)subnodes[2]).layoutOptions.flexShrink = YES;
// A width of 400px results in a violation of 200px. This is distributed equally among each flexible child,
// causing both of them to be shrunk by 100px, resulting in widths of 300px, 100px, and 50px.
// In the W3 flexbox standard, flexible children are shrunk proportionate to their original sizes,
// resulting in widths of 180px, 100px, and 120px.
// This test verifies the current behavior--the snapshot contains widths 300px, 100px, and 50px.
static ASSizeRange kSize = {{400, 0}, {400, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
@end