Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / bookmarks / bookmark_context_menu_test.cc
blob4577f3eb1ffd2a3e44623c50d43a10b748da0820
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/views/bookmarks/bookmark_context_menu.h"
7 #include <string>
8 #include <vector>
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/bookmarks/bookmark_model.h"
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
17 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "content/public/browser/page_navigator.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "grit/generated_resources.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/base/clipboard/clipboard.h"
27 #if defined(OS_WIN)
28 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
29 #endif
31 using base::ASCIIToUTF16;
32 using content::BrowserThread;
33 using content::OpenURLParams;
34 using content::PageNavigator;
35 using content::WebContents;
37 namespace {
39 // PageNavigator implementation that records the URL.
40 class TestingPageNavigator : public PageNavigator {
41 public:
42 virtual WebContents* OpenURL(const OpenURLParams& params) OVERRIDE {
43 urls_.push_back(params.url);
44 return NULL;
47 std::vector<GURL> urls_;
50 } // namespace
52 class BookmarkContextMenuTest : public testing::Test {
53 public:
54 BookmarkContextMenuTest()
55 : ui_thread_(BrowserThread::UI, &message_loop_),
56 file_thread_(BrowserThread::FILE, &message_loop_),
57 model_(NULL) {
60 virtual void SetUp() OVERRIDE {
61 profile_.reset(new TestingProfile());
62 profile_->CreateBookmarkModel(true);
64 model_ = BookmarkModelFactory::GetForProfile(profile_.get());
65 test::WaitForBookmarkModelToLoad(model_);
67 AddTestData();
70 virtual void TearDown() OVERRIDE {
71 ui::Clipboard::DestroyClipboardForCurrentThread();
73 BrowserThread::GetBlockingPool()->FlushForTesting();
74 // Flush the message loop to make application verifiers happy.
75 message_loop_.RunUntilIdle();
78 protected:
79 base::MessageLoopForUI message_loop_;
80 content::TestBrowserThread ui_thread_;
81 content::TestBrowserThread file_thread_;
82 scoped_ptr<TestingProfile> profile_;
83 BookmarkModel* model_;
84 TestingPageNavigator navigator_;
86 private:
87 // Creates the following structure:
88 // a
89 // F1
90 // f1a
91 // -f1b as "chrome://settings"
92 // F11
93 // f11a
94 // F2
95 // F3
96 // F4
97 // f4a
98 void AddTestData() {
99 const BookmarkNode* bb_node = model_->bookmark_bar_node();
100 std::string test_base = "file:///c:/tmp/";
101 model_->AddURL(bb_node, 0, ASCIIToUTF16("a"), GURL(test_base + "a"));
102 const BookmarkNode* f1 = model_->AddFolder(bb_node, 1, ASCIIToUTF16("F1"));
103 model_->AddURL(f1, 0, ASCIIToUTF16("f1a"), GURL(test_base + "f1a"));
104 model_->AddURL(f1, 1, ASCIIToUTF16("f1b"), GURL("chrome://settings"));
105 const BookmarkNode* f11 = model_->AddFolder(f1, 2, ASCIIToUTF16("F11"));
106 model_->AddURL(f11, 0, ASCIIToUTF16("f11a"), GURL(test_base + "f11a"));
107 model_->AddFolder(bb_node, 2, ASCIIToUTF16("F2"));
108 model_->AddFolder(bb_node, 3, ASCIIToUTF16("F3"));
109 const BookmarkNode* f4 = model_->AddFolder(bb_node, 4, ASCIIToUTF16("F4"));
110 model_->AddURL(f4, 0, ASCIIToUTF16("f4a"), GURL(test_base + "f4a"));
114 // Tests Deleting from the menu.
115 TEST_F(BookmarkContextMenuTest, DeleteURL) {
116 std::vector<const BookmarkNode*> nodes;
117 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
118 BookmarkContextMenu controller(
119 NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes, false);
120 GURL url = model_->bookmark_bar_node()->GetChild(0)->url();
121 ASSERT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE));
122 // Delete the URL.
123 controller.ExecuteCommand(IDC_BOOKMARK_BAR_REMOVE, 0);
124 // Model shouldn't have URL anymore.
125 ASSERT_FALSE(model_->IsBookmarked(url));
128 // Tests open all on a folder with a couple of bookmarks.
129 TEST_F(BookmarkContextMenuTest, OpenAll) {
130 const BookmarkNode* folder = model_->bookmark_bar_node()->GetChild(1);
131 chrome::OpenAll(NULL, &navigator_, folder, NEW_FOREGROUND_TAB, NULL);
133 // Should have navigated to F1's child but not F11's child.
134 ASSERT_EQ(static_cast<size_t>(2), navigator_.urls_.size());
135 ASSERT_TRUE(folder->GetChild(0)->url() == navigator_.urls_[0]);
138 // Tests open all on a folder with a couple of bookmarks in incognito window.
139 TEST_F(BookmarkContextMenuTest, OpenAllIngonito) {
140 const BookmarkNode* folder = model_->bookmark_bar_node()->GetChild(1);
141 chrome::OpenAll(NULL, &navigator_, folder, OFF_THE_RECORD, NULL);
143 // Should have navigated to only f1a but not f2a.
144 ASSERT_EQ(static_cast<size_t>(1), navigator_.urls_.size());
145 ASSERT_TRUE(folder->GetChild(0)->url() == navigator_.urls_[0]);
148 // Tests the enabled state of the menus when supplied an empty vector.
149 TEST_F(BookmarkContextMenuTest, EmptyNodes) {
150 BookmarkContextMenu controller(
151 NULL, NULL, profile_.get(), NULL, model_->other_node(),
152 std::vector<const BookmarkNode*>(), false);
153 EXPECT_FALSE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
154 EXPECT_FALSE(
155 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
156 EXPECT_FALSE(
157 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
158 EXPECT_FALSE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE));
159 EXPECT_TRUE(
160 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
161 EXPECT_TRUE(
162 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
165 // Tests the enabled state of the menus when supplied a vector with a single
166 // url.
167 TEST_F(BookmarkContextMenuTest, SingleURL) {
168 std::vector<const BookmarkNode*> nodes;
169 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
170 BookmarkContextMenu controller(
171 NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes, false);
172 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
173 EXPECT_TRUE(
174 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
175 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
176 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE));
177 EXPECT_TRUE(
178 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
179 EXPECT_TRUE(
180 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
183 // Tests the enabled state of the menus when supplied a vector with multiple
184 // urls.
185 TEST_F(BookmarkContextMenuTest, MultipleURLs) {
186 std::vector<const BookmarkNode*> nodes;
187 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
188 nodes.push_back(model_->bookmark_bar_node()->GetChild(1)->GetChild(0));
189 BookmarkContextMenu controller(
190 NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes, false);
191 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
192 EXPECT_TRUE(
193 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
194 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
195 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE));
196 EXPECT_TRUE(
197 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
198 EXPECT_TRUE(
199 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
202 // Tests the enabled state of the menus when supplied an vector with a single
203 // folder.
204 TEST_F(BookmarkContextMenuTest, SingleFolder) {
205 std::vector<const BookmarkNode*> nodes;
206 nodes.push_back(model_->bookmark_bar_node()->GetChild(2));
207 BookmarkContextMenu controller(
208 NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes, false);
209 EXPECT_FALSE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
210 EXPECT_FALSE(
211 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
212 EXPECT_FALSE(
213 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
214 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE));
215 EXPECT_TRUE(
216 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
217 EXPECT_TRUE(
218 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
221 // Tests the enabled state of the menus when supplied a vector with multiple
222 // folders, all of which are empty.
223 TEST_F(BookmarkContextMenuTest, MultipleEmptyFolders) {
224 std::vector<const BookmarkNode*> nodes;
225 nodes.push_back(model_->bookmark_bar_node()->GetChild(2));
226 nodes.push_back(model_->bookmark_bar_node()->GetChild(3));
227 BookmarkContextMenu controller(
228 NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes, false);
229 EXPECT_FALSE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
230 EXPECT_FALSE(
231 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
232 EXPECT_FALSE(
233 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
234 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE));
235 EXPECT_TRUE(
236 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
237 EXPECT_TRUE(
238 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
241 // Tests the enabled state of the menus when supplied a vector with multiple
242 // folders, some of which contain URLs.
243 TEST_F(BookmarkContextMenuTest, MultipleFoldersWithURLs) {
244 std::vector<const BookmarkNode*> nodes;
245 nodes.push_back(model_->bookmark_bar_node()->GetChild(3));
246 nodes.push_back(model_->bookmark_bar_node()->GetChild(4));
247 BookmarkContextMenu controller(
248 NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes, false);
249 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
250 EXPECT_TRUE(
251 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
252 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
253 EXPECT_TRUE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE));
254 EXPECT_TRUE(
255 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
256 EXPECT_TRUE(
257 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
260 // Tests the enabled state of open incognito.
261 TEST_F(BookmarkContextMenuTest, DisableIncognito) {
262 std::vector<const BookmarkNode*> nodes;
263 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
264 TestingProfile::Builder builder;
265 builder.SetIncognito();
266 scoped_ptr<TestingProfile> incognito_ = builder.Build().Pass();
267 incognito_->SetOriginalProfile(profile_.get());
268 BookmarkContextMenu controller(
269 NULL, NULL, incognito_.get(), NULL, nodes[0]->parent(), nodes, false);
270 EXPECT_FALSE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_INCOGNITO));
271 EXPECT_FALSE(
272 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
275 // Tests that you can't remove/edit when showing the other node.
276 TEST_F(BookmarkContextMenuTest, DisabledItemsWithOtherNode) {
277 std::vector<const BookmarkNode*> nodes;
278 nodes.push_back(model_->other_node());
279 BookmarkContextMenu controller(
280 NULL, NULL, profile_.get(), NULL, nodes[0], nodes, false);
281 EXPECT_FALSE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_EDIT));
282 EXPECT_FALSE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE));
285 // Tests the enabled state of the menus when supplied an empty vector and null
286 // parent.
287 TEST_F(BookmarkContextMenuTest, EmptyNodesNullParent) {
288 BookmarkContextMenu controller(
289 NULL, NULL, profile_.get(), NULL, NULL,
290 std::vector<const BookmarkNode*>(), false);
291 EXPECT_FALSE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
292 EXPECT_FALSE(
293 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
294 EXPECT_FALSE(
295 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
296 EXPECT_FALSE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE));
297 EXPECT_FALSE(
298 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
299 EXPECT_FALSE(
300 controller.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
303 TEST_F(BookmarkContextMenuTest, CutCopyPasteNode) {
304 const BookmarkNode* bb_node = model_->bookmark_bar_node();
305 std::vector<const BookmarkNode*> nodes;
306 nodes.push_back(bb_node->GetChild(0));
307 scoped_ptr<BookmarkContextMenu> controller(new BookmarkContextMenu(
308 NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes, false));
309 EXPECT_TRUE(controller->IsCommandEnabled(IDC_COPY));
310 EXPECT_TRUE(controller->IsCommandEnabled(IDC_CUT));
312 // Copy the URL.
313 controller->ExecuteCommand(IDC_COPY, 0);
315 controller.reset(new BookmarkContextMenu(
316 NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes, false));
317 int old_count = bb_node->child_count();
318 controller->ExecuteCommand(IDC_PASTE, 0);
320 ASSERT_TRUE(bb_node->GetChild(1)->is_url());
321 ASSERT_EQ(old_count + 1, bb_node->child_count());
322 ASSERT_EQ(bb_node->GetChild(0)->url(), bb_node->GetChild(1)->url());
324 controller.reset(new BookmarkContextMenu(
325 NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes, false));
326 // Cut the URL.
327 controller->ExecuteCommand(IDC_CUT, 0);
328 ASSERT_TRUE(bb_node->GetChild(0)->is_url());
329 ASSERT_TRUE(bb_node->GetChild(1)->is_folder());
330 ASSERT_EQ(old_count, bb_node->child_count());