Files
firebase-ios-sdk/InAppMessaging/Example/Tests/FIRIAMFetchResponseParserTests.m
T
christibbs 4a33b267d9 Support card template display in FIAM iOS SDK (#2947)
* Add card scene to storyboard, use IBDesignable rounded corners view

* Use IBdesignable rounded corners view for modal message

* Add card message type

* Parse card message type, add new fields for secondary actions and landscape URLs to view model classes

* Display a really basic card message

* Naive landscape image loading (needs error handling)

* Quick bug fix

* Wire up Display to show cards

* Wire up Display to show cards

* Fix tablet width

* Pick right image for orientation

* Remove placeholder image sizing constraints at build time

* Add more test functionality

* Better image sizes

* Size message image out

* Switch to body text view rather than body label

* Make the body text view scrollable. Set correct text color on button.

* Hook up buttons and text color

* Include action URL in messageClicked callback

* Fix action URL

* Update pod specs

* Update message clicked delegate to pass an action object, not just the actionURL

* Change podspec versions back, hook up background color

* Don't animate scroll to top on scroll view

* Fix text scrolling bug

* Fix portrait and landscape fetching code. Fix background color bug

* Flesh out FIRIAMMessageContentDataWithImageURLTests

* Fix up FIRIAMFetchFlowTests

* Fix up FIRIAMMessageClientCacheTests

* Fix up FIRIAMMessageDisplayForTesting

* Fix fetching in FIRIAMMessageContentDataWithImageURL

* Tests for message parsing

* Fix up view controllers for banner, modal, image only. Add a view controller for testing card

* Add required vertical compression resistance for primary and secondary action button

* Add UI tests for card view controller

* Add back deprecated messageClicked: method, call it if the newer method isn't implemented. Make all display delegate methods optional.

* Run scripts/styles.sh

* Documentation cleanup

* Fix style in FIRIAMMessageContentDataWithImageURLTests.m

* Add missing copyright notices

* Bump versions of both FIAM SDKS

* Remove default.profraw

* Address some logging punctuation nits. Cancel landscape image load if portrait image load fails.

* Add missing error flow in image fetch. A few comment nits.

* Shorten up init method for CardDisplayMessage

* Documentation updates

* Deintegrate cocoa pods

* Include landscape image data in test image load class

* Fix Swift naming for InAppMessagingAction in UItests

* Remove card message initializer from public API, place into a private header

* Mark some properties on the card object as readonly that should've been already, Find a way to initialize a card message in the UI tests.

* Bump dependency on InAppMessaging given changes

* Bump versions in podspecs for both SDKs

* Make all view model properties for message subclasses readonly

* Revert changes to other message subclasses

* Make card view model properties readonly

* Run styles.sh
2019-05-24 18:31:16 -04:00

202 lines
9.4 KiB
Objective-C

/*
* Copyright 2017 Google
*
* 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 <OCMock/OCMock.h>
#import <XCTest/XCTest.h>
#import "FIRIAMDisplayTriggerDefinition.h"
#import "FIRIAMFetchResponseParser.h"
#import "FIRIAMMessageContentDataWithImageURL.h"
#import "FIRIAMMessageDefinition.h"
#import "FIRIAMTimeFetcher.h"
#import "UIColor+FIRIAMHexString.h"
@interface FIRIAMFetchResponseParserTests : XCTestCase
@property(nonatomic, copy) NSString *jsonResposne;
@property(nonatomic) FIRIAMFetchResponseParser *parser;
@property(nonatomic) id<FIRIAMTimeFetcher> mockTimeFetcher;
@end
@implementation FIRIAMFetchResponseParserTests
- (void)setUp {
[super setUp];
self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
self.parser = [[FIRIAMFetchResponseParser alloc] initWithTimeFetcher:self.mockTimeFetcher];
}
- (void)tearDown {
[super tearDown];
}
- (void)testRegularConversion {
NSString *testJsonDataFilePath =
[[NSBundle bundleForClass:[self class]] pathForResource:@"TestJsonDataFromFetch"
ofType:@"txt"];
NSTimeInterval currentMoment = 100000000;
OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
self.jsonResposne = [[NSString alloc] initWithContentsOfFile:testJsonDataFilePath
encoding:NSUTF8StringEncoding
error:nil];
NSData *data = [self.jsonResposne dataUsingEncoding:NSUTF8StringEncoding];
NSError *errorJson = nil;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&errorJson];
NSInteger discardCount;
NSNumber *fetchWaitTime;
NSArray<FIRIAMMessageDefinition *> *results =
[self.parser parseAPIResponseDictionary:responseDict
discardedMsgCount:&discardCount
fetchWaitTimeInSeconds:&fetchWaitTime];
double nextFetchEpochTimeInResponse =
[responseDict[@"expirationEpochTimestampMillis"] doubleValue];
// Fetch wait time should be (next fetch epoch time - current moment).
XCTAssertEqualWithAccuracy([fetchWaitTime doubleValue],
nextFetchEpochTimeInResponse / 1000 - currentMoment, 0.1);
XCTAssertEqual(5, [results count]);
XCTAssertEqual(0, discardCount);
FIRIAMMessageDefinition *first = results[0];
XCTAssertEqualObjects(@"13313766398414028800", first.renderData.messageID);
XCTAssertEqualObjects(@"first campaign", first.renderData.name);
XCTAssertEqualObjects(@"I heard you like In-App Messages",
first.renderData.contentData.titleText);
XCTAssertEqualObjects(@"This is message body", first.renderData.contentData.bodyText);
XCTAssertEqual(FIRIAMRenderAsModalView, first.renderData.renderingEffectSettings.viewMode);
XCTAssertEqualWithAccuracy(1523986039, first.startTime, 0.1);
XCTAssertEqualWithAccuracy(1526986039, first.endTime, 0.1);
XCTAssertNotNil(first.renderData.renderingEffectSettings.textColor);
XCTAssertEqualObjects(first.renderData.renderingEffectSettings.displayBGColor,
[UIColor firiam_colorWithHexString:@"#fffff8"]);
XCTAssertEqualObjects(first.renderData.renderingEffectSettings.btnBGColor,
[UIColor firiam_colorWithHexString:@"#000000"]);
XCTAssertEqualObjects(first.renderData.contentData.actionURL.absoluteString,
@"https://www.google.com");
XCTAssertEqual(FIRIAMRenderTriggerOnAppForeground, first.renderTriggers[0].triggerType);
FIRIAMMessageDefinition *second = results[1];
XCTAssertEqualObjects(@"9350598726327992320", second.renderData.messageID);
XCTAssertEqualObjects(@"Inception1", second.renderData.name);
XCTAssertEqualObjects(@"Test 2", second.renderData.contentData.titleText);
XCTAssertNil(second.renderData.contentData.bodyText);
XCTAssertEqual(FIRIAMRenderAsModalView, second.renderData.renderingEffectSettings.viewMode);
XCTAssertEqual(2, second.renderTriggers.count);
XCTAssertEqualObjects(second.renderData.renderingEffectSettings.displayBGColor,
[UIColor firiam_colorWithHexString:@"#ffffff"]);
// Third message is a banner view message based on an analytics event trigger.
FIRIAMMessageDefinition *third = results[2];
XCTAssertEqualObjects(@"14819094573862617088", third.renderData.messageID);
XCTAssertEqual(FIRIAMRenderAsBannerView, third.renderData.renderingEffectSettings.viewMode);
XCTAssertEqual(1, third.renderTriggers.count);
XCTAssertEqualObjects(@"jackpot", third.renderTriggers[0].firebaseEventName);
// Fifth message is a card view message based on an analytics event trigger.
FIRIAMMessageDefinition *fifth = results[4];
XCTAssertEqualObjects(@"5432869654332221", fifth.renderData.messageID);
XCTAssertEqual(FIRIAMRenderAsCardView, fifth.renderData.renderingEffectSettings.viewMode);
XCTAssertEqual(1, fifth.renderTriggers.count);
XCTAssertEqual(FIRIAMRenderTriggerOnAppForeground, fifth.renderTriggers[0].triggerType);
XCTAssertEqualObjects(@"Super Bowl LIV", fifth.renderData.name);
XCTAssertEqualObjects(@"Eagles are going to win", fifth.renderData.contentData.titleText);
XCTAssertEqualObjects(@"Start of a dynasty.", fifth.renderData.contentData.bodyText);
XCTAssertEqualObjects(@"https://image.com/birds.png",
fifth.renderData.contentData.imageURL.absoluteString);
XCTAssertEqualObjects(@"https://image.com/ls_birds.png",
fifth.renderData.contentData.landscapeImageURL.absoluteString);
XCTAssertEqualObjects(@"https://www.google.com",
fifth.renderData.contentData.actionURL.absoluteString);
XCTAssertEqualObjects(@"Win Super Bowl LIV", fifth.renderData.contentData.actionButtonText);
XCTAssertEqualObjects(@"https://www.google.com",
fifth.renderData.contentData.secondaryActionURL.absoluteString);
XCTAssertEqualObjects(@"Win Super Bowl LV",
fifth.renderData.contentData.secondaryActionButtonText);
}
- (void)testParsingTestMessage {
NSString *testJsonDataFilePath = [[NSBundle bundleForClass:[self class]]
pathForResource:@"TestJsonDataWithTestMessageFromFetch"
ofType:@"txt"];
self.jsonResposne = [[NSString alloc] initWithContentsOfFile:testJsonDataFilePath
encoding:NSUTF8StringEncoding
error:nil];
NSData *data = [self.jsonResposne dataUsingEncoding:NSUTF8StringEncoding];
NSError *errorJson = nil;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&errorJson];
NSInteger discardCount;
NSNumber *fetchWaitTime;
NSArray<FIRIAMMessageDefinition *> *results =
[self.parser parseAPIResponseDictionary:responseDict
discardedMsgCount:&discardCount
fetchWaitTimeInSeconds:&fetchWaitTime];
// In our fixture file used in this test, there is no fetch expiration time
XCTAssertNil(fetchWaitTime);
XCTAssertEqual(2, [results count]);
XCTAssertEqual(0, discardCount);
// First is a test message and the second one is not.
XCTAssertTrue(results[0].isTestMessage);
XCTAssertTrue(results[0].renderData.renderingEffectSettings.isTestMessage);
XCTAssertFalse(results[1].isTestMessage);
XCTAssertFalse(results[1].renderData.renderingEffectSettings.isTestMessage);
}
- (void)testParsingInvalidTestMessageNodes {
NSString *testJsonDataFilePath = [[NSBundle bundleForClass:[self class]]
pathForResource:@"JsonDataWithInvalidMessagesFromFetch"
ofType:@"txt"];
self.jsonResposne = [[NSString alloc] initWithContentsOfFile:testJsonDataFilePath
encoding:NSUTF8StringEncoding
error:nil];
NSData *data = [self.jsonResposne dataUsingEncoding:NSUTF8StringEncoding];
NSError *errorJson = nil;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&errorJson];
NSInteger discardCount;
NSNumber *fetchWaitTime;
NSArray<FIRIAMMessageDefinition *> *results =
[self.parser parseAPIResponseDictionary:responseDict
discardedMsgCount:&discardCount
fetchWaitTimeInSeconds:&fetchWaitTime];
XCTAssertEqual(0, [results count]);
// First node missing title, second one missig triggering conditions and the third one
// contains invalid type node.
XCTAssertEqual(3, discardCount);
}
@end