FIRMessagingLinkHandlingTest.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <XCTest/XCTest.h>
  17. #import "OCMock.h"
  18. #import <GoogleUtilities/GULUserDefaults.h>
  19. #import "Firebase/InstanceID/Public/FIRInstanceID.h"
  20. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  21. #import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  22. #import "FirebaseMessaging/Tests/UnitTests/FIRMessagingTestNotificationUtilities.h"
  23. #import "FirebaseMessaging/Tests/UnitTests/FIRMessagingTestUtilities.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 =
  36. [[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingTestsLinkHandlingSuiteName];
  37. _testUtil = [[FIRMessagingTestUtilities alloc] initWithUserDefaults:defaults withRMQManager:NO];
  38. _messaging = _testUtil.messaging;
  39. }
  40. - (void)tearDown {
  41. [_testUtil cleanupAfterTest:self];
  42. _messaging = nil;
  43. [[[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingTestsLinkHandlingSuiteName]
  44. removePersistentDomainForName:kFIRMessagingTestsLinkHandlingSuiteName];
  45. [super tearDown];
  46. }
  47. #pragma mark - Link Handling Testing
  48. - (void)testNonExistentLinkInMessage {
  49. NSMutableDictionary *notification =
  50. [FIRMessagingTestNotificationUtilities createBasicNotificationWithUniqueMessageID];
  51. NSURL *url = [_messaging linkURLFromMessage:notification];
  52. XCTAssertNil(url);
  53. }
  54. - (void)testEmptyLinkInMessage {
  55. NSMutableDictionary *notification =
  56. [FIRMessagingTestNotificationUtilities createBasicNotificationWithUniqueMessageID];
  57. notification[kFIRMessagingMessageLinkKey] = @"";
  58. NSURL *url = [_messaging linkURLFromMessage:notification];
  59. XCTAssertNil(url);
  60. }
  61. - (void)testNonStringLinkInMessage {
  62. NSMutableDictionary *notification =
  63. [FIRMessagingTestNotificationUtilities createBasicNotificationWithUniqueMessageID];
  64. notification[kFIRMessagingMessageLinkKey] = @(5);
  65. NSURL *url = [_messaging linkURLFromMessage:notification];
  66. XCTAssertNil(url);
  67. }
  68. - (void)testInvalidURLStringLinkInMessage {
  69. NSMutableDictionary *notification =
  70. [FIRMessagingTestNotificationUtilities createBasicNotificationWithUniqueMessageID];
  71. notification[kFIRMessagingMessageLinkKey] = @"This is not a valid url string";
  72. NSURL *url = [_messaging linkURLFromMessage:notification];
  73. XCTAssertNil(url);
  74. }
  75. - (void)testValidURLStringLinkInMessage {
  76. NSMutableDictionary *notification =
  77. [FIRMessagingTestNotificationUtilities createBasicNotificationWithUniqueMessageID];
  78. notification[kFIRMessagingMessageLinkKey] = @"https://www.google.com/";
  79. NSURL *url = [_messaging linkURLFromMessage:notification];
  80. XCTAssertTrue([url.absoluteString isEqualToString:@"https://www.google.com/"]);
  81. }
  82. @end