Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #80 from facebook/webworkers

Adding webworkers test.
NOTE: too much feature detection.
  • Loading branch information...
commit 34ba5d254d0903f33828b53b9c411d35ee15b6e8 2 parents d55c9d1 + 3dd5ef4
@darobin darobin authored
View
4 tests/webworkers/config.yml
@@ -0,0 +1,4 @@
+---
+ r: 1
+ spec: "http://www.w3.org/TR/workers/"
+ title: "Web Workers"
View
282 tests/webworkers/test.js
@@ -0,0 +1,282 @@
+test("Web Workers", function() {
+ var Worker = H.API( window, "Worker", true );
+
+ assert( !!Worker, "Worker supported" );
+});
+
+asyncTest("Web Worker navigator", function( async ) {
+ var Worker = H.API( window, "Worker", true ),
+ worker;
+
+
+ if ( !Worker ) {
+ assert( false, "Workers not supported, skipping tests" );
+ async.done();
+ } else {
+ worker = new Worker("/tests/webworkers/worker.js");
+
+ worker.postMessage({
+ test: "navigator"
+ });
+
+ worker.onmessage = function( event ) {
+ async.step(function() {
+ var data = event.data,
+ pass = false;
+
+ pass = Object.keys( data ).every(function( key ) {
+ assert( data[ key ] === navigator[ key ], data[ key ] + " === " + navigator[ key ] );
+ return data[ key ] === navigator[ key ];
+ });
+
+ assert( pass, "Worker correctly implements navigator" );
+
+ async.done();
+
+ worker.terminate();
+ worker = null;
+ });
+ };
+ }
+});
+
+asyncTest("Web Worker location", function( async ) {
+ var Worker = H.API( window, "Worker", true ),
+ worker;
+
+
+ if ( !Worker ) {
+ assert( false, "Workers not supported, skipping tests" );
+ async.done();
+ } else {
+ worker = new Worker("/tests/webworkers/worker.js");
+
+
+ worker.postMessage({
+ test: "location"
+ });
+
+ worker.onmessage = function( event ) {
+ async.step(function() {
+ var data = event.data,
+ pass = false;
+
+ pass = Object.keys( data ).every(function( key ) {
+
+ assert( key in location, key + " in location" );
+ return key in location;
+ });
+
+ assert( pass, "Worker implements location" );
+
+ async.done();
+
+ worker.terminate();
+ worker = null;
+ });
+ };
+ }
+});
+
+asyncTest("Web Worker global", function( async ) {
+ var Worker = H.API( window, "Worker", true ),
+ worker;
+
+
+ if ( !Worker ) {
+ assert( false, "Workers not supported, skipping tests" );
+ async.done();
+ } else {
+ worker = new Worker("/tests/webworkers/worker.js");
+
+ worker.postMessage({
+ test: "worker"
+ });
+
+ worker.onmessage = function( event ) {
+ async.step(function() {
+ var data = event.data,
+ pass = false;
+
+ pass = Object.keys( data ).every(function( key ) {
+
+ assert( key, "WorkerGlobalScope implements " + key );
+ return data[ key ];
+ });
+
+ assert( pass, "Worker implements global properties" );
+
+ async.done();
+
+ worker.terminate();
+ worker = null;
+ });
+ };
+ }
+});
+
+asyncTest("Web Worker data messaging", function( async ) {
+ var Worker = H.API( window, "Worker", true ),
+ prop, array, data, worker;
+
+
+ if ( !Worker ) {
+ assert( false, "Workers not supported, skipping tests" );
+ async.done();
+ } else {
+ worker = new Worker("/tests/webworkers/worker.js");
+ array = [];
+ data = {
+ objectA: {
+ a: 1,
+ b: null,
+ c: [{}],
+ d: {
+ a: 3.14159,
+ b: false,
+ c: {
+ d: 0,
+ f: [[[]]],
+ g: {
+ j: {
+ k: {
+ n: {
+ r: "r",
+ s: [1, 2, 3],
+ u: 0,
+ v: {
+ w: {
+ x: {
+ y: "Yahoo!",
+ z: null
+ }
+ }
+ }
+ },
+ o: 99,
+ q: []
+ },
+ m: null
+ }
+ },
+ h: "false",
+ i: true
+ }
+ },
+ e: String("constructed string"),
+ f: {},
+ g: "",
+ h: "h",
+ i: []
+ },
+ array: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, [
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, [
+ 1, 2, 3, 4, 5, [
+ [6, 7, 8, 9, [
+ [
+ 1, 2, 3, 4, [
+ 2, 3, 4, [
+ 1, 2, [
+ 1, 2, 3, 4, [
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, [ 0 ], 1, 2, 3, 4, 5, 6, 7, 8, 9
+ ], 5, 6, 7, 8, 9
+ ], 4, 5, 6, 7, 8, 9
+ ], 5, 6, 7, 8, 9
+ ], 5, 6, 7
+ ]
+ ]
+ ]
+ ]
+ ]
+ ]
+ ],
+ string: "this is a standard string",
+ bool: true,
+ loc: {},
+ ua: navigator.userAgent
+ };
+
+ for ( prop in data ) {
+ array.push( data[ prop ] );
+ }
+
+ worker.postMessage({
+ test: "messaging",
+ args: array
+ });
+
+ worker.onmessage = function( event ) {
+ async.step(function() {
+
+ assert( JSON.stringify(event.data) === JSON.stringify(data), "Worker supports complex data objects" );
+
+ async.done();
+
+ worker.terminate();
+ worker = null;
+ });
+ };
+ }
+});
+
+
+asyncTest("Web Worker Blob URL", function( async ) {
+ var Worker = H.API( window, "Worker", true ),
+ URL = H.API( window, "URL", true ),
+ BlobBuilder = H.API( window, "BlobBuilder", true ),
+ worker, builder;
+
+
+ if ( !Worker || !BlobBuilder ) {
+ assert( false, "Workers or BlobBuilder not supported, skipping tests" );
+ async.done();
+ } else {
+ worker = new Worker("/tests/webworkers/worker.js");
+ builder = new BlobBuilder();
+
+
+ builder.append(
+ "onmessage = function( event ) { postMessage( event.data ) };"
+ );
+
+ worker = new Worker(
+ URL.createObjectURL( builder.getBlob() )
+ );
+
+ worker.postMessage("The Blob!");
+
+ worker.onmessage = function( event ) {
+ async.step(function() {
+ assert( event.data === "The Blob!", "Blob Web Worker" );
+
+ async.done();
+
+ worker.terminate();
+ worker = null;
+ });
+ };
+ }
+});
+
+
+// asyncTest("Web Worker Data URL", function( async ) {
+//
+// var source = [
+// "data:text/javascript;base64,",
+// window.btoa("onmessage = function( event ) { postMessage( event.data ) };")
+// ].join(""),
+// worker = new Worker(source);
+//
+// worker.postMessage("The Data!");
+//
+// worker.onmessage = function( event ) {
+// async.step(function() {
+// assert( event.data === "The Data!", "Data Web Worker" );
+//
+// async.done();
+//
+// worker.terminate();
+// worker = null;
+// });
+// };
+// });
View
76 tests/webworkers/worker-setup.js
@@ -0,0 +1,76 @@
+var prop,
+ array = [],
+ data = {
+ objectA: {
+ a: 1,
+ b: null,
+ c: [{}],
+ d: {
+ a: 3.14159,
+ b: false,
+ c: {
+ d: 0,
+ f: [[[]]],
+ g: {
+ j: {
+ k: {
+ n: {
+ r: "r",
+ s: [1, 2, 3],
+ u: 0,
+ v: {
+ w: {
+ x: {
+ y: "Yahoo!",
+ z: null
+ }
+ }
+ }
+ },
+ o: 99,
+ q: []
+ },
+ m: null
+ }
+ },
+ h: "false",
+ i: true
+ }
+ },
+ e: String("constructed string"),
+ f: {},
+ g: "",
+ h: "h",
+ i: []
+ },
+ array: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, [
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, [
+ 1, 2, 3, 4, 5, [
+ [6, 7, 8, 9, [
+ [
+ 1, 2, 3, 4, [
+ 2, 3, 4, [
+ 1, 2, [
+ 1, 2, 3, 4, [
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, [ 0 ], 1, 2, 3, 4, 5, 6, 7, 8, 9
+ ], 5, 6, 7, 8, 9
+ ], 4, 5, 6, 7, 8, 9
+ ], 5, 6, 7, 8, 9
+ ], 5, 6, 7
+ ]
+ ]
+ ]
+ ]
+ ]
+ ]
+ ],
+ string: "this is a standard string",
+ bool: true,
+ loc: {},
+ ua: navigator.userAgent
+ };
+
+
+for ( prop in data ) {
+ array.push( data[ prop ] );
+}
View
56 tests/webworkers/worker.js
@@ -0,0 +1,56 @@
+importScripts( "worker-setup.js" );
+
+var replies = {
+ navigator: function() {
+ var obj = {};
+
+ [ "appName", "appVersion", "onLine", "platform", "userAgent" ].forEach(function( prop ) {
+ obj[ prop ] = navigator[ prop ];
+ });
+ return obj;
+ },
+ location: function() {
+ var obj = {};
+
+ [ "hash", "href", "host", "hostname", "pathname", "port", "protocol", "search" ].forEach(function( prop ) {
+ obj[ prop ] = location[ prop ];
+ });
+ return obj;
+ },
+ worker: function() {
+ var obj = {};
+
+ [
+ "addEventListener", "removeEventListener", "dispatchEvent",
+ "importScripts", "close",
+ "location", "navigator", "postMessage",
+ "self"
+ ].forEach(function( prop ) {
+ obj[ prop ] = (prop in self);
+ });
+ return obj;
+ },
+ nesting: function() {
+ return self.Worker;
+ },
+ messaging: function( args ) {
+ var correct = true;
+
+ args.forEach(function( arg, index ) {
+ correct = JSON.stringify( arg ) === JSON.stringify( self.array[ index ] );
+ });
+ return correct ? self.data : "";
+ }
+};
+
+
+self.addEventListener("message", function( event ) {
+ var test = event.data.test,
+ args = event.data.args;
+
+ self.postMessage(
+ test ?
+ replies[ test ]( args ) :
+ "pass"
+ );
+}, false);
Please sign in to comment.
Something went wrong with that request. Please try again.