- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / panels / focus_change_on_minimize / test.js
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var waitingForFocus = true;
6 var panelWinId = chrome.windows.WINDOW_ID_NONE;
7 var focusedWinId = chrome.windows.WINDOW_ID_NONE;
8 var listenDoneCallback;
9
10 // Focus change event handler to watch for created panel to gain focus.
11 // Then minimize the panel and wait for it to lose focus by watching
12 // for an onFocusChanged event with WINDOW_ID_NONE, which is expected
13 // whenever a panel loses focus.
14 function onFocusChanged(changedWinId) {
15   if (waitingForFocus) {
16     if (chrome.windows.WINDOW_ID_NONE != changedWinId) {
17       focusedWinId = changedWinId;
18       // Only minimize if the focused window is the panel created by
19       // this test. Tests might be run in parallel so there might be
20       // other focus events that we don't care about.
21       if (focusedWinId == panelWinId) {
22          minimizePanel();
23       }
24     }
25   } else if (chrome.windows.WINDOW_ID_NONE == changedWinId) {
26     listenDoneCallback();
27   }
28 }
29
30 // Minimize the created panel after we know it has the focus.
31 function minimizePanel() {
32   chrome.test.assertEq(focusedWinId, panelWinId);
33   waitingForFocus = false;
34   chrome.windows.update(panelWinId, {'state': 'minimized'},
35       chrome.test.callbackPass(function(win) {
36           chrome.test.assertEq('minimized', win.state);
37       }));
38 }
39
40 // Activate panel so we can minimize it.
41 function activatePanel() {
42   chrome.windows.update(panelWinId, {'focused': true},
43       chrome.test.callbackPass(function(win) {
44       }));
45 }
46
47 chrome.test.runTests([
48   function createPanelToMinimize() {
49     listenDoneCallback = chrome.test.listenForever(
50         chrome.windows.onFocusChanged, onFocusChanged);
51     chrome.windows.create(
52         {'url': 'about:blank','type': 'panel'},
53         chrome.test.callbackPass(function(win) {
54             chrome.test.assertEq('panel', win.type);
55             chrome.test.assertEq(true, win.alwaysOnTop);
56             panelWinId = win.id;
57             activatePanel();
58         }));
59   }
60 ]);