FIRBundleUtilTest.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "FIRTestCase.h"
  15. #import <FirebaseCore/FIRBundleUtil.h>
  16. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  17. static NSString *const kResultPath = @"resultPath";
  18. static NSString *const kResourceName = @"resourceName";
  19. static NSString *const kFileType = @"fileType";
  20. @interface FIRBundleUtilTest : FIRTestCase
  21. @property(nonatomic, strong) id mockBundle;
  22. @end
  23. @implementation FIRBundleUtilTest
  24. - (void)setUp {
  25. [super setUp];
  26. self.mockBundle = OCMClassMock([NSBundle class]);
  27. }
  28. - (void)testRelevantBundles_mainIsFirst {
  29. // Pointer compare to same instance of main bundle.
  30. XCTAssertEqual([NSBundle mainBundle], [FIRBundleUtil relevantBundles][0]);
  31. }
  32. // TODO: test that adding a bundle appears in "all bundles"
  33. // once the use-case is understood.
  34. - (void)testFindOptionsDictionaryPath {
  35. [OCMStub([self.mockBundle pathForResource:kResourceName ofType:kFileType]) andReturn:kResultPath];
  36. XCTAssertEqualObjects([FIRBundleUtil optionsDictionaryPathWithResourceName:kResourceName
  37. andFileType:kFileType
  38. inBundles:@[ self.mockBundle ]],
  39. kResultPath);
  40. }
  41. - (void)testFindOptionsDictionaryPath_notFound {
  42. XCTAssertNil([FIRBundleUtil optionsDictionaryPathWithResourceName:kResourceName
  43. andFileType:kFileType
  44. inBundles:@[ self.mockBundle ]]);
  45. }
  46. - (void)testFindOptionsDictionaryPath_secondBundle {
  47. NSBundle *mockBundleEmpty = OCMClassMock([NSBundle class]);
  48. [OCMStub([self.mockBundle pathForResource:kResourceName ofType:kFileType]) andReturn:kResultPath];
  49. NSArray *bundles = @[ mockBundleEmpty, self.mockBundle ];
  50. XCTAssertEqualObjects([FIRBundleUtil optionsDictionaryPathWithResourceName:kResourceName
  51. andFileType:kFileType
  52. inBundles:bundles],
  53. kResultPath);
  54. }
  55. - (void)testBundleIdentifierExistsInBundles {
  56. NSString *bundleID = @"com.google.test";
  57. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:bundleID];
  58. XCTAssertTrue([FIRBundleUtil hasBundleIdentifierPrefix:bundleID inBundles:@[ self.mockBundle ]]);
  59. }
  60. - (void)testBundleIdentifierExistsInBundles_notExist {
  61. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.test"];
  62. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"not-exist"
  63. inBundles:@[ self.mockBundle ]]);
  64. }
  65. - (void)testBundleIdentifierExistsInBundles_emptyBundlesArray {
  66. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test" inBundles:@[]]);
  67. }
  68. - (void)testBundleIdentifierHasPrefixInBundlesForExtension {
  69. id environmentUtilsMock = [OCMockObject mockForClass:[GULAppEnvironmentUtil class]];
  70. [[[environmentUtilsMock stub] andReturnValue:@(YES)] isAppExtension];
  71. // Mock bundle should have what app extension has, the extension bundle ID.
  72. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.test.someextension"];
  73. XCTAssertTrue([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test"
  74. inBundles:@[ self.mockBundle ]]);
  75. [environmentUtilsMock stopMocking];
  76. }
  77. - (void)testBundleIdentifierHasPrefixInBundlesNotValidExtension {
  78. id environmentUtilsMock = [OCMockObject mockForClass:[GULAppEnvironmentUtil class]];
  79. [[[environmentUtilsMock stub] andReturnValue:@(YES)] isAppExtension];
  80. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.test.someextension.some"];
  81. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test"
  82. inBundles:@[ self.mockBundle ]]);
  83. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.testsomeextension"];
  84. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test"
  85. inBundles:@[ self.mockBundle ]]);
  86. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"com.google.testsomeextension.some"];
  87. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test"
  88. inBundles:@[ self.mockBundle ]]);
  89. [OCMStub([self.mockBundle bundleIdentifier]) andReturn:@"not-exist"];
  90. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.test"
  91. inBundles:@[ self.mockBundle ]]);
  92. // Should be NO, since if @"com.google.tests" is an app extension identifier, then the app bundle
  93. // identifier is @"com.google"
  94. XCTAssertFalse([FIRBundleUtil hasBundleIdentifierPrefix:@"com.google.tests"
  95. inBundles:@[ self.mockBundle ]]);
  96. [environmentUtilsMock stopMocking];
  97. }
  98. @end