summaryrefslogtreecommitdiff
path: root/platform/ios/Integration Tests/MGLCameraTransitionTests.mm
blob: 60d5fc6c9a6f480d4249b8275fb87050b4258384 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#import "MGLMapViewIntegrationTest.h"
#import "MGLTestUtility.h"
#import "../../darwin/src/MGLGeometry_Private.h"

@interface MBCameraTransitionTests : MGLMapViewIntegrationTest
@end

@implementation MBCameraTransitionTests

- (void)testSetAndResetNorthWithDispatchAsyncInDelegateMethod {

    XCTestExpectation *expectation = [self expectationWithDescription:@"regionDidChange expectation"];
    expectation.expectedFulfillmentCount = 2;
    expectation.assertForOverFulfill = YES;

    __weak typeof(self) weakself = self;

    self.regionDidChange = ^(MGLMapView *mapView, MGLCameraChangeReason reason, BOOL animated) {

        MBCameraTransitionTests *strongSelf = weakself;

        if (!strongSelf) return;

        [expectation fulfill];

        MGLTestAssert(strongSelf, mapView.userTrackingMode != MGLUserTrackingModeFollowWithHeading);
        if (mapView.direction != 0.0) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [mapView resetNorth];
            });
        }
    };

    [self.mapView setDirection:90 animated:YES];

    // loop, render, and wait
    [self waitForExpectations:@[expectation] timeout:10];
}


- (void)testSetAndResetNorthInDelegateMethod {

    XCTestExpectation *expectation = [self expectationWithDescription:@"regionDidChange expectation"];
    expectation.expectedFulfillmentCount = 2;
    expectation.assertForOverFulfill = YES;

    __weak typeof(self) weakself = self;

    self.regionDidChange = ^(MGLMapView *mapView, MGLCameraChangeReason reason, BOOL animated) {

        MBCameraTransitionTests *strongSelf = weakself;

        if (!strongSelf) return;

        [expectation fulfill];

        MGLTestAssert(strongSelf, mapView.userTrackingMode != MGLUserTrackingModeFollowWithHeading);
        if (mapView.direction != 0.0) {
            NSLog(@"Reset to north");
            [mapView resetNorth];
        }
    };

    [self.mapView setDirection:90 animated:YES];
    [self waitForExpectations:@[expectation] timeout:10];
}

- (void)testInterruptingAndResetNorthOnlyOnceInIsChanging {

    // Reset to non-zero, prior to testing
    [self.mapView setDirection:45 animated:NO];

    XCTestExpectation *expectation = [self expectationWithDescription:@"regionDidChange expectation"];
    expectation.expectedFulfillmentCount = 1;
    expectation.assertForOverFulfill = YES;

    __weak typeof(self) weakself = self;
    __block BOOL startedReset = NO;
    __block BOOL finishedReset = NO;

    self.regionIsChanging = ^(MGLMapView *mapView) {
        MBCameraTransitionTests *strongSelf = weakself;
        if (!strongSelf) return;

        if (!startedReset) {
            NSLog(@"Reset to north, interrupting the previous transition");
            startedReset = YES;
            [mapView resetNorth];
            finishedReset = YES;
        }
    };

    self.regionDidChange = ^(MGLMapView *mapView, MGLCameraChangeReason reason, BOOL animated) {
        MBCameraTransitionTests *strongSelf = weakself;
        if (!strongSelf) return;

        MGLTestAssert(strongSelf, startedReset);

        if (finishedReset) {
            MGLTestAssert(strongSelf, !(reason & MGLCameraChangeReasonTransitionCancelled));
            [expectation fulfill];
        }
        else {
            MGLTestAssert(strongSelf, reason & MGLCameraChangeReasonTransitionCancelled);
        }
    };

    [self.mapView setDirection:90 animated:YES];
    [self waitForExpectations:@[expectation] timeout:10];

    XCTAssertEqualWithAccuracy(self.mapView.direction, 0.0, 0.001, @"Camera should have reset to north. %0.3f", self.mapView.direction);
}

