Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / tabs / basics / move.js
blob25d119e91829fc6e98862b0df326d3fecb23ebe8
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.
5 var firstWindowId;
6 var secondWindowId;
7 var moveTabIds = {};
8 var kChromeUINewTabURL = "chrome://newtab/";
10 chrome.test.runTests([
11 // Do a series of moves and removes so that we get the following
13 // Before:
14 // Window1: (newtab),a,b,c,d,e
15 // Window2: (newtab)
17 // After moveToInvalidTab:
18 // Window1: (newtab)
19 // Window2: b,a,(newtab)
20 function setupLetterPages() {
21 var pages = [kChromeUINewTabURL, pageUrl('a'), pageUrl('b'),
22 pageUrl('c'), pageUrl('d'), pageUrl('e')];
23 createWindow(pages, {}, pass(function(winId, tabIds) {
24 firstWindowId = winId;
25 moveTabIds['a'] = tabIds[1];
26 moveTabIds['b'] = tabIds[2];
27 moveTabIds['c'] = tabIds[3];
28 moveTabIds['d'] = tabIds[4];
29 moveTabIds['e'] = tabIds[5];
30 createWindow([kChromeUINewTabURL], {}, pass(function(winId, tabIds) {
31 secondWindowId = winId;
32 }));
33 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) {
34 assertEq(pages.length, tabs.length);
35 for (var i in tabs) {
36 assertEq(pages[i], tabs[i].url);
38 }));
39 }));
42 function move() {
43 // Check that the tab/window state is what we expect after doing moves.
44 function checkMoveResults() {
45 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) {
46 assertEq(4, tabs.length);
47 assertEq(kChromeUINewTabURL, tabs[0].url);
48 assertEq(pageUrl("a"), tabs[1].url);
49 assertEq(pageUrl("e"), tabs[2].url);
50 assertEq(pageUrl("c"), tabs[3].url);
52 chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) {
53 assertEq(3, tabs.length);
54 assertEq(pageUrl("b"), tabs[0].url);
55 assertEq(kChromeUINewTabURL, tabs[1].url);
56 assertEq(pageUrl("d"), tabs[2].url);
57 }));
58 }));
61 chrome.tabs.move(moveTabIds['b'], {"windowId": secondWindowId, "index": 0},
62 pass(function(tabB) {
63 assertEq(0, tabB.index);
64 chrome.tabs.move(moveTabIds['e'], {"index": 2},
65 pass(function(tabE) {
66 assertEq(2, tabE.index);
67 chrome.tabs.move(moveTabIds['d'], {"windowId": secondWindowId,
68 "index": 2}, pass(function(tabD) {
69 assertEq(2, tabD.index);
70 checkMoveResults();
71 }));
72 }));
73 }));
76 function moveWithNegativeIndex() {
77 // Check that the tab/window state is what we expect after doing moves.
78 function checkMoveResults() {
79 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) {
80 assertEq(3, tabs.length);
81 assertEq(kChromeUINewTabURL, tabs[0].url);
82 assertEq(pageUrl("a"), tabs[1].url);
83 assertEq(pageUrl("c"), tabs[2].url);
85 chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) {
86 assertEq(4, tabs.length);
87 assertEq(pageUrl("b"), tabs[0].url);
88 assertEq(kChromeUINewTabURL, tabs[1].url);
89 assertEq(pageUrl("d"), tabs[2].url);
90 assertEq(pageUrl("e"), tabs[3].url);
91 }));
92 }));
95 // A -1 move index means move the tab to the end of the window.
96 chrome.tabs.move(moveTabIds['e'], {"windowId": secondWindowId, "index": -1},
97 pass(function(tabE) {
98 assertEq(3, tabE.index);
99 checkMoveResults();
100 }));
103 function remove() {
104 chrome.tabs.remove(moveTabIds["d"], pass(function() {
105 chrome.tabs.getAllInWindow(secondWindowId,
106 pass(function(tabs) {
107 assertEq(3, tabs.length);
108 assertEq(pageUrl("b"), tabs[0].url);
109 assertEq(kChromeUINewTabURL, tabs[1].url);
110 assertEq(pageUrl("e"), tabs[2].url);
111 }));
112 }));
115 function moveMultipleTabs() {
116 chrome.tabs.move([moveTabIds['c'], moveTabIds['a']],
117 {"windowId": secondWindowId, "index": 1},
118 pass(function(tabsA) {
119 assertEq(2, tabsA.length);
120 assertEq(secondWindowId, tabsA[0].windowId);
121 assertEq(pageUrl('c'), tabsA[0].url);
122 assertEq(1, tabsA[0].index);
123 assertEq(secondWindowId, tabsA[1].windowId);
124 assertEq(pageUrl('a'), tabsA[1].url);
125 assertEq(2, tabsA[1].index);
126 chrome.tabs.query({"windowId": secondWindowId}, pass(function(tabsB) {
127 assertEq(5, tabsB.length);
128 }));
129 }));
132 function removeMultipleTabs() {
133 chrome.tabs.remove([moveTabIds['e'], moveTabIds['c']], pass(function() {
134 chrome.tabs.query({"windowId": secondWindowId}, pass(function(tabs) {
135 assertEq(3, tabs.length);
136 assertEq(pageUrl("b"), tabs[0].url);
137 assertEq(pageUrl("a"), tabs[1].url);
138 assertEq(kChromeUINewTabURL, tabs[2].url);
139 }));
140 }));
143 // Make sure we don't crash when the index is out of range.
144 function moveToInvalidTab() {
145 var error_msg = "Invalid value for argument 2. Property 'index': " +
146 "Value must not be less than -1.";
147 try {
148 chrome.tabs.move(moveTabIds['b'], {"index": -2}, function(tab) {
149 chrome.test.fail("Moved a tab to an invalid index");
151 } catch (e) {
152 assertEq(error_msg, e.message);
154 chrome.tabs.move(moveTabIds['b'], {"index": 10000}, pass(function(tabB) {
155 assertEq(2, tabB.index);
156 }));
159 // Check that attempting to move an empty list of tabs doesn't crash browser
160 function moveEmptyTabList() {
161 chrome.tabs.move([], {"index": 0}, fail("No tabs given."));
164 // Move a tab to the current window.
165 function moveToCurrentWindow() {
166 chrome.windows.getCurrent(pass(function(win) {
167 var targetWin = win.id == firstWindowId ? secondWindowId : firstWindowId;
168 chrome.tabs.query({"windowId": targetWin}, pass(function(tabs) {
169 chrome.tabs.move(tabs[0].id,
170 {"windowId": chrome.windows.WINDOW_ID_CURRENT,
171 "index": 0},
172 pass(function(tab) {
173 assertEq(win.id, tab.windowId);
174 }));
175 }));
176 }));