| 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 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); |
| 84 |
| 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 })); |
| 93 } |
| 94 |
| 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 })); |
| 101 }, |
| 102 |
| 75 function remove() { | 103 function remove() { |
| 76 chrome.tabs.remove(moveTabIds["d"], pass(function() { | 104 chrome.tabs.remove(moveTabIds["d"], pass(function() { |
| 77 chrome.tabs.getAllInWindow(secondWindowId, | 105 chrome.tabs.getAllInWindow(secondWindowId, |
| 78 pass(function(tabs) { | 106 pass(function(tabs) { |
| 79 assertEq(2, tabs.length); | 107 assertEq(3, tabs.length); |
| 80 assertEq(pageUrl("b"), tabs[0].url); | 108 assertEq(pageUrl("b"), tabs[0].url); |
| 81 assertEq("chrome://newtab/", tabs[1].url); | 109 assertEq(kChromeUINewTabURL, tabs[1].url); |
| 110 assertEq(pageUrl("e"), tabs[2].url); |
| 82 })); | 111 })); |
| 83 })); | 112 })); |
| 84 }, | 113 }, |
| 85 | 114 |
| 86 function moveMultipleTabs() { | 115 function moveMultipleTabs() { |
| 87 chrome.tabs.move([moveTabIds['e'], moveTabIds['c']], | 116 chrome.tabs.move([moveTabIds['c'], moveTabIds['a']], |
| 88 {windowId: secondWindowId, index: 1}, | 117 {"windowId": secondWindowId, "index": 1}, |
| 89 pass(function(tabsA) { | 118 pass(function(tabsA) { |
| 90 assertEq(2, tabsA.length); | 119 assertEq(2, tabsA.length); |
| 91 assertEq(secondWindowId, tabsA[0].windowId); | 120 assertEq(secondWindowId, tabsA[0].windowId); |
| 92 assertEq(pageUrl('e'), tabsA[0].url); | 121 assertEq(pageUrl('c'), tabsA[0].url); |
| 93 assertEq(1, tabsA[0].index); | 122 assertEq(1, tabsA[0].index); |
| 94 assertEq(secondWindowId, tabsA[1].windowId); | 123 assertEq(secondWindowId, tabsA[1].windowId); |
| 95 assertEq(pageUrl('c'), tabsA[1].url); | 124 assertEq(pageUrl('a'), tabsA[1].url); |
| 96 assertEq(2, tabsA[1].index); | 125 assertEq(2, tabsA[1].index); |
| 97 chrome.tabs.query({windowId: secondWindowId}, pass(function(tabsB) { | 126 chrome.tabs.query({"windowId": secondWindowId}, pass(function(tabsB) { |
| 98 assertEq(4, tabsB.length); | 127 assertEq(5, tabsB.length); |
| 99 })); | 128 })); |
| 100 })); | 129 })); |
| 101 }, | 130 }, |
| 102 | 131 |
| 103 function removeMultipleTabs() { | 132 function removeMultipleTabs() { |
| 104 chrome.tabs.remove([moveTabIds['e'], moveTabIds['c']], pass(function() { | 133 chrome.tabs.remove([moveTabIds['e'], moveTabIds['c']], pass(function() { |
| 105 chrome.tabs.query({windowId: secondWindowId}, pass(function(tabs) { | 134 chrome.tabs.query({"windowId": secondWindowId}, pass(function(tabs) { |
| 106 assertEq(2, tabs.length); | 135 assertEq(3, tabs.length); |
| 107 assertEq(pageUrl("b"), tabs[0].url); | 136 assertEq(pageUrl("b"), tabs[0].url); |
| 108 assertEq("chrome://newtab/", tabs[1].url); | 137 assertEq(pageUrl("a"), tabs[1].url); |
| 138 assertEq(kChromeUINewTabURL, tabs[2].url); |
| 109 })); | 139 })); |
| 110 })); | 140 })); |
| 111 }, | 141 }, |
| 112 | 142 |
| 113 // Make sure we don't crash when the index is out of range. | 143 // Make sure we don't crash when the index is out of range. |
| 114 function moveToInvalidTab() { | 144 function moveToInvalidTab() { |
| 115 var error_msg = "Invalid value for argument 2. Property 'index': " + | 145 var error_msg = "Invalid value for argument 2. Property 'index': " + |
| 116 "Value must not be less than 0."; | 146 "Value must not be less than -1."; |
| 117 try { | 147 try { |
| 118 chrome.tabs.move(moveTabIds['b'], {index: -1}, function(tab) { | 148 chrome.tabs.move(moveTabIds['b'], {"index": -2}, function(tab) { |
| 119 chrome.test.fail("Moved a tab to an invalid index"); | 149 chrome.test.fail("Moved a tab to an invalid index"); |
| 120 }); | 150 }); |
| 121 } catch (e) { | 151 } catch (e) { |
| 122 assertEq(error_msg, e.message); | 152 assertEq(error_msg, e.message); |
| 123 } | 153 } |
| 124 chrome.tabs.move(moveTabIds['b'], {index: 10000}, pass(function(tabB) { | 154 chrome.tabs.move(moveTabIds['b'], {"index": 10000}, pass(function(tabB) { |
| 125 assertEq(1, tabB.index); | 155 assertEq(2, tabB.index); |
| 126 })); | 156 })); |
| 127 }, | 157 }, |
| 128 | 158 |
| 129 // Move a tab to the current window. | 159 // Move a tab to the current window. |
| 130 function moveToCurrentWindow() { | 160 function moveToCurrentWindow() { |
| 131 chrome.windows.getCurrent(pass(function(win) { | 161 chrome.windows.getCurrent(pass(function(win) { |
| 132 var targetWin = win.id == firstWindowId ? secondWindowId : firstWindowId; | 162 var targetWin = win.id == firstWindowId ? secondWindowId : firstWindowId; |
| 133 chrome.tabs.query({windowId: targetWin}, pass(function(tabs) { | 163 chrome.tabs.query({"windowId": targetWin}, pass(function(tabs) { |
| 134 chrome.tabs.move(tabs[0].id, | 164 chrome.tabs.move(tabs[0].id, |
| 135 {windowId: chrome.windows.WINDOW_ID_CURRENT, index:0}, | 165 {"windowId": chrome.windows.WINDOW_ID_CURRENT, |
| 166 "index": 0}, |
| 136 pass(function(tab) { | 167 pass(function(tab) { |
| 137 assertEq(win.id, tab.windowId); | 168 assertEq(win.id, tab.windowId); |
| 138 })); | 169 })); |
| 139 })); | 170 })); |
| 140 })); | 171 })); |
| 141 } | 172 } |
| 142 ]); | 173 ]); |
| OLD | NEW |