forked from material-components/material-components-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Chips] Fix accessoryPadding encoding key (material-components#2773)
Encoding issues a warning because the titlePadding value is overwritten with the accessoryPadding value during archival.
- Loading branch information
Robert Moore
committed
Dec 30, 2017
1 parent
e031865
commit 873e687
Showing
3 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Copyright 2017-present the Material Components for iOS authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#import <XCTest/XCTest.h> | ||
|
||
#import "MaterialChips.h" | ||
#import "MDCPillShapeGenerator.h" | ||
|
||
static UIImage *FakeImage(void) { | ||
CGSize imageSize = CGSizeMake(24, 24); | ||
UIGraphicsBeginImageContext(imageSize); | ||
[UIColor.whiteColor setFill]; | ||
UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height)); | ||
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | ||
UIGraphicsEndImageContext(); | ||
return image; | ||
} | ||
|
||
@interface ChipsEncodingTests : XCTestCase | ||
|
||
@end | ||
|
||
@implementation ChipsEncodingTests | ||
|
||
- (void)testEncoding { | ||
// Given | ||
MDCChipView *chip = [[MDCChipView alloc] init]; | ||
chip.inkColor = UIColor.cyanColor; | ||
chip.titleLabel.text = @"Title"; | ||
chip.imageView.image = FakeImage(); | ||
chip.selectedImageView.image = | ||
[FakeImage() imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; | ||
chip.imagePadding = UIEdgeInsetsMake(1, 2, 3, 4); | ||
chip.titlePadding = UIEdgeInsetsMake(2, 3, 4, 5); | ||
chip.contentPadding = UIEdgeInsetsMake(3, 4, 5, 6); | ||
chip.accessoryPadding = UIEdgeInsetsMake(4, 5, 6, 7); | ||
chip.shapeGenerator = [[MDCPillShapeGenerator alloc] init]; | ||
chip.mdc_adjustsFontForContentSizeCategory = YES; | ||
[chip setBackgroundColor:UIColor.orangeColor forState:UIControlStateNormal]; | ||
[chip setTitleColor:UIColor.magentaColor forState:UIControlStateNormal]; | ||
[chip setBorderColor:UIColor.greenColor forState:UIControlStateNormal]; | ||
[chip setBorderWidth:1.5 forState:UIControlStateNormal]; | ||
[chip setElevation:MDCShadowElevationMenu forState:UIControlStateNormal]; | ||
[chip setShadowColor:UIColor.purpleColor forState:UIControlStateNormal]; | ||
|
||
// When | ||
NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:chip]; | ||
MDCChipView *unarchivedChip = [NSKeyedUnarchiver unarchiveObjectWithData:archive]; | ||
|
||
// Then | ||
// TODO(#2772): Check for equality once MDCInkView supports encoding | ||
XCTAssertNotEqualObjects(unarchivedChip.inkColor, chip.inkColor); | ||
XCTAssertEqualObjects(unarchivedChip.titleLabel.text, chip.titleLabel.text); | ||
XCTAssertNotNil(unarchivedChip.imageView.image); | ||
XCTAssertNotNil(unarchivedChip.selectedImageView.image); | ||
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(unarchivedChip.imagePadding, chip.imagePadding), | ||
@"%@ is not equal to %@.", | ||
NSStringFromUIEdgeInsets(unarchivedChip.imagePadding), | ||
NSStringFromUIEdgeInsets(chip.imagePadding)); | ||
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(unarchivedChip.titlePadding, chip.titlePadding), | ||
@"%@ is not equal to %@.", | ||
NSStringFromUIEdgeInsets(unarchivedChip.titlePadding), | ||
NSStringFromUIEdgeInsets(chip.titlePadding)); | ||
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(unarchivedChip.contentPadding, chip.contentPadding), | ||
@"%@ is not equal to %@.", | ||
NSStringFromUIEdgeInsets(unarchivedChip.contentPadding), | ||
NSStringFromUIEdgeInsets(chip.contentPadding)); | ||
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(unarchivedChip.accessoryPadding, | ||
chip.accessoryPadding), | ||
@"%@ is not equal to %@.", | ||
NSStringFromUIEdgeInsets(unarchivedChip.accessoryPadding), | ||
NSStringFromUIEdgeInsets(chip.accessoryPadding)); | ||
// TODO(#2771): Support encoding shapeGenerator | ||
XCTAssertNil(unarchivedChip.shapeGenerator); | ||
XCTAssertTrue(unarchivedChip.mdc_adjustsFontForContentSizeCategory); | ||
XCTAssertEqualObjects([unarchivedChip backgroundColorForState:UIControlStateNormal], | ||
[chip backgroundColorForState:UIControlStateNormal]); | ||
XCTAssertEqualObjects([unarchivedChip titleColorForState:UIControlStateNormal], | ||
[chip titleColorForState:UIControlStateNormal]); | ||
XCTAssertEqualObjects([unarchivedChip borderColorForState:UIControlStateNormal], | ||
[chip borderColorForState:UIControlStateNormal]); | ||
XCTAssertEqualWithAccuracy([unarchivedChip borderWidthForState:UIControlStateNormal], | ||
[chip borderWidthForState:UIControlStateNormal], | ||
0.0001); | ||
XCTAssertEqual([unarchivedChip elevationForState:UIControlStateNormal], | ||
[chip elevationForState:UIControlStateNormal]); | ||
XCTAssertEqualObjects([unarchivedChip shadowColorForState:UIControlStateNormal], | ||
[chip shadowColorForState:UIControlStateNormal]); | ||
} | ||
|
||
@end |