Bug 568995 - Update tests to use waitForClipboard [r=dao, r=dolske]

This commit is contained in:
Paul O’Shannessy 2010-06-02 13:38:35 -07:00
parent 33279d473c
commit df92fb3c14
2 changed files with 10 additions and 88 deletions

View File

@ -63,62 +63,21 @@ var gTests = [
function test() {
waitForExplicitFinish();
// Put a multi-line string in the clipboard.
info("About to put a string in clipboard");
Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper)
.copyString(kTestString);
let cbHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].
getService(Ci.nsIClipboardHelper);
// Put a multi-line string in the clipboard.
// Setting the clipboard value is an async OS operation, so we need to poll
// the clipboard for valid data before going on.
setTimeout(poll_clipboard, 100);
}
var runCount = 0;
function poll_clipboard() {
// Poll for a maximum of 5s (each run happens after 100ms).
if (++runCount > 50) {
// Log the failure.
ok(false, "Timed out while polling clipboard for pasted data");
// Cleanup and interrupt the test.
finish_test();
return;
}
info("Polling clipboard cycle " + runCount);
var clip = Cc["@mozilla.org/widget/clipboard;1"].
getService(Ci.nsIClipboard);
var trans = Cc["@mozilla.org/widget/transferable;1"].
createInstance(Ci.nsITransferable);
trans.addDataFlavor("text/unicode");
var str = new Object();
try {
// This code could throw if the clipboard is not set yet.
clip.getData(trans, clip.kGlobalClipboard);
trans.getTransferData("text/unicode", str, {});
str = str.value.QueryInterface(Ci.nsISupportsString);
}
catch(ex) {}
if (kTestString == str) {
next_test();
}
else
setTimeout(poll_clipboard, 100);
waitForClipboard(kTestString, function() { cbHelper.copyString(kTestString); },
next_test, finish);
}
function next_test() {
if (gTests.length) {
var currentTest = gTests.shift();
test_paste(currentTest);
}
else {
// No more tests to run.
// Clear the clipboard, emptyClipboard would not clear the native one, so
// we are setting it to an empty string.
Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper)
.copyString("");
if (gTests.length)
test_paste(gTests.shift());
else
finish();
}
}
function test_paste(aCurrentTest) {

View File

@ -75,47 +75,10 @@ function test() {
let data = "";
let polls = 0;
function step1() {
Cc["@mozilla.org/widget/clipboardhelper;1"].
getService(Ci.nsIClipboardHelper).copyString("manatee");
waitForClipboard("manatee", step2);
}
function step2() {
function copyPassword() {
let doc = pwmgrdlg.document;
doc.getElementById("signonsTree").currentIndex = 2;
doc.getElementById("context-copypassword").doCommand();
polls = 0;
waitForClipboard("coded", cleanUp);
}
function waitForClipboard(expectedValue, callback) {
if (++polls > 50) {
ok(false, "Timed out while polling clipboard");
cleanUp();
return;
}
let data = null;
let trans = Cc["@mozilla.org/widget/transferable;1"].
createInstance(Ci.nsITransferable);
trans.addDataFlavor("text/unicode");
clip.getData(trans, clip.kGlobalClipboard);
try {
let str = {};
trans.getTransferData("text/unicode", str, {});
data = str.value.QueryInterface(Ci.nsISupportsString).data;
} catch (e) {}
if (data == expectedValue) {
is(data, expectedValue, "Clipboard should match expected value");
callback();
}
else
setTimeout(waitForClipboard, 100, expectedValue, callback);
}
function cleanUp() {
@ -127,6 +90,6 @@ function test() {
pwmgrdlg.close();
}
step1();
waitForClipboard("coded", copyPassword, cleanUp, cleanUp);
}
}