Bug 651107 - Allow waitForClipboard to check for any custom flavor.
authorMarco Bonardo <mbonardo@mozilla.com>
Sat, 23 Apr 2011 02:04:15 +0200
changeset 68467 b87a0f69f78e2ad95d78960d8c3796a7ec40072c
parent 68406 fbe7830f27c067764bb2e34a32f62e146a45e678
child 68468 ddd40a5b413c260f41ede7717071a6dc72b147f7
push id19654
push usermak77@bonardo.net
push dateSat, 23 Apr 2011 10:52:50 +0000
treeherdermozilla-central@5794bcf0f85e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs651107
milestone6.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 651107 - Allow waitForClipboard to check for any custom flavor. r=ehsan,ted
browser/components/places/tests/browser/browser_410196_paste_into_tags.js
testing/mochitest/browser-test.js
testing/mochitest/tests/SimpleTest/SimpleTest.js
--- a/browser/components/places/tests/browser/browser_410196_paste_into_tags.js
+++ b/browser/components/places/tests/browser/browser_410196_paste_into_tags.js
@@ -34,18 +34,20 @@ function onLibraryReady() {
   ok(PlacesUIUtils, "PlacesUIUtils in scope");
 
   PlacesOrganizer = gLibrary.PlacesOrganizer;
   ok(PlacesOrganizer, "Places organizer in scope");
 
   tests.makeHistVisit();
   tests.makeTag();
   tests.focusTag();
-  tests.copyHistNode();
-  tests.waitForClipboard();
+  waitForClipboard(function(aData) !!aData,
+                   tests.copyHistNode,
+                   onClipboardReady,
+                   PlacesUtils.TYPE_X_MOZ_PLACE);
 }
 
 function onClipboardReady() {
   tests.pasteToTag();
   tests.historyNode();
   tests.checkForBookmarkInUI();
 
   gLibrary.close();
@@ -109,34 +111,16 @@ let tests = {
     this.histNode = PlacesOrganizer._content.view.nodeForTreeIndex(0);
     PlacesOrganizer._content.selectNode(this.histNode);
     is(this.histNode.uri, MOZURISPEC,
        "historyNode exists: " + this.histNode.uri);
     // copy the history node
     PlacesOrganizer._content.controller.copy();
   },
 
-  waitForClipboard: function (){
-    try {
-      let xferable = Cc["@mozilla.org/widget/transferable;1"].
-                     createInstance(Ci.nsITransferable);
-      xferable.addDataFlavor(PlacesUtils.TYPE_X_MOZ_PLACE);
-      let clipboard = Cc["@mozilla.org/widget/clipboard;1"].
-                      getService(Ci.nsIClipboard);
-      clipboard.getData(xferable, Ci.nsIClipboard.kGlobalClipboard);
-      let data = { }, type = { };
-      xferable.getAnyTransferData(type, data, { });
-      // Data is in the clipboard
-      onClipboardReady();
-    } catch (ex) {
-      // check again after 100ms.
-      setTimeout(arguments.callee, 100);
-    }
-  },
-
   pasteToTag: function (){
     // paste history node into tag
     this.focusTag(true);
   },
 
   historyNode: function (){
     // re-focus the history again
     PlacesOrganizer.selectLeftPaneQuery("History");
--- a/testing/mochitest/browser-test.js
+++ b/testing/mochitest/browser-test.js
@@ -391,18 +391,18 @@ function testScope(aTester, aTest) {
   this.waitForExplicitFinish = function test_waitForExplicitFinish() {
     self.__done = false;
   };
 
   this.waitForFocus = function test_waitForFocus(callback, targetWindow, expectBlankPage) {
     self.SimpleTest.waitForFocus(callback, targetWindow, expectBlankPage);
   };
 
-  this.waitForClipboard = function test_waitForClipboard(expected, setup, success, failure) {
-    self.SimpleTest.waitForClipboard(expected, setup, success, failure);
+  this.waitForClipboard = function test_waitForClipboard(expected, setup, success, failure, flavor) {
+    self.SimpleTest.waitForClipboard(expected, setup, success, failure, flavor);
   };
 
   this.registerCleanupFunction = function test_registerCleanupFunction(aFunction) {
     self.__cleanupFunctions.push(aFunction);
   };
 
   this.requestLongerTimeout = function test_requestLongerTimeout(aFactor) {
     self.__timeoutFactor = aFactor;
--- a/testing/mochitest/tests/SimpleTest/SimpleTest.js
+++ b/testing/mochitest/tests/SimpleTest/SimpleTest.js
@@ -428,88 +428,107 @@ SimpleTest.waitForClipboard_polls = 0;
 
 /*
  * Polls the clipboard waiting for the expected value. A known value different than
  * the expected value is put on the clipboard first (and also polled for) so we
  * can be sure the value we get isn't just the expected value because it was already
  * on the clipboard. This only uses the global clipboard and only for text/unicode
  * values.
  *
- * @param aExpectedVal
- *        The string value that is expected to be on the clipboard
+ * @param aExpectedStringOrValidatorFn
+ *        The string value that is expected to be on the clipboard or a
+ *        validator function getting cripboard data and returning a bool.
  * @param aSetupFn
  *        A function responsible for setting the clipboard to the expected value,
  *        called after the known value setting succeeds.
  * @param aSuccessFn
  *        A function called when the expected value is found on the clipboard.
  * @param aFailureFn
  *        A function called if the expected value isn't found on the clipboard
  *        within 5s. It can also be called if the known value can't be found.
+ * @param aFlavor [optional] The flavor to look for.  Defaults to "text/unicode".
  */
-SimpleTest.waitForClipboard = function(aExpectedVal, aSetupFn, aSuccessFn, aFailureFn) {
+SimpleTest.__waitForClipboardMonotonicCounter = 0;
+SimpleTest.__defineGetter__("_waitForClipboardMonotonicCounter", function () {
+  return SimpleTest.__waitForClipboardMonotonicCounter++;
+});
+SimpleTest.waitForClipboard = function(aExpectedStringOrValidatorFn, aSetupFn,
+                                       aSuccessFn, aFailureFn, aFlavor) {
     if (ipcMode) {
       //TODO: support waitForClipboard via events to chrome
       dump("E10S_TODO: bug 573735 addresses adding support for this");
       return;
     }
 
     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
     var cbSvc = Components.classes["@mozilla.org/widget/clipboard;1"].
                 getService(Components.interfaces.nsIClipboard);
 
+    var requestedFlavor = aFlavor || "text/unicode";
+
+    function dataToString(aData)
+      aData.QueryInterface(Components.interfaces.nsISupportsString).data;
+
+    // Build a default validator function for common string input.
+    var inputValidatorFn = typeof(aExpectedStringOrValidatorFn) == "string"
+        ? function(aData) aData && dataToString(aData) == aExpectedStringOrValidatorFn
+        : aExpectedStringOrValidatorFn;
+
     // reset for the next use
     function reset() {
         SimpleTest.waitForClipboard_polls = 0;
     }
 
-    function wait(expectedVal, successFn, failureFn) {
+    function wait(validatorFn, successFn, failureFn, flavor) {
         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
         if (++SimpleTest.waitForClipboard_polls > 50) {
             // Log the failure.
-            SimpleTest.ok(false, "Timed out while polling clipboard for pasted data. " +
-                                 "Expected " + expectedVal);
+            SimpleTest.ok(false, "Timed out while polling clipboard for pasted data.");
             reset();
             failureFn();
             return;
         }
 
         var xferable = Components.classes["@mozilla.org/widget/transferable;1"].
                        createInstance(Components.interfaces.nsITransferable);
-        xferable.addDataFlavor("text/unicode");
+        xferable.addDataFlavor(flavor);
         cbSvc.getData(xferable, cbSvc.kGlobalClipboard);
         var data = {};
         try {
-            xferable.getTransferData("text/unicode", data, {});
-            data = data.value.QueryInterface(Components.interfaces.nsISupportsString).data;
+            xferable.getTransferData(flavor, data, {});
         } catch (e) {}
+        data = data.value || null;
 
-        if (data == expectedVal) {
+        if (validatorFn(data)) {
             // Don't show the success message when waiting for preExpectedVal
-            if (data != preExpectedVal)
-                SimpleTest.ok(true,
-                              "Clipboard has the correct value (" + expectedVal + ")");
+            if (preExpectedVal)
+                preExpectedVal = null;
+            else
+                SimpleTest.ok(true, "Clipboard has the correct value");
             reset();
             successFn();
         } else {
-            setTimeout(function() wait(expectedVal, successFn, failureFn), 100);
+            setTimeout(function() wait(validatorFn, successFn, failureFn, flavor), 100);
         }
     }
 
-    // First we wait for a known value != aExpectedVal
-    var preExpectedVal = aExpectedVal + "-waitForClipboard-known-value";
+    // First we wait for a known value different from the expected one.
+    var preExpectedVal = SimpleTest._waitForClipboardMonotonicCounter +
+                         "-waitForClipboard-known-value";
     var cbHelperSvc = Components.classes["@mozilla.org/widget/clipboardhelper;1"].
                       getService(Components.interfaces.nsIClipboardHelper);
     cbHelperSvc.copyString(preExpectedVal);
-    wait(preExpectedVal, function() {
-        // Call the original setup fn
-        aSetupFn();
-        wait(aExpectedVal, aSuccessFn, aFailureFn);
-    }, aFailureFn);
+    wait(function(aData) aData && dataToString(aData) == preExpectedVal,
+         function() {
+           // Call the original setup fn
+           aSetupFn();
+           wait(inputValidatorFn, aSuccessFn, aFailureFn, requestedFlavor);
+         }, aFailureFn, "text/unicode");
 }
 
 /**
  * Executes a function shortly after the call, but lets the caller continue
  * working (or finish).
  */
 SimpleTest.executeSoon = function(aFunc) {
     if ("Components" in window && "classes" in window.Components) {