FIRMessagingUtilitiesTest.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. NSString *bundleIdentifier = @"com.me.myapp.watchkit.watchkitextensions";
  60. NSString *expectedIdentifier = @"com.me.myapp.watchkit";
  61. #else
  62. NSString *bundleIdentifier = @"com.me.myapp";
  63. NSString *expectedIdentifier = @"com.me.myapp";
  64. #endif
  65. [[[_mainBundleMock stub] andReturn:bundleIdentifier] bundleIdentifier];
  66. NSString *appIdentifier = FIRMessagingAppIdentifier();
  67. XCTAssertEqualObjects(appIdentifier, expectedIdentifier);
  68. }
  69. - (void)testAppIdentifierReturnsEmptyStringWhenNotFound {
  70. [[[_mainBundleMock stub] andReturn:@""] bundleIdentifier];
  71. NSString *appIdentifier = FIRMessagingAppIdentifier();
  72. XCTAssertEqualObjects(appIdentifier, @"");
  73. }
  74. - (void)testLocaleHasChanged {
  75. NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
  76. [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
  77. XCTAssertTrue(FIRMessagingHasLocaleChanged());
  78. [[NSUserDefaults standardUserDefaults] setObject:FIRMessagingCurrentLocale()
  79. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  80. XCTAssertFalse(FIRMessagingHasLocaleChanged());
  81. [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant"
  82. forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
  83. XCTAssertTrue(FIRMessagingHasLocaleChanged());
  84. }
  85. @end