1 // Copyright (c) 2011 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. 4 5 #import <Cocoa/Cocoa.h> 6 7 #include "base/memory/scoped_nsobject.h" 8 #include "base/utf_string_conversions.h" 9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_name_folder_controller.h" 10 #include "chrome/browser/ui/cocoa/browser_test_helper.h" 11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 #import "testing/gtest_mac.h" 14 #include "testing/platform_test.h" 15 16 class BookmarkNameFolderControllerTest : public CocoaTest { 17 public: 18 BrowserTestHelper helper_; 19 }; 20 21 22 // Simple add of a node (at the end). 23 TEST_F(BookmarkNameFolderControllerTest, AddNew) { 24 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 25 const BookmarkNode* parent = model->GetBookmarkBarNode(); 26 EXPECT_EQ(0, parent->child_count()); 27 28 scoped_nsobject<BookmarkNameFolderController> 29 controller([[BookmarkNameFolderController alloc] 30 initWithParentWindow:test_window() 31 profile:helper_.profile() 32 parent:parent 33 newIndex:0]); 34 [controller window]; // force nib load 35 36 // Do nothing. 37 [controller cancel:nil]; 38 EXPECT_EQ(0, parent->child_count()); 39 40 // Change name then cancel. 41 [controller setFolderName:@"Bozo"]; 42 [controller cancel:nil]; 43 EXPECT_EQ(0, parent->child_count()); 44 45 // Add a new folder. 46 [controller ok:nil]; 47 EXPECT_EQ(1, parent->child_count()); 48 EXPECT_TRUE(parent->GetChild(0)->is_folder()); 49 EXPECT_EQ(ASCIIToUTF16("Bozo"), parent->GetChild(0)->GetTitle()); 50 } 51 52 // Add new but specify a sibling. 53 TEST_F(BookmarkNameFolderControllerTest, AddNewWithSibling) { 54 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 55 const BookmarkNode* parent = model->GetBookmarkBarNode(); 56 57 // Add 2 nodes. We will place the new folder in the middle of these. 58 model->AddURL(parent, 0, ASCIIToUTF16("title 1"), 59 GURL("http://www.google.com")); 60 model->AddURL(parent, 1, ASCIIToUTF16("title 3"), 61 GURL("http://www.google.com")); 62 EXPECT_EQ(2, parent->child_count()); 63 64 scoped_nsobject<BookmarkNameFolderController> 65 controller([[BookmarkNameFolderController alloc] 66 initWithParentWindow:test_window() 67 profile:helper_.profile() 68 parent:parent 69 newIndex:1]); 70 [controller window]; // force nib load 71 72 // Add a new folder. 73 [controller setFolderName:@"middle"]; 74 [controller ok:nil]; 75 76 // Confirm we now have 3, and that the new one is in the middle. 77 EXPECT_EQ(3, parent->child_count()); 78 EXPECT_TRUE(parent->GetChild(1)->is_folder()); 79 EXPECT_EQ(ASCIIToUTF16("middle"), parent->GetChild(1)->GetTitle()); 80 } 81 82 // Make sure we are allowed to create a folder named "New Folder". 83 TEST_F(BookmarkNameFolderControllerTest, AddNewDefaultName) { 84 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 85 const BookmarkNode* parent = model->GetBookmarkBarNode(); 86 EXPECT_EQ(0, parent->child_count()); 87 88 scoped_nsobject<BookmarkNameFolderController> 89 controller([[BookmarkNameFolderController alloc] 90 initWithParentWindow:test_window() 91 profile:helper_.profile() 92 parent:parent 93 newIndex:0]); 94 95 [controller window]; // force nib load 96 97 // Click OK without changing the name 98 [controller ok:nil]; 99 EXPECT_EQ(1, parent->child_count()); 100 EXPECT_TRUE(parent->GetChild(0)->is_folder()); 101 } 102 103 // Make sure we are allowed to create a folder with an empty name. 104 TEST_F(BookmarkNameFolderControllerTest, AddNewBlankName) { 105 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 106 const BookmarkNode* parent = model->GetBookmarkBarNode(); 107 EXPECT_EQ(0, parent->child_count()); 108 109 scoped_nsobject<BookmarkNameFolderController> 110 controller([[BookmarkNameFolderController alloc] 111 initWithParentWindow:test_window() 112 profile:helper_.profile() 113 parent:parent 114 newIndex:0]); 115 [controller window]; // force nib load 116 117 // Change the name to blank, click OK. 118 [controller setFolderName:@""]; 119 [controller ok:nil]; 120 EXPECT_EQ(1, parent->child_count()); 121 EXPECT_TRUE(parent->GetChild(0)->is_folder()); 122 } 123 124 TEST_F(BookmarkNameFolderControllerTest, Rename) { 125 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 126 const BookmarkNode* parent = model->GetBookmarkBarNode(); 127 const BookmarkNode* folder = model->AddFolder(parent, 128 parent->child_count(), 129 ASCIIToUTF16("folder")); 130 131 // Rename the folder by creating a controller that originates from 132 // the node. 133 scoped_nsobject<BookmarkNameFolderController> 134 controller([[BookmarkNameFolderController alloc] 135 initWithParentWindow:test_window() 136 profile:helper_.profile() 137 node:folder]); 138 [controller window]; // force nib load 139 140 EXPECT_NSEQ(@"folder", [controller folderName]); 141 [controller setFolderName:@"Zobo"]; 142 [controller ok:nil]; 143 EXPECT_EQ(1, parent->child_count()); 144 EXPECT_TRUE(parent->GetChild(0)->is_folder()); 145 EXPECT_EQ(ASCIIToUTF16("Zobo"), parent->GetChild(0)->GetTitle()); 146 } 147 148 TEST_F(BookmarkNameFolderControllerTest, EditAndConfirmOKButton) { 149 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 150 const BookmarkNode* parent = model->GetBookmarkBarNode(); 151 EXPECT_EQ(0, parent->child_count()); 152 153 scoped_nsobject<BookmarkNameFolderController> 154 controller([[BookmarkNameFolderController alloc] 155 initWithParentWindow:test_window() 156 profile:helper_.profile() 157 parent:parent 158 newIndex:0]); 159 [controller window]; // force nib load 160 161 // We start enabled since the default "New Folder" is added for us. 162 EXPECT_TRUE([[controller okButton] isEnabled]); 163 164 [controller setFolderName:@"Bozo"]; 165 EXPECT_TRUE([[controller okButton] isEnabled]); 166 [controller setFolderName:@" "]; 167 EXPECT_TRUE([[controller okButton] isEnabled]); 168 169 [controller setFolderName:@""]; 170 EXPECT_TRUE([[controller okButton] isEnabled]); 171 } 172 173