<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>

<!-- the condition in the focus event handler is because pressing Tab
     unfocuses and refocuses the window on Windows -->

<window title="Popup Tests"
  xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <title>Popup Tests</title>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>      
  <script type="application/javascript" src="popup_shared.js"></script>      

<!--
  Need to investigate these tests a bit more. Some of the accessibility events
  are firing multiple times or in different orders in different circumstances.
  Note that this was also the case before bug 279703.
  -->

<hbox style="margin-left: 275px; margin-top: 275px;">
<menubar id="menubar">
  <menu id="filemenu" label="File" accesskey="F">
    <menupopup id="filepopup">
      <menuitem id="item1" label="Open" accesskey="O"/>
      <menuitem id="item2" label="Save" accesskey="S"/>
      <menuitem id="item3" label="Close" accesskey="C"/>
    </menupopup>
  </menu>
  <menu id="secretmenu" label="Secret Menu" accesskey="S" disabled="true">
    <menupopup>
      <menuitem label="Secret Command" accesskey="S"/>
    </menupopup>
  </menu>
  <menu id="editmenu" label="Edit" accesskey="E">
    <menupopup id="editpopup">
      <menuitem id="cut" label="Cut" accesskey="t" disabled="true"/>
      <menuitem id="copy" label="Copy" accesskey="C"/>
      <menuitem id="paste" label="Paste" accesskey="P"/>
    </menupopup>
  </menu>
  <menu id="viewmenu" label="View" accesskey="V">
    <menupopup id="viewpopup">
      <menu id="toolbar" label="Toolbar" accesskey="T">
        <menupopup id="toolbarpopup">
          <menuitem id="navigation" label="Navigation" accesskey="N" disabled="true"/>
          <menuitem label="Bookmarks" accesskey="B" disabled="true"/>
        </menupopup>
      </menu>
      <menuitem label="Status Bar" accesskey="S"/>
      <menu label="Sidebar" accesskey="d">
        <menupopup>
          <menuitem label="Bookmarks" accesskey="B"/>
          <menuitem label="History" accesskey="H"/>
        </menupopup>
      </menu>
    </menupopup>
  </menu>
  <menu id="helpmenu" label="Help" accesskey="H">
    <menupopup id="helppopup" >
      <label value="Unselectable"/>
      <menuitem id="contents" label="Contents" accesskey="C"/>
      <menuitem label="More Info" accesskey="I"/>
      <menuitem id="amenu" label="A Menu" accesskey="M"/>
      <menuitem label="Another Menu"/>
      <menuitem id="one" label="One"/>
      <menu id="only" label="Only Menu">
        <menupopup>
          <menuitem label="Test Submenu"/>
        </menupopup>
      </menu>
      <menuitem label="Second Menu"/>
      <menuitem id="other" disabled="true" label="Other Menu"/>
      <menuitem id="third" label="Third Menu"/>
      <menuitem label="One Other Menu"/>
      <label value="Unselectable"/>
      <menuitem id="about" label="About" accesskey="A"/>
    </menupopup>
  </menu>
</menubar>
</hbox>

