aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Example/Tests/Integration/API/FIRListenerRegistrationTests.m
blob: 9751844b6766275cc974ca964c66ef523b0e7326 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

@import FirebaseFirestore;

#import <XCTest/XCTest.h>

#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
#import "Firestore/Source/API/FIRFirestore+Internal.h"
#import "Firestore/Source/Core/FSTFirestoreClient.h"

@interface FIRListenerRegistrationTests : FSTIntegrationTestCase
@end

@implementation FIRListenerRegistrationTests

- (void)testCanBeRemoved {
  FIRCollectionReference *collectionRef = [self collectionRef];
  FIRDocumentReference *docRef = [collectionRef documentWithAutoID];

  __block int callbacks = 0;
  id<FIRListenerRegistration> one = [collectionRef
      addSnapshotListener:^(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error) {
        XCTAssertNil(error);
        callbacks++;
      }];

  id<FIRListenerRegistration> two = [collectionRef
      addSnapshotListener:^(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error) {
        XCTAssertNil(error);
        callbacks++;
      }];

  // Wait for initial events
  [self waitUntil:^BOOL {
    return callbacks == 2;
  }];

  // Trigger new events
  [self writeDocumentRef:docRef data:@{@"foo" : @"bar"}];

  // Write events should have triggered
  XCTAssertEqual(4, callbacks);

  // No more events should occur
  [one remove];
  [two remove];

  [self writeDocumentRef:docRef data:@{@"foo" : @"new-bar"}];

  // Assert no further events occurred
  XCTAssertEqual(4, callbacks);
}

- (void)testCanBeRemovedTwice {
  FIRCollectionReference *collectionRef = [self collectionRef];
  FIRDocumentReference *docRef = [collectionRef documentWithAutoID];

  id<FIRListenerRegistration> one = [collectionRef
      addSnapshotListener:^(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error){
      }];
  id<FIRListenerRegistration> two = [docRef
      addSnapshotListener:^(FIRDocumentSnapshot *_Nullable snapshot, NSError *_Nullable error){
      }];

  [one remove];
  [one remove];

  [two remove];
  [two remove];
}

- (void)testCanBeRemovedIndependently {
  FIRCollectionReference *collectionRef = [self collectionRef];
  FIRDocumentReference *docRef = [collectionRef documentWithAutoID];

  __block int callbacksOne = 0;
  __block int callbacksTwo = 0;
  id<FIRListenerRegistration> one = [collectionRef
      addSnapshotListener:^(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error) {
        XCTAssertNil(error);
        callbacksOne++;
      }];

  id<FIRListenerRegistration> two = [collectionRef
      addSnapshotListener:^(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error) {
        XCTAssertNil(error);
        callbacksTwo++;
      }];

  // Wait for initial events
  [self waitUntil:^BOOL {
    return callbacksOne == 1 && callbacksTwo == 1;
  }];

  // Trigger new events
  [self writeDocumentRef:docRef data:@{@"foo" : @"bar"}];

  // Write events should have triggered
  XCTAssertEqual(2, callbacksOne);
  XCTAssertEqual(2, callbacksTwo);

  // Should leave "two" unaffected
  [one remove];

  [self writeDocumentRef:docRef data:@{@"foo" : @"new-bar"}];

  // Assert only events for "two" actually occurred
  XCTAssertEqual(2, callbacksOne);
  XCTAssertEqual(3, callbacksTwo);

  [self writeDocumentRef:docRef data:@{@"foo" : @"new-bar"}];

  // No more events should occur
  [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