summaryrefslogtreecommitdiffstats
path: root/widget/tests/test_bug428405.xhtml
blob: 25d796ab7b4612d97cdb074424f5d303f8da4fe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>

<window id="window1" title="Test Bug 428405"
  onload="setGlobals(); loadFirstTab();"
  xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/NativeKeyCodes.js"/>

  <tabbox id="tabbox" style="-moz-box-flex: 100">
    <tabs>
      <tab label="Tab 1"/>
      <tab label="Tab 2"/>
    </tabs>
    <tabpanels style="-moz-box-flex: 100">
      <browser onload="configureFirstTab();" id="tab1browser" style="-moz-box-flex: 100"/>
      <browser onload="configureSecondTab();" id="tab2browser" style="-moz-box-flex: 100"/>
    </tabpanels>
  </tabbox>

  <script type="application/javascript"><![CDATA[
    const {BrowserTestUtils} = ChromeUtils.importESModule(
      "resource://testing-common/BrowserTestUtils.sys.mjs"
    );

    SimpleTest.waitForExplicitFinish();

    var gCmdOptYReceived = false;

    // Look for a cmd-opt-y event.
    function onKeyPress(aEvent) {
      gCmdOptYReceived = false;
      if (String.fromCharCode(aEvent.charCode) != 'y')
        return;
      if (aEvent.ctrlKey || aEvent.shiftKey || !aEvent.metaKey || !aEvent.altKey)
        return;
      gCmdOptYReceived = true;
    }

    function setGlobals() {
      let chromeWindow = Services.wm.getMostRecentWindow("navigator:browser");
      // For some reason, a global <key> element's oncommand handler only gets
      // invoked if the focus is outside both of the <browser> elements
      // (tab1browser and tab2browser).  So, to make sure we can see a
      // cmd-opt-y event in window1 (if one is available), regardless of where
      // the focus is in this window, we need to add a "keypress" event
      // listener to gChromeWindow, and then check (in onKeyPress()) to see if
      // it's a cmd-opt-y event.
      chromeWindow.addEventListener("keypress", onKeyPress);
    }

    // 1) Start loading first tab.
    // 6) Start reloading first tab.
    function loadFirstTab() {
      var browser = document.getElementById("tab1browser");
      BrowserTestUtils.startLoadingURIString(browser, "data:text/html;charset=utf-8,<body><h2>First Tab</h2><p><input type='submit' value='Button' id='button1'/></body>");
    }

    function configureFirstTab() {
      try {
        var button = document.getElementById("tab1browser").contentDocument.getElementById("button1");
        button.addEventListener("click", onFirstTabButtonClicked);
        button.focus();
        if (document.getElementById("tabbox").selectedIndex == 0) {
          // 2) When first tab has finished loading (while first tab is
          //    focused), hit Return to trigger the action of first tab's
          //    button.
          synthesizeNativeReturnKey();
        } else {
          // 7) When first tab has finished reloading (while second tab is
          //    focused), start loading second tab.
          loadSecondTab();
        }
      } catch(e) {
      }
    }

    // 8) Start loading second tab.
    function loadSecondTab() {
      var browser = document.getElementById("tab2browser");
      BrowserTestUtils.startLoadingURIString(browser, "data:text/html;charset=utf-8,<body><h2>Second Tab</h2><p><input type='submit' value='Button' id='button1'/></body>");
    }

    function configureSecondTab() {
      try {
        var button = document.getElementById("tab2browser").contentDocument.getElementById("button1");
        button.addEventListener("click", onSecondTabButtonClicked);
        button.focus();
        if (document.getElementById("tabbox").selectedIndex == 1) {
          // 9) When second tab has finished loading (while second tab is
          //    focused), hit Return to trigger action of second tab's
          //    button.
          synthesizeNativeReturnKey();
        }
      } catch(e) {
      }
    }

    // 3) First tab's button clicked.
    function onFirstTabButtonClicked() {
      switchToSecondTabAndReloadFirst();
    }

    // 10) Second tab's button clicked.
    function onSecondTabButtonClicked() {
      switchToFirstTab();
    }

    function switchToSecondTabAndReloadFirst() {
      // 4) Switch to second tab.
      document.getElementById("tabbox").selectedIndex = 1;
      // 5) Start reloading first tab (while second tab is focused).
      loadFirstTab();
    }

    function switchToFirstTab() {
      // 11) Switch back to first tab.
      document.getElementById("tabbox").selectedIndex = 0;
      doCmdY();
    }

    function doCmdY() {
      // 12) Back in first tab, try cmd-y.
      gCmdOptYReceived = false;
      if (!synthesizeNativeCmdOptY(finishTest)) {
        ok(false, "Failed to synthesize native key");
        finishTest();
      }
    }

    function finishTest() {
      // 13) Check result.
      is(gCmdOptYReceived, true);

      SimpleTest.finish();
    }

    // synthesizeNativeReturnKey() and synthesizeNativeCmdOptY() are needed
    // because their synthesizeKey() counterparts don't work properly -- the
    // latter make this test succeed when it should fail.

    // The 'aNativeKeyCode', 'aCharacters' and 'aUnmodifiedCharacters'
    // parameters used below (in synthesizeNativeReturnKey() and
    // synthesizeNativeCmdOptY()) were confirmed accurate using the
    // DebugEventsPlugin v1.01 from bmo bug 441880.

    function synthesizeNativeReturnKey() {
      synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_Return, {}, "\u000a", "\u000a");
    }

    function synthesizeNativeCmdOptY(aCallback) {
      return synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_Y, {metaKey:1, altKey:1}, "y", "y", aCallback);
    }

  ]]></script>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
    <p id="display"></p>
    <div id="content" style="display: none"></div>
    <pre id="test"></pre>
  </body>

</window>