summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/bugs/test_bug369306.html
blob: c35431a56dad1f1a4944af2a87eb3530736cdad5 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=369306
-->
<head>
  <title>Test for Bug 369306</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=369306">Mozilla Bug 369306</a>
<p id="display"></p>
<div id='content'>
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 369306 **/

var originatingWindow = self;

function focusShouldNotChange(aAction, nextTest)
{
  var w = window.open('about:blank', '', 'foo');
  var fail = false;

  SimpleTest.waitForFocus(function () {
    function failHandler() { fail = true; }

    originatingWindow.addEventListener("focus", failHandler, false);
    w.addEventListener("blur", failHandler, false);

    aAction(w);

    SimpleTest.executeSoon(function () {
      originatingWindow.removeEventListener("focus", failHandler, false);
      w.removeEventListener("blur", failHandler, false);

      ok(!fail, "The focus should not have been changed!");

      // Cleaning and running next test.
      w.close();
      SimpleTest.waitForFocus(nextTest, originatingWindow);
    });
  }, w, true);
}

function focusShouldNotChange2(aURL, nextTest)
{
  var w = window.open(aURL, '', 'foo');
  var fail = false;

  SimpleTest.waitForFocus(function () {
    function failHandler() { fail = true; }

    originatingWindow.addEventListener("focus", failHandler, false);
    w.addEventListener("blur", failHandler, false);

    /**
     * NOTE: This setTimeout can cause a random green.
     * onload handler + executeSoon doesn't work too so we have to use setTimeout.
     * The check may be call before w script being executed but that would make
     * this check green even if it should be orange.
     */
    setTimeout(function () {
      originatingWindow.removeEventListener("focus", failHandler, false);
      w.removeEventListener("blur", failHandler, false);

      ok(!fail, "The focus should not have been changed with URL=" + aURL);

      // Cleaning and running next test.
      w.close();
      SimpleTest.waitForFocus(nextTest, originatingWindow);
    }, 1000);
  }, w);
}

function test1()
{
  focusShouldNotChange(function (aW) { aW.blur(); }, test2);
}

function test2()
{
  focusShouldNotChange(function () { originatingWindow.focus(); }, test3);
}

function test3()
{
  focusShouldNotChange2("data:text/html,\<script>opener.focus();\<\/script>", test4);
}

function test4()
{
  focusShouldNotChange2("data:text/html,\<script>blur();\<\/script>", test5);
}

function test5()
{
  var w = window.open('about:blank', '', 'foo');

  SimpleTest.waitForFocus(function () {
    SimpleTest.waitForFocus(function () {
      SimpleTest.waitForFocus(function () {
        ok(true, "The last opened window should be able to get focus");
        w.close();
        SimpleTest.executeSoon(SimpleTest.finish);
      }, w, true);

      w.focus();
    }, originatingWindow);

    SimpleTest.executeSoon(function() {
      // We have to focus back the originating window but we can't do that with
      // .focus() or .blur() anymore.
      SpecialPowers.focus(window);
    });
  }, w, true);
}

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");

function startTest() {
  // dom.disable_window_flip has to be set to true for this test.
  SpecialPowers.pushPrefEnv({"set": [["dom.disable_window_flip", true]]}, test1);
}

// startTest is going to call the next tests.
SimpleTest.waitForFocus(startTest);

</script>
</pre>
</body>
</html>