FIRMessagingExtensionHelperTest.m 4.3 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 <XCTest/XCTest.h>
  17. #import <OCMock/OCMock.h>
  18. #import "FIRMessaging.h"
  19. #import "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 = @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : kValidImageURL}};
  57. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  58. [validPayloadExpectation fulfill];
  59. };
  60. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  61. OCMVerify([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  62. completionHandler:[OCMArg any]]);
  63. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  64. }
  65. }
  66. #endif
  67. - (void)testModifyNotificationWithInvalidPayloadData {
  68. if (@available(macOS 10.14, iOS 10.0, *)) {
  69. XCTestExpectation *validPayloadExpectation =
  70. [self expectationWithDescription:@"Test payload is valid."];
  71. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  72. content.userInfo =
  73. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
  74. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  75. [validPayloadExpectation fulfill];
  76. };
  77. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  78. OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  79. completionHandler:[OCMArg any]]);
  80. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  81. }
  82. }
  83. - (void)testModifyNotificationWithEmptyPayloadData {
  84. if (@available(macOS 10.14, iOS 10.0, *)) {
  85. XCTestExpectation *validPayloadExpectation =
  86. [self expectationWithDescription:@"Test payload is valid."];
  87. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  88. content.userInfo =
  89. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
  90. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  91. [validPayloadExpectation fulfill];
  92. };
  93. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  94. OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  95. completionHandler:[OCMArg any]]);
  96. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  97. }
  98. }
  99. @end
  100. #endif