- (void)testSetCenterCancelsTransitions {
    XCTestExpectation *cameraIsInDCExpectation = [self expectationWithDescription:@"camera reset to DC"];

    CLLocationCoordinate2D dc = CLLocationCoordinate2DMake(38.894368, -77.036487);
    CLLocationCoordinate2D dc_west = CLLocationCoordinate2DMake(38.894368, -77.076487);

    double zoomLevel = 15.0;

    [self.mapView setCenterCoordinate:dc zoomLevel:zoomLevel animated:NO];
    [self.mapView setCenterCoordinate:dc_west zoomLevel:zoomLevel animated:YES];

    __weak typeof(self) weakself = self;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.15 * NSEC_PER_SEC),
                   dispatch_get_main_queue(),
                   ^{
                       MBCameraTransitionTests *strongSelf = weakself;

                       [strongSelf.mapView setCenterCoordinate:dc zoomLevel:zoomLevel animated:NO];
                       MGLTestAssertEqualWithAccuracy(strongSelf,
                                                      dc.latitude,
                                                      strongSelf.mapView.centerCoordinate.latitude,
                                                      0.0005,
                                                      @"setting center coordinate should cancel transitions");
                       MGLTestAssertEqualWithAccuracy(strongSelf,
                                                      dc.longitude,
                                                      strongSelf.mapView.centerCoordinate.longitude,
                                                      0.0005,
                                                      @"setting center coordinate should cancel transitions");
                       [cameraIsInDCExpectation fulfill];
                   });

    [self waitForExpectations:@[cameraIsInDCExpectation] timeout:10.0];
}

- (void)testSetCenterCoordinateInDelegateMethod {

    XCTestExpectation *expectation = [self expectationWithDescription:@"regionDidChange expectation"];
    expectation.expectedFulfillmentCount = 2;
    expectation.assertForOverFulfill = YES;

    __weak typeof(self) weakself = self;
    __block NSInteger delegateCallCount = 0;

    CLLocationCoordinate2D target = CLLocationCoordinate2DMake(40.0, 40.0);
    CLLocationCoordinate2D target2 = CLLocationCoordinate2DMake(-40.0, -40.0);

    self.regionDidChange = ^(MGLMapView *mapView, MGLCameraChangeReason reason, BOOL animated) {

        MBCameraTransitionTests *strongSelf = weakself;

        if (!strongSelf) return;

        MGLTestAssert(strongSelf, mapView.userTrackingMode != MGLUserTrackingModeFollowWithHeading);

        CLLocationCoordinate2D center = mapView.centerCoordinate;

        switch(delegateCallCount) {
            case 0:
            {
                // Our center coordinate should match our target (assuming we're not
                // constrained by zoom level)
                MGLTestAssertEqualWithAccuracy(strongSelf,
                                               target.longitude,
                                               center.longitude,
                                               0.0005,
                                               @"center coordinate longitude should be at target");

                MGLTestAssertEqualWithAccuracy(strongSelf,
                                               target.latitude,
                                               center.latitude,
                                               0.0005,
                                               @"center coordinate latitude should be at target");

                // Now set another coordinate.
                // Should take MGLAnimationDuration seconds (0.3s)
                [mapView setCenterCoordinate:target2 animated:YES];
                break;
            }

            case 1:
            {
                // Our center coordinate should match our target (assuming we're not
                // constrained by zoom level)
                MGLTestAssertEqualWithAccuracy(strongSelf,
                                               target2.longitude,
                                               center.longitude,
                                               0.0005,
                                               @"center coordinate longitude should be at target2");

                MGLTestAssertEqualWithAccuracy(strongSelf,
                                               target2.latitude,
                                               center.latitude,
                                               0.0005,
                                               @"center coordinate latitude should be at target2");
                break;

            }

            default:
                MGLTestFail(strongSelf);
                break;
        }

        delegateCallCount++;

        [expectation fulfill];
    };

    // Should take MGLAnimationDuration seconds (0.3)
    [self.mapView setCenterCoordinate:target zoomLevel:15.0 animated:YES];
    [self waitForExpectations:@[expectation] timeout:10];
}

