summaryrefslogtreecommitdiff
path: root/platform/node/test/js/map.test.js
blob: ce22816ef253f5a0a2d222504cb2428bc85983a9 (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
'use strict';

var test = require('tape');
var mbgl = require('../../index');
var fs = require('fs');
var path = require('path');
var style = require('../fixtures/style.json');

test('Map', function(t) {
    // This test is skipped because of the req.respond shim in index.js
    // which will always call new mbgl.Map()
    t.skip('must be constructed with new', function(t) {
        t.throws(function() {
            mbgl.Map();
        }, /Use the new operator to create new Map objects/);

        t.end();
    });

    t.test('must be constructed with options object', function(t) {
        t.throws(function() {
            new mbgl.Map();
        }, /Requires an options object as first argument/);

        t.throws(function() {
            new mbgl.Map('options');
        }, /Requires an options object as first argument/);

        t.end();
    });

    t.test('requires request property', function(t) {
        var options = {};

        t.throws(function() {
            new mbgl.Map(options);
        }, /Options object must have a 'request' method/);

        options.request = 'test';
        t.throws(function() {
            new mbgl.Map(options);
        }, /Options object must have a 'request' method/);

        options.request = function() {};
        t.doesNotThrow(function() {
            var map = new mbgl.Map(options);
            map.release();
        });

        t.end();
    });

    t.test('optional cancel property must be a function', function(t) {
        var options = {
            request: function() {}
        };

        options.cancel = 'test';
        t.throws(function() {
            new mbgl.Map(options);
        }, /Options object 'cancel' property must be a function/);

        options.cancel = function() {};
        t.doesNotThrow(function() {
            var map = new mbgl.Map(options);
            map.release();
        });

        t.end();
    });


    t.test('optional ratio property must be a number', function(t) {
        var options = {
            request: function() {}
        };

        options.ratio = 'test';
        t.throws(function() {
            new mbgl.Map(options);
        }, /Options object 'ratio' property must be a number/);

        options.ratio = 1.0;
        t.doesNotThrow(function() {
            var map = new mbgl.Map(options);
            map.release();
        });

        t.end();
    });

    t.test('instanceof mbgl.Map', function(t) {
        var options = {
            request: function() {},
            ratio: 1
        };

        var map = new mbgl.Map(options);

        t.ok(map instanceof mbgl.Map);
        t.equal(map.__proto__, mbgl.Map.prototype);

        var keys = Object.keys(mbgl.Map.prototype);

        t.deepEqual(keys, [
            'load',
            'loaded',
            'render',
            'release',
            'cancel',
            'addSource',
            'removeSource',
            'addLayer',
            'removeLayer',
            'addImage',
            'removeImage',
            'setLayerZoomRange',
            'setLayoutProperty',
            'setPaintProperty',
            'setFilter',
            'setCenter',
            'setZoom',
            'setBearing',
            'setPitch',
            'setLight',
            'setAxonometric',
            'setXSkew',
            'setYSkew',
            'dumpDebugLogs',
            'queryRenderedFeatures'
        ]);

        for (var key in keys) {
            t.equal(map[key], mbgl.Map.prototype[key]);
        }

        t.end();
    });

    t.test('.addImage', function(t) {
        var options = {
            request: function() {},
            ratio: 1
        };

        t.test('requires 3 arguments', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.addImage();
            }, /Three arguments required/);

            map.release();
            t.end();
        });

        t.test('requires image argument to be an object', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.addImage('foo', '', {});
            }, /Second argument must be an object/);

            map.release();
            t.end();
        });

        t.test('requires options argument to have a height param', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.addImage('foo', {}, {
                    width: 40,
                    pixelRatio: 2
                });
            }, /height parameter required/);

            map.release();
            t.end();
        });

        t.test('requires options argument to have a pixelRatio param', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.addImage('foo', {}, {
                    width: 40,
                    height: 40
                });
            }, /pixelRatio parameter required/);

            map.release();
            t.end();
        });

        t.test('requires specified height to be actual height of image', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.addImage('foo', new Buffer(''), {
                    width: 401,
                    height: 400,
                    pixelRatio: 1
                }, 'Image size does not match buffer size');
            });

            map.release();
            t.end();
        });

        t.test('requires height and width to be less than 1024', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.addImage('foo', new Buffer(''), {
                    width: 1025,
                    height: 1025,
                    pixelRatio: 1
                }, 'Max height and width is 1024');
            });

            map.release();
            t.end();
        });


        t.test('requires specified height to be actual height of image', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.addImage('foo', new Buffer('   '), {
                    width: 401,
                    height: 400,
                    pixelRatio: 1
                }, 'Image size does not match buffer size');
            });

            map.release();
            t.end();
        });

        t.test('No error', function(t) {
            var map = new mbgl.Map(options);

            t.doesNotThrow(function() {
                map.addImage('foo', new Buffer('    '), {
                    width: 1,
                    height: 1,
                    pixelRatio: 1
                });
            });

            map.release();
            t.end();
        });
    });

    t.test('.removeImage', function(t) {
        var options = {
            request: function() {},
            ratio: 1
        };

        t.test('requires one argument', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.removeImage();
            }, /One argument required/);

            map.release();
            t.end();
        });

        t.test('requires string as first argument', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.removeImage({});
            }, /First argument must be a string/);

            map.release();
            t.end();
        });

        t.test('removes image', function(t) {
            var map = new mbgl.Map(options);

            t.doesNotThrow(function() {
                map.removeImage('fooBar');
            });

            map.release();
            t.end();
        });
    });

    t.test('.load', function(t) {
        var options = {
            request: function() {},
            ratio: 1
        };

        t.test('requires a map style as first argument', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.load();
            }, /Requires a map style as first argument/);

            map.release();
            t.end();
        });

        t.test('requires a map style to be a valid style JSON', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.load('foo bar');
            }, /Failed to parse style: 1 - Invalid value./);

            t.throws(function() {
                map.load('""');
            }, /Failed to parse style: style must be an object/);

            map.release();
            t.end();
        });

        t.test('expect either an object or array at root', { timeout: 1000 }, function(t) {
            var map = new mbgl.Map(options);

            mbgl.once('message', function(msg) {
                t.equal(msg.severity, 'ERROR');
                t.equal(msg.class, 'ParseStyle');
                t.ok(msg.text.match(/Invalid value/));

                map.release();
                t.end();
            });

            t.throws(function() {
                map.load('invalid');
            }, /Failed to parse style: 0 - Invalid value./);
        });

        t.test('accepts an empty stylesheet string', function(t) {
            var map = new mbgl.Map(options);

            t.doesNotThrow(function() {
                map.load('{}');
            });

            map.release();
            t.end();
        });

        t.test('accepts a JSON stylesheet', { timeout: 1000 }, function(t) {
            var map = new mbgl.Map(options);

            t.doesNotThrow(function() {
                map.load(style);
            });

            map.release();
            t.end();
        });

        t.test('accepts a stringified stylesheet', { timeout: 1000 }, function(t) {
            var map = new mbgl.Map(options);

            t.doesNotThrow(function() {
                map.load(JSON.stringify(style));
            });

            map.release();
            t.end();
        });

        t.test('does not immediately trigger any tile loads', function(t) {
            var map = new mbgl.Map({
                request: function(req) {
                    t.fail('unexpected request ' + req.url);
                },
                ratio: 1
            });

            map.load(style);

            setTimeout(function() {
                map.release();
                t.end();
            }, 100);
        });
    });

    t.test('.render', function(t) {
        var options = {
            request: function(req, callback) {
                fs.readFile(path.join(__dirname, '..', req.url), function(err, data) {
                    callback(err, { data: data });
                });
            },
            ratio: 1
        };

        t.test('requires an object as the first parameter', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.render();
            }, /First argument must be an options object/);

            t.throws(function() {
                map.render('invalid');
            }, /First argument must be an options object/);

            map.release();
            t.end();
        });

        t.test('requires a callback as the second parameter', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.render({});
            }, /Second argument must be a callback function/);

            t.throws(function() {
                map.render({}, 'invalid');
            }, /Second argument must be a callback function/);

            map.release();
            t.end();
        });

        t.test('requires a style to be set', function(t) {
            var map = new mbgl.Map(options);

            t.throws(function() {
                map.render({}, function() {});
            }, /Style is not loaded/);

            map.release();
            t.end();
        });

        t.test('returns an error delayed', function(t) {
            var delay = 0;
            var map = new mbgl.Map({
                request: function(req, callback) {
                    delay += 100;
                    setTimeout(function() {
                        callback(new Error('not found'));
                    }, delay);
                },
                ratio: 1
            });
            map.load(style);
            map.render({ zoom: 1 }, function(err, data) {
                map.release();

                t.ok(err, 'returns error');
                t.end();
            });
        });

        t.test('returns an error', function(t) {
            var map = new mbgl.Map(options);
            map.load(style);
            map.render({ zoom: 1 }, function(err, data) {
                map.release();

                t.ok(err, 'returns error');
                t.end();
            });
        });

        t.test('double release', function(t) {
            var map = new mbgl.Map(options);
            map.release();

            t.throws(function() {
                map.release();
            }, /Map resources have already been released/);

            t.end();
        });

        t.test('returns an image', function(t) {
            var map = new mbgl.Map(options);
            map.load(style);
            map.render({}, function(err, pixels) {
                t.error(err);
                map.release();
                t.ok(pixels);
                t.ok(pixels instanceof Buffer);
                t.equal(pixels.length, 512 * 512 * 4)
                t.end();
            });
        });

        t.test('can be called several times in serial', function(t) {
            var completed = 0;
            var remaining = 10;
            var start = +new Date;

            var map = new mbgl.Map(options);
            map.load(style);

            function render() {
                map.render({}, function(err, data) {
                    t.error(err);

                    t.ok(true, 'render @ ' + ((+new Date) - start) + 'ms');
                    if (++completed === remaining) {
                        map.release();
                        t.end();
                    } else {
                        render();
                    }
                });
            }

            render();
        });

        t.test('throws if called in parallel', function(t) {
            var map = new mbgl.Map(options);
            map.load(style);

            t.throws(function() {
                map.render({}, function() {});
                map.render({}, function() {});
            }, /Map is currently rendering an image/);

            map.release();
            t.end();
        });

        // This can't be tested with a test-suite render test because zoom and center
        // are set via a different code path when included as style properties.
        t.test('sets zoom before center', function(t) {
            var map = new mbgl.Map(options);
            map.load({
                "version": 8,
                "sources": {
                    "geojson": {
                        "type": "geojson",
                        "data": {
                            "type": "Point",
                            "coordinates": [
                                18.05489,
                                59.32744
                            ]
                        }
                    }
                },
                "layers": [
                    {
                        "id": "circle",
                        "type": "circle",
                        "source": "geojson",
                        "paint": {
                            "circle-color": "red"
                        }
                    }
                ]
            });
            map.render({width: 400, height: 400, zoom: 5, center: [18.05489, 59.32744]}, function(err, actual) {
                t.error(err);

                var PNG = require('pngjs').PNG;
                var pixelmatch = require('pixelmatch');
                var expected = PNG.sync.read(
                    fs.readFileSync(path.join(__dirname, '../fixtures/zoom-center/expected.png'))).data;
                var numPixels = pixelmatch(actual, expected, undefined, 400, 400, { threshold: 0.13 });
                t.equal(numPixels, 0);
                t.end();
            });
        })
    });

    t.test('request callback', function (t) {
        t.test('returning an error', function(t) {
            var map = new mbgl.Map({
                request: function(req, callback) {
                    setImmediate(function () {
                        callback(new Error('request error'));
                    });
                },
            });
            map.load(style);
            map.render({ zoom: 1 }, function(err, data) {
                map.release();
                t.ok(err, 'returns error');
                t.equal(err.message, 'request error');
                t.end();
            });
        });

        t.test('returning no content for a tile', function(t) {
            var map = new mbgl.Map({
                request: function(req, callback) {
                    callback();
                },
            });
            map.load(style);
            map.render({ zoom: 1 }, function(err, data) {
                map.release();
                t.ok(data, 'no error');
                t.end();
            });
        });

        t.test('not holding references', function(t) {
            var options = {
                request: function() {},
                ratio: 1
            };

            // We explicitly don't call release. mbgl.Map should
            // not hold any reference to the node's main loop and
            // prevent the test from exit.
            var map1 = new mbgl.Map(options);
            var map2 = new mbgl.Map(options);
            var map3 = new mbgl.Map(options);

            t.end();
        });
    });
});