-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathelements-interfaces.js
More file actions
352 lines (292 loc) · 14.8 KB
/
Copy pathelements-interfaces.js
File metadata and controls
352 lines (292 loc) · 14.8 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
module("Element attributes tests", {});
test("Event attribute tests", function() {
var e = document.createElementNS(XML3D.xml3dNS, "xml3d");
var getterText = "alert('get function');";
// Set via attribute and get via interface
equal(e.onclick, null, "xml3d::onclick is null initially.");
e.setAttribute("onclick", getterText);
equal(e.getAttribute("onclick"), getterText, "Value set.");
equal(typeof e.onclick, "function", "onclick is of type function");
notEqual(e.onclick.toString().match(new RegExp("function onclick.*\n.*alert(.*get function.*).*\n.*")), null , "text");
//console.log(e.onclick.toString().match());
// Set via interface
var v = function() {
console.log("set function");
};
e.onclick = v;
equal(e.getAttribute("onclick"), getterText,
"Value of attribute remains unchanged after setting event function per interface.");
equal(e.onclick, v, "Function is set.");
e.onclick = "hallo";
equal(e.onclick, null, "Assign invalid type via interface: null.");
e.onclick = v;
e.setAttribute("onclick", "alert('override');");
notEqual(e.onclick, v, "Valid setAttribute changed function.");
equal(typeof e.onclick, "function", "onclick is of type function after setAttribute");
e.setAttribute("onframedrawn", getterText);
equal(e.getAttribute("onframedrawn"), getterText, "Framedrawn was set");
e.setAttribute("onframedrawn", "alert('test')");
equal(typeof e.onframedrawn, "function", "onframedrawn was set to a function through setAttribute");
e.onframedrawn = null;
equal(e.onframedrawn, null, "onframedrawn was set back to null");
});
test("Int interface tests", function() {
// Behavior copied from HTMLCanvas::width
// Check this first:
var e = document.createElement("canvas");
equal(e.getAttribute("width"), null, "no Attribute set");
equal(e.width, 300, "canvas.width is 300 by default.");
e.setAttribute("width", "509.5");
equal(e.width, 509, "canvas.width ='509.5' // 509;");
e.setAttribute("width", 509.5);
equal(e.width, 509, "canvas.width = 509.5 // 509;");
e.setAttribute("width", "asdf");
equal(e.width, 300, "canvas.width set back to default, if invalid value.");
// Now xml3d
e = document.createElementNS(XML3D.xml3dNS, "xml3d");
equal(e.getAttribute("width"), null, "no Attribute set");
// Set via interface
equal(e.width, 800, "xml3d.width is 800 by default.");
e.width = 300;
equal(e.width, 300, "xml3d.width = 300.");
e.width = true;
equal(e.width, 1, "xml3d.width = 1.");
e.width = 500.9;
equal(e.width, 500, "xml3d.width = 500.9.");
// Set via attribute
e.setAttribute("width", "123");
equal(e.width, 123, "Value set via setAttribute to 123.");
equal(e.getAttribute("width"), "123", "Value set via setAttribute to 123. getAttribute = '123'");
e.setAttribute("width", "509.5");
equal(e.width, 509, "xml3d.width ='509.5' // 509;");
equal(e.getAttribute("width"), "509.5", "getAttribute = '509.5'");
e.setAttribute("width", 509.5);
equal(e.width, 509, "setAttribute(509.5) => xml3d.width == 509;");
e.setAttribute("width", "asdf");
equal(e.getAttribute("width"), "asdf", "Invalid value is set to attribute");
equal(e.width, 800, "Invalid value set via setAttribute. Back to default: 800.");
});
test("String interface tests", function() {
e = document.createElementNS(XML3D.xml3dNS, "xml3d");
equal(e.getAttribute("id"), null, "no Attribute set");
// Set via interface
equal(e.id, "", "No Attribute set.");
e.id = "myxml3d";
equal(e.id, "myxml3d", "xml3d.id = 'myxml3d'.");
equal(e.getAttribute("id"), "myxml3d", "getAttribute = 'myxml3d'.");
e.id = true;
equal(e.id, "true", "xml3d.id == 'true'.");
equal(e.getAttribute("id"), "true", "getAttribute = 'true'.");
// Set via attribute
e.setAttribute("id", "123");
equal(e.id, "123", "Value set via setAttribute to 123.");
});
test("Reference interface tests", function() {
e = document.createElementNS(XML3D.xml3dNS, "xml3d");
equal(e.getAttribute("view"), null, "no Attribute set");
// Set via interface
e.view = "myxml3d";
equal(e.view, "myxml3d", "xml3d.view = 'myxml3d'.");
equal(e.getAttribute("view"), "myxml3d", "getAttribute = 'myxml3d'.");
e.view = true;
equal(e.view, "true", "xml3d.view == 'true'.");
equal(e.getAttribute("view"), "true", "getAttribute = 'true'.");
// Set via attribute
e.setAttribute("view", "#myView");
equal(e.view, "#myView", "Value set via setAttribute to 123.");
});
test("Float interface tests (XHTML: Case sensitive)", function() {
var e = document.createElementNS(XML3D.xml3dNS, "float");
// Set via interface
QUnit.close(e.key, 0.0, EPSILON, "float.key is 0.0 initially.");
e.key = 0.4;
QUnit.close(e.key, 0.4, EPSILON, "float.key = 0.4.");
equal(e.getAttribute("key"), "0.4", "getAttribute = '0.4'.");
e.key = true;
QUnit.close(e.key, 1, EPSILON, "float.key = 1.");
// Set via attribute
e.setAttribute("key", "0.5");
QUnit.close(e.key, 0.5, EPSILON, "Value set via setAttribute to 0.5.");
equal(e.getAttribute("key"), "0.5", "Value set via setAttribute to 0.5.");
e.setAttribute("key", 0.6);
QUnit.close(e.key, 0.6, EPSILON, "Value set via setAttribute to 0.6.");
equal(e.getAttribute("key"), "0.6", "Value set via setAttribute to 0.6.");
e.setAttribute("key", "asdf");
equal(e.getAttribute("key"), "asdf", "attribute value invalid");
QUnit.close(e.key, 0.0, EPSILON, "Invalid value set via setAttribute. Back to default: 0.0.");
});
test("Float interface tests (HTML: Case insensitive)", function() {
var e = document.createElement("float");
// Set via interface
QUnit.close(e.key, 0.0, EPSILON, "float.key is 0.0 initially.");
e.key = 0.87;
QUnit.close(e.key, 0.87, EPSILON, "float.key = 0.87.");
equal(e.getAttribute("key"), "0.87", "getAttribute = '0.87'.");
e.key = true;
QUnit.close(e.key, 1, EPSILON, "float.key = 1.");
// Set via attribute
e.setAttribute("key", "0.5");
QUnit.close(e.key, 0.5, EPSILON, "Value set via setAttribute to 0.5.");
equal(e.getAttribute("key"), "0.5", "Value set via setAttribute to 0.5.");
e.setAttribute("key", 0.6);
QUnit.close(e.key, 0.6, EPSILON, "Value set via setAttribute to 0.6.");
equal(e.getAttribute("key"), "0.6", "Value set via setAttribute to 0.6.");
e.setAttribute("key", "asdf");
equal(e.getAttribute("key"), "asdf", "attribute value invalid");
QUnit.close(e.key, 0.0, EPSILON, "Invalid value set via setAttribute. Back to default: 0.0.");
});
test("Boolean interface tests", function() {
// Close to behavior of HTMLInputElement::disabled
var e = document.createElement("input");
equal(e.disabled, false, "input.disabled is 'false' by default.");
// Set via interface
e.disabled = true;
equal(e.disabled, true, "input.disabled set to true.");
e.disabled = 0;
equal(e.disabled, false, "input.disabled set to 0.");
e.disabled = "false";
equal(e.disabled, true, "input.disabled set to non-empty string.");
e.visible = true;
equal(e.getAttribute("visible"), null, "Attribute has not been set yet.");
// now XML3DGroupElement::visible
// We do not completely emulate the weird behavior of boolean values in HTML
// HTML boolean attributes have a real strange behavior, mainly for backward compatibility.
// HTML/SGML allows something like <input disabled> which maps to following in XHTML:
// <input disabled="true"/> => disabled as expected
// <input disabled="false"/> => also disabled
// The semantic is: If the disabled attribute is set, it's disabled otherwise not
// This makes only sense for booleans, where the default is false.
// Thus we use the "standard" behavior as for example an integer value
e = document.createElement("float");
// Set via interface
equal(e.param, false, "float.param is 'true' initially.");
e.param = true;
equal(e.param, true, "float.param set to false;");
e.param = false;
equal(e.param, false, "float.param set to true;");
e.param = 0;
equal(e.param, false, "float.param set to 0;");
e.param = "false"; // Non-empty string evaulates to 'true'
equal(e.param, true, "float.param set to non-empty string.");
equal(e.getAttribute("param"), "true", "Attribute evaluates to 'true'.");
// Set via attribute
e.param = false;
e.setAttribute("param", "true");
equal(e.param, true, "Value set via setAttribute to 'true'.");
e.param = true;
equal(e.getAttribute("param"), "true", "getAttribute = 'true'.");
e.param = false;
equal(e.getAttribute("param"), "false", "getAttribute = 'false'.");
e.setAttribute("param", "asdf");
equal(e.param, true, "Invalid value set via setAttribute.");
});
test("Vec3 interface tests", function() {
var e = document.createElementNS(XML3D.xml3dNS, "transform");
equal(e.getAttribute("scale"), null, "Attribute has not been set yet.");
// Set via interface
QUnit.closeArray(e.scale.data, [1,1,1], EPSILON, "transform.scale is '1 1 1' initially.");
e.scale = new XML3D.Vec3(1,0,0);
QUnit.closeArray(e.scale.data, [1,0,0], EPSILON, "transform.scale changed after set");
// Attribute is synced
equal(e.getAttribute("scale"), "1 0 0", "getAttribute = '1 0 0'.");
// Set via attribute
e.setAttribute("scale", "1 2 3");
QUnit.closeArray(e.scale.data, [1,2,3], EPSILON, "Value set via setAttribute to '1 2 3'.");
e.setAttribute("scale", "asdf");
QUnit.closeArray(e.scale.data, [1,1,1], EPSILON, "Invalid value set via setAttribute. Back to default: 1 1 1.");
});
test("Vec4 interface tests", function() {
var e = document.createElementNS(XML3D.xml3dNS, "transform");
// Set via interface
QUnit.closeArray(e.rotation.data, [0,0,1,0], EPSILON, "rotation is '0 0 1 0' initially.");
e.rotation = [0,1,0,0];
QUnit.closeArray(e.rotation.data, [0,1,0,0], EPSILON, "rotation changed after set");
// Attribute is synced
equal(e.getAttribute("rotation"), "0 1 0 0", "getAttribute = '0 1 0 0'.");
// Set via attribute
e.setAttribute("rotation", "1 0 0 3.14");
QUnit.closeArray(e.rotation.data,[1,0,0,3.14], EPSILON, "Value set via setAttribute to '1 0 0 3.14'.");
e.setAttribute("rotation", "asdf");
QUnit.closeArray(e.rotation.data, [0,0,1,0], EPSILON, "Invalid value set via setAttribute. Back to default.");
});
test("Enumeration interface tests", function() {
// Behavior copied from HTMLInputElement::type
var e = document.createElementNS(XML3D.xml3dNS, "texture");
// Attribute not set
equal(e.hasAttribute("type"), false, "hasAttribute = false.");
equal(e.getAttribute("type"), null, "getAttribute = null.");
// Set via interface
equal(e.type, "2d", "texture.type is '2d' initially.");
e.type = "3d";
equal(e.type, "3d", "texture.type = '3d';");
e.type = "1D";
equal(e.type, "1d", "texture.type = '1D'; // case insensitive");
// Set invalid, TODO: Right behavior?
e.type = "asdf";
equal(e.type, "2d", "texture.type set to invalid. Back to default: '2d'.");
// Attribute is sync'ed
e.type = "3d";
equal(e.getAttribute("type"), "3d", "getAttribute = '3d'.");
e.type = "asdf"; // Invalid values are set anyway
equal(e.getAttribute("type"), "asdf", "getAttribute = 'asdf'.");
// Set via attribute
e.setAttribute("type", "1d");
equal(e.type, "1d", "Value set via setAttribute to '1d'.");
e.setAttribute("type", "3D"); // case insensitive
equal(e.type, "3d", "Value set via setAttribute to '3D'.");
e.setAttribute("type", "asdf"); // invalid
equal(e.getAttribute("type"), "asdf", "getAttribute = 'asdf'.");
equal(e.type, "2d", "Invalid value set via setAttribute. Back to default: '2d'.");
});
test("Typed Array interface tests", function() {
var e = document.createElementNS(XML3D.xml3dNS, "float");
notEqual(e.value, null, "Value must not be null");
console.dir(e.value);
ok(e.value instanceof Float32Array, "<float> has Float32Array");
equal(e.value.length, 0, "Initial length is zero.");
e = document.createElementNS(XML3D.xml3dNS, "int");
notEqual(e.value, null, "Value must not be null");
ok(e.value instanceof Int32Array, "<int> has Int32Array");
equal(e.value.length, 0, "Initial length is zero.");
e = document.createElementNS(XML3D.xml3dNS, "bool");
notEqual(e.value, null, "Value must not be null");
ok(e.value instanceof Uint8Array,"<bool> has Uint8Array");
equal(e.value.length, 0, "Initial length is zero.");
});
module("Element initialization tests", {
setup : function() {
stop();
var that = this;
this.cb = function(e) {
ok(true, "Scene loaded");
that.doc = document.getElementById("xml3dframe").contentDocument;
start();
};
loadDocument("scenes/basic.html", this.cb);
},
teardown : function() {
var v = document.getElementById("xml3dframe");
v.removeEventListener("load", this.cb, true);
}
});
test("Interface initialization", function() {
var t = this.doc.getElementById("t_mixed");
var v = this.doc.getElementById("myView");
var x = this.doc.getElementById("myXml3d");
var m = this.doc.getElementById("myMesh01");
var g = this.doc.getElementById("myGroup");
QUnit.closeArray(t.translation.data, [1,2,3], EPSILON, "XML3D.Vec3 (transform::translation) initialized.");
QUnit.closeArray(t.rotation.data, [1,0,0,1.5708], EPSILON, "XML3D.Vec4 (transform::rotation) initialized.");
equal(x.width, 1000, "Int (xml3d::width) initialized.");
equal(m.type, "lines", "Enumeration (mesh::type) initialized.");
equal(g.onclick, null, "Event attribute (group::onclick) non-initialized.");
equal(typeof m.onclick, "function", "Event attribute (mesh::onclick) initialized.");
});
test("Typed array initialization", 5, function() {
var value = this.doc.getElementById("indices").value;
equal(value.length, 6 , "6 values in array");
value = this.doc.getElementById("positions").value;
equal(value.length, 12 , "12 indices in array");
value = this.doc.getElementById("texcoords").value;
equal(value.length, 8 , "8 indices in array");
});