summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/custom-elements/v0/custom-element-lifecycle/types-of-callbacks/attribute-changed-callback-change-attribute-test.html
blob: cd5f3a4b8c635125928595f4a388465ac750c760 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html>
<head>
<title>Test attributeChanged callback is called if custom element attribute value is changed</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="attributeChanged callback must be enqueued whenever custom element's attribute is added, changed or removed">
<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();
    var GeneratedConstructor = doc.registerElement('x-a', {prototype: proto});
    var customElement = new GeneratedConstructor();

    customElement.setAttribute('class', 'someClass');
    proto.attributeChangedCallbackCalledCounter = 0;
    customElement.setAttribute('class', 'someClass2');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is changed by method ' +
    'setAttribute(). The custom element is created via constructor');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-b', {prototype: proto});

    doc.body.innerHTML = '<x-b id="x-b" class="oldValue"></x-b>';
    var customElement = doc.querySelector('#x-b');
    customElement.setAttribute('class', 'newValue');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should be called');
    assert_array_equals(proto.attributeChangedCallbackArgs,
        ['class', 'oldValue', 'newValue'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is changed by method ' +
    'setAttribute(). The custom element is created via innerHTML property');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-c', {prototype: proto});
    var customElement = new GeneratedConstructor();

    customElement.setAttribute('class', 'someClass');
    proto.attributeChangedCallbackCalledCounter = 0;
    customElement.classList.add('someClass3');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is changed by method ' +
    'classList.add(). The custom element is created via constructor');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-d', {prototype: proto});

    doc.body.innerHTML = '<x-d id="x-d" class="oldValue"></x-d>';
    var customElement = doc.querySelector('#x-d');
    customElement.classList.add('newestValue');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should be called');
    assert_array_equals(proto.attributeChangedCallbackArgs,
        ['class', 'oldValue', 'oldValue newestValue'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is changed by method ' +
    'classList.add(). The custom element is created via innerHTML property');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-e', {prototype: proto});
    var customElement = new GeneratedConstructor();

    customElement.setAttribute('class', 'someClass');
    proto.attributeChangedCallbackCalledCounter = 0;
    customElement.id = 'someId';
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is changed as property. ' +
    'The custom element is created via constructor');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-f', {prototype: proto});

    doc.body.innerHTML = '<x-f id="x-f" class="oldValue"></x-f>';
    var customElement = doc.querySelector('#x-f');
    customElement.className = 'lastValue';
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should be called');
    assert_array_equals(proto.attributeChangedCallbackArgs,
        ['class', 'oldValue', 'lastValue'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is changed as property. ' +
    'The custom element is created via innerHTML property');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-g', {prototype: proto});
    var customElement = new GeneratedConstructor();

    customElement.setAttribute('class', 'someClass someSuperClass');
    proto.attributeChangedCallbackCalledCounter = 0;
    customElement.classList.toggle('someClass');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is changed by classList.toggle(). ' +
    'The custom element is created via constructor');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-h', {prototype: proto});

    doc.body.innerHTML = '<x-h id="x-h" class="oldValue lastValue"></x-h>';
    var customElement = doc.querySelector('#x-h');
    customElement.classList.toggle('lastValue');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should be called');
    assert_array_equals(proto.attributeChangedCallbackArgs,
        ['class', 'oldValue lastValue', 'oldValue'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is changed by classList.toggle(). ' +
    'The custom element is created via innerHTML property');


test(function() {
    var doc = newHTMLDocument();
    doc.body.innerHTML = '<x-i id="x-i" class="oldValue"></x-i>';
    var customElement = doc.querySelector('#x-i');
    customElement.setAttribute('class', 'newValue');
    customElement.setAttribute('class', 'newestValue');

    var proto = newHTMLElementPrototype();
    doc.registerElement('x-i', {prototype: proto});

    assert_equals(proto.attributeChangedCallbackCalledCounter, 0,
        'Callback attributeChanged should not be called');

    customElement.setAttribute('class', 'rightValue');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should not be called');
    assert_array_equals(proto.attributeChangedCallbackArgs,
        ['class', 'newestValue', 'rightValue'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback is not called if custom element is not registered');


testInIFrame('../../resources/x-element.html', function(doc) {
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});

    var customElement = doc.querySelector('#x-element');
    customElement.setAttribute('class', 'firstValue');
    customElement.setAttribute('class', 'secondValue');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 2, 'Callback ' +
        'attributeChanged should be called after call to setAttribute()');

    customElement.classList.add('someClass3');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 3, 'Callback ' +
        'attributeChanged should be called after call to classList.add()');

    customElement.id = 'someId';
    assert_equals(proto.attributeChangedCallbackCalledCounter, 4, 'Callback ' +
        'attributeChanged should be called after changing attribute as property');
}, 'Test attributeChanged callback is called if attribute value is changed. ' +
    'The document has browsing context');


testInIFrame('../../resources/x-element.html', function(doc) {
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});
    var customElement = doc.querySelector('#x-element');

    customElement.setAttribute('class', 'firstValue');
    customElement.setAttribute('class', 'secondValue');
    assert_array_equals(proto.attributeChangedCallbackArgs,
        ['class', 'firstValue', 'secondValue'],
        'Unexpected callback attributeChanged arguments after call to setAttribute()');

    customElement.classList.add('newestValue');
    assert_array_equals(proto.attributeChangedCallbackArgs,
        ['class', 'secondValue', 'secondValue newestValue'],
        'Unexpected callback attributeChanged arguments after call to classList.add()');

    customElement.className = 'lastValue';
    assert_array_equals(proto.attributeChangedCallbackArgs,
        ['class', 'secondValue newestValue', 'lastValue'],
        'Unexpected callback attributeChanged arguments after changing attribute as property');
}, 'Test attributeChanged callback arguments if attribute value is changed. ' +
    'The document has browsing context');


testInIFrame('../../resources/x-element.html', function(doc) {
    var customElement = doc.querySelector('#x-element');
    customElement.setAttribute('class', 'firstValue');
    customElement.setAttribute('class', 'secondValue');

    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});
    customElement.setAttribute('class', 'thirdValue');

    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should be called');
    assert_array_equals(proto.attributeChangedCallbackArgs,
        ['class', 'secondValue', 'thirdValue'],
        'Unexpected callback attributeChanged arguments after setAttribute() call');
}, 'Test attributeChanged callback is not called if custom element is not registered. ' +
    'The document has browsing context');
</script>
</body>
</html>