Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Chips] Expose visibleAreaInsets as a public readonly API.
PiperOrigin-RevId: 325101887
  • Loading branch information
wenyuzhang666 authored and material-automation committed Aug 5, 2020
1 parent 899c57e commit f716061
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions components/Chips/src/MDCChipView.h
Expand Up @@ -176,6 +176,13 @@
*/
@property(nonatomic, assign) BOOL centerVisibleArea;

/**
The calculated inset or outset margins for the rectangle surrounding all of the chip’s visible area.
When @c centerVisibleArea is @c NO, this value is @c UIEdgeInsetsZero.
*/
@property(nonatomic, readonly) UIEdgeInsets visibleAreaInsets;

/**
A block that is invoked when the MDCChipView receives a call to @c
traitCollectionDidChange:. The block is called after the call to the superclass.
Expand Down
34 changes: 34 additions & 0 deletions components/Chips/tests/unit/MDCChipViewTests.m
Expand Up @@ -15,6 +15,7 @@
#import <XCTest/XCTest.h>

#import "MDCChipView.h"
#import "MaterialMath.h"

static inline UIImage *TestImage(CGSize size) {
CGFloat scale = [UIScreen mainScreen].scale;
Expand Down Expand Up @@ -248,6 +249,39 @@ - (void)testChipUpdatesSizeWhenImageChanges {
XCTAssertGreaterThan(CGRectGetWidth(chip.bounds), chipSize.width);
}

- (void)testVisibleAreaInsetsIsZeroWhenCenterVisibleAreaIsNO {
// Given
MDCChipView *chip = [[MDCChipView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
chip.titleLabel.text = @"Chip";

// When
chip.centerVisibleArea = NO;

// Then
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(chip.visibleAreaInsets, UIEdgeInsetsZero));
}

- (void)testVisibleAreaInsetsIsCorrectWhenCenterVisibleAreaIsYES {
// Given
MDCChipView *chip = [[MDCChipView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
chip.titleLabel.text = @"Chip";

// When
chip.centerVisibleArea = YES;

// Then
CGSize visibleAreaSize = [chip sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
CGFloat verticalInsets = 50 - visibleAreaSize.height;
CGFloat horizontalInsets = 300 - visibleAreaSize.width;
CGFloat topInsets = MDCCeil(verticalInsets * 0.5f);
CGFloat bottomInsets = verticalInsets - topInsets;
CGFloat leftInsets = MDCCeil(horizontalInsets * 0.5f);
CGFloat rightInsets = horizontalInsets - leftInsets;
UIEdgeInsets expectedVisibleAreaInsets =
UIEdgeInsetsMake(topInsets, leftInsets, bottomInsets, rightInsets);
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(chip.visibleAreaInsets, expectedVisibleAreaInsets));
}

- (void)forceAutoLayoutUpdateForView:(UIView *)view {
[view setNeedsLayout];
[view layoutIfNeeded];
Expand Down

0 comments on commit f716061

Please sign in to comment.