summaryrefslogtreecommitdiffstats
path: root/toolkit/components/windowwatcher/test/test_alwaysOnTop_windows.html
blob: 05c768c93f90330585db724169776a071cecfe6d (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
<!DOCTYPE HTML>
<html>
<!--
Tests the alwaysOnTop window feature for the Windows OS.
-->
<head>
  <meta charset="utf-8">
  <title>Test an alwaysOnTop window on Windows</title>

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">

  <script type="application/javascript">

  const {BrowserTestUtils} = ChromeUtils.importESModule(
    "resource://testing-common/BrowserTestUtils.sys.mjs"
  );
  const {ctypes} = ChromeUtils.importESModule(
    "resource://gre/modules/ctypes.sys.mjs"
  );

  function assertAlwaysOnTop(win, expected) {
    let docShell = win.docShell;
    let chromeFlags = docShell.treeOwner
                              .QueryInterface(Ci.nsIInterfaceRequestor)
                              .getInterface(Ci.nsIAppWindow)
                              .chromeFlags;
    let hasFlag = !!(chromeFlags & Ci.nsIWebBrowserChrome.CHROME_ALWAYS_ON_TOP);
    is(hasFlag, expected, "Window should have CHROME_ALWAYS_ON_TOP flag.");

    const hWND = Number(win.docShell.treeOwner.nsIBaseWindow.nativeHandle);
    const WS_EX_TOPMOST = 0x00000008;
    const GWL_EXSTYLE = -20;

    let lib = ctypes.open("user32.dll");
    // On 32-bit systems, the function we need to call is GetWindowLongW. On
    // 64-bit systems, the function is GetWindowLongPtrW. Interestingly,
    // the MSDN page[1] for GetWindowLongPtrW claims that calling it should work
    // on both 32-bit and 64-bit, but that didn't appear to be the case here
    // with local testing, hence the conditional.
    //
    // [1]: https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowlongptrw
    let GetWindowFuncSymbol = Services.appinfo.is64Bit ? "GetWindowLongPtrW"
                                                       : "GetWindowLongW";
    let GetWindowFunc = lib.declare(GetWindowFuncSymbol, ctypes.winapi_abi,
                                    ctypes.intptr_t, ctypes.uintptr_t,
                                    ctypes.int);

    let styles = GetWindowFunc(hWND, GWL_EXSTYLE);
    let isAlwaysOnTop = !!(styles & WS_EX_TOPMOST);
    is(isAlwaysOnTop, expected, "Window should be always on top.");
    lib.close();
  }

  if (Services.appinfo.OS != "WINNT") {
    ok(false, "This test is only designed to run on Windows.");
  } else {
    add_task(async function() {
      let normalWinOpened = BrowserTestUtils.domWindowOpenedAndLoaded();
      window.openDialog("about:blank",
                        "_blank", "chrome,width=100,height=100,noopener", null);
      let normalWin = await normalWinOpened;
      ok(normalWin, "Normal window opened");
      assertAlwaysOnTop(normalWin, false);
      await BrowserTestUtils.closeWindow(normalWin);

      let alwaysOnTopWinOpened = BrowserTestUtils.domWindowOpenedAndLoaded();
      window.openDialog("about:blank",
                        "_blank", "chrome,width=100,height=100,alwaysOnTop,noopener", null);
      let alwaysOnTopWin = await alwaysOnTopWinOpened;
      ok(alwaysOnTopWin, "AlwaysOnTop window opened");
      assertAlwaysOnTop(alwaysOnTopWin, true);
      await BrowserTestUtils.closeWindow(alwaysOnTopWin);
    });
  }

  </script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>
</html>