diff options
author | snej@chromium.org <snej@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-05 23:09:30 +0000 |
---|---|---|
committer | snej@chromium.org <snej@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-05 23:09:30 +0000 |
commit | 080bf942acf0599e0d1811f07f5cef304cf355c4 (patch) | |
tree | 1e9104f67bd1c1ecf75b53f4c0003b02493979cf /chrome/browser/cocoa | |
parent | 0f28580a84220d218288bfdec6bb689b2e1b8d29 (diff) | |
download | chromium_src-080bf942acf0599e0d1811f07f5cef304cf355c4.zip chromium_src-080bf942acf0599e0d1811f07f5cef304cf355c4.tar.gz chromium_src-080bf942acf0599e0d1811f07f5cef304cf355c4.tar.bz2 |
Add additional pasteboard tests to BookmarkTreeControllerTest.
Split up one method in BookmarkTreeController to improve testability.
BUG=13149
TEST=BookmarkTreeControllerTest.MoveNodes
Review URL: http://codereview.chromium.org/523069
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35573 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa')
3 files changed, 110 insertions, 23 deletions
diff --git a/chrome/browser/cocoa/bookmark_tree_controller.h b/chrome/browser/cocoa/bookmark_tree_controller.h index 5e7b66e..cd0730e 100644 --- a/chrome/browser/cocoa/bookmark_tree_controller.h +++ b/chrome/browser/cocoa/bookmark_tree_controller.h @@ -55,11 +55,14 @@ class BookmarkNode; // Exposed only for unit tests. @interface BookmarkTreeController (UnitTesting) - +@property (readonly) NSOutlineView* outline; - (BOOL)copyToPasteboard:(NSPasteboard*)pb; - (BOOL)pasteFromPasteboard:(NSPasteboard*)pb; -@property (readonly) NSOutlineView* outline; - +- (const BookmarkNode*)nodeForDropOnItem:(id)item + proposedIndex:(NSInteger*)childIndex; +- (void)moveNodes:(std::vector<const BookmarkNode*>)nodes + toFolder:(const BookmarkNode*)dstParent + atIndex:(int)dstIndex; @end diff --git a/chrome/browser/cocoa/bookmark_tree_controller_pasteboard.mm b/chrome/browser/cocoa/bookmark_tree_controller_pasteboard.mm index e617b4c..4ae5bab 100644 --- a/chrome/browser/cocoa/bookmark_tree_controller_pasteboard.mm +++ b/chrome/browser/cocoa/bookmark_tree_controller_pasteboard.mm @@ -111,13 +111,13 @@ static void flattenNode(const BookmarkNode* node, for (id item in items) { draggedNodes_.push_back([self nodeFromItem:item]); } - [pb addTypes:[NSArray arrayWithObject: kCustomPboardType] owner: self]; + [pb addTypes:[NSArray arrayWithObject:kCustomPboardType] owner:self]; [pb setData:[NSData data] forType:kCustomPboardType]; } // Add single URL: if ([urls count] == 1) { - [pb addTypes:[NSArray arrayWithObject: NSURLPboardType] owner: self]; + [pb addTypes:[NSArray arrayWithObject:NSURLPboardType] owner:self]; NSString* firstURLStr = [urls objectAtIndex:0]; [pb setString:firstURLStr forType:NSURLPboardType]; } @@ -339,34 +339,43 @@ static NSDictionary* makeBookmarkPlistEntry(NSString* name, NSString* urlStr) { return NSDragOperationCopy; } -// Actually handles the drop. -- (BOOL)outlineView:(NSOutlineView*)outlineView - acceptDrop:(id <NSDraggingInfo>)info - item:(id)item - childIndex:(NSInteger)childIndex -{ - NSPasteboard* pb = [info draggingPasteboard]; - +// Determine the parent to insert into and the child index to insert at. +- (const BookmarkNode*)nodeForDropOnItem:(id)item + proposedIndex:(NSInteger*)childIndex { const BookmarkNode* targetNode = [self nodeFromItem:item]; - - // Determine the parent to insert into and the child index to insert at. if (!targetNode->is_folder()) { // If our target is a leaf, and we are dropping on it. - if (childIndex == NSOutlineViewDropOnItemIndex) { - return NO; + if (*childIndex == NSOutlineViewDropOnItemIndex) { + return NULL; } else { // We will be dropping on the item's parent at the target index // of this child, plus one. const BookmarkNode* oldTargetNode = targetNode; targetNode = targetNode->GetParent(); - childIndex = targetNode->IndexOfChild(oldTargetNode) + 1; + *childIndex = targetNode->IndexOfChild(oldTargetNode) + 1; } } else { - if (childIndex == NSOutlineViewDropOnItemIndex) { + if (*childIndex == NSOutlineViewDropOnItemIndex) { // Insert it at the end, if we were dropping on it - childIndex = targetNode->GetChildCount(); + *childIndex = targetNode->GetChildCount(); } } + return targetNode; +} + +// Actually handles the drop. +- (BOOL)outlineView:(NSOutlineView*)outlineView + acceptDrop:(id <NSDraggingInfo>)info + item:(id)item + childIndex:(NSInteger)childIndex +{ + NSPasteboard* pb = [info draggingPasteboard]; + + // Determine the parent to insert into and the child index to insert at. + const BookmarkNode* targetNode = [self nodeForDropOnItem:item + proposedIndex:&childIndex]; + if (!targetNode) + return NO; if ([info draggingSource] == outlineView && [[pb types] containsObject:kCustomPboardType]) { @@ -394,7 +403,7 @@ static NSDictionary* makeBookmarkPlistEntry(NSString* name, NSString* urlStr) { } - (BOOL)pasteFromPasteboard:(NSPasteboard*)pb { - NSArray* plist = [self readPropertyListFromPasteboard: pb]; + NSArray* plist = [self readPropertyListFromPasteboard:pb]; if (!plist) return NO; diff --git a/chrome/browser/cocoa/bookmark_tree_controller_unittest.mm b/chrome/browser/cocoa/bookmark_tree_controller_unittest.mm index dc22f19..0720a1c 100644 --- a/chrome/browser/cocoa/bookmark_tree_controller_unittest.mm +++ b/chrome/browser/cocoa/bookmark_tree_controller_unittest.mm @@ -50,6 +50,14 @@ class BookmarkTreeControllerTest : public CocoaTest { return [manager_ itemFromNode:node]; } + id AddFolderToBar(const std::wstring& title) { + BookmarkModel* model = [manager_ bookmarkModel]; + const BookmarkNode* bar = model->GetBookmarkBarNode(); + const BookmarkNode* node; + node = model->AddGroup(bar, bar->GetChildCount(), title); + return [manager_ itemFromNode:node]; + } + BrowserTestHelper browser_test_helper_; BookmarkManagerController* manager_; BookmarkTreeController* treeController_; @@ -60,11 +68,16 @@ TEST_F(BookmarkTreeControllerTest, Model) { // Select nothing in the group list and check tree is empty: BookmarkGroupsController* groupsController = [manager_ groupsController]; [groupsController setSelectedGroup:nil]; - ASSERT_EQ(nil, [treeController_ group]); + EXPECT_EQ(nil, [treeController_ group]); // Select the bookmarks bar and check that it's shown in the tree: id barItem = SelectBar(); - ASSERT_EQ(barItem, [treeController_ group]); + EXPECT_EQ(barItem, [treeController_ group]); + + // Check the outline-item mapping methods: + const BookmarkNode* barNode = [manager_ nodeFromItem:barItem]; + EXPECT_EQ(nil, [treeController_ itemFromNode:barNode]); + EXPECT_EQ(barNode, [treeController_ nodeFromItem:nil]); } TEST_F(BookmarkTreeControllerTest, Selection) { @@ -88,6 +101,68 @@ TEST_F(BookmarkTreeControllerTest, Selection) { EXPECT_TRUE([sel isEqual:[treeController_ selectedItems]]); } +TEST_F(BookmarkTreeControllerTest, MoveNodes) { + // Add three bookmarks and a folder. + NSOutlineView* outline = [treeController_ outline]; + SelectBar(); + id test1 = AddToBar(L"Test 1", "http://example.com/test1"); + id test2 = AddToBar(L"Test 2", "http://example.com/test2"); + id folder = AddFolderToBar(L"Folder"); + id test3 = AddToBar(L"Test 3", "http://example.com/test3"); + [outline expandItem:folder]; + + const BookmarkNode* groupNode = [treeController_ nodeFromItem:nil]; + const BookmarkNode* node1 = [manager_ nodeFromItem:test1]; + const BookmarkNode* node2 = [manager_ nodeFromItem:test2]; + const BookmarkNode* folderNode = [manager_ nodeFromItem:folder]; + const BookmarkNode* node3 = [manager_ nodeFromItem:test3]; + + // Check where dropped URLs would go: + NSInteger dropIndex = NSOutlineViewDropOnItemIndex; + const BookmarkNode* target = [treeController_ nodeForDropOnItem:test1 + proposedIndex:&dropIndex]; + EXPECT_EQ(NULL, target); + dropIndex = 0; + target = [treeController_ nodeForDropOnItem:test1 + proposedIndex:&dropIndex]; + EXPECT_EQ(groupNode, target); + EXPECT_EQ(1, dropIndex); + dropIndex = 0; + target = [treeController_ nodeForDropOnItem:folder + proposedIndex:&dropIndex]; + EXPECT_EQ(folderNode, target); + EXPECT_EQ(0, dropIndex); + dropIndex = NSOutlineViewDropOnItemIndex; + target = [treeController_ nodeForDropOnItem:folder + proposedIndex:&dropIndex]; + EXPECT_EQ(folderNode, target); + EXPECT_EQ(0, dropIndex); + + // Move the first and third item into the folder. + std::vector<const BookmarkNode*> nodes; + nodes.push_back(node1); + nodes.push_back(node3); + [treeController_ moveNodes:nodes toFolder:folderNode atIndex:0]; + + // Verify bookmark model hierarchy. + EXPECT_EQ(folderNode, node1->GetParent()); + EXPECT_EQ(folderNode, node3->GetParent()); + EXPECT_EQ(groupNode, folderNode->GetParent()); + EXPECT_EQ(groupNode, node2->GetParent()); + + // Verify NSOutlineView hierarchy. + EXPECT_EQ(folder, [outline parentForItem:test1]); + EXPECT_EQ(folder, [outline parentForItem:test3]); + EXPECT_EQ(nil, [outline parentForItem:test2]); + EXPECT_EQ(nil, [outline parentForItem:folder]); + + // Verify the moved nodes are selected. + EXPECT_TRUE([[outline selectedRowIndexes] + isEqual:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)]]); + NSArray* sel = [treeController_ selectedItems]; + EXPECT_TRUE([sel isEqual:([NSArray arrayWithObjects:test1, test3, nil])]); +} + TEST_F(BookmarkTreeControllerTest, CopyURLs) { SelectBar(); AddToBar(L"Test 1", "http://example.com/test1"); |