summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resources/examples/apisample11.html
blob: 32e8984c3f16407929d037928291e9a8722f91ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE HTML>
<html>
<head>
<title>Example with iframe that notifies containing document via callbacks</title>
<script src="../testharness.js"></script>
<script src="../testharnessreport.js"></script>
</head>
<body onload="start_test_in_iframe()">
<h1>Callbacks From Tests Running In An IFRAME</h1>
<p>A test is run inside an <tt>iframe</tt> with a same origin document. The
containing document should receive callbacks as the tests progress inside the
<tt>iframe</tt>. A single passing test is expected in the summary below.
<div id="log"></div>

<script>
var callbacks = [];
var START = 1
var TEST_STATE = 2
var RESULT = 3
var COMPLETION = 4
var test_complete = false;

setup({explicit_done: true});

// The following callbacks are called for tests in this document as well as the
// tests in the IFRAME. Currently, callbacks invoked from this document and any
// child document are indistinguishable from each other.

function start_callback(properties) {
    callbacks.push(START);
}

function test_state_callback(test) {
    callbacks.push(TEST_STATE);
}

function result_callback(test) {
    callbacks.push(RESULT);
}

function completion_callback(tests, status) {
    if (test_complete) {
        return;
    }
    test_complete = true;
    callbacks.push(COMPLETION);
    verify_received_callbacks();
    done();
}

function verify_received_callbacks() {
    var copy_of_callbacks = callbacks.slice(0);

    // Note that you can't run test assertions directly in a callback even if
    // this is a file test. When the callback is invoked from a same-origin child
    // page, the callstack reaches into the calling child document. Any
    // exception thrown in a callback will be handled by the child rather than
    // this document.
    test(
        function() {
            // callbacks list should look like:
            //     START 1*(TEST_STATE) RESULT COMPLETION
            assert_equals(copy_of_callbacks.shift(), START,
                          "The first received callback should be 'start_callback'.");
            assert_equals(copy_of_callbacks.shift(), TEST_STATE,
                          "'test_state_callback' should be received before any " +
                          "result or completion callbacks.");
            while(copy_of_callbacks.length > 0) {
                var callback = copy_of_callbacks.shift();
                if (callback != TEST_STATE) {
                    copy_of_callbacks.unshift(callback);
                    break;
                }
            }
            assert_equals(copy_of_callbacks.shift(), RESULT,
                          "'test_state_callback' should be followed by 'result_callback'.");
            assert_equals(copy_of_callbacks.shift(), COMPLETION,
                          "Final 'result_callback' should be followed by 'completion_callback'.");
            assert_equals(copy_of_callbacks.length, 0,
                          "'completion_callback' should be the last callback.");
        });
}

function start_test_in_iframe() {
    // This document is going to clear any received callbacks and maintain
    // radio silence until the test in the iframe runs to completion. The
    // completion_callback() will then complete the testing on this document.
    callbacks.length = 0;
    var iframe = document.createElement("iframe");
    // apisample6.html has a single test.
    iframe.src = "apisample6.html";
    iframe.style.setProperty("display", "none");
    document.getElementById("target").appendChild(iframe);
}
</script>

<div id="target">
</div>
</body>