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 "testing/gtest/include/gtest/gtest.h"
14 #import "testing/gtest_mac.h"
15 #include "testing/platform_test.h"
17 using base::ASCIIToUTF16;
19 class BookmarkNameFolderControllerTest : public CocoaProfileTest {
23 // Simple add of a node (at the end).
24 TEST_F(BookmarkNameFolderControllerTest, AddNew) {
25 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
26 const BookmarkNode* parent = model->bookmark_bar_node();
27 EXPECT_EQ(0, parent->child_count());
29 base::scoped_nsobject<BookmarkNameFolderController> controller(
30 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
34 [controller window]; // force nib load
37 [controller cancel:nil];
38 EXPECT_EQ(0, parent->child_count());
40 // Change name then cancel.
41 [controller setFolderName:@"Bozo"];
42 [controller cancel:nil];
43 EXPECT_EQ(0, parent->child_count());
47 EXPECT_EQ(1, parent->child_count());
48 EXPECT_TRUE(parent->GetChild(0)->is_folder());
49 EXPECT_EQ(ASCIIToUTF16("Bozo"), parent->GetChild(0)->GetTitle());
52 // Add new but specify a sibling.
53 TEST_F(BookmarkNameFolderControllerTest, AddNewWithSibling) {
54 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
55 const BookmarkNode* parent = model->bookmark_bar_node();
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());
64 base::scoped_nsobject<BookmarkNameFolderController> controller(
65 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
69 [controller window]; // force nib load
72 [controller setFolderName:@"middle"];
75 // Confirm we now have 3, and that the new one is in the middle.
76 EXPECT_EQ(3, parent->child_count());
77 EXPECT_TRUE(parent->GetChild(1)->is_folder());
78 EXPECT_EQ(ASCIIToUTF16("middle"), parent->GetChild(1)->GetTitle());
81 // Make sure we are allowed to create a folder named "New Folder".
82 TEST_F(BookmarkNameFolderControllerTest, AddNewDefaultName) {
83 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
84 const BookmarkNode* parent = model->bookmark_bar_node();
85 EXPECT_EQ(0, parent->child_count());
87 base::scoped_nsobject<BookmarkNameFolderController> controller(
88 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
93 [controller window]; // force nib load
95 // Click OK without changing the name
97 EXPECT_EQ(1, parent->child_count());
98 EXPECT_TRUE(parent->GetChild(0)->is_folder());
101 // Make sure we are allowed to create a folder with an empty name.
102 TEST_F(BookmarkNameFolderControllerTest, AddNewBlankName) {
103 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
104 const BookmarkNode* parent = model->bookmark_bar_node();
105 EXPECT_EQ(0, parent->child_count());
107 base::scoped_nsobject<BookmarkNameFolderController> controller(
108 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
112 [controller window]; // force nib load
114 // Change the name to blank, click OK.
115 [controller setFolderName:@""];
117 EXPECT_EQ(1, parent->child_count());
118 EXPECT_TRUE(parent->GetChild(0)->is_folder());
121 TEST_F(BookmarkNameFolderControllerTest, Rename) {
122 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
123 const BookmarkNode* parent = model->bookmark_bar_node();
124 const BookmarkNode* folder = model->AddFolder(parent,
125 parent->child_count(),
126 ASCIIToUTF16("folder"));
128 // Rename the folder by creating a controller that originates from
130 base::scoped_nsobject<BookmarkNameFolderController> controller(
131 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
134 [controller window]; // force nib load
136 EXPECT_NSEQ(@"folder", [controller folderName]);
137 [controller setFolderName:@"Zobo"];
139 EXPECT_EQ(1, parent->child_count());
140 EXPECT_TRUE(parent->GetChild(0)->is_folder());
141 EXPECT_EQ(ASCIIToUTF16("Zobo"), parent->GetChild(0)->GetTitle());
144 TEST_F(BookmarkNameFolderControllerTest, EditAndConfirmOKButton) {
145 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
146 const BookmarkNode* parent = model->bookmark_bar_node();
147 EXPECT_EQ(0, parent->child_count());
149 base::scoped_nsobject<BookmarkNameFolderController> controller(
150 [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
154 [controller window]; // force nib load
156 // We start enabled since the default "New Folder" is added for us.
157 EXPECT_TRUE([[controller okButton] isEnabled]);
159 [controller setFolderName:@"Bozo"];
160 EXPECT_TRUE([[controller okButton] isEnabled]);
161 [controller setFolderName:@" "];
162 EXPECT_TRUE([[controller okButton] isEnabled]);
164 [controller setFolderName:@""];
165 EXPECT_TRUE([[controller okButton] isEnabled]);