FIRAppAssociationRegistrationUnitTests.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <XCTest/XCTest.h>
  15. #import "FirebaseCore/Sources/FIRAppAssociationRegistration.h"
  16. /** @var kKey
  17. @brief A unique string key.
  18. */
  19. static NSString *kKey = @"key";
  20. /** @var kKey1
  21. @brief A unique string key.
  22. */
  23. static NSString *kKey1 = @"key1";
  24. /** @var kKey2
  25. @brief A unique string key.
  26. */
  27. static NSString *kKey2 = @"key2";
  28. /** @var gCreateNewObject
  29. @brief A block that returns a new object everytime it is called.
  30. */
  31. static id _Nullable (^gCreateNewObject)(void) = ^id _Nullable() {
  32. return [[NSObject alloc] init];
  33. };
  34. /** @class FIRAppAssociationRegistrationTests
  35. @brief Tests for @c FIRAppAssociationRegistration
  36. */
  37. @interface FIRAppAssociationRegistrationTests : XCTestCase
  38. @end
  39. @implementation FIRAppAssociationRegistrationTests
  40. - (void)testPassObject {
  41. id host = gCreateNewObject();
  42. id obj = gCreateNewObject();
  43. id result = [FIRAppAssociationRegistration registeredObjectWithHost:host
  44. key:kKey
  45. creationBlock:^id _Nullable() {
  46. return obj;
  47. }];
  48. XCTAssertEqual(obj, result);
  49. }
  50. - (void)testPassNil {
  51. id host = gCreateNewObject();
  52. id obj = [FIRAppAssociationRegistration registeredObjectWithHost:host
  53. key:kKey
  54. creationBlock:^id _Nullable() {
  55. return nil;
  56. }];
  57. XCTAssertNil(obj);
  58. }
  59. - (void)testObjectOwnership {
  60. __weak id weakHost;
  61. __block __weak id weakObj;
  62. @autoreleasepool {
  63. id host = gCreateNewObject();
  64. weakHost = host;
  65. [FIRAppAssociationRegistration registeredObjectWithHost:host
  66. key:kKey
  67. creationBlock:^id _Nullable() {
  68. id obj = gCreateNewObject();
  69. weakObj = obj;
  70. return obj;
  71. }];
  72. // Verify that neither the host nor the object is released yet, i.e., the host owns the object
  73. // because nothing else retains the object.
  74. XCTAssertNotNil(weakHost);
  75. XCTAssertNotNil(weakObj);
  76. }
  77. // Verify that both the host and the object are released upon exit of the autorelease pool,
  78. // i.e., the host is the sole owner of the object.
  79. XCTAssertNil(weakHost);
  80. XCTAssertNil(weakObj);
  81. }
  82. - (void)testSameHostSameKey {
  83. id host = gCreateNewObject();
  84. id obj1 = [FIRAppAssociationRegistration registeredObjectWithHost:host
  85. key:kKey
  86. creationBlock:gCreateNewObject];
  87. id obj2 = [FIRAppAssociationRegistration registeredObjectWithHost:host
  88. key:kKey
  89. creationBlock:gCreateNewObject];
  90. XCTAssertEqual(obj1, obj2);
  91. }
  92. - (void)testSameHostDifferentKey {
  93. id host = gCreateNewObject();
  94. id obj1 = [FIRAppAssociationRegistration registeredObjectWithHost:host
  95. key:kKey1
  96. creationBlock:gCreateNewObject];
  97. id obj2 = [FIRAppAssociationRegistration registeredObjectWithHost:host
  98. key:kKey2
  99. creationBlock:gCreateNewObject];
  100. XCTAssertNotEqual(obj1, obj2);
  101. }
  102. - (void)testDifferentHostSameKey {
  103. id host1 = gCreateNewObject();
  104. id obj1 = [FIRAppAssociationRegistration registeredObjectWithHost:host1
  105. key:kKey
  106. creationBlock:gCreateNewObject];
  107. id host2 = gCreateNewObject();
  108. id obj2 = [FIRAppAssociationRegistration registeredObjectWithHost:host2
  109. key:kKey
  110. creationBlock:gCreateNewObject];
  111. XCTAssertNotEqual(obj1, obj2);
  112. }
  113. - (void)testDifferentHostDifferentKey {
  114. id host1 = gCreateNewObject();
  115. id obj1 = [FIRAppAssociationRegistration registeredObjectWithHost:host1
  116. key:kKey1
  117. creationBlock:gCreateNewObject];
  118. id host2 = gCreateNewObject();
  119. id obj2 = [FIRAppAssociationRegistration registeredObjectWithHost:host2
  120. key:kKey2
  121. creationBlock:gCreateNewObject];
  122. XCTAssertNotEqual(obj1, obj2);
  123. }
  124. - (void)testReentrySameHostSameKey {
  125. id host = gCreateNewObject();
  126. XCTAssertThrows([FIRAppAssociationRegistration
  127. registeredObjectWithHost:host
  128. key:kKey
  129. creationBlock:^id _Nullable() {
  130. [FIRAppAssociationRegistration registeredObjectWithHost:host
  131. key:kKey
  132. creationBlock:gCreateNewObject];
  133. return gCreateNewObject();
  134. }]);
  135. }
  136. - (void)testReentrySameHostDifferentKey {
  137. id host = gCreateNewObject();
  138. [FIRAppAssociationRegistration registeredObjectWithHost:host
  139. key:kKey1
  140. creationBlock:^id _Nullable() {
  141. [FIRAppAssociationRegistration
  142. registeredObjectWithHost:host
  143. key:kKey2
  144. creationBlock:gCreateNewObject];
  145. return gCreateNewObject();
  146. }];
  147. // Expect no exception raised.
  148. }
  149. - (void)testReentryDifferentHostSameKey {
  150. id host1 = gCreateNewObject();
  151. id host2 = gCreateNewObject();
  152. [FIRAppAssociationRegistration registeredObjectWithHost:host1
  153. key:kKey
  154. creationBlock:^id _Nullable() {
  155. [FIRAppAssociationRegistration
  156. registeredObjectWithHost:host2
  157. key:kKey
  158. creationBlock:gCreateNewObject];
  159. return gCreateNewObject();
  160. }];
  161. // Expect no exception raised.
  162. }
  163. - (void)testReentryDifferentHostDifferentKey {
  164. id host1 = gCreateNewObject();
  165. id host2 = gCreateNewObject();
  166. [FIRAppAssociationRegistration registeredObjectWithHost:host1
  167. key:kKey1
  168. creationBlock:^id _Nullable() {
  169. [FIRAppAssociationRegistration
  170. registeredObjectWithHost:host2
  171. key:kKey2
  172. creationBlock:gCreateNewObject];
  173. return gCreateNewObject();
  174. }];
  175. // Expect no exception raised.
  176. }
  177. @end