FIRMessagingLinkHandlingTest.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2017 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 <FirebaseInstanceID/FIRInstanceID.h>
  19. #import <GoogleUtilities/GULUserDefaults.h>
  20. #import <FirebaseMessaging/FIRMessaging.h>
  21. #import "Example/Messaging/Tests/FIRMessagingTestNotificationUtilities.h"
  22. #import "Example/Messaging/Tests/FIRMessagingTestUtilities.h"
  23. #import "Firebase/Messaging/FIRMessagingConstants.h"
  24. NSString *const kFIRMessagingTestsLinkHandlingSuiteName = @"com.messaging.test_linkhandling";
  25. @interface FIRMessaging ()
  26. - (NSURL *)linkURLFromMessage:(NSDictionary *)message;
  27. @end
  28. @interface FIRMessagingLinkHandlingTest : XCTestCase
  29. @property(nonatomic, readonly, strong) FIRMessaging *messaging;
  30. @property(nonatomic, strong) FIRMessagingTestUtilities * testUtil;
  31. @end
  32. @implementation FIRMessagingLinkHandlingTest
  33. - (void)setUp {
  34. [super setUp];
  35. NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingTestsLinkHandlingSuiteName];
  36. _testUtil = [[FIRMessagingTestUtilities alloc] initWithUserDefaults:defaults withRMQManager:NO];
  37. _messaging = _testUtil.messaging;
  38. }
  39. - (void)tearDown {
  40. [_testUtil cleanupAfterTest];
  41. _messaging = nil;
  42. [[[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingTestsLinkHandlingSuiteName] removePersistentDomainForName:kFIRMessagingTestsLinkHandlingSuiteName];
  43. [super tearDown];
  44. }
  45. #pragma mark - Link Handling Testing
  46. - (void)testNonExistentLinkInMessage {
  47. NSMutableDictionary *notification =
  48. [FIRMessagingTestNotificationUtilities createBasicNotificationWithUniqueMessageID];
  49. NSURL *url = [_messaging linkURLFromMessage:notification];
  50. XCTAssertNil(url);
  51. }
  52. - (void)testEmptyLinkInMessage {
  53. NSMutableDictionary *notification =
  54. [FIRMessagingTestNotificationUtilities createBasicNotificationWithUniqueMessageID];
  55. notification[kFIRMessagingMessageLinkKey] = @"";
  56. NSURL *url = [_messaging linkURLFromMessage:notification];
  57. XCTAssertNil(url);
  58. }
  59. - (void)testNonStringLinkInMessage {
  60. NSMutableDictionary *notification =
  61. [FIRMessagingTestNotificationUtilities createBasicNotificationWithUniqueMessageID];
  62. notification[kFIRMessagingMessageLinkKey] = @(5);
  63. NSURL *url = [_messaging linkURLFromMessage:notification];
  64. XCTAssertNil(url);
  65. }
  66. - (void)testInvalidURLStringLinkInMessage {
  67. NSMutableDictionary *notification =
  68. [FIRMessagingTestNotificationUtilities createBasicNotificationWithUniqueMessageID];
  69. notification[kFIRMessagingMessageLinkKey] = @"This is not a valid url string";
  70. NSURL *url = [_messaging linkURLFromMessage:notification];
  71. XCTAssertNil(url);
  72. }
  73. - (void)testValidURLStringLinkInMessage {
  74. NSMutableDictionary *notification =
  75. [FIRMessagingTestNotificationUtilities createBasicNotificationWithUniqueMessageID];
  76. notification[kFIRMessagingMessageLinkKey] = @"https://www.google.com/";
  77. NSURL *url = [_messaging linkURLFromMessage:notification];
  78. XCTAssertTrue([url.absoluteString isEqualToString:@"https://www.google.com/"]);
  79. }
  80. @end