Skip to content
Permalink
Browse files

[HeaderStackView] Add traitCollectionDidChange block (#8061)

Adds a traitCollectionDidChangeBlock to MDCHeaderStackView, called when its trait collection changes.

Closes #8049
  • Loading branch information
codeman7 committed Jul 24, 2019
1 parent 6c3877d commit 6190a33c72554fbd92254c4281168a07408624ec
@@ -49,4 +49,12 @@ IB_DESIGNABLE
/** The bottom bar. Bottom aligned. */
@property(nonatomic, strong, nullable) UIView *bottomBar;

/**
A block that is invoked when the @c MDCHeaderStackView receives a call to @c
traitCollectionDidChange:. The block is called after the call to the superclass.
*/
@property(nonatomic, copy, nullable) void (^traitCollectionDidChangeBlock)
(MDCHeaderStackView *_Nonnull headerStackView,
UITraitCollection *_Nullable previousTraitCollection);

@end
@@ -53,6 +53,14 @@ - (void)layoutSubviews {
_bottomBar.frame = bottomBarFrame;
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];

if (self.traitCollectionDidChangeBlock) {
self.traitCollectionDidChangeBlock(self, previousTraitCollection);
}
}

#pragma mark - Public

- (void)setTopBar:(UIView *)topBar {
@@ -22,9 +22,29 @@ @interface HeaderStackViewNoopTest : XCTestCase

@implementation HeaderStackViewNoopTest

- (void)testExample {
MDCHeaderStackView *view = [[MDCHeaderStackView alloc] init];
XCTAssertNotNil(view);
- (void)testTraitCollectionDidChangeBlockCalledWithExpectedParameters {
// Given
MDCHeaderStackView *testHeaderStackView = [[MDCHeaderStackView alloc] init];
XCTestExpectation *expectation =
[[XCTestExpectation alloc] initWithDescription:@"traitCollection"];
__block UITraitCollection *passedTraitCollection = nil;
__block MDCHeaderStackView *passedHeaderStackView = nil;
testHeaderStackView.traitCollectionDidChangeBlock =
^(MDCHeaderStackView *_Nonnull headerStackView,
UITraitCollection *_Nullable previousTraitCollection) {
passedTraitCollection = previousTraitCollection;
passedHeaderStackView = headerStackView;
[expectation fulfill];
};
UITraitCollection *fakeTraitCollection = [UITraitCollection traitCollectionWithDisplayScale:7];

// When
[testHeaderStackView traitCollectionDidChange:fakeTraitCollection];

// Then
[self waitForExpectations:@[ expectation ] timeout:1];
XCTAssertEqual(passedHeaderStackView, testHeaderStackView);
XCTAssertEqual(passedTraitCollection, fakeTraitCollection);
}

@end

0 comments on commit 6190a33

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