FIRMessagingExtensionHelperTest.m 4.2 KB

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