blob: 97a239710a44e24cff6851ea66cb3bae89620459 [file] [log] [blame]
[email protected]0568e6ca2010-05-06 21:06:481// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]7f856be2008-10-29 23:38:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1df533b2010-02-02 22:50:075#include "base/scoped_ptr.h"
[email protected]7f856be2008-10-29 23:38:066#include "chrome/browser/bookmarks/bookmark_model.h"
7#include "chrome/browser/bookmarks/bookmark_utils.h"
[email protected]6fad2632009-11-02 05:59:378#include "chrome/browser/chrome_thread.h"
[email protected]052313b2010-02-19 09:43:089#include "chrome/browser/pref_service.h"
[email protected]7f856be2008-10-29 23:38:0610#include "chrome/browser/profile.h"
[email protected]f3ec7742009-01-15 00:59:1611#include "chrome/browser/tab_contents/page_navigator.h"
[email protected]052313b2010-02-19 09:43:0812#include "chrome/browser/views/bookmark_context_menu.h"
13#include "chrome/common/pref_names.h"
[email protected]7f856be2008-10-29 23:38:0614#include "chrome/test/testing_profile.h"
[email protected]34ac8f32009-02-22 23:03:2715#include "grit/generated_resources.h"
[email protected]7f856be2008-10-29 23:38:0616#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]66cb8352009-04-22 00:12:2918#if defined(OS_WIN)
19#include "chrome/browser/views/bookmark_bar_view.h"
20#endif
21
[email protected]7f856be2008-10-29 23:38:0622namespace {
23
24// PageNavigator implementation that records the URL.
25class TestingPageNavigator : public PageNavigator {
26 public:
27 virtual void OpenURL(const GURL& url,
28 const GURL& referrer,
29 WindowOpenDisposition disposition,
30 PageTransition::Type transition) {
31 urls_.push_back(url);
32 }
33
34 std::vector<GURL> urls_;
35};
36
37} // namespace
38
39class BookmarkContextMenuTest : public testing::Test {
40 public:
41 BookmarkContextMenuTest()
[email protected]6fad2632009-11-02 05:59:3742 : ui_thread_(ChromeThread::UI, &message_loop_),
43 file_thread_(ChromeThread::FILE, &message_loop_),
44 model_(NULL) {
[email protected]7f856be2008-10-29 23:38:0645 }
46
47 virtual void SetUp() {
[email protected]66cb8352009-04-22 00:12:2948#if defined(OS_WIN)
[email protected]7f856be2008-10-29 23:38:0649 BookmarkBarView::testing_ = true;
[email protected]66cb8352009-04-22 00:12:2950#endif
[email protected]7f856be2008-10-29 23:38:0651
52 profile_.reset(new TestingProfile());
53 profile_->set_has_history_service(true);
54 profile_->CreateBookmarkModel(true);
55 profile_->BlockUntilBookmarkModelLoaded();
56
57 model_ = profile_->GetBookmarkModel();
58
59 AddTestData();
60 }
61
62 virtual void TearDown() {
[email protected]66cb8352009-04-22 00:12:2963#if defined(OS_WIN)
[email protected]7f856be2008-10-29 23:38:0664 BookmarkBarView::testing_ = false;
[email protected]66cb8352009-04-22 00:12:2965#endif
[email protected]7f856be2008-10-29 23:38:0666
67 // Flush the message loop to make Purify happy.
68 message_loop_.RunAllPending();
69 }
70
71 protected:
72 MessageLoopForUI message_loop_;
[email protected]6fad2632009-11-02 05:59:3773 ChromeThread ui_thread_;
74 ChromeThread file_thread_;
[email protected]7f856be2008-10-29 23:38:0675 scoped_ptr<TestingProfile> profile_;
76 BookmarkModel* model_;
77 TestingPageNavigator navigator_;
78
79 private:
80 // Creates the following structure:
81 // a
82 // F1
83 // f1a
84 // F11
85 // f11a
86 // F2
87 // F3
88 // F4
89 // f4a
90 void AddTestData() {
91 std::string test_base = "file:///c:/tmp/";
92
93 model_->AddURL(model_->GetBookmarkBarNode(), 0, L"a",
94 GURL(test_base + "a"));
[email protected]b3c33d462009-06-26 22:29:2095 const BookmarkNode* f1 =
[email protected]7f856be2008-10-29 23:38:0696 model_->AddGroup(model_->GetBookmarkBarNode(), 1, L"F1");
97 model_->AddURL(f1, 0, L"f1a", GURL(test_base + "f1a"));
[email protected]b3c33d462009-06-26 22:29:2098 const BookmarkNode* f11 = model_->AddGroup(f1, 1, L"F11");
[email protected]7f856be2008-10-29 23:38:0699 model_->AddURL(f11, 0, L"f11a", GURL(test_base + "f11a"));
100 model_->AddGroup(model_->GetBookmarkBarNode(), 2, L"F2");
101 model_->AddGroup(model_->GetBookmarkBarNode(), 3, L"F3");
[email protected]b3c33d462009-06-26 22:29:20102 const BookmarkNode* f4 =
[email protected]7f856be2008-10-29 23:38:06103 model_->AddGroup(model_->GetBookmarkBarNode(), 4, L"F4");
104 model_->AddURL(f4, 0, L"f4a", GURL(test_base + "f4a"));
105 }
106};
107
108// Tests Deleting from the menu.
109TEST_F(BookmarkContextMenuTest, DeleteURL) {
[email protected]b3c33d462009-06-26 22:29:20110 std::vector<const BookmarkNode*> nodes;
[email protected]7f856be2008-10-29 23:38:06111 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(0));
112 BookmarkContextMenu controller(
[email protected]0568e6ca2010-05-06 21:06:48113 NULL, profile_.get(), NULL, nodes[0]->GetParent(), nodes);
[email protected]7f856be2008-10-29 23:38:06114 GURL url = model_->GetBookmarkBarNode()->GetChild(0)->GetURL();
115 ASSERT_TRUE(controller.IsCommandEnabled(IDS_BOOKMARK_BAR_REMOVE));
116 // Delete the URL.
117 controller.ExecuteCommand(IDS_BOOKMARK_BAR_REMOVE);
118 // Model shouldn't have URL anymore.
119 ASSERT_FALSE(model_->IsBookmarked(url));
120}
121
122// Tests open all on a folder with a couple of bookmarks.
123TEST_F(BookmarkContextMenuTest, OpenAll) {
[email protected]b3c33d462009-06-26 22:29:20124 const BookmarkNode* folder = model_->GetBookmarkBarNode()->GetChild(1);
[email protected]7f856be2008-10-29 23:38:06125 bookmark_utils::OpenAll(
126 NULL, profile_.get(), &navigator_, folder, NEW_FOREGROUND_TAB);
127
128 // Should have navigated to F1's children.
[email protected]66cb8352009-04-22 00:12:29129 ASSERT_EQ(static_cast<size_t>(2), navigator_.urls_.size());
[email protected]7f856be2008-10-29 23:38:06130 ASSERT_TRUE(folder->GetChild(0)->GetURL() == navigator_.urls_[0]);
131 ASSERT_TRUE(folder->GetChild(1)->GetChild(0)->GetURL() ==
132 navigator_.urls_[1]);
133}
134
135// Tests the enabled state of the menus when supplied an empty vector.
136TEST_F(BookmarkContextMenuTest, EmptyNodes) {
137 BookmarkContextMenu controller(
[email protected]5aa458fc2009-06-27 06:07:55138 NULL, profile_.get(), NULL, model_->other_node(),
[email protected]0568e6ca2010-05-06 21:06:48139 std::vector<const BookmarkNode*>());
[email protected]7f856be2008-10-29 23:38:06140 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL));
141 EXPECT_FALSE(
142 controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW));
143 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO));
144 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOKMARK_BAR_REMOVE));
[email protected]7f856be2008-10-29 23:38:06145 EXPECT_TRUE(
146 controller.IsCommandEnabled(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK));
147 EXPECT_TRUE(
148 controller.IsCommandEnabled(IDS_BOOMARK_BAR_NEW_FOLDER));
149}
150
151// Tests the enabled state of the menus when supplied a vector with a single
152// url.
153TEST_F(BookmarkContextMenuTest, SingleURL) {
[email protected]b3c33d462009-06-26 22:29:20154 std::vector<const BookmarkNode*> nodes;
[email protected]7f856be2008-10-29 23:38:06155 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(0));
156 BookmarkContextMenu controller(
[email protected]0568e6ca2010-05-06 21:06:48157 NULL, profile_.get(), NULL, nodes[0]->GetParent(), nodes);
[email protected]7f856be2008-10-29 23:38:06158 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL));
159 EXPECT_TRUE(
160 controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW));
161 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO));
162 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOKMARK_BAR_REMOVE));
[email protected]7f856be2008-10-29 23:38:06163 EXPECT_TRUE(
164 controller.IsCommandEnabled(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK));
165 EXPECT_TRUE(
166 controller.IsCommandEnabled(IDS_BOOMARK_BAR_NEW_FOLDER));
167}
168
169// Tests the enabled state of the menus when supplied a vector with multiple
170// urls.
171TEST_F(BookmarkContextMenuTest, MultipleURLs) {
[email protected]b3c33d462009-06-26 22:29:20172 std::vector<const BookmarkNode*> nodes;
[email protected]7f856be2008-10-29 23:38:06173 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(0));
174 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(1)->GetChild(0));
175 BookmarkContextMenu controller(
[email protected]0568e6ca2010-05-06 21:06:48176 NULL, profile_.get(), NULL, nodes[0]->GetParent(), nodes);
[email protected]7f856be2008-10-29 23:38:06177 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL));
178 EXPECT_TRUE(
179 controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW));
180 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO));
181 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOKMARK_BAR_REMOVE));
[email protected]7f856be2008-10-29 23:38:06182 EXPECT_TRUE(
183 controller.IsCommandEnabled(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK));
184 EXPECT_TRUE(
185 controller.IsCommandEnabled(IDS_BOOMARK_BAR_NEW_FOLDER));
186}
187
188// Tests the enabled state of the menus when supplied an vector with a single
189// folder.
190TEST_F(BookmarkContextMenuTest, SingleFolder) {
[email protected]b3c33d462009-06-26 22:29:20191 std::vector<const BookmarkNode*> nodes;
[email protected]7f856be2008-10-29 23:38:06192 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(2));
193 BookmarkContextMenu controller(
[email protected]0568e6ca2010-05-06 21:06:48194 NULL, profile_.get(), NULL, nodes[0]->GetParent(), nodes);
[email protected]7f856be2008-10-29 23:38:06195 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL));
196 EXPECT_FALSE(
197 controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW));
198 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO));
199 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOKMARK_BAR_REMOVE));
[email protected]7f856be2008-10-29 23:38:06200 EXPECT_TRUE(
201 controller.IsCommandEnabled(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK));
202 EXPECT_TRUE(
203 controller.IsCommandEnabled(IDS_BOOMARK_BAR_NEW_FOLDER));
204}
205
206// Tests the enabled state of the menus when supplied a vector with multiple
207// folders, all of which are empty.
208TEST_F(BookmarkContextMenuTest, MultipleEmptyFolders) {
[email protected]b3c33d462009-06-26 22:29:20209 std::vector<const BookmarkNode*> nodes;
[email protected]7f856be2008-10-29 23:38:06210 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(2));
211 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(3));
212 BookmarkContextMenu controller(
[email protected]0568e6ca2010-05-06 21:06:48213 NULL, profile_.get(), NULL, nodes[0]->GetParent(), nodes);
[email protected]7f856be2008-10-29 23:38:06214 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL));
215 EXPECT_FALSE(
216 controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW));
217 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO));
218 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOKMARK_BAR_REMOVE));
[email protected]7f856be2008-10-29 23:38:06219 EXPECT_TRUE(
220 controller.IsCommandEnabled(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK));
221 EXPECT_TRUE(
222 controller.IsCommandEnabled(IDS_BOOMARK_BAR_NEW_FOLDER));
223}
224
225// Tests the enabled state of the menus when supplied a vector with multiple
226// folders, some of which contain URLs.
227TEST_F(BookmarkContextMenuTest, MultipleFoldersWithURLs) {
[email protected]b3c33d462009-06-26 22:29:20228 std::vector<const BookmarkNode*> nodes;
[email protected]7f856be2008-10-29 23:38:06229 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(3));
230 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(4));
231 BookmarkContextMenu controller(
[email protected]0568e6ca2010-05-06 21:06:48232 NULL, profile_.get(), NULL, nodes[0]->GetParent(), nodes);
[email protected]7f856be2008-10-29 23:38:06233 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL));
234 EXPECT_TRUE(
235 controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW));
236 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO));
237 EXPECT_TRUE(controller.IsCommandEnabled(IDS_BOOKMARK_BAR_REMOVE));
[email protected]7f856be2008-10-29 23:38:06238 EXPECT_TRUE(
239 controller.IsCommandEnabled(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK));
240 EXPECT_TRUE(
241 controller.IsCommandEnabled(IDS_BOOMARK_BAR_NEW_FOLDER));
242}
243
244// Tests the enabled state of open incognito.
245TEST_F(BookmarkContextMenuTest, DisableIncognito) {
[email protected]b3c33d462009-06-26 22:29:20246 std::vector<const BookmarkNode*> nodes;
[email protected]7f856be2008-10-29 23:38:06247 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(0));
248 BookmarkContextMenu controller(
[email protected]0568e6ca2010-05-06 21:06:48249 NULL, profile_.get(), NULL, nodes[0]->GetParent(), nodes);
[email protected]7f856be2008-10-29 23:38:06250 profile_->set_off_the_record(true);
251 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_INCOGNITO));
252 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO));
253}
254
255// Tests that you can't remove/edit when showing the other node.
256TEST_F(BookmarkContextMenuTest, DisabledItemsWithOtherNode) {
[email protected]b3c33d462009-06-26 22:29:20257 std::vector<const BookmarkNode*> nodes;
[email protected]7f856be2008-10-29 23:38:06258 nodes.push_back(model_->other_node());
259 BookmarkContextMenu controller(
[email protected]0568e6ca2010-05-06 21:06:48260 NULL, profile_.get(), NULL, nodes[0], nodes);
[email protected]7f856be2008-10-29 23:38:06261 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOKMARK_BAR_EDIT));
262 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOKMARK_BAR_REMOVE));
263}
[email protected]5a433972008-11-05 15:39:44264
265// Tests the enabled state of the menus when supplied an empty vector and null
266// parent.
267TEST_F(BookmarkContextMenuTest, EmptyNodesNullParent) {
268 BookmarkContextMenu controller(
[email protected]0568e6ca2010-05-06 21:06:48269 NULL, profile_.get(), NULL, NULL, std::vector<const BookmarkNode*>());
[email protected]5a433972008-11-05 15:39:44270 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL));
271 EXPECT_FALSE(
272 controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW));
273 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO));
274 EXPECT_FALSE(controller.IsCommandEnabled(IDS_BOOKMARK_BAR_REMOVE));
275 EXPECT_FALSE(
[email protected]5a433972008-11-05 15:39:44276 controller.IsCommandEnabled(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK));
277 EXPECT_FALSE(
278 controller.IsCommandEnabled(IDS_BOOMARK_BAR_NEW_FOLDER));
279}
[email protected]a5bf5802010-02-02 21:24:55280
281TEST_F(BookmarkContextMenuTest, CutCopyPasteNode) {
282 std::vector<const BookmarkNode*> nodes;
283 nodes.push_back(model_->GetBookmarkBarNode()->GetChild(0));
[email protected]1df533b2010-02-02 22:50:07284 scoped_ptr<BookmarkContextMenu> controller(new BookmarkContextMenu(
[email protected]0568e6ca2010-05-06 21:06:48285 NULL, profile_.get(), NULL, nodes[0]->GetParent(), nodes));
[email protected]a5bf5802010-02-02 21:24:55286 EXPECT_TRUE(controller->IsCommandEnabled(IDS_COPY));
287 EXPECT_TRUE(controller->IsCommandEnabled(IDS_CUT));
288
289 // Copy the URL.
290 controller->ExecuteCommand(IDS_COPY);
291
[email protected]1df533b2010-02-02 22:50:07292 controller.reset(new BookmarkContextMenu(
[email protected]0568e6ca2010-05-06 21:06:48293 NULL, profile_.get(), NULL, nodes[0]->GetParent(), nodes));
[email protected]a5bf5802010-02-02 21:24:55294 int old_count = model_->GetBookmarkBarNode()->GetChildCount();
295 controller->ExecuteCommand(IDS_PASTE);
296
297 ASSERT_TRUE(model_->GetBookmarkBarNode()->GetChild(1)->is_url());
298 ASSERT_EQ(old_count + 1, model_->GetBookmarkBarNode()->GetChildCount());
299 ASSERT_EQ(model_->GetBookmarkBarNode()->GetChild(0)->GetURL(),
300 model_->GetBookmarkBarNode()->GetChild(1)->GetURL());
301
[email protected]1df533b2010-02-02 22:50:07302 controller.reset(new BookmarkContextMenu(
[email protected]0568e6ca2010-05-06 21:06:48303 NULL, profile_.get(), NULL, nodes[0]->GetParent(), nodes));
[email protected]a5bf5802010-02-02 21:24:55304 // Cut the URL.
305 controller->ExecuteCommand(IDS_CUT);
306 ASSERT_TRUE(model_->GetBookmarkBarNode()->GetChild(0)->is_url());
307 ASSERT_TRUE(model_->GetBookmarkBarNode()->GetChild(1)->is_folder());
308 ASSERT_EQ(old_count, model_->GetBookmarkBarNode()->GetChildCount());
[email protected]a5bf5802010-02-02 21:24:55309}
310