FIRAppAssociationRegistrationUnitTests.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "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)() = ^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 registeredObjectWithHost:host
  127. key:kKey
  128. creationBlock:^id _Nullable() {
  129. [FIRAppAssociationRegistration registeredObjectWithHost:host
  130. key:kKey
  131. creationBlock:gCreateNewObject];
  132. return gCreateNewObject();
  133. }]);
  134. }
  135. - (void)testReentrySameHostDifferentKey {
  136. id host = gCreateNewObject();
  137. [FIRAppAssociationRegistration registeredObjectWithHost:host
  138. key:kKey1
  139. creationBlock:^id _Nullable() {
  140. [FIRAppAssociationRegistration registeredObjectWithHost:host
  141. key:kKey2
  142. creationBlock:gCreateNewObject];
  143. return gCreateNewObject();
  144. }];
  145. // Expect no exception raised.
  146. }
  147. - (void)testReentryDifferentHostSameKey {
  148. id host1 = gCreateNewObject();
  149. id host2 = gCreateNewObject();
  150. [FIRAppAssociationRegistration registeredObjectWithHost:host1
  151. key:kKey
  152. creationBlock:^id _Nullable() {
  153. [FIRAppAssociationRegistration registeredObjectWithHost:host2
  154. key:kKey
  155. creationBlock:gCreateNewObject];
  156. return gCreateNewObject();
  157. }];
  158. // Expect no exception raised.
  159. }
  160. - (void)testReentryDifferentHostDifferentKey {
  161. id host1 = gCreateNewObject();
  162. id host2 = gCreateNewObject();
  163. [FIRAppAssociationRegistration registeredObjectWithHost:host1
  164. key:kKey1
  165. creationBlock:^id _Nullable() {
  166. [FIRAppAssociationRegistration registeredObjectWithHost:host2
  167. key:kKey2
  168. creationBlock:gCreateNewObject];
  169. return gCreateNewObject();
  170. }];
  171. // Expect no exception raised.
  172. }
  173. @end