- (void)testFlyToCameraInDelegateMethod {

    XCTestExpectation *expectation = [self expectationWithDescription:@"regionDidChange expectation"];

    __weak typeof(self) weakself = self;
    __block NSInteger delegateCallCount = 0;
    expectation.expectedFulfillmentCount = 3;
    expectation.assertForOverFulfill = YES;

    CLLocationCoordinate2D target = CLLocationCoordinate2DMake(40.0, 40.0);
    CLLocationCoordinate2D target2 = CLLocationCoordinate2DMake(30.0, 30.0);

    __block BOOL runloop = YES;

    NSTimeInterval stop0 = CACurrentMediaTime();
    __block NSTimeInterval stop1 = 0.0;
    __block NSTimeInterval stop2 = 0.0;

    double zoomLevel = 5.0;
    double altitude = MGLAltitudeForZoomLevel(zoomLevel, 0.0, target.latitude, self.mapView.frame.size);

    self.regionDidChange = ^(MGLMapView *mapView, MGLCameraChangeReason reason, BOOL animated) {

        MBCameraTransitionTests *strongSelf = weakself;

        if (!strongSelf) return;

        MGLTestAssert(strongSelf, mapView.userTrackingMode != MGLUserTrackingModeFollowWithHeading);

        CLLocationCoordinate2D center = mapView.centerCoordinate;

        switch(delegateCallCount) {
            case 0:
            {
                stop1 = CACurrentMediaTime();

                // Our center coordinate should match our target (assuming we're not
                // constrained by zoom level)
                MGLTestAssertEqualWithAccuracy(strongSelf,
                                               target.longitude,
                                               center.longitude,
                                               0.0005,
                                               @"center coordinate longitude should be at target");

                MGLTestAssertEqualWithAccuracy(strongSelf,
                                               target.latitude,
                                               center.latitude,
                                               0.0005,
                                               @"center coordinate latitude should be at target");

                // Now set another coordinate.
                MGLMapCamera *camera = [MGLMapCamera cameraLookingAtCenterCoordinate:target2
                                                                            altitude:altitude
                                                                               pitch:0.0
                                                                             heading:0.0];

                // flyToCamera can take a while...
                [mapView flyToCamera:camera completionHandler:^{
                    MGLTestAssert(strongSelf, !runloop, @"Completion block should be called after delegate method");
                    [expectation fulfill];
                    stop2 = CACurrentMediaTime();
                }];
                break;
            }

            case 1:
            {
                // Our center coordinate should match our target (assuming we're not
                // constrained by zoom level)
                MGLTestAssertEqualWithAccuracy(strongSelf,
                                               target2.longitude,
                                               center.longitude,
                                               0.0005,
                                               @"center coordinate longitude should be at target2");

                MGLTestAssertEqualWithAccuracy(strongSelf,
                                               target2.latitude,
                                               center.latitude,
                                               0.0005,
                                               @"center coordinate latitude should be at target2");

                runloop = NO;
                break;
            }

            default:
                MGLTestFail(strongSelf);
                break;
        }

        delegateCallCount++;

        [expectation fulfill];
    };

    // Should take MGLAnimationDuration
    [self.mapView setCenterCoordinate:target zoomLevel:zoomLevel animated:YES];

    [self waitForExpectations:@[expectation] timeout:10];

    NSLog(@"setCenterCoordinate: %0.4fs", stop1 - stop0);
    NSLog(@"flyToCamera: %0.4fs", stop2 - stop1);

    XCTAssert(delegateCallCount == 2, @"Expecting 2 regionDidChange callbacks, got %ld", delegateCallCount); // Once for the setDirection and once for the reset north
}

#pragma mark - Pending tests

- (void)testContinuallyResettingNorthInIsChangingPENDING {
    // See https://github.com/mapbox/mapbox-gl-native/pull/11614
    // This test currently fails, unsurprisingly, since we're continually
    // setting the camera to the same parameters during its update.
    //
    // Possible solutions/expectations:
    // - If you set camera parameters that match the *current* target parameters
    //      then the transition could be a no-op. We'd need to consider any completion
    //      block
    //  - Ideally we would detect this case and disallow it.

    // Reset to non-zero, prior to testing
    [self.mapView setDirection:45 animated:NO];

    XCTestExpectation *expectation = [self expectationWithDescription:@"regionDidChange expectation"];
    expectation.expectedFulfillmentCount = 2;
    expectation.assertForOverFulfill = YES;

    self.regionIsChanging = ^(MGLMapView *mapView) {
        [mapView resetNorth];
    };

    self.regionDidChange = ^(MGLMapView *mapView, MGLCameraChangeReason reason, BOOL animated) {
        [expectation fulfill];
    };

    [self.mapView setDirection:90 animated:YES];
    [self waitForExpectations:@[expectation] timeout:10];

    XCTAssertEqualWithAccuracy(self.mapView.direction, 0.0, 0.001, @"Camera should have reset to north. %0.3f", self.mapView.direction);
}

- (void)testContinuallySettingCoordinateInIsChangingPENDING {
    // See above comment in `-testContinuallyResettingNorthInIsChangingPENDING`

    // Reset to non-zero, prior to testing
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(0.0, 0.0) animated:NO];

    XCTestExpectation *expectation = [self expectationWithDescription:@"regionDidChange expectation"];
    expectation.expectedFulfillmentCount = 2;
    expectation.assertForOverFulfill = YES;

    __weak typeof(self) weakself = self;

    self.regionIsChanging = ^(MGLMapView *mapView) {
        [weakself.mapView setCenterCoordinate:CLLocationCoordinate2DMake(-40.0, -40.0) animated:YES];
    };

    self.regionDidChange = ^(MGLMapView *mapView, MGLCameraChangeReason reason, BOOL animated) {
        [expectation fulfill];
    };

    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(40.0, 40.0) animated:YES];
    [self waitForExpectations:@[expectation] timeout:10];

    XCTAssertEqualWithAccuracy(self.mapView.direction, 0.0, 0.001, @"Camera should have reset to north. %0.3f", self.mapView.direction);
}

@end