gecko/dom/future/tests/test_future.html
Ed Morley 3b410f3a76 Backed out changeset 96036434bb78 (bug 875289) for mochitest-chrome failures on a CLOSED TREE
--HG--
rename : dom/promise/Promise.cpp => dom/future/Future.cpp
rename : dom/promise/Promise.h => dom/future/Future.h
rename : dom/promise/PromiseCallback.cpp => dom/future/FutureCallback.cpp
rename : dom/promise/PromiseCallback.h => dom/future/FutureCallback.h
rename : dom/promise/PromiseResolver.cpp => dom/future/FutureResolver.cpp
rename : dom/promise/PromiseResolver.h => dom/future/FutureResolver.h
rename : dom/promise/Makefile.in => dom/future/Makefile.in
rename : dom/promise/moz.build => dom/future/moz.build
rename : dom/promise/tests/Makefile.in => dom/future/tests/Makefile.in
rename : dom/promise/tests/moz.build => dom/future/tests/moz.build
rename : dom/promise/tests/test_promise.html => dom/future/tests/test_future.html
rename : dom/promise/tests/test_resolve.html => dom/future/tests/test_resolve.html
rename : dom/webidl/Promise.webidl => dom/webidl/Future.webidl
2013-06-20 11:20:49 +01:00

413 lines
10 KiB
HTML

