FIRMessagingExtensionHelperTest.m 4.4 KB

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