FEventTests.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright 2017 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 "FEventTests.h"
  17. #import "FTestHelpers.h"
  18. #import "FTupleEventTypeString.h"
  19. #import "FEventTester.h"
  20. @implementation FEventTests
  21. - (void) testInvalidEventType {
  22. FIRDatabaseReference * f = [FTestHelpers getRandomNode];
  23. XCTAssertThrows([f observeEventType:-4 withBlock:^(FIRDataSnapshot *s) {}], @"Invalid event type properly throws an error");
  24. }
  25. - (void) testWriteLeafExpectValueChanged {
  26. FTupleFirebase* tuple = [FTestHelpers getRandomNodePair];
  27. FIRDatabaseReference * writeNode = tuple.one;
  28. FIRDatabaseReference * readNode = tuple.two;
  29. __block BOOL done = NO;
  30. [writeNode setValue:@1234 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) { done = YES; }];
  31. [self waitUntil:^BOOL{ return done; }];
  32. [super snapWaiter:readNode withBlock:^(FIRDataSnapshot *s) {
  33. XCTAssertEqualObjects([s value], @1234, @"Proper value in snapshot");
  34. }];
  35. }
  36. - (void) testWRiteLeafNodeThenExpectValueEvent {
  37. FIRDatabaseReference * writeNode = [FTestHelpers getRandomNode];
  38. [writeNode setValue:@42];
  39. [super snapWaiter:writeNode withBlock:^(FIRDataSnapshot *s) {
  40. XCTAssertEqualObjects([s value], @42, @"Proper value in snapshot");
  41. }];
  42. }
  43. - (void) testWriteLeafNodeThenExpectChildAddedEventThenValueEvent {
  44. FIRDatabaseReference * writeNode = [FTestHelpers getRandomNode];
  45. [[writeNode child:@"foo"] setValue:@878787];
  46. NSArray* lookingFor = @[
  47. [[FTupleEventTypeString alloc] initWithFirebase:writeNode withEvent:FIRDataEventTypeChildAdded withString:@"foo"],
  48. [[FTupleEventTypeString alloc] initWithFirebase:writeNode withEvent:FIRDataEventTypeValue withString:nil],
  49. ];
  50. FEventTester* et = [[FEventTester alloc] initFrom:self];
  51. [et addLookingFor:lookingFor];
  52. [et wait];
  53. [super snapWaiter:writeNode withBlock:^(FIRDataSnapshot *s) {
  54. XCTAssertEqualObjects([[s childSnapshotForPath:@"foo"] value], @878787, @"Got proper value");
  55. }];
  56. }
  57. - (void) testSetMultipleEventListenersOnSameNode {
  58. FTupleFirebase* tuple = [FTestHelpers getRandomNodePair];
  59. FIRDatabaseReference * writeNode = tuple.one;
  60. FIRDatabaseReference * readNode = tuple.two;
  61. [writeNode setValue:@42];
  62. // two write nodes
  63. FEventTester* et = [[FEventTester alloc] initFrom:self];
  64. [et addLookingFor:@[[[FTupleEventTypeString alloc] initWithFirebase:writeNode withEvent:FIRDataEventTypeValue withString:nil] ]];
  65. [et wait];
  66. et = [[FEventTester alloc] initFrom:self];
  67. [et addLookingFor:@[[[FTupleEventTypeString alloc] initWithFirebase:writeNode withEvent:FIRDataEventTypeValue withString:nil] ]];
  68. [et wait];
  69. // two read nodes
  70. et = [[FEventTester alloc] initFrom:self];
  71. [et addLookingFor:@[[[FTupleEventTypeString alloc] initWithFirebase:readNode withEvent:FIRDataEventTypeValue withString:nil] ]];
  72. [et wait];
  73. et = [[FEventTester alloc] initFrom:self];
  74. [et addLookingFor:@[[[FTupleEventTypeString alloc] initWithFirebase:readNode withEvent:FIRDataEventTypeValue withString:nil] ]];
  75. [et wait];
  76. }
  77. - (void) testUnsubscribeEventsAndConfirmThatEventsNoLongerFire {
  78. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  79. __block int numValueCB = 0;
  80. FIRDatabaseHandle handle = [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *s) {
  81. numValueCB = numValueCB + 1;
  82. }];
  83. // Set
  84. for(int i = 0; i < 3; i++) {
  85. [node setValue:[NSNumber numberWithInt:i]];
  86. }
  87. // bye
  88. [node removeObserverWithHandle:handle];
  89. // set again
  90. for(int i = 10; i < 15; i++) {
  91. [node setValue:[NSNumber numberWithInt:i]];
  92. }
  93. for(int i = 20; i < 25; i++) {
  94. [node setValue:[NSNumber numberWithInt:i]];
  95. }
  96. // Should just be 3
  97. [self waitUntil:^BOOL{
  98. return numValueCB == 3;
  99. }];
  100. }
  101. - (void) testCanWriteACompoundObjectAndGetMoreGranularEventsForIndividualChanges {
  102. FTupleFirebase* tuple = [FTestHelpers getRandomNodePair];
  103. FIRDatabaseReference * writeNode = tuple.one;
  104. FIRDatabaseReference * readNode = tuple.two;
  105. __block BOOL done = NO;
  106. [writeNode setValue:@{@"a": @10, @"b": @20} withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  107. done = YES;
  108. }];
  109. [self waitUntil:^BOOL{ return done; }];
  110. NSArray* lookingForW = @[
  111. [[FTupleEventTypeString alloc] initWithFirebase:[writeNode child:@"a"] withEvent:FIRDataEventTypeValue withString:nil],
  112. [[FTupleEventTypeString alloc] initWithFirebase:[writeNode child:@"b"] withEvent:FIRDataEventTypeValue withString:nil],
  113. ];
  114. NSArray* lookingForR = @[
  115. [[FTupleEventTypeString alloc] initWithFirebase:[readNode child:@"a"] withEvent:FIRDataEventTypeValue withString:nil],
  116. [[FTupleEventTypeString alloc] initWithFirebase:[readNode child:@"b"] withEvent:FIRDataEventTypeValue withString:nil],
  117. ];
  118. FEventTester* etW = [[FEventTester alloc] initFrom:self];
  119. [etW addLookingFor:lookingForW];
  120. [etW wait];
  121. FEventTester* etR = [[FEventTester alloc] initFrom:self];
  122. [etR addLookingFor:lookingForR];
  123. [etR wait];
  124. // Modify compound but just change one of them
  125. lookingForW = @[[[FTupleEventTypeString alloc] initWithFirebase:[writeNode child:@"b"] withEvent:FIRDataEventTypeValue withString:nil] ];
  126. lookingForR = @[[[FTupleEventTypeString alloc] initWithFirebase:[readNode child:@"b"] withEvent:FIRDataEventTypeValue withString:nil] ];
  127. [etW addLookingFor:lookingForW];
  128. [etR addLookingFor:lookingForR];
  129. [writeNode setValue:@{@"a": @10, @"b": @30}];
  130. [etW wait];
  131. [etR wait];
  132. }
  133. - (void) testValueEventIsFiredForEmptyNode {
  134. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  135. __block BOOL valueFired = NO;
  136. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *s) {
  137. XCTAssertTrue([[s value] isEqual:[NSNull null]], @"Value is properly nil");
  138. valueFired = YES;
  139. }];
  140. [self waitUntil:^BOOL{
  141. return valueFired;
  142. }];
  143. }
  144. - (void) testCorrectEventsRaisedWhenLeafTurnsIntoInternalNode {
  145. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  146. NSMutableString* eventString = [[NSMutableString alloc] init];
  147. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *s) {
  148. if ([s hasChildren]) {
  149. [eventString appendString:@", got children"];
  150. }
  151. else {
  152. [eventString appendFormat:@", value %@", [s value]];
  153. }
  154. }];
  155. [node observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *s) {
  156. [eventString appendFormat:@", child_added %@", [s key]];
  157. }];
  158. [node setValue:@42];
  159. [node setValue:@{@"a": @2}];
  160. [node setValue:@84];
  161. __block BOOL done = NO;
  162. [node setValue:nil withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) { done = YES; }];
  163. [self waitUntil:^BOOL{ return done; }];
  164. XCTAssertEqualObjects(@", value 42, child_added a, got children, value 84, value <null>", eventString, @"Proper order seen");
  165. }
  166. - (void) testRegisteringCallbackMultipleTimesAndUnregistering {
  167. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  168. __block int changes = 0;
  169. fbt_void_datasnapshot cb = ^(FIRDataSnapshot *snapshot) { changes = changes + 1; };
  170. FIRDatabaseHandle handle1 = [node observeEventType:FIRDataEventTypeValue withBlock:cb];
  171. FIRDatabaseHandle handle2 = [node observeEventType:FIRDataEventTypeValue withBlock:cb];
  172. FIRDatabaseHandle handle3 = [node observeEventType:FIRDataEventTypeValue withBlock:cb];
  173. __block BOOL done = NO;
  174. [node setValue:@42 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) { done = YES; }];
  175. [self waitUntil:^BOOL{ return done; }];
  176. done = NO;
  177. XCTAssertTrue(changes == 3, @"Saw 3 callback events %d", changes);
  178. [node removeObserverWithHandle:handle1];
  179. [node setValue:@84 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) { done = YES; }];
  180. [self waitUntil:^BOOL{ return done; }];
  181. done = NO;
  182. XCTAssertTrue(changes == 5, @"Saw 5 callback events %d", changes);
  183. [node removeObserverWithHandle:handle2];
  184. [node setValue:@168 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) { done = YES; }];
  185. [self waitUntil:^BOOL{ return done; }];
  186. done = NO;
  187. XCTAssertTrue(changes == 6, @"Saw 6 callback events %d", changes);
  188. [node removeObserverWithHandle:handle3];
  189. [node setValue:@376 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) { done = YES; }];
  190. [self waitUntil:^BOOL{ return done; }];
  191. done = NO;
  192. XCTAssertTrue(changes == 6, @"Saw 6 callback events %d", changes);
  193. NSLog(@"callbacks: %d", changes);
  194. }
  195. - (void) testUnregisteringTheSameCallbackTooManyTimesDoesNothing {
  196. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  197. fbt_void_datasnapshot cb = ^(FIRDataSnapshot *snapshot) { };
  198. FIRDatabaseHandle handle1 = [node observeEventType:FIRDataEventTypeValue withBlock:cb];
  199. [node removeObserverWithHandle:handle1];
  200. [node removeObserverWithHandle:handle1];
  201. XCTAssertTrue(YES, @"Properly reached end of test without throwing errors.");
  202. }
  203. - (void) testOnceValueFiresExactlyOnce {
  204. FIRDatabaseReference * path = [FTestHelpers getRandomNode];
  205. __block BOOL firstCall = YES;
  206. [path observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  207. XCTAssertTrue(firstCall, @"Properly saw first call");
  208. firstCall = NO;
  209. XCTAssertEqualObjects(@42, [snapshot value], @"Properly saw node value");
  210. }];
  211. [path setValue:@42];
  212. [path setValue:@84];
  213. __block BOOL done = NO;
  214. [path setValue:nil withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) { done = YES; }];
  215. [self waitUntil:^BOOL{ return done; }];
  216. }
  217. - (void) testOnceChildAddedFiresExaclyOnce {
  218. __block int badCount = 0;
  219. // for(int i = 0; i < 100; i++) {
  220. FIRDatabaseReference * path = [FTestHelpers getRandomNode];
  221. __block BOOL firstCall = YES;
  222. __block BOOL done = NO;
  223. [path observeSingleEventOfType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
  224. XCTAssertTrue(firstCall, @"Properly saw first call");
  225. firstCall = NO;
  226. XCTAssertEqualObjects(@42, [snapshot value], @"Properly saw node value");
  227. XCTAssertEqualObjects(@"foo", [snapshot key], @"Properly saw the first node");
  228. if (![[snapshot value] isEqual:@42]) {
  229. exit(-1);
  230. badCount = badCount + 1;
  231. }
  232. done = YES;
  233. }];
  234. [[path child:@"foo"] setValue:@42];
  235. [[path child:@"bar"] setValue:@84]; // XXX FIXME sometimes this event fires first
  236. [[path child:@"foo"] setValue:@168];
  237. // [path setValue:nil withCompletionBlock:^(BOOL status) { done = YES; }];
  238. [self waitUntil:^BOOL{ return done; }];
  239. // }
  240. NSLog(@"BADCOUNT: %d", badCount);
  241. }
  242. - (void) testOnceValueFiresExacltyOnceEvenIfThereIsASetInsideCallback {
  243. FIRDatabaseReference * path = [FTestHelpers getRandomNode];
  244. __block BOOL firstCall = YES;
  245. __block BOOL done = NO;
  246. [path observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  247. XCTAssertTrue(firstCall, @"Properly saw first call");
  248. if (firstCall) {
  249. firstCall = NO;
  250. XCTAssertEqualObjects(@42, [snapshot value], @"Properly saw node value");
  251. [path setValue:@43];
  252. done = YES;
  253. }
  254. else {
  255. XCTFail(@"Callback got called more than once.");
  256. }
  257. }];
  258. [path setValue:@42];
  259. [path setValue:@84];
  260. [self waitUntil:^BOOL{ return done; }];
  261. }
  262. - (void) testOnceChildAddedFiresOnceEvenWithCompoundObject {
  263. FIRDatabaseReference * path = [FTestHelpers getRandomNode];
  264. __block BOOL firstCall = YES;
  265. [path observeSingleEventOfType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
  266. XCTAssertTrue(firstCall, @"Properly saw first call");
  267. firstCall = NO;
  268. XCTAssertEqualObjects(@84, [snapshot value], @"Properly saw node value");
  269. XCTAssertEqualObjects(@"bar", [snapshot key], @"Properly saw the first node");
  270. }];
  271. [path setValue:@{@"foo": @42, @"bar": @84}];
  272. __block BOOL done = NO;
  273. [path setValue:nil withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) { done = YES; }];
  274. [self waitUntil:^BOOL{ return done; }];
  275. }
  276. - (void) testOnEmptyChildFires {
  277. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  278. __block BOOL done = NO;
  279. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  280. }];
  281. [[node child:@"test"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  282. XCTAssertTrue([[snapshot value] isEqual:[NSNull null]], @"Properly saw nil child node");
  283. done = YES;
  284. }];
  285. [self waitUntil:^BOOL{ return done; }];
  286. }
  287. - (void) testOnEmptyChildEvenAfterParentIsSynched {
  288. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  289. __block BOOL parentDone = NO;
  290. __block BOOL done = NO;
  291. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  292. parentDone = YES;
  293. }];
  294. [self waitUntil:^BOOL{
  295. return parentDone;
  296. }];
  297. [[node child:@"test"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  298. XCTAssertTrue([[snapshot value] isEqual:[NSNull null]], @"Child is properly nil");
  299. done = YES;
  300. }];
  301. // This test really isn't in the same spirit as the JS test; we can't currently make sure that the test fires right away since the ON and callback are async
  302. [self waitUntil:^BOOL{
  303. return done;
  304. }];
  305. XCTAssertTrue(done, @"Done fired.");
  306. }
  307. - (void) testEventsAreRaisedChildRemovedChildAddedChildMoved {
  308. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  309. NSMutableArray* events = [[NSMutableArray alloc] init];
  310. [node observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snap) {
  311. [events addObject:[NSString stringWithFormat:@"added %@", [snap key]]];
  312. }];
  313. [node observeEventType:FIRDataEventTypeChildRemoved withBlock:^(FIRDataSnapshot *snap) {
  314. [events addObject:[NSString stringWithFormat:@"removed %@", [snap key]]];
  315. }];
  316. [node observeEventType:FIRDataEventTypeChildMoved withBlock:^(FIRDataSnapshot *snap) {
  317. [events addObject:[NSString stringWithFormat:@"moved %@", [snap key]]];
  318. }];
  319. __block BOOL done = NO;
  320. [node setValue:@{
  321. @"a": @{@".value": @1, @".priority": @0 },
  322. @"b": @{@".value": @1, @".priority": @1 },
  323. @"c": @{@".value": @1, @".priority": @2 },
  324. @"d": @{@".value": @1, @".priority": @3 },
  325. @"e": @{@".value": @1, @".priority": @4 },
  326. @"f": @{@".value": @1, @".priority": @5 },
  327. } withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  328. done = YES;
  329. }];
  330. [self waitUntil:^BOOL{
  331. return done;
  332. }];
  333. [events removeAllObjects];
  334. done = NO;
  335. [node setValue:@{
  336. @"a": @{@".value": @1, @".priority": @5 },
  337. @"aa": @{@".value": @1, @".priority": @0 },
  338. @"b": @{@".value": @1, @".priority": @1 },
  339. @"bb": @{@".value": @1, @".priority": @2 },
  340. @"d": @{@".value": @1, @".priority": @3 },
  341. @"e": @{@".value": @1, @".priority": @6 },
  342. }
  343. withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  344. done = YES;
  345. }
  346. ];
  347. [self waitUntil:^BOOL{
  348. return done;
  349. }];
  350. XCTAssertEqualObjects(@"removed c, removed f, added aa, added bb, moved a, moved e", [events componentsJoinedByString:@", "], @"Got expected results");
  351. }
  352. - (void) testIntegerToDoubleConversions {
  353. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  354. NSMutableArray<NSString *>* events = [[NSMutableArray alloc] init];
  355. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snap) {
  356. [events addObject:[NSString stringWithFormat:@"value %@", [snap value]]];
  357. }];
  358. for(NSNumber *number in @[@1, @1.0, @1, @1.1]) {
  359. [self waitForCompletionOf:node setValue:number];
  360. }
  361. XCTAssertEqualObjects(@"value 1, value 1.1", [events componentsJoinedByString:@", "],
  362. @"Got expected results");
  363. }
  364. - (void) testEventsAreRaisedProperlyWithOnQueryLimits {
  365. // xxx impl query
  366. }
  367. @end