browser/metro/base/tests/mochitest/browser_context_menu_tests.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/base/tests/mochitest/browser_context_menu_tests.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,959 @@
     1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* Any copyright is dedicated to the Public Domain.
     1.7 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.8 +
     1.9 +"use strict";
    1.10 +
    1.11 +function debugClipFlavors(aClip)
    1.12 +{
    1.13 +  let xfer = Cc["@mozilla.org/widget/transferable;1"].
    1.14 +             createInstance(Ci.nsITransferable);
    1.15 +  xfer.init(null);
    1.16 +  aClip.getData(xfer, Ci.nsIClipboard.kGlobalClipboard);
    1.17 +  let array = xfer.flavorsTransferableCanExport();
    1.18 +  let count = array.Count();
    1.19 +  info("flavors:" + count);
    1.20 +  for (let idx = 0; idx < count; idx++) {
    1.21 +    let string = array.GetElementAt(idx).QueryInterface(Ci.nsISupportsString);
    1.22 +    info("[" + idx + "] " + string);
    1.23 +  }
    1.24 +}
    1.25 +
    1.26 +function checkContextMenuPositionRange(aElement, aMinLeft, aMaxLeft, aMinTop, aMaxTop) {
    1.27 +  ok(aElement.left > aMinLeft && aElement.left < aMaxLeft,
    1.28 +    "Left position is " + aElement.left + ", expected between " + aMinLeft + " and " + aMaxLeft);
    1.29 +
    1.30 +  ok(aElement.top > aMinTop && aElement.top < aMaxTop,
    1.31 +    "Top position is " + aElement.top + ", expected between " + aMinTop + " and " + aMaxTop);
    1.32 +}
    1.33 +
    1.34 +gTests.push({
    1.35 +  desc: "text context menu",
    1.36 +  run: function test() {
    1.37 +    info(chromeRoot + "browser_context_menu_tests_02.html");
    1.38 +    yield addTab(chromeRoot + "browser_context_menu_tests_02.html");
    1.39 +
    1.40 +    purgeEventQueue();
    1.41 +    emptyClipboard();
    1.42 +
    1.43 +    let win = Browser.selectedTab.browser.contentWindow;
    1.44 +
    1.45 +    yield hideContextUI();
    1.46 +
    1.47 +    ////////////////////////////////////////////////////////////
    1.48 +    // Context menu in content on selected text
    1.49 +
    1.50 +    // select some text
    1.51 +    let span = win.document.getElementById("text1");
    1.52 +    win.getSelection().selectAllChildren(span);
    1.53 +
    1.54 +    yield waitForMs(0);
    1.55 +
    1.56 +    // invoke selection context menu
    1.57 +    let promise = waitForEvent(document, "popupshown");
    1.58 +    sendContextMenuClickToElement(win, span);
    1.59 +    yield promise;
    1.60 +
    1.61 +    // should be visible
    1.62 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
    1.63 +
    1.64 +    // selected text context:
    1.65 +    checkContextUIMenuItemVisibility(["context-copy",
    1.66 +                                      "context-search"]);
    1.67 +
    1.68 +    let menuItem = document.getElementById("context-copy");
    1.69 +    promise = waitForEvent(document, "popuphidden");
    1.70 +    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
    1.71 +
    1.72 +    yield promise;
    1.73 +
    1.74 +    // The wait is needed to give time to populate the clipboard.
    1.75 +    let string = "";
    1.76 +    yield waitForCondition(function () {
    1.77 +      string = SpecialPowers.getClipboardData("text/unicode");
    1.78 +      return string === span.textContent;
    1.79 +    });
    1.80 +    ok(string === span.textContent, "copied selected text from span");
    1.81 +
    1.82 +    win.getSelection().removeAllRanges();
    1.83 +
    1.84 +    ////////////////////////////////////////////////////////////
    1.85 +    // Context menu in content on selected text that includes a link
    1.86 +
    1.87 +    // invoke selection with link context menu
    1.88 +    let link = win.document.getElementById("text2-link");
    1.89 +    win.getSelection().selectAllChildren(link);
    1.90 +    promise = waitForEvent(document, "popupshown");
    1.91 +    sendContextMenuClickToElement(win, link);
    1.92 +    yield promise;
    1.93 +
    1.94 +    // should be visible
    1.95 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
    1.96 +
    1.97 +    // selected text context:
    1.98 +    checkContextUIMenuItemVisibility(["context-copy",
    1.99 +                                      "context-search",
   1.100 +                                      "context-open-in-new-tab",
   1.101 +                                      "context-copy-link"]);
   1.102 +
   1.103 +    promise = waitForEvent(document, "popuphidden");
   1.104 +    win.scrollBy(0, 1);
   1.105 +    let hidden = yield promise;
   1.106 +    ok(hidden && !(hidden instanceof Error), "scrolling hides the context menu");
   1.107 +    win.getSelection().removeAllRanges();
   1.108 +    win.scrollBy(0, -1);
   1.109 +
   1.110 +    ////////////////////////////////////////////////////////////
   1.111 +    // Context menu in content on a link
   1.112 +
   1.113 +    link = win.document.getElementById("text2-link");
   1.114 +    promise = waitForEvent(document, "popupshown");
   1.115 +    sendContextMenuClickToElement(win, link);
   1.116 +    yield promise;
   1.117 +
   1.118 +    // should be visible
   1.119 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.120 +
   1.121 +    // selected text context:
   1.122 +    checkContextUIMenuItemVisibility(["context-open-in-new-tab",
   1.123 +                                      "context-copy-link",
   1.124 +                                      "context-bookmark-link"]);
   1.125 +
   1.126 +    promise = waitForEvent(document, "popuphidden");
   1.127 +    ContextMenuUI.hide();
   1.128 +    yield promise;
   1.129 +
   1.130 +    ////////////////////////////////////////////////////////////
   1.131 +    // context in input with no selection, no data on clipboard
   1.132 +
   1.133 +    emptyClipboard();
   1.134 +
   1.135 +    let input = win.document.getElementById("text3-input");
   1.136 +    promise = waitForEvent(document, "popupshown");
   1.137 +    sendContextMenuClickToElement(win, input);
   1.138 +    yield promise;
   1.139 +
   1.140 +    // should be visible
   1.141 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.142 +
   1.143 +    checkContextUIMenuItemVisibility(["context-select",
   1.144 +                                      "context-select-all"]);
   1.145 +
   1.146 +    // copy menu item should not exist when no text is selected
   1.147 +    let menuItem = document.getElementById("context-copy");
   1.148 +    ok(menuItem && menuItem.hidden, "menu item is not visible");
   1.149 +
   1.150 +    promise = waitForEvent(document, "popuphidden");
   1.151 +    ContextMenuUI.hide();
   1.152 +    yield promise;
   1.153 +
   1.154 +    ////////////////////////////////////////////////////////////
   1.155 +    // context in input with selection copied to clipboard
   1.156 +
   1.157 +    let input = win.document.getElementById("text3-input");
   1.158 +    input.value = "hello, I'm sorry but I must be going.";
   1.159 +    input.setSelectionRange(0, 5);
   1.160 +    promise = waitForEvent(document, "popupshown");
   1.161 +    sendContextMenuClickToElement(win, input, 20);
   1.162 +    yield promise;
   1.163 +
   1.164 +    // should be visible
   1.165 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.166 +
   1.167 +    checkContextUIMenuItemVisibility(["context-cut",
   1.168 +                                      "context-copy"]);
   1.169 +
   1.170 +    let menuItem = document.getElementById("context-copy");
   1.171 +    let popupPromise = waitForEvent(document, "popuphidden");
   1.172 +    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
   1.173 +
   1.174 +    yield popupPromise;
   1.175 +
   1.176 +    // The wait is needed to give time to populate the clipboard.
   1.177 +    let string = "";
   1.178 +    yield waitForCondition(function () {
   1.179 +      string = SpecialPowers.getClipboardData("text/unicode");
   1.180 +      return string === "hello";
   1.181 +    });
   1.182 +
   1.183 +    ok(string === "hello", "copied selected text");
   1.184 +
   1.185 +    emptyClipboard();
   1.186 +
   1.187 +    ////////////////////////////////////////////////////////////
   1.188 +    // context in input with text selection, no data on clipboard
   1.189 +
   1.190 +    input = win.document.getElementById("text3-input");
   1.191 +    input.select();
   1.192 +    promise = waitForEvent(document, "popupshown");
   1.193 +    sendContextMenuClickToElement(win, input, 20);
   1.194 +    yield promise;
   1.195 +
   1.196 +    // should be visible
   1.197 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.198 +
   1.199 +    // selected text context:
   1.200 +    checkContextUIMenuItemVisibility(["context-cut",
   1.201 +                                      "context-copy"]);
   1.202 +
   1.203 +    promise = waitForEvent(document, "popuphidden");
   1.204 +    ContextMenuUI.hide();
   1.205 +    yield promise;
   1.206 +
   1.207 +    ////////////////////////////////////////////////////////////
   1.208 +    // context in input with no selection, data on clipboard
   1.209 +
   1.210 +    SpecialPowers.clipboardCopyString("foo");
   1.211 +    input = win.document.getElementById("text3-input");
   1.212 +    input.select();
   1.213 +    promise = waitForEvent(document, "popupshown");
   1.214 +    sendContextMenuClickToElement(win, input, 20);
   1.215 +    yield promise;
   1.216 +
   1.217 +    // should be visible
   1.218 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.219 +
   1.220 +    // selected text context:
   1.221 +    checkContextUIMenuItemVisibility(["context-cut",
   1.222 +                                      "context-copy",
   1.223 +                                      "context-paste"]);
   1.224 +
   1.225 +    promise = waitForEvent(document, "popuphidden");
   1.226 +    ContextMenuUI.hide();
   1.227 +    yield promise;
   1.228 +
   1.229 +    ////////////////////////////////////////////////////////////
   1.230 +    // context in input with selection cut to clipboard
   1.231 +
   1.232 +    emptyClipboard();
   1.233 +
   1.234 +    let input = win.document.getElementById("text3-input");
   1.235 +    input.value = "hello, I'm sorry but I must be going.";
   1.236 +    input.setSelectionRange(0, 5);
   1.237 +    promise = waitForEvent(document, "popupshown");
   1.238 +    sendContextMenuClickToElement(win, input, 20);
   1.239 +    yield promise;
   1.240 +
   1.241 +    // should be visible
   1.242 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.243 +
   1.244 +    checkContextUIMenuItemVisibility(["context-cut",
   1.245 +                                      "context-copy"]);
   1.246 +
   1.247 +    let menuItem = document.getElementById("context-cut");
   1.248 +    let popupPromise = waitForEvent(document, "popuphidden");
   1.249 +    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
   1.250 +
   1.251 +    yield popupPromise;
   1.252 +
   1.253 +    // The wait is needed to give time to populate the clipboard.
   1.254 +    let string = "";
   1.255 +    yield waitForCondition(function () {
   1.256 +      string = SpecialPowers.getClipboardData("text/unicode");
   1.257 +      return string === "hello";
   1.258 +    });
   1.259 +
   1.260 +    let inputValue = input.value;
   1.261 +    ok(string === "hello", "cut selected text in clipboard");
   1.262 +    ok(inputValue === ", I'm sorry but I must be going.", "cut selected text from input value");
   1.263 +
   1.264 +    emptyClipboard();
   1.265 +
   1.266 +    ////////////////////////////////////////////////////////////
   1.267 +    // context in empty input, data on clipboard (paste operation)
   1.268 +
   1.269 +    SpecialPowers.clipboardCopyString("foo");
   1.270 +    input = win.document.getElementById("text3-input");
   1.271 +    input.value = "";
   1.272 +
   1.273 +    promise = waitForEvent(document, "popupshown");
   1.274 +    sendContextMenuClickToElement(win, input, 20);
   1.275 +    yield promise;
   1.276 +
   1.277 +    // should be visible
   1.278 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.279 +
   1.280 +    // selected text context:
   1.281 +    checkContextUIMenuItemVisibility(["context-paste"]);
   1.282 +
   1.283 +    promise = waitForEvent(document, "popuphidden");
   1.284 +    ContextMenuUI.hide();
   1.285 +    yield promise;
   1.286 +
   1.287 +    ////////////////////////////////////////////////////////////
   1.288 +    // context in empty input, no data on clipboard (??)
   1.289 +
   1.290 +    emptyClipboard();
   1.291 +    ContextUI.dismiss();
   1.292 +
   1.293 +    input = win.document.getElementById("text3-input");
   1.294 +    input.value = "";
   1.295 +
   1.296 +    promise = waitForEvent(Elements.tray, "transitionend");
   1.297 +    sendContextMenuClickToElement(win, input, 20);
   1.298 +    yield promise;
   1.299 +
   1.300 +    // should *not* be visible
   1.301 +    ok(!ContextMenuUI._menuPopup.visible, "is visible");
   1.302 +
   1.303 +    // the test above will invoke the app bar
   1.304 +    yield hideContextUI();
   1.305 +
   1.306 +    Browser.closeTab(Browser.selectedTab, { forceClose: true });
   1.307 +    purgeEventQueue();
   1.308 +  }
   1.309 +});
   1.310 +
   1.311 +gTests.push({
   1.312 +  desc: "checks for context menu positioning when browser shifts",
   1.313 +  run: function test() {
   1.314 +    info(chromeRoot + "browser_context_menu_tests_02.html");
   1.315 +    yield addTab(chromeRoot + "browser_context_menu_tests_02.html");
   1.316 +
   1.317 +    purgeEventQueue();
   1.318 +    emptyClipboard();
   1.319 +
   1.320 +    let browserwin = Browser.selectedTab.browser.contentWindow;
   1.321 +
   1.322 +    yield hideContextUI();
   1.323 +
   1.324 +    ////////////////////////////////////////////////////////////
   1.325 +    // test for proper context menu positioning when the browser
   1.326 +    // is offset by a notification box.
   1.327 +
   1.328 +    yield showNotification();
   1.329 +
   1.330 +    // select some text
   1.331 +    let span = browserwin.document.getElementById("text4");
   1.332 +    browserwin.getSelection().selectAllChildren(span);
   1.333 +
   1.334 +    // invoke selection context menu
   1.335 +    let promise = waitForEvent(document, "popupshown");
   1.336 +    sendContextMenuClickToElement(browserwin, span);
   1.337 +    yield promise;
   1.338 +
   1.339 +    // should be visible and at a specific position
   1.340 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.341 +
   1.342 +    let notificationBox = Browser.getNotificationBox();
   1.343 +    let notification = notificationBox.getNotificationWithValue("popup-blocked");
   1.344 +    let notificationHeight = notification.boxObject.height;
   1.345 +
   1.346 +    checkContextMenuPositionRange(ContextMenuUI._panel, 0, 15, 220, 230);
   1.347 +
   1.348 +    promise = waitForEvent(document, "popuphidden");
   1.349 +    ContextMenuUI.hide();
   1.350 +    yield promise;
   1.351 +
   1.352 +    Browser.closeTab(Browser.selectedTab, { forceClose: true });
   1.353 +  }
   1.354 +});
   1.355 +
   1.356 +/*
   1.357 +XXX code used to diagnose bug 880739
   1.358 +
   1.359 +var observeLogger = {
   1.360 +  observe: function (aSubject, aTopic, aData) {
   1.361 +    info("observeLogger: " + aTopic);
   1.362 +  },
   1.363 +  QueryInterface: function (aIID) {
   1.364 +    if (!aIID.equals(Ci.nsIObserver) &&
   1.365 +        !aIID.equals(Ci.nsISupportsWeakReference) &&
   1.366 +        !aIID.equals(Ci.nsISupports)) {
   1.367 +      throw Components.results.NS_ERROR_NO_INTERFACE;
   1.368 +    }
   1.369 +    return this;
   1.370 +  },
   1.371 +  init: function init() {
   1.372 +    Services.obs.addObserver(observeLogger, "dl-start", true);
   1.373 +    Services.obs.addObserver(observeLogger, "dl-done", true);
   1.374 +    Services.obs.addObserver(observeLogger, "dl-failed", true);
   1.375 +    Services.obs.addObserver(observeLogger, "dl-scanning", true);
   1.376 +    Services.obs.addObserver(observeLogger, "dl-blocked", true);
   1.377 +    Services.obs.addObserver(observeLogger, "dl-dirty", true);
   1.378 +    Services.obs.addObserver(observeLogger, "dl-cancel", true);
   1.379 +  },
   1.380 +  shutdown: function shutdown() {
   1.381 +    Services.obs.removeObserver(observeLogger, "dl-start");
   1.382 +    Services.obs.removeObserver(observeLogger, "dl-done");
   1.383 +    Services.obs.removeObserver(observeLogger, "dl-failed");
   1.384 +    Services.obs.removeObserver(observeLogger, "dl-scanning");
   1.385 +    Services.obs.removeObserver(observeLogger, "dl-blocked");
   1.386 +    Services.obs.removeObserver(observeLogger, "dl-dirty");
   1.387 +    Services.obs.removeObserver(observeLogger, "dl-cancel");
   1.388 +  }
   1.389 +}
   1.390 +*/
   1.391 +
   1.392 +// Image context menu tests
   1.393 +gTests.push({
   1.394 +  desc: "image context menu",
   1.395 +  setUp: function() {
   1.396 +    // XXX code used to diagnose bug 880739
   1.397 +    //observeLogger.init();
   1.398 +  },
   1.399 +  tearDown: function() {
   1.400 +    // XXX code used to diagnose bug 880739
   1.401 +    //observeLogger.shutdown();
   1.402 +  },
   1.403 +  run: function test() {
   1.404 +    info(chromeRoot + "browser_context_menu_tests_01.html");
   1.405 +    yield addTab(chromeRoot + "browser_context_menu_tests_01.html");
   1.406 +
   1.407 +    let win = Browser.selectedTab.browser.contentWindow;
   1.408 +
   1.409 +    purgeEventQueue();
   1.410 +
   1.411 +    yield hideContextUI();
   1.412 +
   1.413 +    // If we don't do this, sometimes the first sendContextMenuClickToWindow
   1.414 +    // will trigger the app bar.
   1.415 +    yield waitForImageLoad(win, "image01");
   1.416 +
   1.417 +    ////////////////////////////////////////////////////////////
   1.418 +    // Context menu options
   1.419 +    /*
   1.420 +    XXX disabled temporarily due to bug 880739
   1.421 +
   1.422 +    // image01 - 1x1x100x100
   1.423 +    let promise = waitForEvent(document, "popupshown");
   1.424 +    sendContextMenuClickToWindow(win, 10, 10);
   1.425 +    yield promise;
   1.426 +
   1.427 +    purgeEventQueue();
   1.428 +
   1.429 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.430 +
   1.431 +    checkContextUIMenuItemVisibility(["context-save-image-lib",
   1.432 +                                      "context-copy-image",
   1.433 +                                      "context-copy-image-loc",
   1.434 +                                      "context-open-image-tab"]);
   1.435 +
   1.436 +    ////////////////////////////////////////////////////////////
   1.437 +    // Save to image library
   1.438 +
   1.439 +    let dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
   1.440 +                           .getService(Components.interfaces.nsIProperties);
   1.441 +    let saveLocationPath = dirSvc.get("Pict", Components.interfaces.nsIFile);
   1.442 +    saveLocationPath.append("image01.png");
   1.443 +
   1.444 +    registerCleanupFunction(function () {
   1.445 +      saveLocationPath.remove(false);
   1.446 +    });
   1.447 +
   1.448 +    if (saveLocationPath.exists()) {
   1.449 +      info("had to remove old image!");
   1.450 +      saveLocationPath.remove(false);
   1.451 +    }
   1.452 +
   1.453 +    let menuItem = document.getElementById("context-save-image-lib");
   1.454 +    ok(menuItem, "menu item exists");
   1.455 +    ok(!menuItem.hidden, "menu item visible");
   1.456 +
   1.457 +    // dl-start, dl-failed, dl-scanning, dl-blocked, dl-dirty, dl-cancel
   1.458 +    let downloadPromise = waitForObserver("dl-done", 10000);
   1.459 +
   1.460 +    let popupPromise = waitForEvent(document, "popuphidden");
   1.461 +    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
   1.462 +    yield popupPromise;
   1.463 +    yield downloadPromise;
   1.464 +
   1.465 +    purgeEventQueue();
   1.466 +
   1.467 +    ok(saveLocationPath.exists(), "image saved");
   1.468 +    */
   1.469 +    ////////////////////////////////////////////////////////////
   1.470 +    // Copy image
   1.471 +
   1.472 +    let promise = waitForEvent(document, "popupshown");
   1.473 +    sendContextMenuClickToWindow(win, 20, 20);
   1.474 +    yield promise;
   1.475 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.476 +
   1.477 +    let menuItem = document.getElementById("context-copy-image");
   1.478 +    ok(menuItem, "menu item exists");
   1.479 +    ok(!menuItem.hidden, "menu item visible");
   1.480 +    let popupPromise = waitForEvent(document, "popuphidden");
   1.481 +    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
   1.482 +    yield popupPromise;
   1.483 +
   1.484 +    purgeEventQueue();
   1.485 +
   1.486 +    let clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
   1.487 +    let flavors = ["image/png"];
   1.488 +    ok(clip.hasDataMatchingFlavors(flavors, flavors.length, Ci.nsIClipboard.kGlobalClipboard), "clip has my png flavor");
   1.489 +
   1.490 +    ////////////////////////////////////////////////////////////
   1.491 +    // Copy image location
   1.492 +
   1.493 +    promise = waitForEvent(document, "popupshown");
   1.494 +    sendContextMenuClickToWindow(win, 30, 30);
   1.495 +    yield promise;
   1.496 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.497 +
   1.498 +    menuItem = document.getElementById("context-copy-image-loc");
   1.499 +    ok(menuItem, "menu item exists");
   1.500 +    ok(!menuItem.hidden, "menu item visible");
   1.501 +    popupPromise = waitForEvent(document, "popuphidden");
   1.502 +    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
   1.503 +    yield popupPromise;
   1.504 +
   1.505 +    purgeEventQueue();
   1.506 +
   1.507 +    let clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
   1.508 +    let flavors = ["text/unicode"];
   1.509 +    ok(clip.hasDataMatchingFlavors(flavors, flavors.length, Ci.nsIClipboard.kGlobalClipboard), "clip has my text flavor");
   1.510 +
   1.511 +    let xfer = Cc["@mozilla.org/widget/transferable;1"].
   1.512 +               createInstance(Ci.nsITransferable);
   1.513 +    xfer.init(null);
   1.514 +    xfer.addDataFlavor("text/unicode");
   1.515 +    clip.getData(xfer, Ci.nsIClipboard.kGlobalClipboard);
   1.516 +    let str = new Object();
   1.517 +    let strLength = new Object();
   1.518 +    xfer.getTransferData("text/unicode", str, strLength);
   1.519 +    str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
   1.520 +
   1.521 +    ok(str == chromeRoot + "res/image01.png", "url copied");
   1.522 +
   1.523 +    ////////////////////////////////////////////////////////////
   1.524 +    // Open image in new tab
   1.525 +
   1.526 +    promise = waitForEvent(document, "popupshown");
   1.527 +    sendContextMenuClickToWindow(win, 40, 40);
   1.528 +    yield promise;
   1.529 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.530 +
   1.531 +    menuItem = document.getElementById("context-open-image-tab");
   1.532 +    ok(menuItem, "menu item exists");
   1.533 +    ok(!menuItem.hidden, "menu item visible");
   1.534 +    let tabPromise = waitForEvent(document, "TabOpen");
   1.535 +    popupPromise = waitForEvent(document, "popuphidden");
   1.536 +    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
   1.537 +    yield popupPromise;
   1.538 +    let event = yield tabPromise;
   1.539 +
   1.540 +    purgeEventQueue();
   1.541 +
   1.542 +    let imagetab = Browser.getTabFromChrome(event.originalTarget);
   1.543 +    ok(imagetab != null, "tab created");
   1.544 +
   1.545 +    Browser.closeTab(imagetab, { forceClose: true });
   1.546 +  }
   1.547 +});
   1.548 +
   1.549 +gTests.push({
   1.550 +  desc: "tests for subframe positioning",
   1.551 +  run: function test() {
   1.552 +    info(chromeRoot + "browser_context_menu_tests_03.html");
   1.553 +    yield addTab(chromeRoot + "browser_context_menu_tests_03.html");
   1.554 +
   1.555 +    let win = Browser.selectedTab.browser.contentWindow;
   1.556 +
   1.557 +    // Sometimes the context ui is visible, sometimes it isn't.
   1.558 +    try {
   1.559 +      yield waitForCondition(function () {
   1.560 +        return ContextUI.isVisible;
   1.561 +      }, 500, 50);
   1.562 +    } catch (ex) {}
   1.563 +
   1.564 +    ContextUI.dismiss();
   1.565 +
   1.566 +    let frame1 = win.document.getElementById("frame1");
   1.567 +    let link1 = frame1.contentDocument.getElementById("link1");
   1.568 +
   1.569 +    let promise = waitForEvent(document, "popupshown");
   1.570 +    sendContextMenuClickToElement(frame1.contentDocument.defaultView, link1, 85, 10);
   1.571 +    yield promise;
   1.572 +
   1.573 +    // should be visible
   1.574 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.575 +
   1.576 +    checkContextMenuPositionRange(ContextMenuUI._panel, 265, 280, 175, 190);
   1.577 +
   1.578 +    promise = waitForEvent(document, "popuphidden");
   1.579 +    ContextMenuUI.hide();
   1.580 +    yield promise;
   1.581 +
   1.582 +    frame1.contentDocument.defaultView.scrollBy(0, 200);
   1.583 +
   1.584 +    promise = waitForEvent(document, "popupshown");
   1.585 +    sendContextMenuClickToElement(frame1.contentDocument.defaultView, link1, 85, 10);
   1.586 +    yield promise;
   1.587 +
   1.588 +    // should be visible
   1.589 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.590 +
   1.591 +    checkContextMenuPositionRange(ContextMenuUI._panel, 265, 280, 95, 110);
   1.592 +
   1.593 +    promise = waitForEvent(document, "popuphidden");
   1.594 +    ContextMenuUI.hide();
   1.595 +    yield promise;
   1.596 +
   1.597 +    let rlink1 = win.document.getElementById("rlink1");
   1.598 +
   1.599 +    promise = waitForEvent(document, "popupshown");
   1.600 +    sendContextMenuClickToElement(win, rlink1, 40, 10);
   1.601 +    yield promise;
   1.602 +
   1.603 +    // should be visible
   1.604 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.605 +
   1.606 +    checkContextMenuPositionRange(ContextMenuUI._panel, 295, 310, 540, 555);
   1.607 +
   1.608 +    promise = waitForEvent(document, "popuphidden");
   1.609 +    ContextMenuUI.hide();
   1.610 +    yield promise;
   1.611 +
   1.612 +    win.scrollBy(0, 200);
   1.613 +
   1.614 +    promise = waitForEvent(document, "popupshown");
   1.615 +    sendContextMenuClickToElement(win, rlink1, 40, 10);
   1.616 +    yield promise;
   1.617 +
   1.618 +    // should be visible
   1.619 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.620 +
   1.621 +    checkContextMenuPositionRange(ContextMenuUI._panel, 295, 310, 340, 355);
   1.622 +
   1.623 +    promise = waitForEvent(document, "popuphidden");
   1.624 +    ContextMenuUI.hide();
   1.625 +    yield promise;
   1.626 +
   1.627 +    let link2 = frame1.contentDocument.getElementById("link2");
   1.628 +
   1.629 +    promise = waitForEvent(document, "popupshown");
   1.630 +    sendContextMenuClickToElement(frame1.contentDocument.defaultView, link2, 85, 10);
   1.631 +    yield promise;
   1.632 +
   1.633 +    // should be visible
   1.634 +    ok(ContextMenuUI._menuPopup.visible, "is visible");
   1.635 +
   1.636 +    checkContextMenuPositionRange(ContextMenuUI._panel, 265, 280, 110, 125);
   1.637 +
   1.638 +    promise = waitForEvent(document, "popuphidden");
   1.639 +    ContextMenuUI.hide();
   1.640 +    yield promise;
   1.641 +
   1.642 +    Browser.closeTab(Browser.selectedTab, { forceClose: true });
   1.643 +  }
   1.644 +});
   1.645 +
   1.646 +function reopenSetUp() {
   1.647 +  info(chromeRoot + "browser_context_menu_tests_04.html");
   1.648 +  yield addTab(chromeRoot + "browser_context_menu_tests_04.html");
   1.649 +
   1.650 +  // Sometimes the context UI won't actually show up.
   1.651 +  // Since we're just normalizing, we don't want waitForCondition
   1.652 +  // to cause an orange, so we're putting a try/catch here.
   1.653 +  try {
   1.654 +    yield waitForCondition(() => ContextUI.isVisible);
   1.655 +    ContextUI.dismiss();
   1.656 +  } catch(e) {}
   1.657 +}
   1.658 +
   1.659 +function reopenTearDown() {
   1.660 +  let promise = waitForEvent(document, "popuphidden")
   1.661 +  ContextMenuUI.hide();
   1.662 +  yield promise;
   1.663 +  ok(!ContextMenuUI._menuPopup.visible, "popup is actually hidden");
   1.664 +
   1.665 +  Browser.closeTab(Browser.selectedTab, { forceClose: true });
   1.666 +}
   1.667 +
   1.668 +function getReopenTest(aElementInputFn, aWindowInputFn) {
   1.669 +  return function () {
   1.670 +    let win = Browser.selectedTab.browser.contentWindow;
   1.671 +    let panel = ContextMenuUI._menuPopup._panel;
   1.672 +
   1.673 +    let link1 = win.document.getElementById("text1-link");
   1.674 +    let link2 = win.document.getElementById("text2-link");
   1.675 +
   1.676 +    // Show the menu on link 1
   1.677 +    let showpromise = waitForEvent(panel, "popupshown");
   1.678 +    aElementInputFn(win, link1);
   1.679 +
   1.680 +    ok((yield showpromise), "popupshown event fired");
   1.681 +    ok(ContextMenuUI._menuPopup.visible, "initial popup is visible");
   1.682 +
   1.683 +    // Show the menu on link 2
   1.684 +    let hidepromise = waitForEvent(panel, "popuphidden");
   1.685 +    showpromise = waitForEvent(panel, "popupshown");
   1.686 +    aElementInputFn(win, link2);
   1.687 +
   1.688 +    ok((yield hidepromise), "popuphidden event fired");
   1.689 +    ok((yield showpromise), "popupshown event fired");
   1.690 +    ok(ContextMenuUI._menuPopup.visible, "popup is still visible");
   1.691 +
   1.692 +    // Hide the menu
   1.693 +    hidepromise = waitForEvent(panel, "popuphidden")
   1.694 +    aWindowInputFn(win, 10, 10);
   1.695 +
   1.696 +    ok((yield hidepromise), "popuphidden event fired");
   1.697 +    ok(!ContextMenuUI._menuPopup.visible, "popup is no longer visible");
   1.698 +  }
   1.699 +}
   1.700 +
   1.701 +gTests.push({
   1.702 +  desc: "bug 856264 - mouse - context menu should reopen on other links",
   1.703 +  setUp: reopenSetUp,
   1.704 +  tearDown: reopenTearDown,
   1.705 +  run: getReopenTest(sendContextMenuMouseClickToElement, sendMouseClick)
   1.706 +});
   1.707 +
   1.708 +gTests.push({
   1.709 +  desc: "bug 856264 - touch - context menu should reopen on other links",
   1.710 +  setUp: reopenSetUp,
   1.711 +  tearDown: reopenTearDown,
   1.712 +  run: getReopenTest(sendContextMenuClickToElement, sendTap)
   1.713 +});
   1.714 +
   1.715 +gTests.push({
   1.716 +  desc: "Bug 947505 - Right-click in a designMode document should display a " +
   1.717 +        "context menu",
   1.718 +  run: function test() {
   1.719 +    info(chromeRoot + "browser_context_menu_tests_02.html");
   1.720 +    yield addTab(chromeRoot + "browser_context_menu_tests_02.html");
   1.721 +
   1.722 +    purgeEventQueue();
   1.723 +    emptyClipboard();
   1.724 +    ContextUI.dismiss();
   1.725 +
   1.726 +    yield waitForCondition(() => !ContextUI.navbarVisible);
   1.727 +
   1.728 +    let tabWindow = Browser.selectedTab.browser.contentWindow;
   1.729 +    let testSpan = tabWindow.document.getElementById("text1");
   1.730 +
   1.731 +    // Case #1: Document isn't in design mode and nothing is selected.
   1.732 +    tabWindow.document.designMode = "off";
   1.733 +
   1.734 +    // Simulate right mouse click to reproduce the same step as noted in the
   1.735 +    // appropriate bug. It's valid for non-touch case only.
   1.736 +    synthesizeNativeMouseRDown(Browser.selectedTab.browser, 10, 10);
   1.737 +    synthesizeNativeMouseRUp(Browser.selectedTab.browser, 10, 10);
   1.738 +
   1.739 +    yield waitForCondition(() => ContextUI.navbarVisible);
   1.740 +
   1.741 +    ok(ContextUI.navbarVisible, "Navbar is visible on context menu action.");
   1.742 +    ok(ContextUI.tabbarVisible, "Tabbar is visible on context menu action.");
   1.743 +
   1.744 +    ContextUI.dismiss();
   1.745 +    yield waitForCondition(() => !ContextUI.navbarVisible);
   1.746 +
   1.747 +    // Case #2: Document isn't in design mode and text is selected.
   1.748 +    tabWindow.getSelection().selectAllChildren(testSpan);
   1.749 +
   1.750 +    let promise = waitForEvent(document, "popupshown");
   1.751 +    sendContextMenuClickToSelection(tabWindow);
   1.752 +    yield promise;
   1.753 +
   1.754 +    checkContextUIMenuItemVisibility(["context-copy", "context-search"]);
   1.755 +
   1.756 +    promise = waitForEvent(document, "popuphidden");
   1.757 +    ContextMenuUI.hide();
   1.758 +    yield promise;
   1.759 +
   1.760 +    // Case #3: Document is in design mode and nothing is selected.
   1.761 +    tabWindow.document.designMode = "on";
   1.762 +    tabWindow.getSelection().removeAllRanges();
   1.763 +
   1.764 +    promise = waitForEvent(document, "popupshown");
   1.765 +    sendContextMenuClickToElement(tabWindow, testSpan);
   1.766 +    yield promise;
   1.767 +
   1.768 +    checkContextUIMenuItemVisibility(["context-select-all", "context-select"]);
   1.769 +
   1.770 +    promise = waitForEvent(document, "popuphidden");
   1.771 +    ContextMenuUI.hide();
   1.772 +    yield promise;
   1.773 +
   1.774 +    // Case #4: Document is in design mode and text is selected.
   1.775 +    tabWindow.getSelection().selectAllChildren(testSpan);
   1.776 +
   1.777 +    promise = waitForEvent(document, "popupshown");
   1.778 +    sendContextMenuClickToSelection(tabWindow);
   1.779 +    yield promise;
   1.780 +
   1.781 +    checkContextUIMenuItemVisibility(["context-cut", "context-copy",
   1.782 +                                      "context-select-all", "context-select",
   1.783 +                                      "context-search"]);
   1.784 +
   1.785 +    promise = waitForEvent(document, "popuphidden");
   1.786 +    ContextMenuUI.hide();
   1.787 +    yield promise;
   1.788 +
   1.789 +    Browser.closeTab(Browser.selectedTab, { forceClose: true });
   1.790 +  }
   1.791 +});
   1.792 +
   1.793 +gTests.push({
   1.794 +  desc: "Bug 961702 - 'Copy' context menu action does not copy rich content " +
   1.795 +        "while document in design mode (or inside container that allows to " +
   1.796 +        "edit its content)",
   1.797 +  run: function test() {
   1.798 +    info(chromeRoot + "browser_context_menu_tests_05.html");
   1.799 +    yield addTab(chromeRoot + "browser_context_menu_tests_05.html");
   1.800 +
   1.801 +    purgeEventQueue();
   1.802 +    emptyClipboard();
   1.803 +    ContextUI.dismiss();
   1.804 +
   1.805 +    yield waitForCondition(() => !ContextUI.navbarVisible);
   1.806 +
   1.807 +    let tabWindow = Browser.selectedTab.browser.contentWindow;
   1.808 +    let testDiv = tabWindow.document.getElementById("div1");
   1.809 +
   1.810 +    // Case #1: Document is in design mode.
   1.811 +    tabWindow.document.designMode = "on";
   1.812 +
   1.813 +    let promise = waitForEvent(document, "popupshown");
   1.814 +    sendContextMenuClickToElement(tabWindow, testDiv);
   1.815 +    yield promise;
   1.816 +
   1.817 +    let selectAllMenuItem = document.getElementById("context-select-all");
   1.818 +    promise = waitForEvent(document, "popuphidden");
   1.819 +    sendNativeTap(selectAllMenuItem);
   1.820 +    yield promise;
   1.821 +
   1.822 +    promise = waitForEvent(document, "popupshown");
   1.823 +    sendContextMenuClickToSelection(tabWindow);
   1.824 +    yield promise;
   1.825 +
   1.826 +    let copyMenuItem = document.getElementById("context-copy");
   1.827 +    promise = waitForEvent(document, "popuphidden");
   1.828 +    sendNativeTap(copyMenuItem);
   1.829 +    yield promise;
   1.830 +
   1.831 +    // The wait is needed to give time to populate the clipboard.
   1.832 +    let clipboardContent = "";
   1.833 +    let contentToCopy = tabWindow.document.body.innerHTML;
   1.834 +    yield waitForCondition(function () {
   1.835 +      clipboardContent = SpecialPowers.getClipboardData("text/html");
   1.836 +      return clipboardContent == contentToCopy;
   1.837 +    });
   1.838 +    ok(clipboardContent == contentToCopy, "Rich content copied.");
   1.839 +
   1.840 +    // Case #2: Container with editable content.
   1.841 +    emptyClipboard();
   1.842 +    tabWindow.document.designMode = "off";
   1.843 +    tabWindow.getSelection().removeAllRanges();
   1.844 +
   1.845 +    promise = waitForEvent(tabWindow.document.body, "focus");
   1.846 +    sendNativeTap(testDiv);
   1.847 +    yield promise;
   1.848 +
   1.849 +    promise = waitForEvent(document, "popupshown");
   1.850 +    sendContextMenuClickToElement(tabWindow, testDiv);
   1.851 +    yield promise;
   1.852 +
   1.853 +    selectAllMenuItem = document.getElementById("context-select-all");
   1.854 +    promise = waitForEvent(document, "popuphidden");
   1.855 +    sendNativeTap(selectAllMenuItem);
   1.856 +    yield promise;
   1.857 +
   1.858 +    promise = waitForEvent(document, "popupshown");
   1.859 +    sendContextMenuClickToSelection(tabWindow);
   1.860 +    yield promise;
   1.861 +
   1.862 +    copyMenuItem = document.getElementById("context-copy");
   1.863 +    promise = waitForEvent(document, "popuphidden");
   1.864 +    sendNativeTap(copyMenuItem);
   1.865 +    yield promise;
   1.866 +
   1.867 +     // The wait is needed to give time to populate the clipboard.
   1.868 +    clipboardContent = "";
   1.869 +    contentToCopy = testDiv.innerHTML;
   1.870 +    yield waitForCondition(function () {
   1.871 +      clipboardContent = SpecialPowers.getClipboardData("text/html");
   1.872 +      return clipboardContent == contentToCopy;
   1.873 +    });
   1.874 +    ok(clipboardContent == contentToCopy, "Rich content copied.");
   1.875 +
   1.876 +    Browser.closeTab(Browser.selectedTab, { forceClose: true });
   1.877 +  }
   1.878 +});
   1.879 +
   1.880 +gTests.push({
   1.881 +  desc: "Bug 963067 - 'Cut' in the cut, copy, paste menu is always active " +
   1.882 +        "after a browser launch.",
   1.883 +  run: function test() {
   1.884 +    info(chromeRoot + "browser_context_menu_tests_02.html");
   1.885 +    yield addTab(chromeRoot + "browser_context_menu_tests_02.html");
   1.886 +
   1.887 +    purgeEventQueue();
   1.888 +    emptyClipboard();
   1.889 +
   1.890 +    ContextUI.dismiss();
   1.891 +    yield waitForCondition(() => !ContextUI.navbarVisible);
   1.892 +
   1.893 +    let tabWindow = Browser.selectedTab.browser.contentWindow;
   1.894 +    let input = tabWindow.document.getElementById("text3-input");
   1.895 +    let cutMenuItem = document.getElementById("context-cut");
   1.896 +
   1.897 +    input.select();
   1.898 +
   1.899 +    // Emulate RichListBox's behavior and set first item selected by default.
   1.900 +    cutMenuItem.selected = true;
   1.901 +
   1.902 +    let promise = waitForEvent(document, "popupshown");
   1.903 +    sendContextMenuClickToElement(tabWindow, input);
   1.904 +    yield promise;
   1.905 +
   1.906 +    ok(!cutMenuItem.hidden && !cutMenuItem.selected,
   1.907 +       "Cut menu item is visible and not selected.");
   1.908 +
   1.909 +    promise = waitForEvent(document, "popuphidden");
   1.910 +    ContextMenuUI.hide();
   1.911 +    yield promise;
   1.912 +
   1.913 +    Browser.closeTab(Browser.selectedTab, { forceClose: true });
   1.914 +  }
   1.915 +});
   1.916 +
   1.917 +gTests.push({
   1.918 +  desc: "Bug 867499 - Selecting 'copy' from context menu for selected text " +
   1.919 +        "dismisses selection.",
   1.920 +  run: function test() {
   1.921 +    info(chromeRoot + "browser_context_menu_tests_02.html");
   1.922 +    yield addTab(chromeRoot + "browser_context_menu_tests_02.html");
   1.923 +
   1.924 +    emptyClipboard();
   1.925 +    ContextUI.dismiss();
   1.926 +
   1.927 +    yield waitForCondition(() => !ContextUI.navbarVisible);
   1.928 +
   1.929 +    let tabWindow = Browser.selectedTab.browser.contentWindow;
   1.930 +    let testSpan = tabWindow.document.getElementById("text1");
   1.931 +
   1.932 +    let promise = waitForEvent(document, "popupshown");
   1.933 +    sendContextMenuClickToElement(tabWindow, testSpan, 5, 5);
   1.934 +    yield promise;
   1.935 +
   1.936 +    yield waitForCondition(()=>SelectionHelperUI.isSelectionUIVisible);
   1.937 +
   1.938 +    promise = waitForEvent(document, "popupshown");
   1.939 +    sendContextMenuClickToSelection(tabWindow);
   1.940 +    yield promise;
   1.941 +
   1.942 +    let copyMenuItem = document.getElementById("context-copy");
   1.943 +
   1.944 +    ok(!copyMenuItem.hidden, "Copy menu item should be visible.");
   1.945 +
   1.946 +    promise = waitForEvent(document, "popuphidden");
   1.947 +    sendNativeTap(copyMenuItem, 5, 5);
   1.948 +    yield promise;
   1.949 +    yield waitForCondition(() =>
   1.950 +        !!SpecialPowers.getClipboardData("text/unicode"));
   1.951 +
   1.952 +    ok(SelectionHelperUI.isSelectionUIVisible,
   1.953 +        "Selection monocles should stay active after copy action.");
   1.954 +
   1.955 +    Browser.closeTab(Browser.selectedTab, { forceClose: true });
   1.956 +  }
   1.957 +});
   1.958 +
   1.959 +function test() {
   1.960 +  setDevPixelEqualToPx();
   1.961 +  runTests();
   1.962 +}

mercurial