FIRBundleUtilTest.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebaseCore/Tests/Unit/FIRTestCase.h"
  15. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  16. #import "FirebaseCore/Sources/FIRBundleUtil.h"
  17. #import "SharedTestUtilities/FIROptionsMock.h"
  18. static NSString *const kResultPath = @"resultPath";
  19. static NSString *const kResourceName = @"resourceName";
  20. static NSString *const kFileType = @"fileType";
  21. @interface FIRBundleUtilTest : FIRTestCase
  22. @property(nonatomic, strong) id mockBundle;
  23. @end
  24. @implementation FIRBundleUtilTest
  25. - (void)setUp {
  26. [super setUp];
  27. self.mockBundle = OCMClassMock([NSBundle class]);
  28. }
  29. - (void)testRelevantBundles_mainIsFirst {
  30. // Pointer compare to same instance of main bundle.
  31. XCTAssertEqual([NSBundle mainBundle], [FIRBundleUtil relevantBundles][0]);
  32. }
  33. // TODO: test that adding a bundle appears in "all bundles"
  34. // once the use-case is understood.
  35. - (void)testFindOptionsDictionaryPath {
  36. [OCMStub([self.mockBundle pathForResource:kResourceName ofType:kFileType]) andReturn:kResultPath];
  37. XCTAssertEqualObjects([FIRBundleUtil optionsDictionaryPathWithResourceName:kResourceName
  38. andFileType:kFileType
  39. inBundles:@[ self.mockBundle ]],
  40. kResultPath);
  41. }
  42. - (void)testFindOptionsDictionaryPath_notFound {
  43. XCTAssertNil([FIRBundleUtil optionsDictionaryPathWithResourceName:kResourceName
  44. andFileType:kFileType
  45. inBundles:@[ self.mockBundle ]]);
  46. }
  47. - (void)testFindOptionsDictionaryPath_secondBundle {
  48. NSBundle *mockBundleEmpty = OCMClassMock([NSBundle class]);
  49. [OCMStub([self.mockBundle pathForResource:kResourceName ofType:kFileType]) andReturn:kResultPath];
  50. NSArray *bundles = @[ mockBundleEmpty, self.mockBundle ];
  51. XCTAssertEqualObjects([FIRBundleUtil optionsDictionaryPathWithResourceName:kResourceName
  52. andFileType:kFileType
  53. inBundles:bundles],
  54. kResultPath);
  55. }
  56. - (void)testBundleIdentifierExistsInBundles {
  57. NSString *bundleID = @"com.google.test";
  58. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:bundleID];
  59. XCTAssertTrue([FIRBundleUtil hasBundleIdentifierPrefix:bundleID inBundles:@[ self.mockBundle ]]);
  60. }
  61. - (void)testBundleIdentifierExistsInBundles_notExist {
  62. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.test"];
  63. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"not-exist"
  64. inBundles:@[ self.mockBundle ]]);
  65. }
  66. - (void)testBundleIdentifierExistsInBundles_emptyBundlesArray {
  67. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test" inBundles:@[]]);
  68. }
  69. - (void)testBundleIdentifierHasPrefixInBundlesForExtension {
  70. id environmentUtilsMock = [OCMockObject mockForClass:[GULAppEnvironmentUtil class]];
  71. [[[environmentUtilsMock stub] andReturnValue:@(YES)] isAppExtension];
  72. // Mock bundle should have what app extension has, the extension bundle ID.
  73. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.test.someextension"];
  74. XCTAssertTrue([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test"
  75. inBundles:@[ self.mockBundle ]]);
  76. [environmentUtilsMock stopMocking];
  77. }
  78. - (void)testBundleIdentifierExistsInBundlesForExtensions_exactMatch {
  79. id environmentUtilsMock = [OCMockObject mockForClass:[GULAppEnvironmentUtil class]];
  80. [[[environmentUtilsMock stub] andReturnValue:@(YES)] isAppExtension];
  81. // Mock bundle should have what app extension has, the extension bundle ID.
  82. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.test.someextension"];
  83. XCTAssertTrue([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test.someextension"
  84. inBundles:@[ self.mockBundle ]]);
  85. [environmentUtilsMock stopMocking];
  86. }
  87. - (void)testBundleIdentifierHasPrefixInBundlesNotValidExtension {
  88. id environmentUtilsMock = [OCMockObject mockForClass:[GULAppEnvironmentUtil class]];
  89. [[[environmentUtilsMock stub] andReturnValue:@(YES)] isAppExtension];
  90. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.test.someextension.some"];
  91. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test"
  92. inBundles:@[ self.mockBundle ]]);
  93. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.testsomeextension"];
  94. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test"
  95. inBundles:@[ self.mockBundle ]]);
  96. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.testsomeextension.some"];
  97. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test"
  98. inBundles:@[ self.mockBundle ]]);
  99. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"not-exist"];
  100. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test"
  101. inBundles:@[ self.mockBundle ]]);
  102. // Should be NO, since if @"com.google.tests" is an app extension identifier, then the app bundle
  103. // identifier is @"com.google"
  104. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.tests"
  105. inBundles:@[ self.mockBundle ]]);
  106. [environmentUtilsMock stopMocking];
  107. }
  108. @end