dom/workers/test/importScripts_worker.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/workers/test/importScripts_worker.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,64 @@
     1.4 +/**
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +// Try no args. This shouldn't do anything.
     1.9 +importScripts();
    1.10 +
    1.11 +// This caused security exceptions in the past, make sure it doesn't!
    1.12 +var constructor = {}.constructor;
    1.13 +
    1.14 +importScripts("importScripts_worker_imported1.js");
    1.15 +
    1.16 +// Try to call a function defined in the imported script.
    1.17 +importedScriptFunction();
    1.18 +
    1.19 +function tryBadScripts() {
    1.20 +  var badScripts = [
    1.21 +    // Has a syntax error
    1.22 +    "importScripts_worker_imported3.js",
    1.23 +    // Throws an exception
    1.24 +    "importScripts_worker_imported4.js",
    1.25 +    // Shouldn't exist!
    1.26 +    "http://example.com/non-existing/importScripts_worker_foo.js",
    1.27 +    // Not a valid url
    1.28 +    "http://notadomain::notafile aword"
    1.29 +  ];
    1.30 +
    1.31 +  for (var i = 0; i < badScripts.length; i++) {
    1.32 +    var caughtException = false;
    1.33 +    var url = badScripts[i];
    1.34 +    try {
    1.35 +      importScripts(url);
    1.36 +    }
    1.37 +    catch (e) {
    1.38 +      caughtException = true;
    1.39 +    }
    1.40 +    if (!caughtException) {
    1.41 +      throw "Bad script didn't throw exception: " + url;
    1.42 +    }
    1.43 +  }
    1.44 +}
    1.45 +
    1.46 +const url = "data:text/plain,const startResponse = 'started';";
    1.47 +importScripts(url);
    1.48 +
    1.49 +onmessage = function(event) {
    1.50 +  switch (event.data) {
    1.51 +    case 'start':
    1.52 +      importScripts("importScripts_worker_imported2.js");
    1.53 +      importedScriptFunction2();
    1.54 +      tryBadScripts();
    1.55 +      postMessage(startResponse);
    1.56 +      break;
    1.57 +    case 'stop':
    1.58 +      tryBadScripts();
    1.59 +      postMessage('stopped');
    1.60 +      break;
    1.61 +    default:
    1.62 +      throw new Error("Bad message: " + event.data);
    1.63 +      break;
    1.64 +  }
    1.65 +}
    1.66 +
    1.67 +tryBadScripts();

mercurial