2 Copyright © 2008 Movial Creative Technologies Inc
3 Feel free to use the contents of this file in any way you want
4 If you can make money with it, please contact info@movial.com
5 and you have almost-guaranteed sales position waiting for you!
11 <script type='text/javascript' src='DBus.js'></script>
12 <script type='text/javascript'>
17 var current_test = null;
19 function Test(method_name, arg, convert, xfail, signature, next_test)
21 this.method_name = method_name;
23 this.convert = convert;
24 this.signature = signature;
30 this.next = next_test;
46 if (!this.method_name)
48 alert("Test error: No method name specified!");
52 if (typeof this.arg != 'boolean' && !this.arg)
54 alert("Test error: No argument specified!");
58 var method = DBus.getMethod(DBus.SESSION, 'org.movial.Unit',
60 this.method_name, 'org.movial.Unit', this.signature,
62 this.signal = DBus.getSignal(DBus.SESSION, 'org.movial.Unit', this.method_name,
64 this.signal.onemit = this.onemit;
66 // We need to receive replies as we go to get a meaningful report
67 // but do test with async replies too!
70 var status = document.getElementById('status');
73 if (this.convert instanceof Function)
75 send_arg = this.convert(this.arg);
86 status.innerHTML += '<p>Result: <span style="color: ' + color + ';">Converting "' + this.arg + '" failed</span>';
87 current_test = this.next;
88 setTimeout(run_next, 10);
95 method.onreply = this.method_reply;
96 method.onerror = this.error_reply;
99 if (this.arg instanceof Array || this.arg instanceof Object)
103 for (var propname in this.arg)
110 argstr += propname + ":" + this.arg[propname];
114 argstr = this.arg.toString();
117 status.innerHTML += '<p>Sending ' + this.method_name + ':<span style="color: blue; padding-left: 1em;">' + argstr + ' of type ' + typeof send_arg + ' with signature ' + this.signature + '</span>';
119 this.signal.enabled = !this.xfail;
123 onemit: function (reply_arg)
125 var status = document.getElementById('status');
126 status.innerHTML += '<p>Got signal "' + this.method_name + '"';
127 this.signal.enabled = false;
128 this.method_reply(reply_arg);
130 current_test = this.next;
131 setTimeout(run_next, 10);
135 method_reply: function (reply_arg)
137 var status = document.getElementById('status');
141 status.innerHTML += '<p style="margin-top: -0.5em"><span style="color:red; padding-left: 1em;">This test should have failed, but did not!</span>';
143 current_test = this.next;
144 setTimeout(run_next, 10);
150 if (!is_equal(this.arg, reply_arg))
155 if (reply_arg instanceof Array || reply_arg instanceof Object)
159 for (var propname in reply_arg)
166 argstr += propname + ":" + reply_arg[propname];
170 if (typeof this.arg == 'boolean')
172 argstr = reply_arg.toString();
173 } else if (reply_arg) {
174 argstr = reply_arg.toString();
187 status.innerHTML += '<p style="margin-top: -0.5em">Result: <span style="color: ' + color + '; padding-left: 1em;">' + argstr + ', ' + typeof reply_arg + '</span>';
192 error_reply: function (error_name, error_message)
194 var status = document.getElementById('status');
198 status.innerHTML += '<p style="color: green;">Expected failure:';
201 status.innerHTML += '<p style="color: red;">' + error_name + ':';
204 status.innerHTML += '<span style="color: red; padding-left: 1em;">' + error_message + '</span>';
206 current_test = this.next;
207 setTimeout(run_next, 10);
213 function is_equal(a, b)
215 if ((a instanceof Array && b instanceof Array)
216 || (a instanceof Object && b instanceof Object))
220 if (!is_equal(a[i], b[i]))
240 var status = document.getElementById('status');
241 status.innerHTML += "<p>Ending test...";
242 var end = DBus.getMethod(DBus.SESSION, 'org.movial.Unit',
244 "end", 'org.movial.Unit');
249 function show_results()
251 var results = document.getElementById('results');
252 results.innerHTML = '<p>Total of ' + n_tests + ' tests, <span style="color: green">passed ' + good + '</span>, <span style="color:red">failed ' + bad + '</span>, unknown ' + (n_tests - good - bad) + ", " + Math.round((good/n_tests)*100) + '% success rate';
255 var tests = new Array();
259 var status = document.getElementById('status');
261 setTimeout(show_results, 1000);
263 status.innerHTML += "<p>Starting test...";
264 var start = DBus.getMethod(DBus.SESSION, 'org.movial.Unit',
266 "start", 'org.movial.Unit');
271 tests[i++] = new Test('Boolean', true, null, false);
272 tests[i++] = new Test('Boolean', true, null, false, 'b');
274 tests[i++] = new Test('Int16', 32767, DBus.Int16, false);
275 tests[i++] = new Test('Int16', 32767, null, true);
276 tests[i++] = new Test('Int16', 32767, DBus.Int16, false, 'n');
277 tests[i++] = new Test('Int16', 32767, null, false, 'n');
279 tests[i++] = new Test('Int32', 2147483647, DBus.Int32, false);
280 tests[i++] = new Test('Int32', 2147483647, null, true);
281 tests[i++] = new Test('Int32', 2147483647, DBus.Int32, false, 'i');
282 tests[i++] = new Test('Int32', 2147483647, null, false, 'i');
284 // There's problems with the (u)int64 types since doubles can't hold
285 // large enough values accurately.
286 tests[i++] = new Test('Int64', 17179869176, DBus.Int64, false);
287 tests[i++] = new Test('Int64', 17179869176, null, true);
288 tests[i++] = new Test('Int64', 17179869176, DBus.Int64, false, 'x');
289 tests[i++] = new Test('Int64', 17179869176, null, false, 'x');
291 tests[i++] = new Test('UInt16', 32767, DBus.UInt16, false);
292 tests[i++] = new Test('UInt16', 32767, null, true);
293 tests[i++] = new Test('UInt16', 32767, DBus.UInt16, false, 'q');
294 tests[i++] = new Test('UInt16', 32767, null, false, 'q');
296 tests[i++] = new Test('UInt32', 2147483647, DBus.UInt32, false);
297 tests[i++] = new Test('UInt32', 2147483647, null, true);
298 tests[i++] = new Test('UInt32', 2147483647, DBus.UInt32, false, 'u');
299 tests[i++] = new Test('UInt32', 2147483647, null, false, 'u');
301 tests[i++] = new Test('UInt64', 17179869176, DBus.UInt64, false);
302 tests[i++] = new Test('UInt64', 17179869176, null, true);
303 tests[i++] = new Test('UInt64', 17179869176, DBus.UInt64, false, 't');
304 tests[i++] = new Test('UInt64', 17179869176, null, false, 't');
306 tests[i++] = new Test('Double', 1024.1024, null, false);
307 tests[i++] = new Test('Double', 1024.1024, null, false, 'd');
309 tests[i++] = new Test('Byte', 1, DBus.Byte, false);
310 tests[i++] = new Test('Byte', 1, null, true);
311 tests[i++] = new Test('Byte', 1, DBus.Byte, false, 'y');
312 tests[i++] = new Test('Byte', 1, null, false, 'y');
314 tests[i++] = new Test('String', "Test string", null, false);
315 tests[i++] = new Test('String', "Test string", null, false, 's');
317 tests[i++] = new Test('ObjectPath', "/org/movial/Unit", DBus.ObjectPath, false);
318 tests[i++] = new Test('ObjectPath', "/org/movial/Unit", null, true);
319 tests[i++] = new Test('ObjectPath', "/org/movial/Unit", DBus.ObjectPath, false, 'o');
320 tests[i++] = new Test('ObjectPath', "I'm invalid object path", DBus.ObjectPath, true);
321 tests[i++] = new Test('ObjectPath', "/not//valid/either", DBus.ObjectPath, true);
323 tests[i++] = new Test('Signature', "sib", DBus.Signature, false);
324 tests[i++] = new Test('Signature', "sib", null, true);
325 tests[i++] = new Test('Signature', "sib", DBus.Signature, false, 'g');
326 tests[i++] = new Test('Signature', "sib", null, false, 'g');
328 tests[i++] = new Test("Array", ["woot", "bleep", "wap"], null, false, null);
329 tests[i++] = new Test("Array", [1, 2, 3], null, false, null);
330 tests[i++] = new Test("Array", ["woot", "bleep", "wap"], null, false, 'as');
331 tests[i++] = new Test("Array", [1, 2, 3], null, false, 'ai');
333 var dict = new Object();
335 dict["test2"] = "you";
337 tests[i++] = new Test("Dict", dict, null, false, 'a{ss}');
338 tests[i++] = new Test("Dict", dict, null, false);
340 var dict = new Object();
341 dict["test"] = [1, 2, 3];
342 dict["test2"] = [4, 5, 6];
343 tests[i++] = new Test("Dict", dict, null, false, 'a{sad}');
345 /* TODO: We don't really yet know how exactly we want these to go...
346 tests[i++] = new Test("Variant", "woot", DBus.Variant, false);
347 tests[i++] = new Test("Variant", 1, DBus.Variant, false);
348 tests[i++] = new Test("Variant", "woot", DBus.Variant, false, 'v');
349 tests[i++] = new Test("Variant", ["woot", "bar"], DBus.Variant, false, 'v');
351 var struct = new Object();
352 struct.what = "that";
353 struct.how_many = 42;
355 tests[i++] = new Test("Struct", struct, DBus.Struct, false);
356 tests[i++] = new Test("Struct", struct, DBus.Struct, false, '(si)');
359 for (var j = 0; j < i-1; j++)
361 tests[j].next = tests[j+1]
363 current_test = tests[0];
370 <body onload='do_unit();'>
371 <div><a href="unit.html">Run again</a> <a href="http://google.fi">Leave the page</a></div>
372 <div id='results' style='border: solid blue 0.1em; padding: 1em; margin: 1em;'></div>
373 <div id='status' style='border: dashed black 0.1em; padding: 1em;'></div>