- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / nacl / load_util.js
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var load_util = {
6   report: function(msg) {
7     domAutomationController.setAutomationId(0);
8     // The automation controller seems to choke on Objects, so turn them into
9     // strings.
10     domAutomationController.send(JSON.stringify(msg));
11   },
12
13   log: function(message) {
14     load_util.report({type: "Log", message: message});
15   },
16
17   shutdown: function(message, passed) {
18     load_util.report({type: "Shutdown", message: message, passed: passed});
19   },
20
21   embed: function(manifest_url) {
22     var embed = document.createElement("embed");
23     embed.src = manifest_url;
24     embed.type = "application/x-nacl";
25     return embed;
26   },
27
28   // Use the DOM to determine the absolute URL.
29   absoluteURL: function(url) {
30     var a = document.createElement("a");
31     a.href = url;
32     return a.href;
33   },
34
35   crossOriginURL: function(manifest_url) {
36     manifest_url = load_util.absoluteURL(manifest_url);
37     // The test server is only listening on a specific random port
38     // at 127.0.0.1. So, to inspect a cross-origin request from within
39     // the server code, we load from "localhost" which is a different origin,
40     // yet still served by the server. Otherwise, if we choose a host
41     // other than localhost we would need to modify the DNS/host resolver
42     // to point that host at 127.0.0.1.
43     var cross_url = manifest_url.replace("127.0.0.1", "localhost");
44     if (cross_url == manifest_url) {
45       load_util.shutdown("Could not create a cross-origin URL.", false);
46       throw "abort";
47     }
48     return cross_url;
49   },
50
51   crossOriginEmbed: function(manifest_url) {
52     return load_util.embed(load_util.crossOriginURL(manifest_url));
53   },
54
55   expectLoadFailure: function(embed, message) {
56     embed.addEventListener("load", function(event) {
57       load_util.log("Load succeeded when it should have failed.");
58       load_util.shutdown("1 test failed.", false);
59     }, true);
60
61     embed.addEventListener("error", function(event) {
62       if (embed.lastError != "NaCl module load failed: " + message) {
63         load_util.log("Unexpected load error: " + embed.lastError);
64         load_util.shutdown("1 test failed.", false);
65       } else {
66         load_util.shutdown("1 test passed.", true);
67       }
68     }, true);
69   }
70 };