FIRMessagingExtensionHelperTest.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. - (void)testModifyNotificationWithValidPayloadData {
  48. XCTestExpectation *validPayloadExpectation =
  49. [self expectationWithDescription:@"Test payload is valid."];
  50. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  51. content.userInfo = @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : kValidImageURL}};
  52. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  53. [validPayloadExpectation fulfill];
  54. };
  55. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  56. OCMVerify([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  57. completionHandler:[OCMArg any]]);
  58. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  59. }
  60. - (void)testModifyNotificationWithInvalidPayloadData {
  61. XCTestExpectation *validPayloadExpectation =
  62. [self expectationWithDescription:@"Test payload is valid."];
  63. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  64. content.userInfo =
  65. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
  66. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  67. [validPayloadExpectation fulfill];
  68. };
  69. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  70. OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  71. completionHandler:[OCMArg any]]);
  72. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  73. }
  74. - (void)testModifyNotificationWithEmptyPayloadData {
  75. XCTestExpectation *validPayloadExpectation =
  76. [self expectationWithDescription:@"Test payload is valid."];
  77. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  78. content.userInfo =
  79. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
  80. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  81. [validPayloadExpectation fulfill];
  82. };
  83. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  84. OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  85. completionHandler:[OCMArg any]]);
  86. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  87. }
  88. #endif
  89. @end