Permalink
Browse files

Appended test on titanium.

  • Loading branch information...
1 parent 87bf216 commit 0dc2e4c066dd7d8d9a423c997f5cb1d7757bc4c0 @cho45 committed Dec 20, 2010
Showing with 200 additions and 3 deletions.
  1. +25 −1 test-jsdeferred.js
  2. +1 −1 test-node.js
  3. +1 −1 test-rhino.js
  4. +56 −0 test-titanium.html
  5. +117 −0 test-titanium.js
View
26 test-jsdeferred.js
@@ -15,7 +15,7 @@ var expects = testfuns.length;
function show (msg, expect, result) {
var okng = this;
if (okng == "skip") {
- result = "skipped " + expect + "tests:" + msg;
+ result = "skipped " + expect + " tests:" + msg;
while (expect--) testfuns.pop();
expect = "skipped";
} else {
@@ -80,6 +80,24 @@ function expect (msg, expect, result) {
Deferred.define();
+function calcAccuracy () {
+ var d = new Deferred();
+ var r = [];
+ var i = 30;
+ var t = new Date().getTime();
+ setTimeout(function () {
+ if (i-- > 0) {
+ var n = new Date().getTime();
+ r.push(n - t);
+ t = n;
+ setTimeout(arguments.callee, 0);
+ } else {
+ d.call(r);
+ }
+ }, 0);
+ return d;
+}
+
msg("Loaded "+testfuns.length+" tests;");
log("Deferred.next Mode:" + uneval({
_faster_way_Image : !!Deferred.next_faster_way_Image,
@@ -133,6 +151,12 @@ Deferred.onerror = function (e) {
// Start Main Test
msg("Start Main Tests::");
next(function () {
+ msg("Information");
+ return calcAccuracy().next(function (r) {
+ print('setTimeout Accuracy: '+uneval(r));
+ });
+}).
+next(function () {
msg("Process sequence");
var vs = [];
View
2 test-node.js
@@ -26,7 +26,7 @@ function show (msg, expect, result) {
var out = [];
out.push(color(46, "[", [expects - testfuns.length, expects].join("/"), "]"));
if (okng == "skip") {
- out.push(" ", color(33, "skipped " + expect + "tests: " + msg));
+ out.push(" ", color(33, "skipped " + expect + " tests: " + msg));
console.log(out.join(""));
while (expect--) testfuns.pop();
} else
View
2 test-rhino.js
@@ -20,7 +20,7 @@ function show (msg, expect, result) {
var out = [];
out.push(color(46, "[", [expects - testfuns.length, expects].join("/"), "]"));
if (okng == "skip") {
- out.push(" ", color(33, "skipped " + expect + "tests: " + msg));
+ out.push(" ", color(33, "skipped " + expect + " tests: " + msg));
print(out.join(""));
while (expect--) testfuns.pop();
} else
View
56 test-titanium.html
@@ -0,0 +1,56 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title></title>
+ <meta charset="utf-8"/>
+ <style type="text/css">
+ html {
+ margin: 0;
+ padding: 0;
+ }
+
+ body {
+ line-height: 1.66;
+ font-size: 12px;
+ font-family: sans-serif;
+ margin: 0;
+ padding: 0 5px;
+ }
+
+ div {
+ border-style: solid;
+ border-width: 0 0 1px 0;
+ border-color: #efefef;
+ padding: 2px 5px;
+ overflow: hidden;
+ }
+
+ .info {
+ color: #999;
+ }
+
+ .ok {
+ color: #060;
+ }
+
+ .ng {
+ color: #600;
+ }
+
+ .skip {
+ color: #909;
+ }
+ </style>
+ <script type="text/javascript">
+ function message (obj) {
+ var div = document.createElement('div');
+ div.className = obj.type;
+ div.appendChild(document.createTextNode(obj.text));
+ document.body.appendChild(div);
+ document.body.scrollTop = document.body.scrollHeight;
+ }
+ </script>
+ </head>
+ <body>
+ </body>
+</html>
View
117 test-titanium.js
@@ -0,0 +1,117 @@
+Titanium.include('jsdeferred.js');
+Titanium.UI.setBackgroundColor('#fff');
+
+var container = Ti.UI.createWebView({
+ backgroundColor: '#fff',
+});
+Titanium.UI.currentWindow.add(container);
+
+container.html = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory + '/jsdeferred/test-titanium.html').read().toString();
+container.addEventListener('load', function () {
+
+function showToView (obj) {
+ container.evalJS('message(' + JSON.stringify(obj) + ')');
+}
+
+function msg (m) {
+ Ti.API.warn(m);
+ showToView({
+ type : 'info',
+ text : m
+ });
+}
+log = msg;
+print = msg;
+
+msg('Titanium.version: ' + Titanium.version);
+msg('Titanium.Platform.model: ' + Titanium.Platform.model);
+msg('Titanium.Platform.name: ' + Titanium.Platform.name);
+msg('Titanium.Platform.osname: ' + Titanium.Platform.osname);
+msg('Titanium.Platform.version: ' + Titanium.Platform.version);
+
+var Global = (function () { return this })();
+Deferred.define();
+
+file = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory + '/jsdeferred/test-jsdeferred.js');
+data = file.read().toString();
+data = data.match(/\/\/ ::Test::Start::([\s\S]+)::Test::End::/)[1];
+var testfuns = []; data.replace(/(ok|expect)\(.+/g, function (m) {
+ testfuns.push(m);
+ return m;
+});
+
+var expects = testfuns.length;
+
+function uneval (obj) {
+ return JSON.stringify(obj);
+}
+
+function show (msg, expect, result) {
+ var okng = this;
+
+ var out = [];
+ out.push(color(46, "[", [expects - testfuns.length, expects].join("/"), "]"));
+ if (okng == "skip") {
+ out.push(" ", color(33, "skipped " + expect + " tests: " + msg));
+ Ti.API.info(out.join(""));
+ while (expect--) testfuns.pop();
+ showToView({
+ type : 'skip',
+ text : out.join('')
+ });
+ } else
+ if (okng == "ng") {
+ testfuns.pop();
+ expect = (typeof expect == "function") ? uneval(expect).match(/[^{]+/)+"..." : uneval(expect);
+ result = (typeof result == "function") ? uneval(result).match(/[^{]+/)+"..." : uneval(result);
+ out.push(["NG Test::", msg, expect, result].join("\n"));
+ Ti.API.error(out.join(""));
+ showToView({
+ type : 'ng',
+ text : out.join('')
+ });
+ } else {
+ testfuns.pop();
+ out.push(" ", color(32, "ok"));
+ Ti.API.info(out.join(""));
+ showToView({
+ type : 'ok',
+ text : out.join('')
+ });
+ }
+}
+
+function ok () {
+ show.apply("ok", arguments);
+ return true;
+}
+
+function ng () {
+ show.apply("ng", arguments);
+ return true;
+}
+
+function skip () {
+ show.apply("skip", arguments);
+ return true;
+}
+
+function expect (msg, expect, result) {
+ if (expect == result) {
+ show.apply("ok", arguments);
+ } else {
+ show.apply("ng", arguments);
+ }
+ return true;
+}
+
+function color (code) {
+ var str = "";
+ for (var i = 1; i < arguments.length; i++) str += arguments[i];
+ return str;
+}
+
+// run tests
+eval(data);
+
+});

0 comments on commit 0dc2e4c

Please sign in to comment.