summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/custom-elements/v0/custom-element-lifecycle/types-of-callbacks/detached-callback-no-browsing-context-test.html
blob: 2b420d11c2cb59ae1adf16de821d5700d80dfd94 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
<title>Detached callback of a custom element should not be called if document has no browsing context</title>
<meta name="author" title="Sergey G. Grekhov" href="mailto:sgrekhov@unipro.ru">
<meta name="author" title="Aleksei Yu. Semenov" href="mailto:a.semenov@unipro.ru">
<meta name="assert" content="detached callback ... must be enqueued whenever custom element is removed from the document and this document has a browsing context.">
<link rel="help" href="http://www.w3.org/TR/custom-elements/#types-of-callbacks">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../testcommon.js"></script>
</head>
<body>
<div id="log"></div>
<script>
test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-a', {prototype: proto});
    doc.body.innerHTML = '<x-a id="x-a"></x-a>';
    var customElement = doc.querySelector('#x-a');
    doc.body.removeChild(customElement);

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if custom element is created via innerHTML property. ' +
    'Document has no browsing context');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.body.innerHTML = '<x-b id="x-b"></x-b>';
    doc.registerElement('x-b', {prototype: proto});
    var customElement = doc.querySelector('#x-b');
    doc.body.removeChild(customElement);

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if custom element is via innerHTML property before ' +
    'registration of a custom element. Document has no browsing context');


test(function() {
    var doc = newHTMLDocument();
    doc.body.innerHTML = '<x-c id="x-c"></x-c>';
    var customElement = doc.querySelector('#x-c');

    var proto = newHTMLElementPrototype();
    customElement.constructor.prototype = proto;
    doc.body.removeChild(customElement);

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if custom element is unregistered. ' +
    'Document has no browsing context');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();

    doc.registerElement('x-d', {prototype: proto});
    doc.body.innerHTML = '<x-d id="x-d"></x-d>';
    doc.body.innerHTML = '';

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if removing custom element via innerHTML property. ' +
    'Document has no browsing context');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();

    doc.registerElement('x-e', {prototype: proto});
    doc.body.innerHTML = '<div id="customParent"><x-e id="x-e"></x-e></div>';
    var parent = doc.querySelector('#customParent');
    doc.body.removeChild(parent);

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if removing perent of custom element. ' +
    'Document has no browsing context');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();

    doc.registerElement('x-f', {prototype: proto});
    doc.body.innerHTML = '<div><x-f id="x-f"></x-f></div>';
    doc.body.innerHTML = '';

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if removing perent of custom element via innerHTML property. ' +
    'Document has no browsing context');


var loseBrowsingContextTest = async_test('Test detached callback is not called ' +
    'if document lose browsing context and custom element is removed');

loseBrowsingContextTest.step(function() {
    var iframe = newIFrame('../../resources/x-element.html');
    iframe.onload = loseBrowsingContextTest.step_func(function(){
        var doc = iframe.contentDocument;
        var proto = newHTMLElementPrototype();
        doc.registerElement('x-element', {prototype: proto});

        var customElement = doc.querySelector('#x-element');
        iframe.remove();
        customElement.remove();

        assert_equals(proto.detachedCallbackCalledCounter, 0,
            'Callback detached should not be called if the document has no browsing context');
        loseBrowsingContextTest.done();
    });
});


var navigateTest = async_test('Test detached callback is not called, ' +
    'if document\'s window is navigated to another document and custom element is removed');

navigateTest.step(function() {
    var iframe = newIFrame('../../resources/x-element.html');
    iframe.onload = navigateTest.step_func(function() {
        var doc = iframe.contentDocument;
        var proto = newHTMLElementPrototype();
        doc.registerElement('x-element', {prototype: proto});
        customElement = doc.querySelector('#x-element');

        iframe.onload = navigateTest.step_func(function() {
            customElement.remove();
            assert_equals(proto.detachedCallbackCalledCounter, 0,
                'Callback detached should not be called ' +
                'if the document has no browsing context');
            navigateTest.done();
            iframe.remove();
        });
        iframe.src = '../../resources/blank.html';
    });
});
</script>
</body>
</html>