Skip to content

Commit

Permalink
Rebase + add some basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Budorick committed Dec 22, 2016
1 parent fb090a2 commit e389c74
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 8 deletions.
10 changes: 5 additions & 5 deletions js/source/canvas_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class CanvasSource extends ImageSource {
constructor(id, options, dispatcher, eventedParent) {
super(id, options, dispatcher, eventedParent);
this.options = options;
this.animate = options.hasOwnProperty('animate') ? options.animate : true;
this.dimensions = options.dimensions;
this.resize = false;
}

load() {
const options = this.options;
this._canvas = window.document.getElementById(options.canvas);
this.animate = options.hasOwnProperty('animate') ? options.animate : true;
this.dimensions = options.dimensions;
this._canvas = this._canvas || window.document.getElementById(this.options.canvas);

// detect context type
if (this._canvas.getContext('2d')) {
Expand Down Expand Up @@ -104,8 +104,8 @@ class CanvasSource extends ImageSource {

onAdd(map) {
if (this.map) return;
this.load();
this.map = map;
this.load();
if (this.canvas) {
if (this.animate) this.play();
this.setCoordinates(this.coordinates);
Expand Down
3 changes: 0 additions & 3 deletions js/source/vector_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ class VectorTileSource extends Evented {
this.setEventedParent(eventedParent);
}

this.setEventedParent(eventedParent);
}

load() {
this.fire('dataloading', {dataType: 'source'});

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"babel-eslint": "^7.0.0",
"benchmark": "~2.1.0",
"browserify": "^13.0.0",
"canvas": "^1.6.2",
"clipboard": "^1.5.12",
"concat-stream": "1.5.2",
"coveralls": "^2.11.8",
Expand Down
112 changes: 112 additions & 0 deletions test/js/source/canvas_source.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
'use strict';

const test = require('mapbox-gl-js-test').test;
const CanvasSource = require('../../../js/source/canvas_source');
const Transform = require('../../../js/geo/transform');
const Evented = require('../../../js/util/evented');
const util = require('../../../js/util/util');
const Canvas = require('canvas');

function createSource(options) {
const c = new Canvas(20, 20);

options = util.extend({
canvas: 'id',
coordinates: [[0, 0], [1, 0], [1, 1], [0, 1]],
dimensions: [0, 0, 20, 20]
}, options);

const source = new CanvasSource('id', options, { send: function() {} }, options.eventedParent);

source._canvas = c;

return source;
}

class StubMap extends Evented {
constructor() {
super();
this.transform = new Transform();
this.style = { animationLoop: { set: function() {} } };
}

_rerender() {
this.fire('rerender');
}
}

test('CanvasSource', (t) => {
t.test('constructor', (t) => {
const source = createSource();

source.on('source.load', () => {
t.equal(source.minzoom, 0);
t.equal(source.maxzoom, 22);
t.equal(source.tileSize, 512);
t.equal(source.contextType, '2d');
t.equal(source.animate, true);
t.equal(typeof source.play, 'function');
t.end();
});

source.onAdd(new StubMap());
});

t.test('rerenders if animated', (t) => {
const source = createSource();
const map = new StubMap();

map.on('rerender', () => {
t.ok(true, 'fires rerender event');
t.end();
});

source.onAdd(map);
});

t.test('can be static', (t) => {
const source = createSource({
animate: false
});
const map = new StubMap();

map.on('rerender', () => {
t.notOk(true, 'shouldn\'t rerender here');
t.end();
});

source.on('source.load', () => {
t.ok(true, 'fires load event without rerendering');
t.end();
});

source.onAdd(map);
});

t.end();
});

test('CanvasSource#serialize', (t) => {
const source = createSource();

const serialized = source.serialize();
t.equal(serialized.type, 'canvas');
t.ok(serialized.canvas);
t.deepEqual(serialized.coordinates, [[0, 0], [1, 0], [1, 1], [0, 1]]);

t.end();
});

test('CanvasSource#setDimensions', (t) => {
const source = createSource();

t.equal(source.resize, false);
t.deepEqual(source.dimensions, [0, 0, 20, 20]);

source.setDimensions([0, 0, 30, 30]);

t.equal(source.resize, true);
t.deepEqual(source.dimensions, [0, 0, 30, 30]);

t.end();
});

0 comments on commit e389c74

Please sign in to comment.