- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / tabs / basics / pinned.js
1 // Copyright (c) 2011 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 firstWindowId;
6
7 chrome.test.runTests([
8   function setupWindow() {
9     createWindow(["about:blank", "chrome://newtab/", pageUrl("a")], {},
10                  pass(function(winId, tabIds) {
11       firstWindowId = winId;
12     }));
13   },
14
15   function createPinned() {
16     var winOptions = {"windowId": firstWindowId, "pinned": true};
17     var onUpdatedCompleted = chrome.test.listenForever(
18       chrome.tabs.onUpdated,
19       function(tabId, changeInfo, tab) {
20         if ('pinned' in changeInfo) {
21           assertEq(false, changeInfo.pinned);
22           assertEq(false, tab.pinned);
23           onUpdatedCompleted();
24         }
25       }
26     );
27     chrome.tabs.create(winOptions, pass(function(tab) {
28       assertEq(true, tab.pinned);
29       chrome.tabs.update(tab.id, {"pinned":false}, pass(function() {
30         // Leave a clean slate for the next test.
31         chrome.tabs.remove(tab.id);
32       }));
33     }));
34   },
35
36   function updatePinned() {
37     // A helper function that (un)pins a tab and verifies that both the callback
38     // and the chrome.tabs.onUpdated event listeners are called.
39     var pinTab = function(id, pinnedState, callback) {
40       var onUpdatedCompleted = chrome.test.listenForever(
41         chrome.tabs.onUpdated,
42         function(tabId, changeInfo, tab) {
43           if ('pinned' in changeInfo) {
44             assertEq(tabId, id);
45             assertEq(pinnedState, tab.pinned);
46             onUpdatedCompleted();
47             if (callback)
48               callback(tab);
49           }
50         }
51       );
52       chrome.tabs.update(id, { "pinned": pinnedState }, pass(function(tab) {
53         assertEq(pinnedState, tab.pinned);
54       }));
55     };
56
57     // We pin and unpin these tabs because the TabStripModelObserver used to
58     // have multiple notification code paths depending on the tab moves as a
59     // result of being pinned or unpinned.
60     // This works as follows:
61     //   1.  Pin first tab (does not move, pinning)
62     //   2.  Pin 3rd tab (moves to 2nd tab, pinning)
63     //   3.  Unpin 1st tab (moves to 2nd tab, unpinning)
64     //   4.  Unpin (new) 1st tab (does not move. unpinning)
65     chrome.tabs.getAllInWindow(firstWindowId,
66       pass(function(tabs) {
67         assertEq(tabs.length, 3);
68         for (var i = 0; i < tabs.length; i++)
69           assertEq(false, tabs[i].pinned);
70
71         pinTab(tabs[0].id, true, function(tab) {
72           assertEq(tabs[0].index, tab.index);
73           pinTab(tabs[2].id, true, function(tab) {
74             assertEq(1, tab.index);
75             pinTab(tabs[0].id, false, function(tab) {
76               assertEq(1, tab.index);
77               pinTab(tabs[2].id, false, function (tab) {
78                 assertEq(0, tab.index);
79               });
80             });
81           });
82         });
83       }));
84   }
85 ]);