FIRMessagingExtensionHelperTest.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2019 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 <UIKit/UIKit.h>
  17. #import <XCTest/XCTest.h>
  18. #import <OCMock/OCMock.h>
  19. #import "FIRMessaging.h"
  20. #import "FIRMessagingExtensionHelper.h"
  21. API_AVAILABLE(ios(10.0))
  22. typedef void (^FIRMessagingContentHandler)(UNNotificationContent *content);
  23. #if TARGET_OS_IOS
  24. static NSString *const kFCMPayloadOptionsName = @"fcm_options";
  25. static NSString *const kFCMPayloadOptionsImageURLName = @"image";
  26. static NSString *const kValidImageURL =
  27. @"https://firebasestorage.googleapis.com/v0/b/fcm-ios-f7f9c.appspot.com/o/"
  28. @"chubbyBunny.jpg?alt=media&token=d6c56a57-c007-4b27-b20f-f267cc83e9e5";
  29. #endif
  30. @interface FIRMessagingExtensionHelper (ExposedForTest)
  31. #if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  32. - (void)loadAttachmentForURL:(NSURL *)attachmentURL
  33. completionHandler:(void (^)(UNNotificationAttachment *))completionHandler;
  34. #endif
  35. @end
  36. @interface FIRMessagingExtensionHelperTest : XCTestCase {
  37. id _mockExtensionHelper;
  38. }
  39. @end
  40. @implementation FIRMessagingExtensionHelperTest
  41. - (void)setUp {
  42. [super setUp];
  43. if (@available(iOS 10.0, *)) {
  44. FIRMessagingExtensionHelper *extensionHelper = [FIRMessaging extensionHelper];
  45. _mockExtensionHelper = OCMPartialMock(extensionHelper);
  46. } else {
  47. // Fallback on earlier versions
  48. }
  49. }
  50. - (void)tearDown {
  51. [_mockExtensionHelper stopMocking];
  52. }
  53. #if TARGET_OS_IOS
  54. #ifdef COCOAPODS
  55. // This test requires internet access.
  56. - (void)testModifyNotificationWithValidPayloadData {
  57. if (@available(iOS 10.0, *)) {
  58. XCTestExpectation *validPayloadExpectation =
  59. [self expectationWithDescription:@"Test payload is valid."];
  60. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  61. content.userInfo = @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : kValidImageURL}};
  62. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  63. [validPayloadExpectation fulfill];
  64. };
  65. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  66. OCMVerify([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  67. completionHandler:[OCMArg any]]);
  68. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  69. }
  70. }
  71. #endif
  72. - (void)testModifyNotificationWithInvalidPayloadData {
  73. if (@available(iOS 10.0, *)) {
  74. XCTestExpectation *validPayloadExpectation =
  75. [self expectationWithDescription:@"Test payload is valid."];
  76. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  77. content.userInfo =
  78. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
  79. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  80. [validPayloadExpectation fulfill];
  81. };
  82. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  83. OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  84. completionHandler:[OCMArg any]]);
  85. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  86. }
  87. }
  88. - (void)testModifyNotificationWithEmptyPayloadData {
  89. if (@available(iOS 10.0, *)) {
  90. XCTestExpectation *validPayloadExpectation =
  91. [self expectationWithDescription:@"Test payload is valid."];
  92. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  93. content.userInfo =
  94. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
  95. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  96. [validPayloadExpectation fulfill];
  97. };
  98. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  99. OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  100. completionHandler:[OCMArg any]]);
  101. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  102. }
  103. }
  104. #endif // TARGET_OS_IOS
  105. @end