- add sources.
[platform/framework/web/crosswalk.git] / src / ppapi / native_client / tests / nacl_browser / inbrowser_test_runner / test_runner.html
1 <!DOCTYPE html>
2 <!--
3   Copyright (c) 2012 The Chromium Authors. All rights reserved.
4   Use of this source code is governed by a BSD-style license that can be
5   found in the LICENSE file.
6 -->
7 <html>
8 <head>
9 <title>
10   NativeClient browser test runner
11 </title>
12 <script type="text/javascript" src="nacltest.js"></script>
13 <script type="text/javascript" src="nmf_test_list.js"></script>
14 </head>
15 <body>
16
17 <div id="scratch_space"></div>
18
19 <div id="load_warning">
20 Javascript has failed to load.
21 </div>
22
23 <script type="text/javascript">
24
25 function addTest(tester, url) {
26   tester.addAsyncTest(url, function(status) {
27     var embed = document.createElement('embed');
28     embed.width = 0;
29     embed.height = 0;
30     embed.src = url;
31     embed.type = 'application/x-nacl';
32     embed.name = 'foo';
33
34     // Webkit Bug Workaround
35     // THIS SHOULD BE REMOVED WHEN Webkit IS FIXED
36     // http://code.google.com/p/nativeclient/issues/detail?id=2428
37     // http://code.google.com/p/chromium/issues/detail?id=103588
38     ForcePluginLoadOnTimeout(embed, tester, 15000);
39
40     var div = document.createElement('div');
41     div.appendChild(embed);
42
43     var cleanup = function() {
44       document.getElementById('scratch_space').removeChild(div);
45     };
46
47     // This is the prefix prepended by NaCl's unofficial
48     // "dev://postmessage" feature.
49     var stdout_prefix = 'DEBUG_POSTMESSAGE:';
50
51     // NaCl's "dev://postmessage" feature is unsynchronized, in the
52     // sense that the DEBUG_POSTMESSAGE messages can arrive after the
53     // test result event.  As a workaround, we look for an
54     // "END_OF_LOG" string that the nexe prints.
55     var saw_end_of_log = false;
56     var end_of_log_callbacks = [];
57
58     var runEndOfLogCallbacks = function() {
59       if (!saw_end_of_log) {
60         saw_end_of_log = true;
61         for (var i = 0; i < end_of_log_callbacks.length; i++) {
62           end_of_log_callbacks[i]();
63         }
64         end_of_log_callbacks = [];
65       }
66     };
67
68     var callAtEndOfLog = function(func) {
69       if (saw_end_of_log) {
70         func();
71       } else {
72         end_of_log_callbacks.push(func);
73         // If we do not see the end of the log soon, end the test
74         // anyway.  This will happen if the nexe crashes or exits.
75         window.setTimeout(status.wrap(function() {
76           status.log('Did not see the END_OF_LOG message after timeout; ' +
77                      'continuing anyway');
78           runEndOfLogCallbacks();
79         }), 500);
80       }
81     };
82
83     // Set up an event listener for success messages.
84     div.addEventListener('message', status.wrap(function(message_event) {
85       if (message_event.data.substr(0, stdout_prefix.length) == stdout_prefix) {
86         var msg = message_event.data.substr(stdout_prefix.length);
87         if (msg == '\nEND_OF_LOG\n') {
88           runEndOfLogCallbacks();
89         } else {
90           status.log(msg.replace(/\n/g, '\\n'));
91         }
92       } else {
93         callAtEndOfLog(function() {
94           status.assertEqual(message_event.data, 'passed');
95           cleanup();
96           status.pass();
97         });
98       }
99     }), true);
100
101     // Wait for the load event, which indicates successful loading.
102     div.addEventListener('load', status.wrap(function(e) {
103       status.log('Loaded ' + embed.src);
104       // Start tests in the module.
105       embed.postMessage('run_tests');
106     }), true);
107
108     var onError = status.wrap(function(e) {
109       callAtEndOfLog(function() {
110         cleanup();
111         status.fail(embed.lastError);
112       });
113     });
114
115     div.addEventListener('error', onError, true);
116     div.addEventListener('crash', onError, true);
117
118     // Insert div into the DOM.  This starts the load of the nacl plugin, etc.
119     document.getElementById('scratch_space').appendChild(div);
120   });
121 }
122
123 // Remove the "failed to load" message.
124 document.getElementById('load_warning').innerHTML = '';
125
126 var tester = new Tester();
127 for (var i = 0; i < G_NMF_TEST_LIST.length; i++) {
128   addTest(tester, G_NMF_TEST_LIST[i]);
129 }
130
131 var args = getTestArguments({'parallel': '0'});
132
133 if (parseInt(args['parallel'])) {
134   tester.runParallel();
135 } else {
136   tester.run();
137 }
138
139 </script>
140 </body>
141 </html>