BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_name_folder_controller_unittest.mm
blob7e9a3b032159d32658d7e9b4a4810f3ddcc3a1fa
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;
19 using bookmarks::BookmarkModel;
20 using bookmarks::BookmarkNode;
22 class BookmarkNameFolderControllerTest : public CocoaProfileTest {
26 // Simple add of a node (at the end).
27 TEST_F(BookmarkNameFolderControllerTest, AddNew) {
28   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
29   const BookmarkNode* parent = model->bookmark_bar_node();
30   EXPECT_EQ(0, parent->child_count());
32   base::scoped_nsobject<BookmarkNameFolderController> controller(
33       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
34                                                          profile:profile()
35                                                           parent:parent
36                                                         newIndex:0]);
37   [controller window];  // force nib load
39   // Do nothing.
40   [controller cancel:nil];
41   EXPECT_EQ(0, parent->child_count());
43   // Change name then cancel.
44   [controller setFolderName:@"Bozo"];
45   [controller cancel:nil];
46   EXPECT_EQ(0, parent->child_count());
48   // Add a new folder.
49   [controller ok:nil];
50   EXPECT_EQ(1, parent->child_count());
51   EXPECT_TRUE(parent->GetChild(0)->is_folder());
52   EXPECT_EQ(ASCIIToUTF16("Bozo"), parent->GetChild(0)->GetTitle());
55 // Add new but specify a sibling.
56 TEST_F(BookmarkNameFolderControllerTest, AddNewWithSibling) {
57   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
58   const BookmarkNode* parent = model->bookmark_bar_node();
60   // Add 2 nodes.  We will place the new folder in the middle of these.
61   model->AddURL(parent, 0, ASCIIToUTF16("title 1"),
62                 GURL("http://www.google.com"));
63   model->AddURL(parent, 1, ASCIIToUTF16("title 3"),
64                 GURL("http://www.google.com"));
65   EXPECT_EQ(2, parent->child_count());
67   base::scoped_nsobject<BookmarkNameFolderController> controller(
68       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
69                                                          profile:profile()
70                                                           parent:parent
71                                                         newIndex:1]);
72   [controller window];  // force nib load
74   // Add a new folder.
75   [controller setFolderName:@"middle"];
76   [controller ok:nil];
78   // Confirm we now have 3, and that the new one is in the middle.
79   EXPECT_EQ(3, parent->child_count());
80   EXPECT_TRUE(parent->GetChild(1)->is_folder());
81   EXPECT_EQ(ASCIIToUTF16("middle"), parent->GetChild(1)->GetTitle());
84 // Make sure we are allowed to create a folder named "New Folder".
85 TEST_F(BookmarkNameFolderControllerTest, AddNewDefaultName) {
86  BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
87   const BookmarkNode* parent = model->bookmark_bar_node();
88   EXPECT_EQ(0, parent->child_count());
90   base::scoped_nsobject<BookmarkNameFolderController> controller(
91       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
92                                                          profile:profile()
93                                                           parent:parent
94                                                         newIndex:0]);
96   [controller window];  // force nib load
98   // Click OK without changing the name
99   [controller ok:nil];
100   EXPECT_EQ(1, parent->child_count());
101   EXPECT_TRUE(parent->GetChild(0)->is_folder());
104 // Make sure we are allowed to create a folder with an empty name.
105 TEST_F(BookmarkNameFolderControllerTest, AddNewBlankName) {
106   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
107   const BookmarkNode* parent = model->bookmark_bar_node();
108   EXPECT_EQ(0, parent->child_count());
110   base::scoped_nsobject<BookmarkNameFolderController> controller(
111       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
112                                                          profile:profile()
113                                                           parent:parent
114                                                         newIndex:0]);
115   [controller window];  // force nib load
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());
124 TEST_F(BookmarkNameFolderControllerTest, Rename) {
125   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
126   const BookmarkNode* parent = model->bookmark_bar_node();
127   const BookmarkNode* folder = model->AddFolder(parent,
128                                                 parent->child_count(),
129                                                 ASCIIToUTF16("folder"));
131   // Rename the folder by creating a controller that originates from
132   // the node.
133   base::scoped_nsobject<BookmarkNameFolderController> controller(
134       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
135                                                          profile:profile()
136                                                             node:folder]);
137   [controller window];  // force nib load
139   EXPECT_NSEQ(@"folder", [controller folderName]);
140   [controller setFolderName:@"Zobo"];
141   [controller ok:nil];
142   EXPECT_EQ(1, parent->child_count());
143   EXPECT_TRUE(parent->GetChild(0)->is_folder());
144   EXPECT_EQ(ASCIIToUTF16("Zobo"), parent->GetChild(0)->GetTitle());
147 TEST_F(BookmarkNameFolderControllerTest, EditAndConfirmOKButton) {
148   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
149   const BookmarkNode* parent = model->bookmark_bar_node();
150   EXPECT_EQ(0, parent->child_count());
152   base::scoped_nsobject<BookmarkNameFolderController> controller(
153       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
154                                                          profile:profile()
155                                                           parent:parent
156                                                         newIndex:0]);
157   [controller window];  // force nib load
159   // We start enabled since the default "New Folder" is added for us.
160   EXPECT_TRUE([[controller okButton] isEnabled]);
162   [controller setFolderName:@"Bozo"];
163   EXPECT_TRUE([[controller okButton] isEnabled]);
164   [controller setFolderName:@" "];
165   EXPECT_TRUE([[controller okButton] isEnabled]);
167   [controller setFolderName:@""];
168   EXPECT_TRUE([[controller okButton] isEnabled]);