Skip to content

Commit

Permalink
Remove fake "browser" and "dom" modules (#3106)
Browse files Browse the repository at this point in the history
* Eliminate browser DOM module

* Eliminate browser browser module

* Remove outdated comment

* Fix test-suite

* Ensure CI fails if test-suite fails

* Fix failing test-suite tests
  • Loading branch information
Lucas Wojciechowski committed Sep 1, 2016
1 parent 1c82b0b commit c24473f
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 316 deletions.
4 changes: 1 addition & 3 deletions ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ npm run build-dev
tap --reporter dot --coverage --no-coverage-report test/js test/build/webpack.test.js

# run render tests
istanbul cover --dir .nyc_output --include-pid --report none --print none test/render.test.js &&
istanbul cover --dir .nyc_output --include-pid --report none --print none test/render.test.js
istanbul cover --dir .nyc_output --include-pid --report none --print none test/query.test.js

# send coverage report to coveralls
nyc report --reporter=lcov
(node ./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info) || true

exit $EXIT_CODE
2 changes: 1 addition & 1 deletion js/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var Canvas = require('../util/canvas');
var util = require('../util/util');
var browser = require('../util/browser');
var window = require('../util/browser').window;
var window = require('../util/window');
var Evented = require('../util/evented');
var DOM = require('../util/dom');

Expand Down
72 changes: 52 additions & 20 deletions js/util/browser.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
'use strict';

/**
* @module browser
* @private
*/

var window = require('./window');

exports.window = window;
exports.document = window.document;
/*
* When browserify builds Mapbox GL JS, it redirects all require() statements
* from this file, js/util/browser.js, to js/util/browser/browser.js.
* The latter relies on running in a real browser: 'window' must be defined,
* as well as other browser-specific globals. This file, on the other hand,
* is comfortable running under node.js, which is why it's the default require:
* it's used for tests.
/**
* Provides a function that outputs milliseconds: either performance.now()
* or a fallback to Date.now()
*/
module.exports.now = (function() {
if (window.performance &&
window.performance.now) {
return window.performance.now.bind(window.performance);
} else {
return Date.now.bind(Date);
}
}());

var frame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;

exports.frame = function(fn) {
return setImmediate(fn);
return frame(fn);
};

var cancel = window.cancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.msCancelAnimationFrame;

exports.cancelFrame = function(id) {
return clearImmediate(id);
cancel(id);
};

module.exports.now = Date.now.bind(Date);

exports.timed = function(fn, dur, ctx) {
exports.timed = function (fn, dur, ctx) {
if (!dur) {
fn.call(ctx, 1);
return null;
Expand All @@ -49,11 +64,28 @@ exports.timed = function(fn, dur, ctx) {
return function() { abort = true; };
};

exports.supported = function () {
return true;
};
/**
* Test if the current browser supports Mapbox GL JS
* @param {Object} options
* @param {boolean} [options.failIfMajorPerformanceCaveat=false] Return `false`
* if the performance of Mapbox GL JS would be dramatically worse than
* expected (i.e. a software renderer would be used)
* @return {boolean}
*/
exports.supported = require('mapbox-gl-supported');

exports.hardwareConcurrency = window.navigator.hardwareConcurrency || 4;

Object.defineProperty(exports, 'devicePixelRatio', {
get: function() { return window.devicePixelRatio; }
});

exports.devicePixelRatio = 1;
exports.hardwareConcurrency = 8;
exports.supportsWebp = false;
exports.supportsGeolocation = false;

var webpImgTest = window.document.createElement('img');
webpImgTest.onload = function() {
exports.supportsWebp = true;
};
webpImgTest.src = 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAQAAAAfQ//73v/+BiOh/AAA=';

exports.supportsGeolocation = !!window.navigator.geolocation;
96 changes: 0 additions & 96 deletions js/util/browser/browser.js

This file was deleted.

8 changes: 4 additions & 4 deletions js/util/browser/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ var window = require('./window');

module.exports = Canvas;

function Canvas(parent, container) {
function Canvas(map, container) {
this.canvas = window.document.createElement('canvas');

if (parent && container) {
if (map && container) {
this.canvas.style.position = 'absolute';
this.canvas.classList.add('mapboxgl-canvas');
this.canvas.addEventListener('webglcontextlost', parent._contextLost.bind(parent), false);
this.canvas.addEventListener('webglcontextrestored', parent._contextRestored.bind(parent), false);
this.canvas.addEventListener('webglcontextlost', map._contextLost.bind(map), false);
this.canvas.addEventListener('webglcontextrestored', map._contextRestored.bind(map), false);
this.canvas.setAttribute('tabindex', 0);
container.appendChild(this.canvas);
}
Expand Down
80 changes: 0 additions & 80 deletions js/util/browser/dom.js

This file was deleted.

20 changes: 8 additions & 12 deletions js/util/canvas.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
'use strict';

// Stub implementation for headless rendering with node. The browser implementation
// is in js/browser/ui/canvas.js.

var gl = require('gl');
var browser = require('./browser');
var window = require('./window');

module.exports = Canvas;

function Canvas(parent, container) {
function Canvas(map) {
var requiredContextAttributes = {
antialias: false,
alpha: true,
Expand All @@ -18,17 +15,16 @@ function Canvas(parent, container) {
};

this.context = gl(
((container && container.offsetWidth) || 512) * browser.devicePixelRatio,
((container && container.offsetHeight) || 512) * browser.devicePixelRatio,
requiredContextAttributes);
map.getContainer().offsetWidth * window.devicePixelRatio,
map.getContainer().offsetHeight * window.devicePixelRatio,
requiredContextAttributes
);
}

Canvas.prototype.resize = function() {
};
Canvas.prototype.resize = function() {};

Canvas.prototype.getWebGLContext = function() {
return this.context;
};

Canvas.prototype.getElement = function() {
};
Canvas.prototype.getElement = function() {};

0 comments on commit c24473f

Please sign in to comment.