Skip to content
Permalink
Browse files

[Buttons] Add states snapshot tests. (#6639)

Simple snapshot tests for the main UIControlStates.
  • Loading branch information
romoore committed Feb 19, 2019
1 parent 65426c8 commit 41612628cc3eb845c8d00d79aef8c981b175bdad
@@ -33,6 +33,17 @@ Pod::Spec.new do |s|
end
end

s.subspec "Buttons" do |component|
component.ios.deployment_target = '8.0'
component.test_spec 'tests' do |tests|
tests.test_spec 'snapshot' do |snapshot_tests|
snapshot_tests.requires_app_host = true
snapshot_tests.source_files = "components/#{component.base_name}/tests/snapshot/*.{h,m,swift}"
snapshot_tests.dependency "MaterialComponentsSnapshotTests/private/Snapshot"
end
end
end

s.subspec "Cards" do |component|
component.ios.deployment_target = '8.0'
component.test_spec 'tests' do |tests|
@@ -70,6 +70,7 @@ target "MDCCatalog" do
pod 'MaterialComponentsSnapshotTests', :path => '../', :testspecs => [
'BottomAppBar/tests/snapshot',
'BottomNavigation/tests/snapshot',
'Buttons/tests/snapshot',
'Cards/tests/snapshot',
'Chips/tests/snapshot',
'Dialogs/tests/snapshot',
@@ -21,6 +21,8 @@ load(
"mdc_extension_objc_library",
"mdc_objc_library",
"mdc_public_objc_library",
"mdc_snapshot_objc_library",
"mdc_snapshot_test",
"mdc_unit_test_suite",
)
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
@@ -197,3 +199,15 @@ mdc_unit_test_suite(
":unit_test_swift_sources",
],
)

mdc_snapshot_objc_library(
name = "snapshot_test_lib",
deps = [
":Buttons",
],
)

mdc_snapshot_test(
name = "snapshot_tests",
deps = [":snapshot_test_lib"],
)
@@ -0,0 +1,147 @@
// Copyright 2019-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 "MaterialSnapshot.h"

#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h>

#import "MaterialButtons.h"

/** Snapshot tests for Buttons in different UIControlStates */
@interface ButtonsStatesSnapshotTests : MDCSnapshotTestCase
@property(nonatomic, strong) MDCButton *button;
@end

@implementation ButtonsStatesSnapshotTests

- (void)setUp {
[super setUp];

// Uncomment below to recreate all the goldens (or add the following line to the specific
// test you wish to recreate the golden for).
// self.recordMode = YES;

self.button = [[MDCButton alloc] init];
UIImage *testImage = [[UIImage mdc_testImageOfSize:CGSizeMake(24, 24)]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.button setImage:testImage forState:UIControlStateNormal];
[self.button setInkColor:[UIColor.magentaColor colorWithAlphaComponent:0.25]];
[self configureButton:self.button
forState:UIControlStateNormal
withTitle:@"Red"
red:1
green:0
blue:0];
[self configureButton:self.button
forState:UIControlStateSelected
withTitle:@"Green"
red:0
green:1
blue:0];
[self configureButton:self.button
forState:UIControlStateHighlighted
withTitle:@"Blue"
red:0
green:0
blue:1];
[self configureButton:self.button
forState:UIControlStateDisabled
withTitle:@"Disabled"
red:1
green:1
blue:0];
[self configureButton:self.button
forState:(UIControlStateHighlighted | UIControlStateSelected)
withTitle:@"H + S"
red:0
green:1
blue:1];
[self.button sizeToFit];
}

- (void)tearDown {
self.button = nil;

[super tearDown];
}

- (void)configureButton:(MDCButton *)button
forState:(UIControlState)state
withTitle:(NSString *)title
red:(CGFloat)red
green:(CGFloat)green
blue:(CGFloat)blue {
[button setTitleColor:[UIColor colorWithRed:(CGFloat)(0.5 * red)
green:(CGFloat)(0.5 * green)
blue:(CGFloat)(0.5 * blue)
alpha:1]
forState:state];
[button setTitle:title forState:state];
[button setBackgroundColor:[UIColor colorWithRed:red green:green blue:blue alpha:1]
forState:state];
[button setImageTintColor:[UIColor colorWithRed:(CGFloat)(0.25 * red)
green:(CGFloat)(0.25 * green)
blue:(CGFloat)(0.25 * blue)
alpha:1]
forState:state];
}

- (void)generateSnapshotAndVerifyForView:(UIView *)view {
[view sizeToFit];
UIView *snapshotView = [view mdc_addToBackgroundView];
[self snapshotVerifyView:snapshotView];
}

#pragma mark - Tests

- (void)testNormalState {
// Then
[self generateSnapshotAndVerifyForView:self.button];
}

- (void)testHighlightedState {
// When
self.button.highlighted = YES;

// Then
[self generateSnapshotAndVerifyForView:self.button];
}

- (void)testSelectedState {
// When
self.button.selected = YES;

// Then
[self generateSnapshotAndVerifyForView:self.button];
}

- (void)testDisabledState {
// When
self.button.enabled = NO;

// Then
[self generateSnapshotAndVerifyForView:self.button];
}

- (void)testHighlightedSelectedState {
// When
self.button.highlighted = YES;
self.button.selected = YES;

// Then
[self generateSnapshotAndVerifyForView:self.button];
}

@end
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 4161262

Please sign in to comment.
You can’t perform that action at this time.