FIRMessagingUtilitiesTest.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2021 Google LLC
  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 "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  19. @interface FIRMessagingUtilitiesTest : XCTestCase
  20. @property(nonatomic, strong) id mainBundleMock;
  21. @end
  22. @implementation FIRMessagingUtilitiesTest
  23. - (void)setUp {
  24. _mainBundleMock = OCMPartialMock([NSBundle mainBundle]);
  25. [super setUp];
  26. }
  27. - (void)tearDown {
  28. [_mainBundleMock stopMocking];
  29. [super tearDown];
  30. }
  31. - (void)testAPNSTupleStringReturnsNilIfDeviceTokenNil {
  32. NSString *tupleString = FIRMessagingAPNSTupleStringForTokenAndServerType(nil, NO);
  33. XCTAssertNil(tupleString);
  34. }
  35. - (void)testAPNSTupleStringReturnsValidData {
  36. NSData *deviceToken = [@"FAKE_DEVICE_TOKEN" dataUsingEncoding:NSUTF8StringEncoding];
  37. NSString *expectedTokenString = FIRMessagingStringForAPNSDeviceToken(deviceToken);
  38. NSString *tupleString = FIRMessagingAPNSTupleStringForTokenAndServerType(deviceToken, NO);
  39. NSArray<NSString *> *components = [tupleString componentsSeparatedByString:@"_"];
  40. XCTAssertTrue(components.count == 2);
  41. XCTAssertEqualObjects(components.firstObject, @"p");
  42. XCTAssertEqualObjects(components.lastObject, expectedTokenString);
  43. }
  44. - (void)testAppVersionReturnsExpectedValue {
  45. NSString *expectedVersion = @"1.2.3";
  46. NSDictionary *fakeInfoDictionary = @{@"CFBundleShortVersionString" : expectedVersion};
  47. [[[_mainBundleMock stub] andReturn:fakeInfoDictionary] infoDictionary];
  48. NSString *appVersion = FIRMessagingCurrentAppVersion();
  49. XCTAssertEqualObjects(appVersion, expectedVersion);
  50. }
  51. - (void)testAppVersionReturnsEmptyStringWhenNotFound {
  52. NSDictionary *fakeInfoDictionary = @{};
  53. [[[_mainBundleMock stub] andReturn:fakeInfoDictionary] infoDictionary];
  54. NSString *appVersion = FIRMessagingCurrentAppVersion();
  55. XCTAssertEqualObjects(appVersion, @"");
  56. }
  57. - (void)testAppIdentifierReturnsExpectedValue {
  58. #if TARGET_OS_WATCH
  59. NSDictionary *fakeInfoDictionary =
  60. @{@"NSExtension" : @{@"NSExtensionPointIdentifier" : @"com.apple.watchkit"}};
  61. [[[_mainBundleMock stub] andReturn:fakeInfoDictionary] infoDictionary];
  62. NSString *bundleIdentifier = @"com.me.myapp.watchkit.watchkitextensions";
  63. NSString *expectedIdentifier = @"com.me.myapp.watchkit";
  64. #else // TARGET_OS_WATCH
  65. NSString *bundleIdentifier = @"com.me.myapp";
  66. NSString *expectedIdentifier = @"com.me.myapp";
  67. #endif // TARGET_OS_WATCH
  68. [[[_mainBundleMock stub] andReturn:bundleIdentifier] bundleIdentifier];
  69. NSString *appIdentifier = FIRMessagingAppIdentifier();
  70. XCTAssertEqualObjects(appIdentifier, expectedIdentifier);
  71. }
  72. - (void)testAppIdentifierReturnsEmptyStringWhenNotFound {
  73. [[[_mainBundleMock stub] andReturn:@""] bundleIdentifier];
  74. NSString *appIdentifier = FIRMessagingAppIdentifier();
  75. XCTAssertEqualObjects(appIdentifier, @"");
  76. }
  77. - (void)testLocaleHasChanged {
  78. NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
  79. [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
  80. XCTAssertTrue(FIRMessagingHasLocaleChanged());
  81. [[NSUserDefaults standardUserDefaults] setObject:FIRMessagingCurrentLocale()
  82. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  83. XCTAssertFalse(FIRMessagingHasLocaleChanged());
  84. [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant"
  85. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  86. XCTAssertTrue(FIRMessagingHasLocaleChanged());
  87. }
  88. @end