Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_name_folder_controller_unittest.mm
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.
4
5 #import <Cocoa/Cocoa.h>
6
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/core/browser/bookmark_model.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #import "testing/gtest_mac.h"
16 #include "testing/platform_test.h"
17
18 using base::ASCIIToUTF16;
19
20 class BookmarkNameFolderControllerTest : public CocoaProfileTest {
21 };
22
23
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());
29
30   base::scoped_nsobject<BookmarkNameFolderController> controller(
31       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
32                                                          profile:profile()
33                                                           parent:parent
34                                                         newIndex:0]);
35   [controller window];  // force nib load
36
37   // Do nothing.
38   [controller cancel:nil];
39   EXPECT_EQ(0, parent->child_count());
40
41   // Change name then cancel.
42   [controller setFolderName:@"Bozo"];
43   [controller cancel:nil];
44   EXPECT_EQ(0, parent->child_count());
45
46   // Add a new folder.
47   [controller ok:nil];
48   EXPECT_EQ(1, parent->child_count());
49   EXPECT_TRUE(parent->GetChild(0)->is_folder());
50   EXPECT_EQ(ASCIIToUTF16("Bozo"), parent->GetChild(0)->GetTitle());
51 }
52
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();
57
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());
64
65   base::scoped_nsobject<BookmarkNameFolderController> controller(
66       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
67                                                          profile: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 = BookmarkModelFactory::GetForProfile(profile());
85   const BookmarkNode* parent = model->bookmark_bar_node();
86   EXPECT_EQ(0, parent->child_count());
87
88   base::scoped_nsobject<BookmarkNameFolderController> controller(
89       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
90                                                          profile:profile()
91                                                           parent:parent
92                                                         newIndex:0]);
93
94   [controller window];  // force nib load
95
96   // Click OK without changing the name
97   [controller ok:nil];
98   EXPECT_EQ(1, parent->child_count());
99   EXPECT_TRUE(parent->GetChild(0)->is_folder());
100 }
101
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());
107
108   base::scoped_nsobject<BookmarkNameFolderController> controller(
109       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
110                                                          profile:profile()
111                                                           parent:parent
112                                                         newIndex:0]);
113   [controller window];  // force nib load
114
115   // Change the name to blank, click OK.
116   [controller setFolderName:@""];
117   [controller ok:nil];
118   EXPECT_EQ(1, parent->child_count());
119   EXPECT_TRUE(parent->GetChild(0)->is_folder());
120 }
121
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"));
128
129   // Rename the folder by creating a controller that originates from
130   // the node.
131   base::scoped_nsobject<BookmarkNameFolderController> controller(
132       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
133                                                          profile:profile()
134                                                             node:folder]);
135   [controller window];  // force nib load
136
137   EXPECT_NSEQ(@"folder", [controller folderName]);
138   [controller setFolderName:@"Zobo"];
139   [controller ok:nil];
140   EXPECT_EQ(1, parent->child_count());
141   EXPECT_TRUE(parent->GetChild(0)->is_folder());
142   EXPECT_EQ(ASCIIToUTF16("Zobo"), parent->GetChild(0)->GetTitle());
143 }
144
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());
149
150   base::scoped_nsobject<BookmarkNameFolderController> controller(
151       [[BookmarkNameFolderController alloc] initWithParentWindow:test_window()
152                                                          profile:profile()
153                                                           parent:parent
154                                                         newIndex:0]);
155   [controller window];  // force nib load
156
157   // We start enabled since the default "New Folder" is added for us.
158   EXPECT_TRUE([[controller okButton] isEnabled]);
159
160   [controller setFolderName:@"Bozo"];
161   EXPECT_TRUE([[controller okButton] isEnabled]);
162   [controller setFolderName:@" "];
163   EXPECT_TRUE([[controller okButton] isEnabled]);
164
165   [controller setFolderName:@""];
166   EXPECT_TRUE([[controller okButton] isEnabled]);
167 }
168