FIRIAMFetchResponseParserTests.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the 'License');
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an 'AS IS' BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FIRIAMDisplayTriggerDefinition.h"
  19. #import "FIRIAMFetchResponseParser.h"
  20. #import "FIRIAMMessageContentDataWithImageURL.h"
  21. #import "FIRIAMMessageDefinition.h"
  22. #import "FIRIAMTimeFetcher.h"
  23. #import "UIColor+FIRIAMHexString.h"
  24. @interface FIRIAMFetchResponseParserTests : XCTestCase
  25. @property(nonatomic, copy) NSString *jsonResposne;
  26. @property(nonatomic) FIRIAMFetchResponseParser *parser;
  27. @property(nonatomic) id<FIRIAMTimeFetcher> mockTimeFetcher;
  28. @end
  29. @implementation FIRIAMFetchResponseParserTests
  30. - (void)setUp {
  31. [super setUp];
  32. self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
  33. self.parser = [[FIRIAMFetchResponseParser alloc] initWithTimeFetcher:self.mockTimeFetcher];
  34. }
  35. - (void)tearDown {
  36. [super tearDown];
  37. }
  38. - (void)testRegularConversion {
  39. NSString *testJsonDataFilePath =
  40. [[NSBundle bundleForClass:[self class]] pathForResource:@"TestJsonDataFromFetch"
  41. ofType:@"txt"];
  42. NSTimeInterval currentMoment = 100000000;
  43. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  44. self.jsonResposne = [[NSString alloc] initWithContentsOfFile:testJsonDataFilePath
  45. encoding:NSUTF8StringEncoding
  46. error:nil];
  47. NSData *data = [self.jsonResposne dataUsingEncoding:NSUTF8StringEncoding];
  48. NSError *errorJson = nil;
  49. NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
  50. options:kNilOptions
  51. error:&errorJson];
  52. NSInteger discardCount;
  53. NSNumber *fetchWaitTime;
  54. NSArray<FIRIAMMessageDefinition *> *results =
  55. [self.parser parseAPIResponseDictionary:responseDict
  56. discardedMsgCount:&discardCount
  57. fetchWaitTimeInSeconds:&fetchWaitTime];
  58. double nextFetchEpochTimeInResponse =
  59. [responseDict[@"expirationEpochTimestampMillis"] doubleValue];
  60. // fetch wait time should be (next fetch epoch time - curret moment)
  61. XCTAssertEqualWithAccuracy([fetchWaitTime doubleValue],
  62. nextFetchEpochTimeInResponse / 1000 - currentMoment, 0.1);
  63. XCTAssertEqual(4, [results count]);
  64. XCTAssertEqual(0, discardCount);
  65. FIRIAMMessageDefinition *first = results[0];
  66. XCTAssertEqualObjects(@"13313766398414028800", first.renderData.messageID);
  67. XCTAssertEqualObjects(@"first campaign", first.renderData.name);
  68. XCTAssertEqualObjects(@"I heard you like In-App Messages",
  69. first.renderData.contentData.titleText);
  70. XCTAssertEqualObjects(@"This is message body", first.renderData.contentData.bodyText);
  71. XCTAssertEqual(FIRIAMRenderAsModalView, first.renderData.renderingEffectSettings.viewMode);
  72. XCTAssertEqualWithAccuracy(1523986039, first.startTime, 0.1);
  73. XCTAssertEqualWithAccuracy(1526986039, first.endTime, 0.1);
  74. XCTAssertNotNil(first.renderData.renderingEffectSettings.textColor);
  75. XCTAssertEqualObjects(first.renderData.renderingEffectSettings.displayBGColor,
  76. [UIColor firiam_colorWithHexString:@"#fffff8"]);
  77. XCTAssertEqualObjects(first.renderData.renderingEffectSettings.btnBGColor,
  78. [UIColor firiam_colorWithHexString:@"#000000"]);
  79. XCTAssertEqualObjects(first.renderData.contentData.actionURL.absoluteString,
  80. @"https://www.google.com");
  81. XCTAssertEqual(FIRIAMRenderTriggerOnAppForeground, first.renderTriggers[0].triggerType);
  82. FIRIAMMessageDefinition *second = results[1];
  83. XCTAssertEqualObjects(@"9350598726327992320", second.renderData.messageID);
  84. XCTAssertEqualObjects(@"Inception1", second.renderData.name);
  85. XCTAssertEqualObjects(@"Test 2", second.renderData.contentData.titleText);
  86. XCTAssertNil(second.renderData.contentData.bodyText);
  87. XCTAssertEqual(FIRIAMRenderAsModalView, second.renderData.renderingEffectSettings.viewMode);
  88. XCTAssertEqual(2, second.renderTriggers.count);
  89. XCTAssertEqualObjects(second.renderData.renderingEffectSettings.displayBGColor,
  90. [UIColor firiam_colorWithHexString:@"#ffffff"]);
  91. // Third message is a banner view message based on a analytics event trigger.
  92. FIRIAMMessageDefinition *third = results[2];
  93. XCTAssertEqualObjects(@"14819094573862617088", third.renderData.messageID);
  94. XCTAssertEqual(FIRIAMRenderAsBannerView, third.renderData.renderingEffectSettings.viewMode);
  95. XCTAssertEqual(1, third.renderTriggers.count);
  96. XCTAssertEqualObjects(@"jackpot", third.renderTriggers[0].firebaseEventName);
  97. }
  98. - (void)testParsingTestMessage {
  99. NSString *testJsonDataFilePath = [[NSBundle bundleForClass:[self class]]
  100. pathForResource:@"TestJsonDataWithTestMessageFromFetch"
  101. ofType:@"txt"];
  102. self.jsonResposne = [[NSString alloc] initWithContentsOfFile:testJsonDataFilePath
  103. encoding:NSUTF8StringEncoding
  104. error:nil];
  105. NSData *data = [self.jsonResposne dataUsingEncoding:NSUTF8StringEncoding];
  106. NSError *errorJson = nil;
  107. NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
  108. options:kNilOptions
  109. error:&errorJson];
  110. NSInteger discardCount;
  111. NSNumber *fetchWaitTime;
  112. NSArray<FIRIAMMessageDefinition *> *results =
  113. [self.parser parseAPIResponseDictionary:responseDict
  114. discardedMsgCount:&discardCount
  115. fetchWaitTimeInSeconds:&fetchWaitTime];
  116. // In our fixture file used in this test, there is no fetch expiration time
  117. XCTAssertNil(fetchWaitTime);
  118. XCTAssertEqual(2, [results count]);
  119. XCTAssertEqual(0, discardCount);
  120. // First is a test message and the second one is not.
  121. XCTAssertTrue(results[0].isTestMessage);
  122. XCTAssertTrue(results[0].renderData.renderingEffectSettings.isTestMessage);
  123. XCTAssertFalse(results[1].isTestMessage);
  124. XCTAssertFalse(results[1].renderData.renderingEffectSettings.isTestMessage);
  125. }
  126. - (void)testParsingInvalidTestMessageNodes {
  127. NSString *testJsonDataFilePath = [[NSBundle bundleForClass:[self class]]
  128. pathForResource:@"JsonDataWithInvalidMessagesFromFetch"
  129. ofType:@"txt"];
  130. self.jsonResposne = [[NSString alloc] initWithContentsOfFile:testJsonDataFilePath
  131. encoding:NSUTF8StringEncoding
  132. error:nil];
  133. NSData *data = [self.jsonResposne dataUsingEncoding:NSUTF8StringEncoding];
  134. NSError *errorJson = nil;
  135. NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
  136. options:kNilOptions
  137. error:&errorJson];
  138. NSInteger discardCount;
  139. NSNumber *fetchWaitTime;
  140. NSArray<FIRIAMMessageDefinition *> *results =
  141. [self.parser parseAPIResponseDictionary:responseDict
  142. discardedMsgCount:&discardCount
  143. fetchWaitTimeInSeconds:&fetchWaitTime];
  144. XCTAssertEqual(0, [results count]);
  145. // First node missing title, second one missig triggering conditions and the third one
  146. // contains invalid type node.
  147. XCTAssertEqual(3, discardCount);
  148. }
  149. @end