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
3 // found in the LICENSE file.
5 #import <Cocoa/Cocoa.h>
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_name_folder_controller.h"
11 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/bookmarks/browser/bookmark_model.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #import "testing/gtest_mac.h"
16 #include "testing/platform_test.h"
18 using base::ASCIIToUTF16;
20 class BookmarkNameFolderControllerTest : public CocoaProfileTest {
24 // Simple add of a node (at the end).
25 TEST_F(BookmarkNameFolderControllerTest, AddNew) {
26 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
27 const BookmarkNode* parent = model->bookmark_bar_node();
28 EXPECT_EQ(0, parent->child_count());
30 base::scoped_nsobject<BookmarkNameFolderController> controller(
31 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
35 [controller window]; // force nib load
38 [controller cancel:nil];
39 EXPECT_EQ(0, parent->child_count());
41 // Change name then cancel.
42 [controller setFolderName:@"Bozo"];
43 [controller cancel:nil];
44 EXPECT_EQ(0, parent->child_count());
48 EXPECT_EQ(1, parent->child_count());
49 EXPECT_TRUE(parent->GetChild(0)->is_folder());
50 EXPECT_EQ(ASCIIToUTF16("Bozo"), parent->GetChild(0)->GetTitle());
53 // Add new but specify a sibling.
54 TEST_F(BookmarkNameFolderControllerTest, AddNewWithSibling) {
55 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
56 const BookmarkNode* parent = model->bookmark_bar_node();
58 // Add 2 nodes. We will place the new folder in the middle of these.
59 model->AddURL(parent, 0, ASCIIToUTF16("title 1"),
60 GURL("http://www.google.com"));
61 model->AddURL(parent, 1, ASCIIToUTF16("title 3"),
62 GURL("http://www.google.com"));
63 EXPECT_EQ(2, parent->child_count());
65 base::scoped_nsobject<BookmarkNameFolderController> controller(
66 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
70 [controller window]; // force nib load
73 [controller setFolderName:@"middle"];
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());
82 // Make sure we are allowed to create a folder named "New Folder".
83 TEST_F(BookmarkNameFolderControllerTest, AddNewDefaultName) {
84 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
85 const BookmarkNode* parent = model->bookmark_bar_node();
86 EXPECT_EQ(0, parent->child_count());
88 base::scoped_nsobject<BookmarkNameFolderController> controller(
89 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
94 [controller window]; // force nib load
96 // Click OK without changing the name
98 EXPECT_EQ(1, parent->child_count());
99 EXPECT_TRUE(parent->GetChild(0)->is_folder());
102 // Make sure we are allowed to create a folder with an empty name.
103 TEST_F(BookmarkNameFolderControllerTest, AddNewBlankName) {
104 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
105 const BookmarkNode* parent = model->bookmark_bar_node();
106 EXPECT_EQ(0, parent->child_count());
108 base::scoped_nsobject<BookmarkNameFolderController> controller(
109 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
113 [controller window]; // force nib load
115 // Change the name to blank, click OK.
116 [controller setFolderName:@""];
118 EXPECT_EQ(1, parent->child_count());
119 EXPECT_TRUE(parent->GetChild(0)->is_folder());
122 TEST_F(BookmarkNameFolderControllerTest, Rename) {
123 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
124 const BookmarkNode* parent = model->bookmark_bar_node();
125 const BookmarkNode* folder = model->AddFolder(parent,
126 parent->child_count(),
127 ASCIIToUTF16("folder"));
129 // Rename the folder by creating a controller that originates from
131 base::scoped_nsobject<BookmarkNameFolderController> controller(
132 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
135 [controller window]; // force nib load
137 EXPECT_NSEQ(@"folder", [controller folderName]);
138 [controller setFolderName:@"Zobo"];
140 EXPECT_EQ(1, parent->child_count());
141 EXPECT_TRUE(parent->GetChild(0)->is_folder());
142 EXPECT_EQ(ASCIIToUTF16("Zobo"), parent->GetChild(0)->GetTitle());
145 TEST_F(BookmarkNameFolderControllerTest, EditAndConfirmOKButton) {
146 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
147 const BookmarkNode* parent = model->bookmark_bar_node();
148 EXPECT_EQ(0, parent->child_count());
150 base::scoped_nsobject<BookmarkNameFolderController> controller(
151 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
155 [controller window]; // force nib load
157 // We start enabled since the default "New Folder" is added for us.
158 EXPECT_TRUE([[controller okButton] isEnabled]);
160 [controller setFolderName:@"Bozo"];
161 EXPECT_TRUE([[controller okButton] isEnabled]);
162 [controller setFolderName:@" "];
163 EXPECT_TRUE([[controller okButton] isEnabled]);
165 [controller setFolderName:@""];
166 EXPECT_TRUE([[controller okButton] isEnabled]);