summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_windowactivation.js
blob: ae4ba75dcfd7545867b14dd565ae74cfdc35b643 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * This test checks that window activation state is set properly with multiple tabs.
 */

var testPage = "data:text/html,<body><style>:-moz-window-inactive { background-color: red; }</style><div id='area'></div></body>";

var colorChangeNotifications = 0;
var otherWindow;

var browser1, browser2;

function test() {
  waitForExplicitFinish();
  waitForFocus(reallyRunTests);
}

function reallyRunTests() {

  let tab1 = gBrowser.addTab();
  let tab2 = gBrowser.addTab();
  browser1 = gBrowser.getBrowserForTab(tab1);
  browser2 = gBrowser.getBrowserForTab(tab2);

  gURLBar.focus();

  var loadCount = 0;
  function check()
  {
    // wait for both tabs to load
    if (++loadCount != 2) {
      return;
    }

    browser1.removeEventListener("load", check, true);
    browser2.removeEventListener("load", check, true);

    sendGetBackgroundRequest(true);
  }

  // The test performs four checks, using -moz-window-inactive on two child tabs.
  // First, the initial state should be transparent. The second check is done
  // while another window is focused. The third check is done after that window
  // is closed and the main window focused again. The fourth check is done after
  // switching to the second tab.
  window.messageManager.addMessageListener("Test:BackgroundColorChanged", function(message) {
    colorChangeNotifications++;

    switch (colorChangeNotifications) {
      case 1:
        is(message.data.color, "transparent", "first window initial");
        break;
      case 2:
        is(message.data.color, "transparent", "second window initial");
        runOtherWindowTests();
        break;
      case 3:
        is(message.data.color, "rgb(255, 0, 0)", "first window lowered");
        break;
      case 4:
        is(message.data.color, "rgb(255, 0, 0)", "second window lowered");
        sendGetBackgroundRequest(true);
        otherWindow.close();
        break;
      case 5:
        is(message.data.color, "transparent", "first window raised");
        break;
      case 6:
        is(message.data.color, "transparent", "second window raised");
        gBrowser.selectedTab = tab2;
        break;
      case 7:
        is(message.data.color, "transparent", "first window after tab switch");
        break;
      case 8:
        is(message.data.color, "transparent", "second window after tab switch");
        finishTest();
        break;
      case 9:
        ok(false, "too many color change notifications");
        break;
    }
  });

  window.messageManager.addMessageListener("Test:FocusReceived", function(message) {
    // No color change should occur after a tab switch.
    if (colorChangeNotifications == 6) {
      sendGetBackgroundRequest(false);
    }
  });

  window.messageManager.addMessageListener("Test:ActivateEvent", function(message) {
    ok(message.data.ok, "Test:ActivateEvent");
  });

  window.messageManager.addMessageListener("Test:DeactivateEvent", function(message) {
    ok(message.data.ok, "Test:DeactivateEvent");
  });

  browser1.addEventListener("load", check, true);
  browser2.addEventListener("load", check, true);
  browser1.contentWindow.location = testPage;
  browser2.contentWindow.location = testPage;

  browser1.messageManager.loadFrameScript("data:,(" + childFunction.toString() + ")();", true);
  browser2.messageManager.loadFrameScript("data:,(" + childFunction.toString() + ")();", true);

  gBrowser.selectedTab = tab1;
}

function sendGetBackgroundRequest(ifChanged)
{
  browser1.messageManager.sendAsyncMessage("Test:GetBackgroundColor", { ifChanged: ifChanged });
  browser2.messageManager.sendAsyncMessage("Test:GetBackgroundColor", { ifChanged: ifChanged });
}

function runOtherWindowTests() {
  otherWindow = window.open("data:text/html,<body>Hi</body>", "", "chrome");
  waitForFocus(function () {
    sendGetBackgroundRequest(true);
  }, otherWindow);
}

function finishTest()
{
  gBrowser.removeCurrentTab();
  gBrowser.removeCurrentTab();
  otherWindow = null;
  finish();
}

function childFunction()
{
  let oldColor = null;

  let expectingResponse = false;
  let ifChanged = true;

  addMessageListener("Test:GetBackgroundColor", function(message) {
    expectingResponse = true;
    ifChanged = message.data.ifChanged;
  });

  content.addEventListener("focus", function () {
    sendAsyncMessage("Test:FocusReceived", { });
  }, false);

  var windowGotActivate = false;
  var windowGotDeactivate = false;
  addEventListener("activate", function() {
      sendAsyncMessage("Test:ActivateEvent", { ok: !windowGotActivate });
      windowGotActivate = false;
    });

  addEventListener("deactivate", function() {
      sendAsyncMessage("Test:DeactivateEvent", { ok: !windowGotDeactivate });
      windowGotDeactivate = false;
    });
  content.addEventListener("activate", function() {
      windowGotActivate = true;
    });

  content.addEventListener("deactivate", function() {
      windowGotDeactivate = true;
    });

  content.setInterval(function () {
    if (!expectingResponse) {
      return;
    }

    let area = content.document.getElementById("area");
    if (!area) {
      return; /* hasn't loaded yet */
    }

    let color = content.getComputedStyle(area, "").backgroundColor;
    if (oldColor != color || !ifChanged) {
      expectingResponse = false;
      oldColor = color;
      sendAsyncMessage("Test:BackgroundColorChanged", { color: color });
    }
  }, 20);
}