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