3 <script src='test.js'
></script>
4 <script src='execute_async_script.js'
></script>
7 function resetAsyncScriptInfo() {
8 delete document
[ASYNC_INFO_KEY
];
11 function testScriptThrows() {
12 resetAsyncScriptInfo();
13 var info
= getAsyncScriptInfo();
15 executeAsyncScript('f(123);', [], true);
16 assertEquals(StatusCode
.JAVASCRIPT_ERROR
, info
.result
.status
);
17 executeAsyncScript('f(123);', [], false);
18 assertEquals(StatusCode
.UNKNOWN_ERROR
, info
.result
.status
);
21 function testUserScriptWithArgs() {
22 resetAsyncScriptInfo();
24 var injectedArgs
= null;
25 function captureArguments(args
) {
28 // Pass function captureArguments as the first argument. It is used to capture
29 // the injected arguments to the following script.
31 'var args = arguments; args[0](args); args[args.length - 1](args[1]);';
32 var script_args
= [captureArguments
, 1];
33 executeAsyncScript(script
, script_args
, true);
35 assertEquals(3, injectedArgs
.length
);
36 assertEquals(captureArguments
, injectedArgs
[0]);
37 assertEquals(1, injectedArgs
[1]);
39 var info
= getAsyncScriptInfo();
40 assertEquals(0, info
.result
.status
);
41 assertEquals(1, info
.result
.value
);
42 assertEquals(2, info
.id
);
45 function testNonUserScript() {
46 resetAsyncScriptInfo();
47 var info
= getAsyncScriptInfo();
49 executeAsyncScript('arguments[1](arguments[0])', [33], false);
50 assertEquals(0, info
.result
.status
);
51 assertEquals(33, info
.result
.value
);
53 executeAsyncScript('arguments[2](new Error("ERR"))', [33], false);
54 assertEquals(StatusCode
.UNKNOWN_ERROR
, info
.result
.status
);
55 assertEquals(0, info
.result
.value
.indexOf('ERR'));
57 executeAsyncScript('var e = new Error("ERR"); e.code = 111; arguments[1](e)',
59 assertEquals(111, info
.result
.status
);
60 assertEquals(0, info
.result
.value
.indexOf('ERR'));
63 function testNoResultBeforeTimeout() {
64 resetAsyncScriptInfo();
65 var info
= getAsyncScriptInfo();
68 'var a = arguments; window.setTimeout(function() {a[0](33)}, 0);',
74 function testZeroTimeout(runner
) {
75 resetAsyncScriptInfo();
76 var info
= getAsyncScriptInfo();
79 'var a = arguments; window.setTimeout(function() {a[0](33)}, 0);',
82 window
.setTimeout(function() {
83 assertEquals(0, info
.result
.status
);
84 assertEquals(33, info
.result
.value
);
85 runner
.continueTesting();
87 runner
.waitForAsync();
90 function testUserScriptTimesOut(runner
) {
91 resetAsyncScriptInfo();
92 var info
= getAsyncScriptInfo();
94 executeAsyncScript('', [], true, 500);
96 window
.setTimeout(function() {
97 assertEquals(StatusCode
.SCRIPT_TIMEOUT
, info
.result
.status
);
98 assert(info
.result
.value
.indexOf('0.5') != -1);
99 runner
.continueTesting();
102 runner
.waitForAsync();
105 function testNonUserScriptTimesOut(runner
) {
106 resetAsyncScriptInfo();
107 var info
= getAsyncScriptInfo();
109 executeAsyncScript('', [], false, 500);
111 window
.setTimeout(function() {
112 assertEquals(StatusCode
.UNKNOWN_ERROR
, info
.result
.status
);
113 assert(info
.result
.value
.indexOf('0.5') != -1);
114 runner
.continueTesting();
117 runner
.waitForAsync();
120 function testFirstScriptFinishAfterSecondScriptExecute() {
121 resetAsyncScriptInfo();
124 'var f = arguments[0]; setTimeout(function(){ f(1); }, 100000);', []);
125 var info
= getAsyncScriptInfo();
126 assert(!info
.hasOwnProperty('result'));
127 assertEquals(1, info
.id
);
129 executeAsyncScript('var fn = arguments[0]; fn(2);', []);
130 assertEquals(0, info
.result
.status
);
131 assertEquals(2, info
.result
.value
);
132 assertEquals(3, info
.id
);