<script class="testbody" type="application/javascript">
<![CDATA[

window.opener.SimpleTest.waitForFocus(function () {
  gFilePopup = document.getElementById("filepopup");
  document.getElementById("filemenu").focus();
  startPopupTests(popupTests);
}, window);

var popupTests = [
{
  testname: "press on menu",
  events: [ "popupshowing filepopup", "DOMMenuBarActive menubar",
            "DOMMenuItemActive filemenu", "popupshown filepopup" ],
  test: function() { synthesizeMouse(document.getElementById("filemenu"), 8, 8, { }); },
  result: function (testname) {
    checkActive(gFilePopup, "", testname);
    checkOpen("filemenu", testname);
  }
},
{
  // check that pressing cursor down while there is no selection
  // highlights the first item
  testname: "cursor down no selection",
  events: [ "DOMMenuItemActive item1" ],
  test: function() { synthesizeKey("VK_DOWN", { }); },
  result: function(testname) { checkActive(gFilePopup, "item1", testname); }
},
{
  // check that pressing cursor up wraps and highlights the last item
  testname: "cursor up wrap",
  events: [ "DOMMenuItemInactive item1", "DOMMenuItemActive item3" ],
  test: function() { synthesizeKey("VK_UP", { }); },
  result: function(testname) { checkActive(gFilePopup, "item3", testname); }
},
{
  // check that pressing cursor down wraps and highlights the first item
  testname: "cursor down wrap",
  events: [ "DOMMenuItemInactive item3", "DOMMenuItemActive item1" ],
  test: function() { synthesizeKey("VK_DOWN", { }); },
  result: function(testname) { checkActive(gFilePopup, "item1", testname); }
},
{
  // check that pressing cursor down highlights the second item
  testname: "cursor down",
  events: [ "DOMMenuItemInactive item1", "DOMMenuItemActive item2" ],
  test: function() { synthesizeKey("VK_DOWN", { }); },
  result: function(testname) { checkActive(gFilePopup, "item2", testname); }
},
{
  // check that pressing cursor up highlights the second item
  testname: "cursor up",
  events: [ "DOMMenuItemInactive item2", "DOMMenuItemActive item1" ],
  test: function() { synthesizeKey("VK_UP", { }); },
  result: function(testname) { checkActive(gFilePopup, "item1", testname); }
},

{
  // cursor right should skip the disabled menu and move to the edit menu
  testname: "cursor right skip disabled",
  events: function() {
    var elist = [
      // the file menu gets deactivated, the file menu gets hidden, then
      // the edit menu is activated
      "DOMMenuItemInactive filemenu", "DOMMenuItemActive editmenu",
      "popuphiding filepopup", "popuphidden filepopup",
      // the popupshowing event gets fired when showing the edit menu.
      // The item from the file menu doesn't get deactivated until the
      // next item needs to be selected
      "popupshowing editpopup", "DOMMenuItemInactive item1",
      // not sure why the menu inactivated event is firing so late
      "DOMMenuInactive filepopup"
    ];
    // finally, the first item is activated and popupshown is fired.
    // On Windows, don't skip disabled items.
    if (navigator.platform.indexOf("Win") == 0)
      elist.push("DOMMenuItemActive cut");
    else
      elist.push("DOMMenuItemActive copy");
    elist.push("popupshown editpopup");
    return elist;
  },
  test: function() { synthesizeKey("VK_RIGHT", { }); },
  result: function(testname) {
    var expected = (navigator.platform.indexOf("Win") == 0) ? "cut" : "copy";
    checkActive(document.getElementById("editpopup"), expected, testname);
    checkClosed("filemenu", testname);
    checkOpen("editmenu", testname);
  }
},
{
  // on Windows, a disabled item is selected, so pressing ENTER should close
  // the menu but not fire a command event
  testname: "enter on disabled",
  events: function() {
    if (navigator.platform.indexOf("Win") == 0)
      return [ "popuphiding editpopup", "popuphidden editpopup",
               "DOMMenuItemInactive cut", "DOMMenuInactive editpopup",
               "DOMMenuBarInactive menubar",
               "DOMMenuItemInactive editmenu", "DOMMenuItemInactive editmenu" ];
    else
      return [ "DOMMenuItemInactive copy", "DOMMenuInactive editpopup",
               "DOMMenuBarInactive menubar",
               "DOMMenuItemInactive editmenu", "DOMMenuItemInactive editmenu",
               "command copy", "popuphiding editpopup", "popuphidden editpopup",
               "DOMMenuItemInactive copy" ];
  },
  test: function() { synthesizeKey("VK_ENTER", { }); },
  result: function(testname) { checkClosed("editmenu", testname); }
},
{
  // pressing Alt + a key should open the corresponding menu
  testname: "open with accelerator",
  events: function() {
    return [ "DOMMenuBarActive menubar",
             "popupshowing viewpopup", "DOMMenuItemActive viewmenu",
             "DOMMenuItemActive toolbar", "popupshown viewpopup" ];
  },
  test: function() { synthesizeKey("V", { altKey: true }); },
  result: function(testname) { checkOpen("viewmenu", testname); }
},
{
  // open the submenu with the cursor right key
  testname: "open submenu with cursor right",
  events: function() {
    // on Windows, the disabled 'navigation' item can stll be highlihted
    if (navigator.platform.indexOf("Win") == 0)
      return [ "popupshowing toolbarpopup", "DOMMenuItemActive navigation",
               "popupshown toolbarpopup" ];
    else
      return [ "popupshowing toolbarpopup", "popupshown toolbarpopup" ];
  },
  test: function() { synthesizeKey("VK_RIGHT", { }); },
  result: function(testname) {
    checkOpen("viewmenu", testname);
    checkOpen("toolbar", testname);
  }
},
{
  // close the submenu with the cursor left key
  testname: "close submenu with cursor left",
  events: function() {
    if (navigator.platform.indexOf("Win") == 0)
      return [ "popuphiding toolbarpopup", "popuphidden toolbarpopup",
               "DOMMenuItemInactive navigation", "DOMMenuInactive toolbarpopup",
               "DOMMenuItemActive toolbar" ];
    else
      return [ "popuphiding toolbarpopup", "popuphidden toolbarpopup",
               "DOMMenuInactive toolbarpopup",
               "DOMMenuItemActive toolbar" ];
  },
  test: function() { synthesizeKey("VK_LEFT", { }); },
  result: function(testname) {
    checkOpen("viewmenu", testname);
    checkClosed("toolbar", testname);
  }
},
{
  // open the submenu with the enter key
  testname: "open submenu with enter",
  events: function() {
    // on Windows, the disabled 'navigation' item can stll be highlihted
    if (navigator.platform.indexOf("Win") == 0)
      return [ "popupshowing toolbarpopup", "DOMMenuItemActive navigation",
               "popupshown toolbarpopup" ];
    else
      return [ "popupshowing toolbarpopup", "popupshown toolbarpopup" ];
  },
  test: function() { synthesizeKey("VK_ENTER", { }); },
  result: function(testname) {
    checkOpen("viewmenu", testname);
    checkOpen("toolbar", testname);
  },
},
{
  // close the submenu with the escape key
  testname: "close submenu with escape",
  events: function() {
    if (navigator.platform.indexOf("Win") == 0)
      return [ "popuphiding toolbarpopup", "popuphidden toolbarpopup",
               "DOMMenuItemInactive navigation", "DOMMenuInactive toolbarpopup",
               "DOMMenuItemActive toolbar" ];
    else
      return [ "popuphiding toolbarpopup", "popuphidden toolbarpopup",
               "DOMMenuInactive toolbarpopup",
               "DOMMenuItemActive toolbar" ];
  },
  test: function() { synthesizeKey("VK_ESCAPE", { }); },
  result: function(testname) {
    checkOpen("viewmenu", testname);
    checkClosed("toolbar", testname);
  },
},
{
  // close the main menu with the escape key
  testname: "close menubar menu with escape",
  events: [ "popuphiding viewpopup", "popuphidden viewpopup",
            "DOMMenuItemInactive toolbar", "DOMMenuInactive viewpopup",
            "DOMMenuBarInactive menubar",
            "DOMMenuItemInactive viewmenu" ],
  test: function() { synthesizeKey("VK_ESCAPE", { }); },
  result: function(testname) { checkClosed("viewmenu", testname); },
},
{
  // on Windows, pressing Alt should highlight the first menu but not open it
  testname: "alt to activate menubar",
  condition: function() { return (navigator.platform.indexOf("Win") == 0) },
  events: [ "DOMMenuBarActive menubar", "DOMMenuItemActive filemenu" ],
  test: function() { synthesizeKey("VK_ALT", { }); },
  result: function(testname) { checkClosed("filemenu", testname); },
},
{
  // pressing cursor left should select the previous menu but not open it
  testname: "cursor left on active menubar",
  condition: function() { return (navigator.platform.indexOf("Win") == 0) },
  events: [ "DOMMenuItemInactive filemenu", "DOMMenuItemActive helpmenu" ],
  test: function() { synthesizeKey("VK_LEFT", { }); },
  result: function(testname) { checkClosed("helpmenu", testname); },
},
{
  // pressing cursor right should select the previous menu but not open it
  testname: "cursor right on active menubar",
  condition: function() { return (navigator.platform.indexOf("Win") == 0) },
  events: [ "DOMMenuItemInactive helpmenu", "DOMMenuItemActive filemenu" ],
  test: function() { synthesizeKey("VK_RIGHT", { }); },
  result: function(testname) { checkClosed("filemenu", testname); },
},
{
  // pressing a character should act as an accelerator and open the menu
  testname: "accelerator on active menubar",
  condition: function() { return (navigator.platform.indexOf("Win") == 0) },
  events: [ "popupshowing helppopup",
            "DOMMenuItemInactive filemenu", "DOMMenuItemActive helpmenu",
            "DOMMenuItemActive contents", "popupshown helppopup" ],
  test: function() { synthesizeKey("H", { }); },
  result: function(testname) { checkOpen("helpmenu", testname); },
},
{
  testname: "open with accelerator again",
  condition: function() { return (navigator.platform.indexOf("Win") == -1) },
  events: [ "DOMMenuBarActive menubar", "popupshowing helppopup",
            "DOMMenuItemActive helpmenu", "DOMMenuItemActive contents",
            "popupshown helppopup" ],
  test: function() { synthesizeKey("H", { altKey: true }); },
  result: function(testname) { checkOpen("helpmenu", testname); },
},
{
  // check that pressing cursor up skips non menuitems
  testname: "cursor up wrap",
  events: [ "DOMMenuItemInactive contents", "DOMMenuItemActive about" ],
  test: function() { synthesizeKey("VK_UP", { }); },
  result: function(testname) { }
},
{
  // check that pressing cursor down skips non menuitems
  testname: "cursor down wrap",
  events: [ "DOMMenuItemInactive about", "DOMMenuItemActive contents" ],
  test: function() { synthesizeKey("VK_DOWN", { }); },
  result: function(testname) { }
},
{
  // check that pressing a menuitem's accelerator selects it
  testname: "menuitem accelerator",
  events: [ "DOMMenuItemInactive contents", "DOMMenuItemActive amenu",
            "DOMMenuItemInactive amenu", "DOMMenuInactive helppopup",
            "DOMMenuBarInactive menubar", "DOMMenuItemInactive helpmenu",
            "DOMMenuItemInactive helpmenu",
            "command amenu", "popuphiding helppopup", "popuphidden helppopup",
            "DOMMenuItemInactive amenu",
           ],
  test: function() { synthesizeKey("M", { }); },
  result: function(testname) { checkClosed("helpmenu", testname); }
},
{
  // pressing F10 should highlight the first menu but not open it
  testname: "F10 to activate menubar",
  events: [ "DOMMenuBarActive menubar", "DOMMenuItemActive filemenu" ],
  test: function() { synthesizeKey("VK_F10", { }); },
  result: function(testname) { checkClosed("filemenu", testname); },
},
{
  // pressing cursor down should open a menu
  testname: "cursor down on menu",
  events: [ 
            "popupshowing helppopup",
            "DOMMenuItemInactive filemenu", "DOMMenuItemActive helpmenu",
            // This is in a different order than the
            // "accelerator on active menubar" because menus opened from a
            // shortcut key are fired asynchronously
            "DOMMenuItemActive contents", "popupshown helppopup" ],
  test: function() { synthesizeKey("VK_LEFT", { }); synthesizeKey("VK_DOWN", { }); },
},
{
  // pressing a letter that doesn't correspond to an accelerator. The menu
  // should not close because there is more than one item corresponding to
  // that letter
  testname: "menuitem with no accelerator",
  events: [ "DOMMenuItemInactive contents", "DOMMenuItemActive one" ],
  test: function() { synthesizeKey("O", { }); },
  result: function(testname) { checkOpen("helpmenu", testname); }
},
{
  // pressing the letter again should select the next one that starts with
  // that letter
  testname: "menuitem with no accelerator again",
  events: [ "DOMMenuItemInactive one", "DOMMenuItemActive only" ],
  test: function() { synthesizeKey("O", { }); },
  result: function(testname) {
    // 'only' is a menu but it should not be open
    checkOpen("helpmenu", testname);
    checkClosed("only", testname);
  }
},
{
  // pressing the letter again when the next item is disabled should still
  // select the disabled item
  testname: "menuitem with no accelerator disabled",
  condition: function() { return (navigator.platform.indexOf("Win") == 0) },
  events: [ "DOMMenuItemInactive only", "DOMMenuItemActive other" ],
  test: function() { synthesizeKey("O", { }); },
  result: function(testname) { }
},
{
  // when only one menuitem starting with that letter exists, it should be
  // selected and the menu closed
  testname: "menuitem with no accelerator single",
  events: function() {
    var elist = [ "DOMMenuItemInactive other", "DOMMenuItemActive third",
                  "DOMMenuItemInactive third", "DOMMenuInactive helppopup",
                  "DOMMenuBarInactive menubar",
                  "DOMMenuItemInactive helpmenu",
                  "DOMMenuItemInactive helpmenu",
                  "command third", "popuphiding helppopup",
                  "popuphidden helppopup", "DOMMenuItemInactive third",
                ];
    if (navigator.platform.indexOf("Win") == -1)
      elist[0] = "DOMMenuItemInactive only";
    return elist;
  },
  test: function() { synthesizeKey("T", { }); },
  result: function(testname) { checkClosed("helpmenu", testname); }
},
{
  // pressing F10 should highlight the first menu but not open it
  testname: "F10 to activate menubar again",
  condition: function() { return (navigator.platform.indexOf("Win") == 0) },
  events: [ "DOMMenuBarActive menubar", "DOMMenuItemActive filemenu" ],
  test: function() { synthesizeKey("VK_F10", { }); },
  result: function(testname) { checkClosed("filemenu", testname); },
},
{
  // pressing an accelerator for a disabled item should deactivate the menubar
  testname: "accelerator for disabled menu",
  condition: function() { return (navigator.platform.indexOf("Win") == 0) },
  events: [ "DOMMenuItemInactive filemenu", "DOMMenuBarInactive menubar" ],
  test: function() { synthesizeKey("S", { }); },
  result: function(testname) { checkClosed("secretmenu", testname); },
},
{
  testname: "press on disabled menu",
  test: function() {
    synthesizeMouse(document.getElementById("secretmenu"), 8, 8, { });
  },
  result: function (testname) {
    checkClosed("secretmenu", testname);
  }
},
{
  testname: "press on second menu with shift",
  events: [ "popupshowing editpopup", "DOMMenuBarActive menubar",
            "DOMMenuItemActive editmenu", "popupshown editpopup" ],
  test: function() {
    synthesizeMouse(document.getElementById("editmenu"), 8, 8, { shiftKey : true });
  },
  result: function (testname) {
    checkOpen("editmenu", testname);
    checkActive(document.getElementById("menubar"), "editmenu", testname);
  }
},
{
  testname: "press on disabled menuitem",
  test: function() {
    synthesizeMouse(document.getElementById("cut"), 8, 8, { });
  },
  result: function (testname) {
    checkOpen("editmenu", testname);
  }
},
{
  testname: "press on menuitem",
  events: [ "DOMMenuInactive editpopup",
            "DOMMenuBarInactive menubar",
            "DOMMenuItemInactive editmenu",
            "DOMMenuItemInactive editmenu",
            "command copy", "popuphiding editpopup", "popuphidden editpopup",
            "DOMMenuItemInactive copy",
           ],
  test: function() {
    synthesizeMouse(document.getElementById("copy"), 8, 8, { });
  },
  result: function (testname) {
    checkClosed("editmenu", testname);
  }
},
{
  // this test ensures that the menu can still be opened by clicking after selecting
  // a menuitem from the menu. See bug 399350.
  testname: "press on menu after menuitem selected",
  events: [ "popupshowing editpopup", "DOMMenuBarActive menubar",
            "DOMMenuItemActive editmenu", "popupshown editpopup" ],
  test: function() { synthesizeMouse(document.getElementById("editmenu"), 8, 8, { }); },
  result: function (testname) {
    checkActive(document.getElementById("editpopup"), "", testname);
    checkOpen("editmenu", testname);
  }
},
{  // try selecting a different command
  testname: "press on menuitem again",
  events: [ "DOMMenuInactive editpopup",
            "DOMMenuBarInactive menubar",
            "DOMMenuItemInactive editmenu",
            "DOMMenuItemInactive editmenu",
            "command paste", "popuphiding editpopup", "popuphidden editpopup",
            "DOMMenuItemInactive paste",
           ],
  test: function() {
    synthesizeMouse(document.getElementById("paste"), 8, 8, { });
  },
  result: function (testname) {
    checkClosed("editmenu", testname);
  }
},
{
  testname: "F10 to activate menubar for tab deactivation",
  events: [ "DOMMenuBarActive menubar", "DOMMenuItemActive filemenu" ],
  test: function() { synthesizeKey("VK_F10", { }); },
},
{
  testname: "Deactivate menubar with tab key",
  events: [ "DOMMenuBarInactive menubar", "DOMMenuItemInactive filemenu" ],
  test: function() { synthesizeKey("VK_TAB", { }); },
},
{
  testname: "F10 to activate menubar for escape deactivation",
  events: [ "DOMMenuBarActive menubar", "DOMMenuItemActive filemenu" ],
  test: function() { synthesizeKey("VK_F10", { }); },
},
{
  testname: "Deactivate menubar with escape key",
  events: [ "DOMMenuBarInactive menubar", "DOMMenuItemInactive filemenu" ],
  test: function() { synthesizeKey("VK_ESCAPE", { }); },
},
{
  testname: "F10 to activate menubar for f10 deactivation",
  events: [ "DOMMenuBarActive menubar", "DOMMenuItemActive filemenu" ],
  test: function() { synthesizeKey("VK_F10", { }); },
},
{
  testname: "Deactivate menubar with f10 key",
  events: [ "DOMMenuBarInactive menubar", "DOMMenuItemInactive filemenu"  ],
  test: function() { synthesizeKey("VK_F10", { }); },
},
{
  testname: "F10 to activate menubar for alt deactivation",
  condition: function() { return (navigator.platform.indexOf("Win") == 0) },
  events: [ "DOMMenuBarActive menubar", "DOMMenuItemActive filemenu" ],
  test: function() { synthesizeKey("VK_F10", { }); },
},
{
  testname: "Deactivate menubar with alt key",
  condition: function() { return (navigator.platform.indexOf("Win") == 0) },
  events: [ "DOMMenuBarInactive menubar", "DOMMenuItemInactive filemenu"  ],
  test: function() { synthesizeKey("VK_ALT", { }); },
}

];

]]>
</script>


</window>
