diff options
author | mad@chromium.org <mad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-10 00:08:25 +0000 |
---|---|---|
committer | mad@chromium.org <mad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-10 00:08:25 +0000 |
commit | a340f0e92c4293be6194316541ee6ba05f6f7a57 (patch) | |
tree | 5376f3905da78b3bf7b101ec9a057d5921177e10 | |
parent | b7df1b78329163801d0a0e782153ef26e1ec4ce4 (diff) | |
download | chromium_src-a340f0e92c4293be6194316541ee6ba05f6f7a57.zip chromium_src-a340f0e92c4293be6194316541ee6ba05f6f7a57.tar.gz chromium_src-a340f0e92c4293be6194316541ee6ba05f6f7a57.tar.bz2 |
Added an new argument to chrome.experimental.bookmarkManager.paste so that we can insert the pasted bookmarks after the selection.
BUG=49362
TEST=Make sure that pasting Bookmarks in the BookmarkManager view makes sense.
Review URL: http://codereview.chromium.org/6460011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@74383 0039d316-1c4b-4281-b951-d872f2087c98
4 files changed, 99 insertions, 32 deletions
diff --git a/chrome/browser/extensions/extension_bookmark_manager_api.cc b/chrome/browser/extensions/extension_bookmark_manager_api.cc index 7f77ac5..074f69f 100644 --- a/chrome/browser/extensions/extension_bookmark_manager_api.cc +++ b/chrome/browser/extensions/extension_bookmark_manager_api.cc @@ -43,10 +43,10 @@ const BookmarkNode* GetNodeFromArguments(BookmarkModel* model, // Gets a vector of bookmark nodes from the argument list of IDs. // This returns false in the case of failure. bool GetNodesFromArguments(BookmarkModel* model, const ListValue* args, - std::vector<const BookmarkNode*>* nodes) { + size_t args_index, std::vector<const BookmarkNode*>* nodes) { ListValue* ids; - if (!args->GetList(0, &ids)) + if (!args->GetList(args_index, &ids)) return false; size_t count = ids->GetSize(); @@ -214,7 +214,7 @@ bool ClipboardBookmarkManagerFunction::CopyOrCut(bool cut) { BookmarkModel* model = profile()->GetBookmarkModel(); std::vector<const BookmarkNode*> nodes; EXTENSION_FUNCTION_VALIDATE(GetNodesFromArguments(model, args_.get(), - &nodes)); + 0, &nodes)); bookmark_utils::CopyToClipboard(model, nodes, cut); return true; } @@ -237,7 +237,20 @@ bool PasteBookmarkManagerFunction::RunImpl() { bool can_paste = bookmark_utils::CanPasteFromClipboard(parent_node); if (!can_paste) return false; - bookmark_utils::PasteFromClipboard(model, parent_node, -1); + + // We want to use the highest index of the selected nodes as a destination. + std::vector<const BookmarkNode*> nodes; + // No need to test return value, if we got an empty list, we insert at end. + GetNodesFromArguments(model, args_.get(), 1, &nodes); + int highest_index = -1; // -1 means insert at end of list. + for (size_t node = 0; node < nodes.size(); ++node) { + // + 1 so that we insert after the selection. + int this_node_index = parent_node->IndexOfChild(nodes[node]) + 1; + if (this_node_index > highest_index) + highest_index = this_node_index; + } + + bookmark_utils::PasteFromClipboard(model, parent_node, highest_index); return true; } @@ -340,7 +353,7 @@ bool StartDragBookmarkManagerFunction::RunImpl() { BookmarkModel* model = profile()->GetBookmarkModel(); std::vector<const BookmarkNode*> nodes; EXTENSION_FUNCTION_VALIDATE( - GetNodesFromArguments(model, args_.get(), &nodes)); + GetNodesFromArguments(model, args_.get(), 0, &nodes)); if (dispatcher()->render_view_host()->delegate()->GetRenderViewType() == ViewType::TAB_CONTENTS) { diff --git a/chrome/browser/resources/bookmark_manager/main.html b/chrome/browser/resources/bookmark_manager/main.html index e1987d8..06d9667 100644 --- a/chrome/browser/resources/bookmark_manager/main.html +++ b/chrome/browser/resources/bookmark_manager/main.html @@ -1655,7 +1655,8 @@ function handleCommand(e) { break; case 'paste-command': selectItemsAfterUserAction(list); - chrome.experimental.bookmarkManager.paste(list.parentId); + chrome.experimental.bookmarkManager.paste(list.parentId, + getSelectedBookmarkIds()); break; case 'sort-command': chrome.experimental.bookmarkManager.sortChildren(list.parentId); diff --git a/chrome/common/extensions/api/extension_api.json b/chrome/common/extensions/api/extension_api.json index 4fc7492..f3582aa 100644 --- a/chrome/common/extensions/api/extension_api.json +++ b/chrome/common/extensions/api/extension_api.json @@ -2572,10 +2572,18 @@ { "name": "paste", "type": "function", - "description": "Pastes bookmarks from the clipboard into the parent folder", + "description": "Pastes bookmarks from the clipboard into the parent folder after the last selected node", "nodoc": "true", "parameters": [ {"type": "string", "name": "parentId"}, + { + "name": "selectedIdList", + "description": "An array of string-valued ids for selected bookmarks", + "optional": true, + "type": "array", + "items": {"type": "string"}, + "minItems": 0 + }, {"type": "function", "name": "callback", "optional": true, "parameters": []} ] }, diff --git a/chrome/test/data/extensions/api_test/bookmark_manager/test.js b/chrome/test/data/extensions/api_test/bookmark_manager/test.js index f9d2cdd..e4ec7cd 100644 --- a/chrome/test/data/extensions/api_test/bookmark_manager/test.js +++ b/chrome/test/data/extensions/api_test/bookmark_manager/test.js @@ -7,7 +7,7 @@ const assertEq = chrome.test.assertEq; const assertTrue = chrome.test.assertTrue; const bookmarks = chrome.bookmarks; const bookmarkManager = chrome.experimental.bookmarkManager; -var node, node2, count, emptyFolder, emptyFolder2; +var fooNode, fooNode2, barNode, gooNode, count, emptyFolder, emptyFolder2; var folder, nodeA, nodeB; var childFolder, grandChildFolder, childNodeA, childNodeB; @@ -111,33 +111,58 @@ var tests = [ // operations to finish. function clipboard() { // Create a new bookmark. - node = { + fooNode = { parentId: '1', title: 'Foo', url: 'http://www.example.com/foo' }; - + emptyFolder = { parentId: '1', title: 'Empty Folder' } - bookmarks.create(node, pass(function(result) { - node.id = result.id; - node.index = result.index; + bookmarks.create(fooNode, pass(function(result) { + fooNode.id = result.id; + fooNode.index = result.index; count = result.index + 1; })); - + bookmarks.create(emptyFolder, pass(function(result) { emptyFolder.id = result.id; emptyFolder.index = result.index; count = result.index + 1; })); + + // Create a couple more bookmarks to test proper insertion of pasted items. + barNode = { + parentId: '1', + title: 'Bar', + url: 'http://www.example.com/bar' + }; + + bookmarks.create(barNode, pass(function(result) { + barNode.id = result.id; + barNode.index = result.index; + count = result.index + 1; + })); + + gooNode = { + parentId: '1', + title: 'Goo', + url: 'http://www.example.com/goo' + }; + + bookmarks.create(gooNode, pass(function(result) { + gooNode.id = result.id; + gooNode.index = result.index; + count = result.index + 1; + })); }, function clipboard2() { - // Copy it. - bookmarkManager.copy([node.id]); + // Copy the fooNode. + bookmarkManager.copy([fooNode.id]); // Ensure canPaste is now true. bookmarkManager.canPaste('1', pass(function(result) { @@ -147,22 +172,22 @@ var tests = [ // Paste it. bookmarkManager.paste('1'); - // Ensure it got added. + // Ensure it got added at the end. bookmarks.getChildren('1', pass(function(result) { count++; assertEq(count, result.length); - node2 = result[result.length - 1]; + fooNode2 = result[result.length - 1]; - assertEq(node.title, node2.title); - assertEq(node.url, node2.url); - assertEq(node.parentId, node2.parentId); + assertEq(fooNode.title, fooNode2.title); + assertEq(fooNode.url, fooNode2.url); + assertEq(fooNode.parentId, fooNode2.parentId); })); }, function clipboard3() { - // Cut this and the previous bookmark. - bookmarkManager.cut([node.id, node2.id]); + // Cut fooNode bookmarks. + bookmarkManager.cut([fooNode.id, fooNode2.id]); // Ensure count decreased by 2. bookmarks.getChildren('1', pass(function(result) { @@ -177,19 +202,32 @@ var tests = [ }, function clipboard4() { - // Paste. - bookmarkManager.paste('1'); + // Paste the cut bookmarks at a specific position between bar and goo. + bookmarkManager.paste('1', [barNode.id]); - // Check the last two bookmarks. + // Check that the two bookmarks were pasted after bar. bookmarks.getChildren('1', pass(function(result) { count += 2; assertEq(count, result.length); - var last = result[result.length - 2]; - var last2 = result[result.length - 1]; + // Look for barNode's index. + for (var barIndex = 0; barIndex < result.length; barIndex++) { + if (result[barIndex].id == barNode.id) + break; + } + assertTrue(barIndex + 2 < result.length); + + var last = result[barIndex + 1]; + var last2 = result[barIndex + 2]; + assertEq(fooNode.title, last.title); + assertEq(fooNode.url, last.url); + assertEq(fooNode.parentId, last.parentId); assertEq(last.title, last2.title); assertEq(last.url, last2.url); assertEq(last.parentId, last2.parentId); + + // Remember last2 id, so we can use it in next test. + fooNode2.id = last2.id; })); }, @@ -203,15 +241,22 @@ var tests = [ assertTrue(result, 'Should be able to paste now'); })); - // Paste it. - bookmarkManager.paste('1'); + // Paste it at the end of a multiple selection. + bookmarkManager.paste('1', [barNode.id, fooNode2.id]); - // Ensure it got added. + // Ensure it got added at the right place. bookmarks.getChildren('1', pass(function(result) { count++; assertEq(count, result.length); - emptyFolder2 = result[result.length - 1]; + // Look for fooNode2's index. + for (var foo2Index = 0; foo2Index < result.length; foo2Index++) { + if (result[foo2Index].id == fooNode2.id) + break; + } + assertTrue(foo2Index + 1 < result.length); + + emptyFolder2 = result[foo2Index + 1]; assertEq(emptyFolder2.title, emptyFolder.title); assertEq(emptyFolder2.url, emptyFolder.url); |