FIRInstanceIDUtilitiesTest.m 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "Firebase/InstanceID/FIRInstanceIDUtilities.h"
  19. @interface FIRInstanceIDUtilitiesTest : XCTestCase
  20. @property(nonatomic, strong) id mainBundleMock;
  21. @end
  22. @implementation FIRInstanceIDUtilitiesTest
  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 = FIRInstanceIDAPNSTupleStringForTokenAndServerType(nil, NO);
  33. XCTAssertNil(tupleString);
  34. }
  35. - (void)testAPNSTupleStringReturnsValidData {
  36. NSData *deviceToken = [@"FAKE_DEVICE_TOKEN" dataUsingEncoding:NSUTF8StringEncoding];
  37. NSString *expectedTokenString = FIRInstanceIDStringForAPNSDeviceToken(deviceToken);
  38. NSString *tupleString = FIRInstanceIDAPNSTupleStringForTokenAndServerType(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 = FIRInstanceIDCurrentAppVersion();
  49. XCTAssertEqualObjects(appVersion, expectedVersion);
  50. }
  51. - (void)testAppVersionReturnsEmptyStringWhenNotFound {
  52. NSDictionary *fakeInfoDictionary = @{};
  53. [[[_mainBundleMock stub] andReturn:fakeInfoDictionary] infoDictionary];
  54. NSString *appVersion = FIRInstanceIDCurrentAppVersion();
  55. XCTAssertEqualObjects(appVersion, @"");
  56. }
  57. - (void)testAppIdentifierReturnsExpectedValue {
  58. NSString *expectedIdentifier = @"com.me.myapp";
  59. [[[_mainBundleMock stub] andReturn:expectedIdentifier] bundleIdentifier];
  60. NSString *appIdentifier = FIRInstanceIDAppIdentifier();
  61. XCTAssertEqualObjects(appIdentifier, expectedIdentifier);
  62. }
  63. - (void)testAppIdentifierReturnsEmptyStringWhenNotFound {
  64. [[[_mainBundleMock stub] andReturn:nil] bundleIdentifier];
  65. NSString *appIdentifier = FIRInstanceIDAppIdentifier();
  66. XCTAssertEqualObjects(appIdentifier, @"");
  67. }
  68. - (void)testLocaleHasChanged {
  69. NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
  70. [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
  71. XCTAssertTrue(FIRInstanceIDHasLocaleChanged());
  72. [[NSUserDefaults standardUserDefaults] setObject:FIRInstanceIDCurrentLocale()
  73. forKey:kFIRInstanceIDUserDefaultsKeyLocale];
  74. XCTAssertFalse(FIRInstanceIDHasLocaleChanged());
  75. [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant"
  76. forKey:kFIRInstanceIDUserDefaultsKeyLocale];
  77. XCTAssertTrue(FIRInstanceIDHasLocaleChanged());
  78. }
  79. @end