Skip to content

Commit

Permalink
initial work on browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jun 22, 2015
1 parent bc26469 commit 6a88256
Show file tree
Hide file tree
Showing 5 changed files with 813 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ matter-doc-theme
build/matter-dev.js
build/matter-dev.min.js
demo/js/lib/matter-dev.js
tests/browser/diffs
tests/browser/worlds
3 changes: 3 additions & 0 deletions demo/js/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
_sceneEvents = [],
_useInspector = window.location.hash.indexOf('-inspect') !== -1,
_isMobile = /(ipad|iphone|ipod|android)/gi.test(navigator.userAgent);

window.Matter.Demo = Demo;

// initialise the demo

Expand All @@ -51,6 +53,7 @@
// create a Matter engine
// NOTE: this is actually Matter.Engine.create(), see the aliases at top of this file
_engine = Engine.create(container, options);
window.Matter.Demo._engine = _engine;

// add a mouse controlled constraint
_mouseConstraint = MouseConstraint.create(_engine);
Expand Down
168 changes: 168 additions & 0 deletions tests/browser/TestDemo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
var page = require('webpage').create();
var fs = require('fs');
var Resurrect = require('./lib/resurrect');
var _ = require('./lib/lodash');

page.onConsoleMessage = function(msg) {
console.log(msg);
};

page.onError = function(msg) {
console.log(msg);
};

phantom.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
console.error(msgStack.join('\n'));
phantom.exit(1);
};

var log = function(msg) {
console.log(JSON.stringify(msg));
}

var type = function(obj) {
// https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
if (obj === global) {
return "global";
}
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
};

var compare = function(objectA, objectB) {
if (objectA === objectB) {
return { equal: true };
}

if ((type(objectA) === 'undefined' && type(objectB) === 'null')
|| (type(objectA) === 'null' && type(objectB) === 'undefined')) {
return { equal: true };
}

if (type(objectA) !== type(objectB)) {
return { equal: false, expected: type(objectA), actual: type(objectB) };
}

if (_.isNumber(objectA)) {
if (objectA.toFixed(5) === objectB.toFixed(5)) {
return { equal: true };
} else {
return { equal: false, expected: objectA, actual: objectB };
}
}

if (_.isArray(objectA)) {
var arrayDelta = [],
isEqual = true;

for (var i = 0; i < Math.max(objectA.length, objectB.length); i++) {
var diff = compare(objectA[i], objectB[i]);
arrayDelta[i] = diff;

if (diff.equal !== true) {
isEqual = false;
}
}

return isEqual ? { equal: true } : arrayDelta;
}

if (_.isObject(objectA)) {
var keys = _.union(_.keys(objectA), _.keys(objectB)),
objectDelta = { equal: true };

for (var i = 0; i < keys.length; i++) {
var key = keys[i],
diff = compare(objectA[key], objectB[key]);

if (diff.equal !== true) {
objectDelta[key] = diff;
objectDelta.equal = false;
}
}

return objectDelta.equal ? { equal: true } : objectDelta;
}

return { equal: false, expected: objectA, actual: objectB };
};

page.open('http://localhost:9000/demo/dev.html', function(status) {
var demos = page.evaluate(function() {
var options = Array.prototype.slice.call(document.getElementById('demo-select').options);
return options.map(function(o) { return o.value });
});

var worldsPath = 'tests/browser/worlds',
diffsPath = 'tests/browser/diffs'
resurrect = new Resurrect({ cleanup: true }),
frames = 10;

fs.removeTree(diffsPath);
fs.makeDirectory(diffsPath);

console.log(demos);

for (var i = 0; i < demos.length; i += 1) {
var demo = demos[i],
worldStartPath = worldsPath + '/' + demo + '/' + demo + '-0.json',
worldEndPath = worldsPath + '/' + demo + '/' + demo + '-' + frames + '.json',
worldStartDiffPath = diffsPath + '/' + demo + '/' + demo + '-0.json',
worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json';

var worldStart = page.evaluate(function(demo) {
var engine = Matter.Demo._engine;
Matter.Runner.stop(engine);
Matter.Demo[demo]();
return engine.world;
}, demo);

var worldEnd = page.evaluate(function(demo, frames) {
var engine = Matter.Demo._engine;
for (var j = 0; j <= frames; j += 1) {
Matter.Events.trigger(engine, 'tick', { timestamp: engine.timing.timestamp });
Matter.Engine.update(engine, engine.timing.delta);
Matter.Events.trigger(engine, 'afterTick', { timestamp: engine.timing.timestamp });
}
return engine.world;
}, demo, frames);

if (fs.exists(worldStartPath)) {
var worldStartRef = resurrect.resurrect(fs.read(worldStartPath));
var worldStartDiff = compare(worldStart, worldStartRef);

if (!worldStartDiff.equal) {
fs.write(worldStartDiffPath, JSON.stringify(worldStartDiff, null, 2), 'w');
console.log(demo, 'start equal:', worldStartDiff.equal);
}
} else {
console.warn('no existing start reference world for', demo);
fs.write(worldStartPath, resurrect.stringify(worldStart), 'w');
console.log('wrote', worldEndPath);
}

if (fs.exists(worldEndPath)) {
var worldEndRef = resurrect.resurrect(fs.read(worldEndPath));
var worldEndDiff = compare(worldEnd, worldEndRef);

if (!worldEndDiff.equal) {
fs.write(worldEndDiffPath, JSON.stringify(worldEndDiff, null, 2), 'w');
console.log(demo, 'end equal:', worldEndDiff.equal);
}
} else {
console.warn('no existing end reference world for', demo);
fs.write(worldEndPath, resurrect.stringify(worldEnd), 'w');
console.log('wrote', worldEndPath);
}
}

console.log('done');

phantom.exit();
});
Loading

0 comments on commit 6a88256

Please sign in to comment.