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