2016-10-14 13:25:24 -07:00
|
|
|
/**
|
|
|
|
* 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 <XCTest/XCTest.h>
|
|
|
|
|
|
|
|
#import "UIView+CSSLayout.h"
|
|
|
|
|
2016-11-17 07:29:33 -08:00
|
|
|
@interface CSSLayoutKitTests : XCTestCase
|
2016-10-14 13:25:24 -07:00
|
|
|
@end
|
|
|
|
|
2016-11-17 07:29:33 -08:00
|
|
|
@implementation CSSLayoutKitTests
|
2016-10-14 13:25:24 -07:00
|
|
|
|
2016-11-17 04:32:45 -08:00
|
|
|
#ifndef TRAVIS_CI
|
|
|
|
|
2016-10-14 14:52:22 -07:00
|
|
|
- (void)testNodesAreDeallocedWithSingleView
|
|
|
|
{
|
|
|
|
XCTAssertEqual(0, CSSNodeGetInstanceCount());
|
|
|
|
|
|
|
|
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
|
2016-11-07 14:26:11 -08:00
|
|
|
[view css_setFlexBasis:1];
|
2016-10-14 14:52:22 -07:00
|
|
|
XCTAssertEqual(1, CSSNodeGetInstanceCount());
|
|
|
|
view = nil;
|
|
|
|
|
|
|
|
XCTAssertEqual(0, CSSNodeGetInstanceCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testNodesAreDeallocedCascade
|
|
|
|
{
|
2016-11-17 04:32:45 -08:00
|
|
|
XCTAssertEqual(0, CSSNodeGetInstanceCount());
|
2016-10-14 14:52:22 -07:00
|
|
|
|
|
|
|
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
|
2016-11-07 14:26:11 -08:00
|
|
|
[view css_setFlexBasis:1];
|
2016-10-14 14:52:22 -07:00
|
|
|
|
|
|
|
for (int i=0; i<10; i++) {
|
|
|
|
UIView *subview = [[UIView alloc] initWithFrame:CGRectZero];
|
2016-11-07 14:26:11 -08:00
|
|
|
[subview css_setFlexBasis:1];
|
2016-10-14 14:52:22 -07:00
|
|
|
[view addSubview:subview];
|
|
|
|
}
|
2016-11-17 04:32:45 -08:00
|
|
|
XCTAssertEqual(11, CSSNodeGetInstanceCount());
|
2016-10-14 14:52:22 -07:00
|
|
|
view = nil;
|
|
|
|
|
2016-11-17 04:32:45 -08:00
|
|
|
XCTAssertEqual(0, CSSNodeGetInstanceCount());
|
2016-10-14 14:52:22 -07:00
|
|
|
}
|
|
|
|
|
2016-11-17 04:32:45 -08:00
|
|
|
#endif
|
|
|
|
|
2016-11-03 09:28:37 -07:00
|
|
|
- (void)testUsesFlexbox
|
|
|
|
{
|
|
|
|
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
|
|
|
|
XCTAssertFalse([view css_usesFlexbox]);
|
|
|
|
|
|
|
|
[view css_setUsesFlexbox:YES];
|
|
|
|
XCTAssertTrue([view css_usesFlexbox]);
|
|
|
|
|
|
|
|
[view css_setUsesFlexbox:NO];
|
|
|
|
XCTAssertFalse([view css_usesFlexbox]);
|
|
|
|
}
|
|
|
|
|
2016-11-03 13:00:02 -07:00
|
|
|
- (void)testSizeThatFitsAsserts
|
|
|
|
{
|
|
|
|
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
|
|
|
|
dispatch_sync(dispatch_queue_create("com.facebook.CSSLayout.testing", DISPATCH_QUEUE_SERIAL), ^(void){
|
2016-11-17 07:29:33 -08:00
|
|
|
XCTAssertThrows([view css_intrinsicSize]);
|
2016-11-03 13:00:02 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testSizeThatFitsSmoke
|
|
|
|
{
|
2016-11-17 07:29:33 -08:00
|
|
|
UIView *container = [[UIView alloc] initWithFrame:CGRectZero];
|
|
|
|
[container css_setUsesFlexbox:YES];
|
|
|
|
[container css_setFlexDirection:CSSFlexDirectionRow];
|
|
|
|
[container css_setAlignItems:CSSAlignFlexStart];
|
|
|
|
|
|
|
|
UILabel *longTextLabel = [[UILabel alloc] initWithFrame:CGRectZero];
|
|
|
|
longTextLabel.text = @"This is a very very very very very very very very long piece of text.";
|
|
|
|
longTextLabel.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
|
|
longTextLabel.numberOfLines = 1;
|
|
|
|
[longTextLabel css_setUsesFlexbox:YES];
|
|
|
|
[longTextLabel css_setFlexShrink:1];
|
|
|
|
[container addSubview:longTextLabel];
|
|
|
|
|
|
|
|
UIView *textBadgeView = [[UIView alloc] initWithFrame:CGRectZero];
|
|
|
|
[textBadgeView css_setUsesFlexbox:YES];
|
|
|
|
[textBadgeView css_setMargin:3.0 forEdge:CSSEdgeLeft];
|
|
|
|
[textBadgeView css_setWidth:10];
|
|
|
|
[textBadgeView css_setHeight:10];
|
|
|
|
[container addSubview:textBadgeView];
|
|
|
|
|
|
|
|
const CGSize containerSize = [container css_intrinsicSize];
|
|
|
|
XCTAssertTrue(CGSizeEqualToSize(CGSizeMake(514,21), containerSize), @"Size is actually %@", NSStringFromCGSize(containerSize));
|
2016-11-03 13:00:02 -07:00
|
|
|
}
|
|
|
|
|
2016-11-07 13:32:27 -08:00
|
|
|
- (void)testFrameAndOriginPlacement
|
|
|
|
{
|
|
|
|
const CGSize containerSize = CGSizeMake(320, 50);
|
|
|
|
|
|
|
|
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, containerSize.width, containerSize.height)];
|
|
|
|
[container css_setUsesFlexbox:YES];
|
|
|
|
[container css_setFlexDirection:CSSFlexDirectionRow];
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
UIView *subview = [[UIView alloc] initWithFrame:CGRectZero];
|
|
|
|
[subview css_setUsesFlexbox:YES];
|
|
|
|
[subview css_setFlexGrow:1];
|
|
|
|
|
|
|
|
[container addSubview:subview];
|
|
|
|
}
|
|
|
|
[container css_applyLayout];
|
|
|
|
|
|
|
|
XCTAssertFalse(CGRectIntersectsRect([container.subviews objectAtIndex:0].frame, [container.subviews objectAtIndex:1].frame));
|
|
|
|
XCTAssertFalse(CGRectIntersectsRect([container.subviews objectAtIndex:1].frame, [container.subviews objectAtIndex:2].frame));
|
|
|
|
XCTAssertFalse(CGRectIntersectsRect([container.subviews objectAtIndex:0].frame, [container.subviews objectAtIndex:2].frame));
|
|
|
|
|
|
|
|
CGFloat totalWidth = 0;
|
|
|
|
for (UIView *view in container.subviews) {
|
|
|
|
totalWidth += view.bounds.size.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(containerSize.width, totalWidth, @"The container's width is %.6f, the subviews take up %.6f", containerSize.width, totalWidth);
|
|
|
|
}
|
|
|
|
|
2016-10-14 13:25:24 -07:00
|
|
|
@end
|