aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Database/Tests/Helpers/FEventTester.m
blob: f75e11b47d05b4d3403d3903fb063bddb1804d13 (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
163
164
165
166
167
168
169
170
171
172
/*
 * 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 "FEventTester.h"
#import "FirebaseCommunity/FIRDatabaseReference.h"
#import "FTupleBoolBlock.h"
#import "FTupleEventTypeString.h"
#import "FTestHelpers.h"
#import "SenTest+FWaiter.h"

@implementation FEventTester

@synthesize lookingFor;
@synthesize callbacksCalled;
@synthesize from;
@synthesize errors;
@synthesize seenFirebaseLocations;
@synthesize initializationEvents;
@synthesize actualPathsAndEvents;

- (id)initFrom:(XCTestCase *)elsewhere
{
    self = [super init];
    if (self) {
        self.seenFirebaseLocations = [[NSMutableDictionary alloc] init];
        self.initializationEvents = 0;
        self.lookingFor = [[NSMutableArray alloc] init];
        self.actualPathsAndEvents = [[NSMutableArray alloc] init];
        self.from = elsewhere;
        self.callbacksCalled = 0;
    }
    return self;
}

- (void) addLookingFor:(NSArray *)l {

    // expect them in the order they're given to us
    [self.lookingFor addObjectsFromArray:l];


    // see notes on ordering of listens in init.spec.js
    NSArray* toListen = [l sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        FTupleEventTypeString* a = obj1;
        FTupleEventTypeString* b = obj2;
        NSUInteger lenA = [a.firebase description].length;
        NSUInteger lenB = [b.firebase description].length;
        if (lenA < lenB) {
            return NSOrderedAscending;
        } else if (lenA == lenB) {
            return NSOrderedSame;
        } else {
            return NSOrderedDescending;
        }
    }];

    for(FTupleEventTypeString* fevts in toListen) {
        if(! [self.seenFirebaseLocations objectForKey:[fevts.firebase description]]) {
            fevts.vvcallback = [self listenOnPath:fevts.firebase];
            fevts.initialized = NO;
            [self.seenFirebaseLocations setObject:fevts forKey:[fevts.firebase description]];
        }
    }
}

- (void) unregister {
    for(FTupleEventTypeString* fevts in self.lookingFor) {
        if (fevts.vvcallback) {
            fevts.vvcallback();
        }
    }
    [self.lookingFor removeAllObjects];
}

- (fbt_void_void) listenOnPath:(FIRDatabaseReference *)path {
    FIRDatabaseHandle removedHandle = [path observeEventType:FIRDataEventTypeChildRemoved withBlock:[self makeEventCallback:FIRDataEventTypeChildRemoved]];
    FIRDatabaseHandle addedHandle = [path observeEventType:FIRDataEventTypeChildAdded withBlock:[self makeEventCallback:FIRDataEventTypeChildAdded]];
    FIRDatabaseHandle movedHandle = [path observeEventType:FIRDataEventTypeChildMoved withBlock:[self makeEventCallback:FIRDataEventTypeChildMoved]];
    FIRDatabaseHandle changedHandle = [path observeEventType:FIRDataEventTypeChildChanged withBlock:[self makeEventCallback:FIRDataEventTypeChildChanged]];
    FIRDatabaseHandle valueHandle = [path observeEventType:FIRDataEventTypeValue withBlock:[self makeEventCallback:FIRDataEventTypeValue]];

    fbt_void_void cb = ^() {
        [path removeObserverWithHandle:removedHandle];
        [path removeObserverWithHandle:addedHandle];
        [path removeObserverWithHandle:movedHandle];
        [path removeObserverWithHandle:changedHandle];
        [path removeObserverWithHandle:valueHandle];
    };
    return [cb copy];
}

- (void) wait {
    [self waitUntil:^BOOL{
        return self.actualPathsAndEvents.count >= self.lookingFor.count;
    } timeout:kFirebaseTestTimeout];

    for (int i = 0; i < self.lookingFor.count; ++i) {
        FTupleEventTypeString* target = [self.lookingFor objectAtIndex:i];
        FTupleEventTypeString* recvd = [self.actualPathsAndEvents objectAtIndex:i];
        XCTAssertTrue([target isEqualTo:recvd], @"Expected %@ to match %@", target, recvd);
    }

    if (self.actualPathsAndEvents.count > self.lookingFor.count) {
        NSLog(@"Too many events: %@", self.actualPathsAndEvents);
        XCTFail(@"Received too many events");
    }
}

- (void) waitForInitialization {
    [self waitUntil:^BOOL{
        for (FTupleEventTypeString* evt in [self.seenFirebaseLocations allValues]) {
            if (!evt.initialized) {
                return NO;
            }
        }

        // splice out all of the initialization events
        NSRange theRange;
        theRange.location = 0;
        theRange.length = self.initializationEvents;
        [actualPathsAndEvents removeObjectsInRange:theRange];

        return YES;
    } timeout:kFirebaseTestTimeout];
}

- (fbt_void_datasnapshot) makeEventCallback:(FIRDataEventType)type {

    fbt_void_datasnapshot cb = ^(FIRDataSnapshot * snap) {

        FIRDatabaseReference * ref = snap.ref;
        NSString* name = nil;
        if (type != FIRDataEventTypeValue) {
            ref = ref.parent;
            name = snap.key;
        }

        FTupleEventTypeString* evt = [[FTupleEventTypeString alloc] initWithFirebase:ref withEvent:type withString:name];
        [actualPathsAndEvents addObject:evt];

        NSLog(@"Adding event: %@ (%@)", evt, [snap value]);

        FTupleEventTypeString* targetEvt = [self.seenFirebaseLocations objectForKey:[ref description]];
        if (targetEvt && !targetEvt.initialized) {
            self.initializationEvents++;
            if (type == FIRDataEventTypeValue) {
                targetEvt.initialized = YES;
            }
        }
    };
    return [cb copy];
}


- (void) failWithException:(NSException *) anException {
    //TODO: FIX
    @throw anException;
}

@end