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