Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_editor_base_controller_unittest.mm
blobda854146ee2da06012a15678048361052c3c4306
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/strings/sys_string_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/bookmarks/bookmark_model.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_editor_controller.h"
12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
13 #include "chrome/test/base/test_browser_window.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "grit/generated_resources.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #import "testing/gtest_mac.h"
18 #include "testing/platform_test.h"
19 #include "ui/base/l10n/l10n_util_mac.h"
21 using base::ASCIIToUTF16;
23 class BookmarkEditorBaseControllerTest : public CocoaProfileTest {
24  public:
25   BookmarkEditorBaseController* controller_;  // weak
26   const BookmarkNode* folder_a_;
27   const BookmarkNode* folder_b_;
28   const BookmarkNode* folder_b_0_;
29   const BookmarkNode* folder_b_3_;
30   const BookmarkNode* folder_c_;
32   void CreateModel() {
33     // Set up a small bookmark hierarchy, which will look as follows:
34     //    a      b      c    d
35     //     a-0    b-0    c-0
36     //     a-1     b-00  c-1
37     //     a-2    b-1    c-2
38     //            b-2    c-3
39     //            b-3
40     //             b-30
41     //             b-31
42     //            b-4
43     BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
44     const BookmarkNode* root = model->bookmark_bar_node();
45     folder_a_ = model->AddFolder(root, 0, ASCIIToUTF16("a"));
46     model->AddURL(folder_a_, 0, ASCIIToUTF16("a-0"), GURL("http://a-0.com"));
47     model->AddURL(folder_a_, 1, ASCIIToUTF16("a-1"), GURL("http://a-1.com"));
48     model->AddURL(folder_a_, 2, ASCIIToUTF16("a-2"), GURL("http://a-2.com"));
50     folder_b_ = model->AddFolder(root, 1, ASCIIToUTF16("b"));
51     folder_b_0_ = model->AddFolder(folder_b_, 0, ASCIIToUTF16("b-0"));
52     model->AddURL(folder_b_0_, 0, ASCIIToUTF16("bb-0"),
53                   GURL("http://bb-0.com"));
54     model->AddURL(folder_b_, 1, ASCIIToUTF16("b-1"), GURL("http://b-1.com"));
55     model->AddURL(folder_b_, 2, ASCIIToUTF16("b-2"), GURL("http://b-2.com"));
56     folder_b_3_ = model->AddFolder(folder_b_, 3, ASCIIToUTF16("b-3"));
57     model->AddURL(folder_b_3_, 0, ASCIIToUTF16("b-30"),
58                   GURL("http://b-30.com"));
59     model->AddURL(folder_b_3_, 1, ASCIIToUTF16("b-31"),
60                   GURL("http://b-31.com"));
61     model->AddURL(folder_b_, 4, ASCIIToUTF16("b-4"), GURL("http://b-4.com"));
63     folder_c_ = model->AddFolder(root, 2, ASCIIToUTF16("c"));
64     model->AddURL(folder_c_, 0, ASCIIToUTF16("c-0"), GURL("http://c-0.com"));
65     model->AddURL(folder_c_, 1, ASCIIToUTF16("c-1"), GURL("http://c-1.com"));
66     model->AddURL(folder_c_, 2, ASCIIToUTF16("c-2"), GURL("http://c-2.com"));
67     model->AddURL(folder_c_, 3, ASCIIToUTF16("c-3"), GURL("http://c-3.com"));
68     model->AddFolder(folder_c_, 4, ASCIIToUTF16("c-4"));
70     model->AddURL(root, 3, ASCIIToUTF16("d"), GURL("http://d-0.com"));
71   }
73   virtual BookmarkEditorBaseController* CreateController() {
74     return [[BookmarkEditorBaseController alloc]
75             initWithParentWindow:test_window()
76                          nibName:@"BookmarkAllTabs"
77                          profile:profile()
78                           parent:folder_b_0_
79                              url:GURL()
80                            title:base::string16()
81                    configuration:BookmarkEditor::SHOW_TREE];
82   }
84   virtual void SetUp() OVERRIDE {
85     CocoaProfileTest::SetUp();
86     ASSERT_TRUE(profile());
88     CreateModel();
89     controller_ = CreateController();
90     EXPECT_TRUE([controller_ window]);
91     [controller_ runAsModalSheet];
92   }
94   virtual void TearDown() OVERRIDE {
95     controller_ = NULL;
96     CocoaTest::TearDown();
97   }
99   virtual Browser* CreateBrowser() OVERRIDE {
100     Browser::CreateParams native_params(profile(), chrome::GetActiveDesktop());
101     return chrome::CreateBrowserWithTestWindowForParams(&native_params);
102   }
105 TEST_F(BookmarkEditorBaseControllerTest, VerifyBookmarkTestModel) {
106   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
107   const BookmarkNode* root = model->bookmark_bar_node();
108   EXPECT_EQ(4, root->child_count());
109   // a
110   const BookmarkNode* child = root->GetChild(0);
111   EXPECT_EQ(3, child->child_count());
112   const BookmarkNode* subchild = child->GetChild(0);
113   EXPECT_EQ(0, subchild->child_count());
114   subchild = child->GetChild(1);
115   EXPECT_EQ(0, subchild->child_count());
116   subchild = child->GetChild(2);
117   EXPECT_EQ(0, subchild->child_count());
118   // b
119   child = root->GetChild(1);
120   EXPECT_EQ(5, child->child_count());
121   subchild = child->GetChild(0);
122   EXPECT_EQ(1, subchild->child_count());
123   const BookmarkNode* subsubchild = subchild->GetChild(0);
124   EXPECT_EQ(0, subsubchild->child_count());
125   subchild = child->GetChild(1);
126   EXPECT_EQ(0, subchild->child_count());
127   subchild = child->GetChild(2);
128   EXPECT_EQ(0, subchild->child_count());
129   subchild = child->GetChild(3);
130   EXPECT_EQ(2, subchild->child_count());
131   subsubchild = subchild->GetChild(0);
132   EXPECT_EQ(0, subsubchild->child_count());
133   subsubchild = subchild->GetChild(1);
134   EXPECT_EQ(0, subsubchild->child_count());
135   subchild = child->GetChild(4);
136   EXPECT_EQ(0, subchild->child_count());
137   // c
138   child = root->GetChild(2);
139   EXPECT_EQ(5, child->child_count());
140   subchild = child->GetChild(0);
141   EXPECT_EQ(0, subchild->child_count());
142   subchild = child->GetChild(1);
143   EXPECT_EQ(0, subchild->child_count());
144   subchild = child->GetChild(2);
145   EXPECT_EQ(0, subchild->child_count());
146   subchild = child->GetChild(3);
147   EXPECT_EQ(0, subchild->child_count());
148   subchild = child->GetChild(4);
149   EXPECT_EQ(0, subchild->child_count());
150   // d
151   child = root->GetChild(3);
152   EXPECT_EQ(0, child->child_count());
153   [controller_ cancel:nil];
156 TEST_F(BookmarkEditorBaseControllerTest, NodeSelection) {
157   EXPECT_TRUE([controller_ folderTreeArray]);
158   [controller_ selectTestNodeInBrowser:folder_b_3_];
159   const BookmarkNode* node = [controller_ selectedNode];
160   EXPECT_EQ(node, folder_b_3_);
161   [controller_ cancel:nil];
164 TEST_F(BookmarkEditorBaseControllerTest, CreateFolder) {
165   EXPECT_EQ(2, folder_b_3_->child_count());
166   [controller_ selectTestNodeInBrowser:folder_b_3_];
167   NSString* expectedName =
168       l10n_util::GetNSStringWithFixup(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME);
169   [controller_ setDisplayName:expectedName];
170   [controller_ newFolder:nil];
171   NSArray* selectionPaths = [controller_ tableSelectionPaths];
172   EXPECT_EQ(1U, [selectionPaths count]);
173   NSIndexPath* selectionPath = [selectionPaths objectAtIndex:0];
174   EXPECT_EQ(4U, [selectionPath length]);
175   BookmarkFolderInfo* newFolderInfo = [controller_ selectedFolder];
176   EXPECT_TRUE(newFolderInfo);
177   NSString* newFolderName = [newFolderInfo folderName];
178   EXPECT_NSEQ(expectedName, newFolderName);
179   [controller_ createNewFolders];
180   // Verify that the tab folder was added to the new folder.
181   EXPECT_EQ(3, folder_b_3_->child_count());
182   [controller_ cancel:nil];
185 TEST_F(BookmarkEditorBaseControllerTest, CreateTwoFolders) {
186   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
187   const BookmarkNode* bar = model->bookmark_bar_node();
188   // Create 2 folders which are children of the bar.
189   [controller_ selectTestNodeInBrowser:bar];
190   [controller_ newFolder:nil];
191   [controller_ selectTestNodeInBrowser:bar];
192   [controller_ newFolder:nil];
193   // If we do NOT crash on createNewFolders, success!
194   // (e.g. http://crbug.com/47877 is fixed).
195   [controller_ createNewFolders];
196   [controller_ cancel:nil];
199 TEST_F(BookmarkEditorBaseControllerTest, SelectedFolderDeleted) {
200   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
201   [controller_ selectTestNodeInBrowser:folder_b_3_];
202   EXPECT_EQ(folder_b_3_, [controller_ selectedNode]);
204   // Delete the selected node, and verify it's no longer selected:
205   model->Remove(folder_b_, 3);
206   EXPECT_NE(folder_b_3_, [controller_ selectedNode]);
208   [controller_ cancel:nil];
211 TEST_F(BookmarkEditorBaseControllerTest, SelectedFoldersParentDeleted) {
212   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
213   const BookmarkNode* root = model->bookmark_bar_node();
214   [controller_ selectTestNodeInBrowser:folder_b_3_];
215   EXPECT_EQ(folder_b_3_, [controller_ selectedNode]);
217   // Delete the selected node's parent, and verify it's no longer selected:
218   model->Remove(root, 1);
219   EXPECT_NE(folder_b_3_, [controller_ selectedNode]);
221   [controller_ cancel:nil];
224 TEST_F(BookmarkEditorBaseControllerTest, FolderAdded) {
225   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
226   const BookmarkNode* root = model->bookmark_bar_node();
228   // Add a folder node to the model, and verify it can be selected in the tree:
229   const BookmarkNode* folder_added = model->AddFolder(
230       root, 0, ASCIIToUTF16("added"));
231   [controller_ selectTestNodeInBrowser:folder_added];
232   EXPECT_EQ(folder_added, [controller_ selectedNode]);
234   [controller_ cancel:nil];
237 // Verifies expandeNodes and getExpandedNodes.
238 TEST_F(BookmarkEditorBaseControllerTest, ExpandedState) {
239   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
241   // Sets up the state we're going to expand.
242   BookmarkExpandedStateTracker::Nodes nodes;
243   nodes.insert(model->bookmark_bar_node());
244   nodes.insert(folder_b_);
245   nodes.insert(folder_c_);
247   // Initial state shouldn't match expected state, otherwise this test isn't
248   // really going to test anything.
249   BookmarkExpandedStateTracker::Nodes actual = [controller_ getExpandedNodes];
250   EXPECT_NE(actual, nodes);
252   [controller_ expandNodes:nodes];
254   actual = [controller_ getExpandedNodes];
256   EXPECT_EQ(nodes, actual);
258   [controller_ cancel:nil];
261 class BookmarkFolderInfoTest : public CocoaTest { };
263 TEST_F(BookmarkFolderInfoTest, Construction) {
264   NSMutableArray* children = [NSMutableArray arrayWithObject:@"child"];
265   // We just need a pointer, and any pointer will do.
266   const BookmarkNode* fakeNode =
267       reinterpret_cast<const BookmarkNode*>(&children);
268   BookmarkFolderInfo* info =
269     [BookmarkFolderInfo bookmarkFolderInfoWithFolderName:@"name"
270                                               folderNode:fakeNode
271                                                 children:children];
272   EXPECT_TRUE(info);
273   EXPECT_EQ([info folderName], @"name");
274   EXPECT_EQ([info children], children);
275   EXPECT_EQ([info folderNode], fakeNode);