<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
<title>Basic Future Test</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript"><!--
function futureResolve() {
ok(Future, "Future object should exist");
var future = new Future(function(resolver) {
ok(resolver, "FutureResolver exists");
ok("reject" in resolver, "FutureResolver.reject exists");
ok("resolve" in resolver, "FutureResolver.resolve exists");
resolver.resolve(42);
}).done(function(what) {
ok(true, "Done - resolveCb has been called");
is(what, 42, "ResolveCb received 42");
runTest();
}, function() {
ok(false, "Done - rejectCb has been called");
runTest();
});
}
function futureReject() {
var future = new Future(function(resolver) {
resolver.reject(42);
}).done(function(what) {
ok(false, "Done - resolveCb has been called");
runTest();
}, function(what) {
ok(true, "Done - rejectCb has been called");
is(what, 42, "RejectCb received 42");
runTest();
});
}
function futureException() {
var future = new Future(function(resolver) {
throw 42;
}).done(function(what) {
ok(false, "Done - resolveCb has been called");
runTest();
}, function(what) {
ok(true, "Done - rejectCb has been called");
is(what, 42, "RejectCb received 42");
runTest();
});
}
function futureGC() {
var resolver;
var future = new Future(function(r) {
resolver = r;
}).done(function(what) {
ok(true, "Done - future is still alive");
runTest();
});
future = null;
SpecialPowers.gc();
SpecialPowers.forceGC();
SpecialPowers.forceCC();
resolver.resolve(42);
}
function futureAsync() {
var global = "foo";
var f = new Future(function(r) {
is(global, "foo", "Global should be foo");
r.resolve(42);
is(global, "foo", "Global should still be foo");
setTimeout(function() {
is(global, "bar", "Global should still be bar!");
runTest();
}, 0);
}).done(function() {
global = "bar";
});
is(global, "foo", "Global should still be foo (2)");
}
function futureDoubleDone() {
var steps = 0;
var future = new Future(function(resolver) {
resolver.resolve(42);
});
future.done(function(what) {
ok(true, "Done.resolve has been called");
is(what, 42, "Value == 42");
steps++;
}, function(what) {
ok(false, "Done.reject has been called");
});
future.done(function(what) {
ok(true, "Done.resolve has been called");
is(steps, 1, "Done.resolve - step == 1");
is(what, 42, "Value == 42");
runTest();
}, function(what) {
ok(false, "Done.reject has been called");
});
}
function futureDoneException() {
var future = new Future(function(resolver) {
resolver.resolve(42);
});
onErrorCb = window.onerror;
window.onerror = function(e) {
ok(true, "window.onerror has been called!");
window.onerror = onErrorCb;
runTest();
};
future.done(function(what) {
ok(true, "Done.resolve has been called");
throw "booh";
});
}
function futureThenCatchDone() {
var future = new Future(function(resolver) {
resolver.resolve(42);
});
var future2 = future.then(function(what) {
ok(true, "Then.resolve has been called");
is(what, 42, "Value == 42");
return what + 1;
}, function(what) {
ok(false, "Then.reject has been called");
});
isnot(future, future2, "These 2 future objs are different");
future2.then(function(what) {
ok(true, "Then.resolve has been called");
is(what, 43, "Value == 43");
return what + 1;
}, function(what) {
ok(false, "Then.reject has been called");
}).catch(function() {
ok(false, "Catch has been called");
}).done(function(what) {
ok(true, "Done.resolve has been called");
is(what, 44, "Value == 44");
runTest();
}, function(what) {
ok(false, "Done.reject has been called");
});
}
function futureRejectThenCatchDone() {
var future = new Future(function(resolver) {
resolver.reject(42);
});
var future2 = future.then(function(what) {
ok(false, "Then.resolve has been called");
}, function(what) {
ok(true, "Then.reject has been called");
is(what, 42, "Value == 42");
return what + 1;
});
isnot(future, future2, "These 2 future objs are different");
future2.then(function(what) {
ok(true, "Then.resolve has been called");
is(what, 43, "Value == 43");
return what+1;
}).catch(function(what) {
ok(false, "Catch has been called");
}).done(function(what) {
ok(true, "Then.resolve has been called");
is(what, 44, "Value == 44");
runTest();
});
}
function futureRejectThenCatchDone2() {
var future = new Future(function(resolver) {
resolver.reject(42);
});
future.then(function(what) {
ok(true, "Then.resolve has been called");
is(what, 42, "Value == 42");
return what+1;
}).catch(function(what) {
is(what, 42, "Value == 42");
ok(true, "Catch has been called");
return what+1;
}).done(function(what) {
ok(true, "Then.resolve has been called");
is(what, 43, "Value == 43");
runTest();
});
}
function futureRejectThenCatchExceptionDone() {
var future = new Future(function(resolver) {
resolver.reject(42);
});
future.then(function(what) {
ok(false, "Then.resolve has been called");
}, function(what) {
ok(true, "Then.reject has been called");
is(what, 42, "Value == 42");
throw(what + 1);
}).catch(function(what) {
ok(true, "Catch has been called");
is(what, 43, "Value == 43");
return what + 1;
}).done(function(what) {
ok(true, "Then.resolve has been called");
is(what, 44, "Value == 44");
runTest();
});
}
function futureThenCatchOrderingResolve() {
var global = 0;
var f = new Future(function(r) {
r.resolve(42);
});
f.done(function() {
f.then(function() {
global++;
});
f.catch(function() {
global++;
});
f.done(function() {
global++;
});
setTimeout(function() {
is(global, 2, "Many steps... should return 2");
runTest();
}, 0);
});
}
function futureThenCatchOrderingReject() {
var global = 0;
var f = new Future(function(r) {
r.reject(42);
})
f.done(function() {}, function() {
f.then(function() {
global++;
});
f.catch(function() {
global++;
});
f.done(function() {}, function() {
global++;
});
setTimeout(function() {
is(global, 2, "Many steps... should return 2");
runTest();
}, 0);
});
}
function futureNestedFuture() {
new Future(function(resolver) {
resolver.resolve(new Future(function(r) {
ok(true, "Nested future is executed");
r.resolve(42);
}));
}).then(function(value) {
is(value, 42, "Nested future is executed and then == 42");
runTest();
});
}
function futureNestedNestedFuture() {
new Future(function(resolver) {
resolver.resolve(new Future(function(r) {
ok(true, "Nested future is executed");
r.resolve(42);
}).then(function(what) { return what+1; }));
}).then(function(value) {
is(value, 43, "Nested future is executed and then == 43");
runTest();
});
}
function futureWrongNestedFuture() {
new Future(function(resolver) {
resolver.resolve(new Future(function(r) {
ok(true, "Nested future is executed");
r.resolve(42);
}));
resolver.reject(42);
}).then(function(value) {
is(value, 42, "Nested future is executed and then == 42");
runTest();
}, function(value) {
ok(false, "This is wrong");
});
}
function futureLoop() {
new Future(function(resolver) {
resolver.resolve(new Future(function(r) {
ok(true, "Nested future is executed");
r.resolve(new Future(function(r) {
ok(true, "Nested nested future is executed");
r.resolve(42);
}));
}));
}).then(function(value) {
is(value, 42, "Nested nested future is executed and then == 42");
runTest();
}, function(value) {
ok(false, "This is wrong");
});
}
function futureReject() {
var future = Future.reject(42).done(function(what) {
ok(false, "This should not be called");
}, function(what) {
is(what, 42, "Value == 42");
runTest();
});
}
function futureResolve() {
var future = Future.resolve(42).done(function(what) {
is(what, 42, "Value == 42");
runTest();
}, function() {
ok(false, "This should not be called");
});
}
function futureResolveNestedFuture() {
var future = Future.resolve(new Future(function(r) {
ok(true, "Nested future is executed");
r.resolve(42);
}, function() {
ok(false, "This should not be called");
})).done(function(what) {
is(what, 42, "Value == 42");
runTest();
}, function() {
ok(false, "This should not be called");
});
}
var tests = [ futureResolve, futureReject,
futureException, futureGC, futureAsync,
futureDoubleDone, futureDoneException,
futureThenCatchDone, futureRejectThenCatchDone,
futureRejectThenCatchDone2,
futureRejectThenCatchExceptionDone,
futureThenCatchOrderingResolve,
futureThenCatchOrderingReject,
futureNestedFuture, futureNestedNestedFuture,
futureWrongNestedFuture, futureLoop,
futureReject, futureResolve,
futureResolveNestedFuture,
];
function runTest() {
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
var p = SpecialPowers.getBoolPref("dom.future.enabled");
SpecialPowers.setBoolPref("dom.future.enabled", false);
ok(!("Future" in window), "Future object should not exist if disabled by pref");
SpecialPowers.setBoolPref("dom.future.enabled", p);
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["dom.future.enabled", true]]}, runTest);
// -->
</script>
</pre>
</body>
</html>