OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var firstWindowId; | 5 var firstWindowId; |
6 var secondWindowId; | 6 var secondWindowId; |
7 var moveTabIds = {}; | 7 var moveTabIds = {}; |
| 8 var kChromeUINewTabURL = "chrome://newtab/"; |
8 | 9 |
9 chrome.test.runTests([ | 10 chrome.test.runTests([ |
10 // Do a series of moves and removes so that we get the following | 11 // Do a series of moves and removes so that we get the following |
11 // | 12 // |
12 // Before: | 13 // Before: |
13 // Window1: (newtab),a,b,c,d,e | 14 // Window1: (newtab),a,b,c,d,e |
14 // Window2: (newtab) | 15 // Window2: (newtab) |
15 // | 16 // |
16 // After moveToInvalidTab: | 17 // After moveToInvalidTab: |
17 // Window1: (newtab),a | 18 // Window1: (newtab) |
18 // Window2: b,(newtab) | 19 // Window2: b,a,(newtab) |
19 function setupLetterPages() { | 20 function setupLetterPages() { |
20 var pages = ["chrome://newtab/", pageUrl('a'), pageUrl('b'), | 21 var pages = [kChromeUINewTabURL, pageUrl('a'), pageUrl('b'), |
21 pageUrl('c'), pageUrl('d'), pageUrl('e')]; | 22 pageUrl('c'), pageUrl('d'), pageUrl('e')]; |
22 createWindow(pages, {}, pass(function(winId, tabIds) { | 23 createWindow(pages, {}, pass(function(winId, tabIds) { |
23 firstWindowId = winId; | 24 firstWindowId = winId; |
24 moveTabIds['a'] = tabIds[1]; | 25 moveTabIds['a'] = tabIds[1]; |
25 moveTabIds['b'] = tabIds[2]; | 26 moveTabIds['b'] = tabIds[2]; |
26 moveTabIds['c'] = tabIds[3]; | 27 moveTabIds['c'] = tabIds[3]; |
27 moveTabIds['d'] = tabIds[4]; | 28 moveTabIds['d'] = tabIds[4]; |
28 moveTabIds['e'] = tabIds[5]; | 29 moveTabIds['e'] = tabIds[5]; |
29 createWindow(["chrome://newtab/"], {}, pass(function(winId, tabIds) { | 30 createWindow([kChromeUINewTabURL], {}, pass(function(winId, tabIds) { |
30 secondWindowId = winId; | 31 secondWindowId = winId; |
31 })); | 32 })); |
32 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { | 33 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { |
33 assertEq(pages.length, tabs.length); | 34 assertEq(pages.length, tabs.length); |
34 for (var i in tabs) { | 35 for (var i in tabs) { |
35 assertEq(pages[i], tabs[i].url); | 36 assertEq(pages[i], tabs[i].url); |
36 } | 37 } |
37 })); | 38 })); |
38 })); | 39 })); |
39 }, | 40 }, |
40 | 41 |
41 function move() { | 42 function move() { |
42 // Check that the tab/window state is what we expect after doing moves. | 43 // Check that the tab/window state is what we expect after doing moves. |
43 function checkMoveResults() { | 44 function checkMoveResults() { |
44 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { | 45 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { |
45 assertEq(4, tabs.length); | 46 assertEq(4, tabs.length); |
46 assertEq("chrome://newtab/", tabs[0].url); | 47 assertEq(kChromeUINewTabURL, tabs[0].url); |
47 assertEq(pageUrl("a"), tabs[1].url); | 48 assertEq(pageUrl("a"), tabs[1].url); |
48 assertEq(pageUrl("e"), tabs[2].url); | 49 assertEq(pageUrl("e"), tabs[2].url); |
49 assertEq(pageUrl("c"), tabs[3].url); | 50 assertEq(pageUrl("c"), tabs[3].url); |
50 | 51 |
51 chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) { | 52 chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) { |
52 assertEq(3, tabs.length); | 53 assertEq(3, tabs.length); |
53 assertEq(pageUrl("b"), tabs[0].url); | 54 assertEq(pageUrl("b"), tabs[0].url); |
54 assertEq("chrome://newtab/", tabs[1].url); | 55 assertEq(kChromeUINewTabURL, tabs[1].url); |
55 assertEq(pageUrl("d"), tabs[2].url); | 56 assertEq(pageUrl("d"), tabs[2].url); |
56 })); | 57 })); |
57 })); | 58 })); |
58 } | 59 } |
59 | 60 |
60 chrome.tabs.move(moveTabIds['b'], {"windowId": secondWindowId, "index": 0}, | 61 chrome.tabs.move(moveTabIds['b'], {"windowId": secondWindowId, "index": 0}, |
61 pass(function(tabB) { | 62 pass(function(tabB) { |
62 chrome.test.assertEq(0, tabB.index); | 63 assertEq(0, tabB.index); |
63 chrome.tabs.move(moveTabIds['e'], {"index": 2}, | 64 chrome.tabs.move(moveTabIds['e'], {"index": 2}, |
64 pass(function(tabE) { | 65 pass(function(tabE) { |
65 chrome.test.assertEq(2, tabE.index); | 66 assertEq(2, tabE.index); |
66 chrome.tabs.move(moveTabIds['d'], {"windowId": secondWindowId, | 67 chrome.tabs.move(moveTabIds['d'], {"windowId": secondWindowId, |
67 "index": 2}, pass(function(tabD) { | 68 "index": 2}, pass(function(tabD) { |
68 chrome.test.assertEq(2, tabD.index); | 69 assertEq(2, tabD.index); |
69 checkMoveResults(); | 70 checkMoveResults(); |
70 })); | 71 })); |
71 })); | 72 })); |
72 })); | 73 })); |
73 }, | 74 }, |
74 | 75 |
| 76 function moveWithoutIndex() { |
| 77 function checkMoveResults() { |
| 78 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { |
| 79 assertEq(3, tabs.length); |
| 80 assertEq(kChromeUINewTabURL, tabs[0].url); |
| 81 assertEq(pageUrl("c"), tabs[1].url); |
| 82 assertEq(pageUrl("a"), tabs[2].url); |
| 83 |
| 84 chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) { |
| 85 assertEq(4, tabs.length); |
| 86 assertEq(pageUrl("b"), tabs[0].url); |
| 87 assertEq(kChromeUINewTabURL, tabs[1].url); |
| 88 assertEq(pageUrl("d"), tabs[2].url); |
| 89 assertEq(pageUrl("e"), tabs[3].url); |
| 90 })); |
| 91 })); |
| 92 } |
| 93 |
| 94 // Not specifying any move properties moves a tab to the end of the window |
| 95 chrome.tabs.move(moveTabIds['a'], pass(function(tabA) { |
| 96 assertEq(3, tabA.index); |
| 97 // Specifying only a windowId moves a tab to the end of that window |
| 98 chrome.tabs.move(moveTabIds['e'], {"windowId": secondWindowId}, |
| 99 pass(function(tabE) { |
| 100 assertEq(3, tabE.index); |
| 101 checkMoveResults(); |
| 102 })); |
| 103 })); |
| 104 }, |
| 105 |
75 function remove() { | 106 function remove() { |
76 chrome.tabs.remove(moveTabIds["d"], pass(function() { | 107 chrome.tabs.remove(moveTabIds["d"], pass(function() { |
77 chrome.tabs.getAllInWindow(secondWindowId, | 108 chrome.tabs.getAllInWindow(secondWindowId, |
78 pass(function(tabs) { | 109 pass(function(tabs) { |
79 assertEq(2, tabs.length); | 110 assertEq(3, tabs.length); |
80 assertEq(pageUrl("b"), tabs[0].url); | 111 assertEq(pageUrl("b"), tabs[0].url); |
81 assertEq("chrome://newtab/", tabs[1].url); | 112 assertEq(kChromeUINewTabURL, tabs[1].url); |
| 113 assertEq(pageUrl("e"), tabs[2].url); |
82 })); | 114 })); |
83 })); | 115 })); |
84 }, | 116 }, |
85 | 117 |
86 function moveMultipleTabs() { | 118 function moveMultipleTabs() { |
87 chrome.tabs.move([moveTabIds['e'], moveTabIds['c']], | 119 chrome.tabs.move([moveTabIds['c'], moveTabIds['a']], |
88 {windowId: secondWindowId, index: 1}, | 120 {"windowId": secondWindowId, "index": 1}, |
89 pass(function(tabsA) { | 121 pass(function(tabsA) { |
90 assertEq(2, tabsA.length); | 122 assertEq(2, tabsA.length); |
91 assertEq(secondWindowId, tabsA[0].windowId); | 123 assertEq(secondWindowId, tabsA[0].windowId); |
92 assertEq(pageUrl('e'), tabsA[0].url); | 124 assertEq(pageUrl('c'), tabsA[0].url); |
93 assertEq(1, tabsA[0].index); | 125 assertEq(1, tabsA[0].index); |
94 assertEq(secondWindowId, tabsA[1].windowId); | 126 assertEq(secondWindowId, tabsA[1].windowId); |
95 assertEq(pageUrl('c'), tabsA[1].url); | 127 assertEq(pageUrl('a'), tabsA[1].url); |
96 assertEq(2, tabsA[1].index); | 128 assertEq(2, tabsA[1].index); |
97 chrome.tabs.query({windowId: secondWindowId}, pass(function(tabsB) { | 129 chrome.tabs.query({"windowId": secondWindowId}, pass(function(tabsB) { |
98 assertEq(4, tabsB.length); | 130 assertEq(5, tabsB.length); |
99 })); | 131 })); |
100 })); | 132 })); |
101 }, | 133 }, |
102 | 134 |
103 function removeMultipleTabs() { | 135 function removeMultipleTabs() { |
104 chrome.tabs.remove([moveTabIds['e'], moveTabIds['c']], pass(function() { | 136 chrome.tabs.remove([moveTabIds['e'], moveTabIds['c']], pass(function() { |
105 chrome.tabs.query({windowId: secondWindowId}, pass(function(tabs) { | 137 chrome.tabs.query({"windowId": secondWindowId}, pass(function(tabs) { |
106 assertEq(2, tabs.length); | 138 assertEq(3, tabs.length); |
107 assertEq(pageUrl("b"), tabs[0].url); | 139 assertEq(pageUrl("b"), tabs[0].url); |
108 assertEq("chrome://newtab/", tabs[1].url); | 140 assertEq(pageUrl("a"), tabs[1].url); |
| 141 assertEq(kChromeUINewTabURL, tabs[2].url); |
109 })); | 142 })); |
110 })); | 143 })); |
111 }, | 144 }, |
112 | 145 |
113 // Make sure we don't crash when the index is out of range. | 146 // Make sure we don't crash when the index is out of range. |
114 function moveToInvalidTab() { | 147 function moveToInvalidTab() { |
115 var error_msg = "Invalid value for argument 2. Property 'index': " + | 148 var error_msg = "Invalid value for argument 2. Property 'index': " + |
116 "Value must not be less than 0."; | 149 "Value must not be less than 0."; |
117 try { | 150 try { |
118 chrome.tabs.move(moveTabIds['b'], {index: -1}, function(tab) { | 151 chrome.tabs.move(moveTabIds['b'], {"index": -1}, function(tab) { |
119 chrome.test.fail("Moved a tab to an invalid index"); | 152 chrome.test.fail("Moved a tab to an invalid index"); |
120 }); | 153 }); |
121 } catch (e) { | 154 } catch (e) { |
122 assertEq(error_msg, e.message); | 155 assertEq(error_msg, e.message); |
123 } | 156 } |
124 chrome.tabs.move(moveTabIds['b'], {index: 10000}, pass(function(tabB) { | 157 chrome.tabs.move(moveTabIds['b'], {"index": 10000}, pass(function(tabB) { |
125 assertEq(1, tabB.index); | 158 assertEq(2, tabB.index); |
126 })); | 159 })); |
127 }, | 160 }, |
128 | 161 |
129 // Move a tab to the current window. | 162 // Move a tab to the current window. |
130 function moveToCurrentWindow() { | 163 function moveToCurrentWindow() { |
131 chrome.windows.getCurrent(pass(function(win) { | 164 chrome.windows.getCurrent(pass(function(win) { |
132 var targetWin = win.id == firstWindowId ? secondWindowId : firstWindowId; | 165 var targetWin = win.id == firstWindowId ? secondWindowId : firstWindowId; |
133 chrome.tabs.query({windowId: targetWin}, pass(function(tabs) { | 166 chrome.tabs.query({"windowId": targetWin}, pass(function(tabs) { |
134 chrome.tabs.move(tabs[0].id, | 167 chrome.tabs.move(tabs[0].id, |
135 {windowId: chrome.windows.WINDOW_ID_CURRENT, index:0}, | 168 {"windowId": chrome.windows.WINDOW_ID_CURRENT, |
| 169 "index": 0}, |
136 pass(function(tab) { | 170 pass(function(tab) { |
137 assertEq(win.id, tab.windowId); | 171 assertEq(win.id, tab.windowId); |
138 })); | 172 })); |
139 })); | 173 })); |
140 })); | 174 })); |
141 } | 175 } |
142 ]); | 176 ]); |
OLD | NEW |