Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / bookmarks / bookmark_context_menu_controller_unittest.cc
blob0d8874eda1d05a2cff7ceebaee82c92a05e469b2
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 #include "chrome/browser/ui/bookmarks/bookmark_context_menu_controller.h"
7 #include <string>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/bookmarks/bookmark_model.h"
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
15 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/browser/page_navigator.h"
20 #include "content/public/test/test_browser_thread.h"
21 #include "grit/generated_resources.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/base/clipboard/clipboard.h"
25 using base::ASCIIToUTF16;
26 using content::BrowserThread;
27 using content::OpenURLParams;
28 using content::PageNavigator;
29 using content::WebContents;
31 // PageNavigator implementation that records the URL.
32 class TestingPageNavigator : public PageNavigator {
33 public:
34 virtual WebContents* OpenURL(const OpenURLParams& params) OVERRIDE {
35 urls_.push_back(params.url);
36 return NULL;
39 std::vector<GURL> urls_;
42 class BookmarkContextMenuControllerTest : public testing::Test {
43 public:
44 BookmarkContextMenuControllerTest()
45 : ui_thread_(BrowserThread::UI, &message_loop_),
46 file_thread_(BrowserThread::FILE, &message_loop_),
47 model_(NULL) {
50 virtual void SetUp() OVERRIDE {
51 Reset(false);
53 void Reset(bool incognito) {
54 TestingProfile::Builder builder;
55 if (incognito)
56 builder.SetIncognito();
57 profile_ = builder.Build();
58 profile_->CreateBookmarkModel(true);
60 model_ = BookmarkModelFactory::GetForProfile(profile_.get());
61 test::WaitForBookmarkModelToLoad(model_);
63 AddTestData();
66 virtual void TearDown() OVERRIDE {
67 ui::Clipboard::DestroyClipboardForCurrentThread();
69 // Flush the message loop to make application verifiers happy.
70 message_loop_.RunUntilIdle();
73 protected:
74 base::MessageLoopForUI message_loop_;
75 content::TestBrowserThread ui_thread_;
76 content::TestBrowserThread file_thread_;
77 scoped_ptr<TestingProfile> profile_;
78 BookmarkModel* model_;
79 TestingPageNavigator navigator_;
81 private:
82 // Creates the following structure:
83 // a
84 // F1
85 // f1a
86 // F11
87 // f11a
88 // F2
89 // F3
90 // F4
91 // f4a
92 void AddTestData() {
93 const BookmarkNode* bb_node = model_->bookmark_bar_node();
94 std::string test_base = "file:///c:/tmp/";
95 model_->AddURL(bb_node, 0, ASCIIToUTF16("a"), GURL(test_base + "a"));
96 const BookmarkNode* f1 = model_->AddFolder(bb_node, 1, ASCIIToUTF16("F1"));
97 model_->AddURL(f1, 0, ASCIIToUTF16("f1a"), GURL(test_base + "f1a"));
98 const BookmarkNode* f11 = model_->AddFolder(f1, 1, ASCIIToUTF16("F11"));
99 model_->AddURL(f11, 0, ASCIIToUTF16("f11a"), GURL(test_base + "f11a"));
100 model_->AddFolder(bb_node, 2, ASCIIToUTF16("F2"));
101 model_->AddFolder(bb_node, 3, ASCIIToUTF16("F3"));
102 const BookmarkNode* f4 = model_->AddFolder(bb_node, 4, ASCIIToUTF16("F4"));
103 model_->AddURL(f4, 0, ASCIIToUTF16("f4a"), GURL(test_base + "f4a"));
107 // Tests Deleting from the menu.
108 TEST_F(BookmarkContextMenuControllerTest, DeleteURL) {
109 std::vector<const BookmarkNode*> nodes;
110 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
111 BookmarkContextMenuController controller(
112 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
113 GURL url = model_->bookmark_bar_node()->GetChild(0)->url();
114 ASSERT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
115 // Delete the URL.
116 controller.ExecuteCommand(IDC_BOOKMARK_BAR_REMOVE, 0);
117 // Model shouldn't have URL anymore.
118 ASSERT_FALSE(model_->IsBookmarked(url));
121 // Tests open all on a folder with a couple of bookmarks.
122 TEST_F(BookmarkContextMenuControllerTest, OpenAll) {
123 const BookmarkNode* folder = model_->bookmark_bar_node()->GetChild(1);
124 chrome::OpenAll(NULL, &navigator_, folder, NEW_FOREGROUND_TAB, NULL);
126 // Should have navigated to F1's child, but not F11's child.
127 ASSERT_EQ(static_cast<size_t>(1), navigator_.urls_.size());
128 ASSERT_TRUE(folder->GetChild(0)->url() == navigator_.urls_[0]);
131 // Tests the enabled state of the menus when supplied an empty vector.
132 TEST_F(BookmarkContextMenuControllerTest, EmptyNodes) {
133 BookmarkContextMenuController controller(
134 NULL, NULL, NULL, profile_.get(), NULL, model_->other_node(),
135 std::vector<const BookmarkNode*>());
136 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
137 EXPECT_FALSE(
138 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
139 EXPECT_FALSE(
140 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
141 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
142 EXPECT_TRUE(
143 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
144 EXPECT_TRUE(
145 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
148 // Tests the enabled state of the menus when supplied a vector with a single
149 // url.
150 TEST_F(BookmarkContextMenuControllerTest, SingleURL) {
151 std::vector<const BookmarkNode*> nodes;
152 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
153 BookmarkContextMenuController controller(
154 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
155 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
156 EXPECT_TRUE(
157 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
158 EXPECT_TRUE(
159 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
160 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
161 EXPECT_TRUE(
162 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
163 EXPECT_TRUE(
164 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
167 // Tests the enabled state of the menus when supplied a vector with multiple
168 // urls.
169 TEST_F(BookmarkContextMenuControllerTest, MultipleURLs) {
170 std::vector<const BookmarkNode*> nodes;
171 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
172 nodes.push_back(model_->bookmark_bar_node()->GetChild(1)->GetChild(0));
173 BookmarkContextMenuController controller(
174 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
175 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
176 EXPECT_TRUE(
177 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
178 EXPECT_TRUE(
179 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
180 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
181 EXPECT_TRUE(
182 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
183 EXPECT_TRUE(
184 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
187 // Tests the enabled state of the menus when supplied an vector with a single
188 // folder.
189 TEST_F(BookmarkContextMenuControllerTest, SingleFolder) {
190 std::vector<const BookmarkNode*> nodes;
191 nodes.push_back(model_->bookmark_bar_node()->GetChild(2));
192 BookmarkContextMenuController controller(
193 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
194 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
195 EXPECT_FALSE(
196 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
197 EXPECT_FALSE(
198 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
199 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
200 EXPECT_TRUE(
201 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
202 EXPECT_TRUE(
203 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
206 // Tests the enabled state of the menus when supplied a vector with multiple
207 // folders, all of which are empty.
208 TEST_F(BookmarkContextMenuControllerTest, MultipleEmptyFolders) {
209 std::vector<const BookmarkNode*> nodes;
210 nodes.push_back(model_->bookmark_bar_node()->GetChild(2));
211 nodes.push_back(model_->bookmark_bar_node()->GetChild(3));
212 BookmarkContextMenuController controller(
213 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
214 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
215 EXPECT_FALSE(
216 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
217 EXPECT_FALSE(
218 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
219 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
220 EXPECT_TRUE(
221 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
222 EXPECT_TRUE(
223 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
226 // Tests the enabled state of the menus when supplied a vector with multiple
227 // folders, some of which contain URLs.
228 TEST_F(BookmarkContextMenuControllerTest, MultipleFoldersWithURLs) {
229 std::vector<const BookmarkNode*> nodes;
230 nodes.push_back(model_->bookmark_bar_node()->GetChild(3));
231 nodes.push_back(model_->bookmark_bar_node()->GetChild(4));
232 BookmarkContextMenuController controller(
233 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
234 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
235 EXPECT_TRUE(
236 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
237 EXPECT_TRUE(
238 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
239 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
240 EXPECT_TRUE(
241 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
242 EXPECT_TRUE(
243 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
246 // Tests the enabled state of open incognito.
247 TEST_F(BookmarkContextMenuControllerTest, DisableIncognito) {
248 // Create a new incognito profile.
249 Reset(true);
250 std::vector<const BookmarkNode*> nodes;
251 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
252 BookmarkContextMenuController controller(
253 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
254 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_INCOGNITO));
255 EXPECT_FALSE(
256 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
259 // Tests that you can't remove/edit when showing the other node.
260 TEST_F(BookmarkContextMenuControllerTest, DisabledItemsWithOtherNode) {
261 std::vector<const BookmarkNode*> nodes;
262 nodes.push_back(model_->other_node());
263 BookmarkContextMenuController controller(
264 NULL, NULL, NULL, profile_.get(), NULL, nodes[0], nodes);
265 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_EDIT));
266 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
269 // Tests the enabled state of the menus when supplied an empty vector and null
270 // parent.
271 TEST_F(BookmarkContextMenuControllerTest, EmptyNodesNullParent) {
272 BookmarkContextMenuController controller(
273 NULL, NULL, NULL, profile_.get(), NULL, NULL,
274 std::vector<const BookmarkNode*>());
275 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
276 EXPECT_FALSE(
277 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
278 EXPECT_FALSE(
279 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
280 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
281 EXPECT_FALSE(
282 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
283 EXPECT_FALSE(
284 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
287 // Tests the enabled state of the menus when supplied a vector containing just
288 // the top-level bookmark bar node.
289 TEST_F(BookmarkContextMenuControllerTest, BookmarkBar) {
290 std::vector<const BookmarkNode*> nodes;
291 nodes.push_back(model_->bookmark_bar_node());
292 BookmarkContextMenuController controller(
293 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
294 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
295 EXPECT_TRUE(
296 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
297 EXPECT_TRUE(
298 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
299 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
300 EXPECT_TRUE(
301 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
302 EXPECT_TRUE(
303 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
306 TEST_F(BookmarkContextMenuControllerTest, CutCopyPasteNode) {
307 const BookmarkNode* bb_node = model_->bookmark_bar_node();
308 std::vector<const BookmarkNode*> nodes;
309 nodes.push_back(bb_node->GetChild(0));
310 scoped_ptr<BookmarkContextMenuController> controller(
311 new BookmarkContextMenuController(
312 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes));
313 EXPECT_TRUE(controller->IsCommandIdEnabled(IDC_COPY));
314 EXPECT_TRUE(controller->IsCommandIdEnabled(IDC_CUT));
316 // Copy the URL.
317 controller->ExecuteCommand(IDC_COPY, 0);
319 controller.reset(new BookmarkContextMenuController(
320 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes));
321 int old_count = bb_node->child_count();
322 controller->ExecuteCommand(IDC_PASTE, 0);
324 ASSERT_TRUE(bb_node->GetChild(1)->is_url());
325 ASSERT_EQ(old_count + 1, bb_node->child_count());
326 ASSERT_EQ(bb_node->GetChild(0)->url(), bb_node->GetChild(1)->url());
328 controller.reset(new BookmarkContextMenuController(
329 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes));
330 // Cut the URL.
331 controller->ExecuteCommand(IDC_CUT, 0);
332 ASSERT_TRUE(bb_node->GetChild(0)->is_url());
333 ASSERT_TRUE(bb_node->GetChild(1)->is_folder());
334 ASSERT_EQ(old_count, bb_node->child_count());