From ae171bb226246e8b485f688e6ddf2dd8dc9fa9be Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 11 Oct 2017 04:51:05 +0200 Subject: Adding goOnline/goOffline to the RemoteStore (#347) --- .../Tests/Integration/API/FIRDatabaseTests.m | 79 ++++++++++++++++++++++ .../Integration/API/FIRListenerRegistrationTests.m | 33 +++++++++ 2 files changed, 112 insertions(+) (limited to 'Firestore/Example/Tests/Integration/API') diff --git a/Firestore/Example/Tests/Integration/API/FIRDatabaseTests.m b/Firestore/Example/Tests/Integration/API/FIRDatabaseTests.m index 6a727ad..d22552a 100644 --- a/Firestore/Example/Tests/Integration/API/FIRDatabaseTests.m +++ b/Firestore/Example/Tests/Integration/API/FIRDatabaseTests.m @@ -18,6 +18,8 @@ #import +#import "Core/FSTFirestoreClient.h" +#import "FIRFirestore+Internal.h" #import "FSTIntegrationTestCase.h" @interface FIRDatabaseTests : FSTIntegrationTestCase @@ -766,4 +768,81 @@ XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar/baz/qux"].documentID, @"qux"); } +- (void)testCanQueueWritesWhileOffline { + XCTestExpectation *writeEpectation = [self expectationWithDescription:@"successfull write"]; + XCTestExpectation *networkExpectation = [self expectationWithDescription:@"enable network"]; + + FIRDocumentReference *doc = [self documentRef]; + FIRFirestore *firestore = doc.firestore; + NSDictionary *data = @{@"a" : @"b"}; + + [firestore.client disableNetworkWithCompletion:^(NSError *error) { + XCTAssertNil(error); + + [doc setData:data + completion:^(NSError *error) { + XCTAssertNil(error); + [writeEpectation fulfill]; + }]; + + [firestore.client enableNetworkWithCompletion:^(NSError *error) { + XCTAssertNil(error); + [networkExpectation fulfill]; + }]; + }]; + + [self awaitExpectations]; + + XCTestExpectation *getExpectation = [self expectationWithDescription:@"successfull get"]; + [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + XCTAssertEqualObjects(snapshot.data, data); + XCTAssertFalse(snapshot.metadata.isFromCache); + + [getExpectation fulfill]; + }]; + + [self awaitExpectations]; +} + +- (void)testCantGetDocumentsWhileOffline { + FIRDocumentReference *doc = [self documentRef]; + FIRFirestore *firestore = doc.firestore; + NSDictionary *data = @{@"a" : @"b"}; + + XCTestExpectation *onlineExpectation = [self expectationWithDescription:@"online read"]; + XCTestExpectation *networkExpectation = [self expectationWithDescription:@"network online"]; + + __weak FIRDocumentReference *weakDoc = doc; + + [firestore.client disableNetworkWithCompletion:^(NSError *error) { + XCTAssertNil(error); + [doc setData:data + completion:^(NSError *_Nullable error) { + XCTAssertNil(error); + + [weakDoc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + + // Verify that we are not reading from cache. + XCTAssertFalse(snapshot.metadata.isFromCache); + [onlineExpectation fulfill]; + }]; + }]; + + [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + + // Verify that we are reading from cache. + XCTAssertTrue(snapshot.metadata.fromCache); + XCTAssertEqualObjects(snapshot.data, data); + [firestore.client enableNetworkWithCompletion:^(NSError *error) { + [networkExpectation fulfill]; + }]; + }]; + }]; + + [self awaitExpectations]; +} + @end diff --git a/Firestore/Example/Tests/Integration/API/FIRListenerRegistrationTests.m b/Firestore/Example/Tests/Integration/API/FIRListenerRegistrationTests.m index 19771ff..5824314 100644 --- a/Firestore/Example/Tests/Integration/API/FIRListenerRegistrationTests.m +++ b/Firestore/Example/Tests/Integration/API/FIRListenerRegistrationTests.m @@ -18,6 +18,8 @@ #import +#import "Core/FSTFirestoreClient.h" +#import "FIRFirestore+Internal.h" #import "FSTIntegrationTestCase.h" @interface FIRListenerRegistrationTests : FSTIntegrationTestCase @@ -126,4 +128,35 @@ [two remove]; } +- (void)testWatchSurvivesNetworkDisconnect { + XCTestExpectation *testExpectiation = + [self expectationWithDescription:@"testWatchSurvivesNetworkDisconnect"]; + + FIRCollectionReference *collectionRef = [self collectionRef]; + FIRDocumentReference *docRef = [collectionRef documentWithAutoID]; + + FIRFirestore *firestore = collectionRef.firestore; + + FIRQueryListenOptions *options = [[[FIRQueryListenOptions options] + includeDocumentMetadataChanges:YES] includeQueryMetadataChanges:YES]; + + [collectionRef addSnapshotListenerWithOptions:options + listener:^(FIRQuerySnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + if (!snapshot.empty && !snapshot.metadata.fromCache) { + [testExpectiation fulfill]; + } + }]; + + [firestore.client disableNetworkWithCompletion:^(NSError *error) { + XCTAssertNil(error); + [docRef setData:@{@"foo" : @"bar"}]; + [firestore.client enableNetworkWithCompletion:^(NSError *error) { + XCTAssertNil(error); + }]; + }]; + + [self awaitExpectations]; +} + @end -- cgit v1.2.3