Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / bookmarks / bookmark_context_menu_controller_unittest.cc
blobfa3dea41b983376d3a1fcf3775d264c175ef323b
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 "base/values.h"
13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/testing_pref_service_syncable.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "components/bookmarks/browser/bookmark_model.h"
21 #include "components/bookmarks/browser/bookmark_node.h"
22 #include "components/bookmarks/test/bookmark_test_helpers.h"
23 #include "content/public/browser/page_navigator.h"
24 #include "content/public/test/test_browser_thread.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/base/clipboard/clipboard.h"
28 using base::ASCIIToUTF16;
29 using bookmarks::BookmarkModel;
30 using bookmarks::BookmarkNode;
31 using content::BrowserThread;
32 using content::OpenURLParams;
33 using content::PageNavigator;
34 using content::WebContents;
36 // PageNavigator implementation that records the URL.
37 class TestingPageNavigator : public PageNavigator {
38 public:
39 WebContents* OpenURL(const OpenURLParams& params) override {
40 urls_.push_back(params.url);
41 return NULL;
44 std::vector<GURL> urls_;
47 class BookmarkContextMenuControllerTest : public testing::Test {
48 public:
49 BookmarkContextMenuControllerTest()
50 : ui_thread_(BrowserThread::UI, &message_loop_),
51 file_thread_(BrowserThread::FILE, &message_loop_),
52 model_(NULL) {
55 void SetUp() override {
56 TestingProfile::Builder builder;
57 profile_ = builder.Build();
58 profile_->CreateBookmarkModel(true);
59 model_ = BookmarkModelFactory::GetForProfile(profile_.get());
60 bookmarks::test::WaitForBookmarkModelToLoad(model_);
61 AddTestData(model_);
64 void TearDown() override {
65 ui::Clipboard::DestroyClipboardForCurrentThread();
67 // Flush the message loop to make application verifiers happy.
68 message_loop_.RunUntilIdle();
71 // Creates the following structure:
72 // a
73 // F1
74 // f1a
75 // F11
76 // f11a
77 // F2
78 // F3
79 // F4
80 // f4a
81 static void AddTestData(BookmarkModel* model) {
82 const BookmarkNode* bb_node = model->bookmark_bar_node();
83 std::string test_base = "file:///c:/tmp/";
84 model->AddURL(bb_node, 0, ASCIIToUTF16("a"), GURL(test_base + "a"));
85 const BookmarkNode* f1 = model->AddFolder(bb_node, 1, ASCIIToUTF16("F1"));
86 model->AddURL(f1, 0, ASCIIToUTF16("f1a"), GURL(test_base + "f1a"));
87 const BookmarkNode* f11 = model->AddFolder(f1, 1, ASCIIToUTF16("F11"));
88 model->AddURL(f11, 0, ASCIIToUTF16("f11a"), GURL(test_base + "f11a"));
89 model->AddFolder(bb_node, 2, ASCIIToUTF16("F2"));
90 model->AddFolder(bb_node, 3, ASCIIToUTF16("F3"));
91 const BookmarkNode* f4 = model->AddFolder(bb_node, 4, ASCIIToUTF16("F4"));
92 model->AddURL(f4, 0, ASCIIToUTF16("f4a"), GURL(test_base + "f4a"));
95 protected:
96 base::MessageLoopForUI message_loop_;
97 content::TestBrowserThread ui_thread_;
98 content::TestBrowserThread file_thread_;
99 scoped_ptr<TestingProfile> profile_;
100 BookmarkModel* model_;
101 TestingPageNavigator navigator_;
104 // Tests Deleting from the menu.
105 TEST_F(BookmarkContextMenuControllerTest, DeleteURL) {
106 std::vector<const BookmarkNode*> nodes;
107 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
108 BookmarkContextMenuController controller(
109 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
110 GURL url = model_->bookmark_bar_node()->GetChild(0)->url();
111 ASSERT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
112 // Delete the URL.
113 controller.ExecuteCommand(IDC_BOOKMARK_BAR_REMOVE, 0);
114 // Model shouldn't have URL anymore.
115 ASSERT_FALSE(model_->IsBookmarked(url));
118 // Tests open all on a folder with a couple of bookmarks.
119 TEST_F(BookmarkContextMenuControllerTest, OpenAll) {
120 const BookmarkNode* folder = model_->bookmark_bar_node()->GetChild(1);
121 chrome::OpenAll(NULL, &navigator_, folder, NEW_FOREGROUND_TAB, NULL);
123 // Should have navigated to F1's child, but not F11's child.
124 ASSERT_EQ(static_cast<size_t>(1), navigator_.urls_.size());
125 ASSERT_TRUE(folder->GetChild(0)->url() == navigator_.urls_[0]);
128 // Tests the enabled state of the menus when supplied an empty vector.
129 TEST_F(BookmarkContextMenuControllerTest, EmptyNodes) {
130 BookmarkContextMenuController controller(
131 NULL, NULL, NULL, profile_.get(), NULL, model_->other_node(),
132 std::vector<const BookmarkNode*>());
133 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
134 EXPECT_FALSE(
135 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
136 EXPECT_FALSE(
137 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
138 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
139 EXPECT_TRUE(
140 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
141 EXPECT_TRUE(
142 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
145 // Tests the enabled state of the menus when supplied a vector with a single
146 // url.
147 TEST_F(BookmarkContextMenuControllerTest, SingleURL) {
148 std::vector<const BookmarkNode*> nodes;
149 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
150 BookmarkContextMenuController controller(
151 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
152 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
153 EXPECT_TRUE(
154 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
155 EXPECT_TRUE(
156 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
157 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
158 EXPECT_TRUE(
159 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
160 EXPECT_TRUE(
161 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
164 // Tests the enabled state of the menus when supplied a vector with multiple
165 // urls.
166 TEST_F(BookmarkContextMenuControllerTest, MultipleURLs) {
167 std::vector<const BookmarkNode*> nodes;
168 nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
169 nodes.push_back(model_->bookmark_bar_node()->GetChild(1)->GetChild(0));
170 BookmarkContextMenuController controller(
171 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
172 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
173 EXPECT_TRUE(
174 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
175 EXPECT_TRUE(
176 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
177 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
178 EXPECT_TRUE(
179 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
180 EXPECT_TRUE(
181 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
184 // Tests the enabled state of the menus when supplied an vector with a single
185 // folder.
186 TEST_F(BookmarkContextMenuControllerTest, SingleFolder) {
187 std::vector<const BookmarkNode*> nodes;
188 nodes.push_back(model_->bookmark_bar_node()->GetChild(2));
189 BookmarkContextMenuController controller(
190 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
191 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
192 EXPECT_FALSE(
193 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
194 EXPECT_FALSE(
195 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
196 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
197 EXPECT_TRUE(
198 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
199 EXPECT_TRUE(
200 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
203 // Tests the enabled state of the menus when supplied a vector with multiple
204 // folders, all of which are empty.
205 TEST_F(BookmarkContextMenuControllerTest, MultipleEmptyFolders) {
206 std::vector<const BookmarkNode*> nodes;
207 nodes.push_back(model_->bookmark_bar_node()->GetChild(2));
208 nodes.push_back(model_->bookmark_bar_node()->GetChild(3));
209 BookmarkContextMenuController controller(
210 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
211 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
212 EXPECT_FALSE(
213 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
214 EXPECT_FALSE(
215 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
216 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
217 EXPECT_TRUE(
218 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
219 EXPECT_TRUE(
220 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
223 // Tests the enabled state of the menus when supplied a vector with multiple
224 // folders, some of which contain URLs.
225 TEST_F(BookmarkContextMenuControllerTest, MultipleFoldersWithURLs) {
226 std::vector<const BookmarkNode*> nodes;
227 nodes.push_back(model_->bookmark_bar_node()->GetChild(3));
228 nodes.push_back(model_->bookmark_bar_node()->GetChild(4));
229 BookmarkContextMenuController controller(
230 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
231 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
232 EXPECT_TRUE(
233 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
234 EXPECT_TRUE(
235 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
236 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
237 EXPECT_TRUE(
238 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
239 EXPECT_TRUE(
240 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
243 // Tests the enabled state of open incognito.
244 TEST_F(BookmarkContextMenuControllerTest, DisableIncognito) {
245 TestingProfile* incognito =
246 TestingProfile::Builder().BuildIncognito(profile_.get());
248 incognito->CreateBookmarkModel(true);
249 BookmarkModel* model = BookmarkModelFactory::GetForProfile(incognito);
250 bookmarks::test::WaitForBookmarkModelToLoad(model);
251 AddTestData(model);
253 std::vector<const BookmarkNode*> nodes;
254 nodes.push_back(model->bookmark_bar_node()->GetChild(0));
255 BookmarkContextMenuController controller(
256 NULL, NULL, NULL, incognito, NULL, nodes[0]->parent(), nodes);
257 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_INCOGNITO));
258 EXPECT_FALSE(
259 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
262 // Tests that you can't remove/edit when showing the other node.
263 TEST_F(BookmarkContextMenuControllerTest, DisabledItemsWithOtherNode) {
264 std::vector<const BookmarkNode*> nodes;
265 nodes.push_back(model_->other_node());
266 BookmarkContextMenuController controller(
267 NULL, NULL, NULL, profile_.get(), NULL, nodes[0], nodes);
268 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_EDIT));
269 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
272 // Tests the enabled state of the menus when supplied an empty vector and null
273 // parent.
274 TEST_F(BookmarkContextMenuControllerTest, EmptyNodesNullParent) {
275 BookmarkContextMenuController controller(
276 NULL, NULL, NULL, profile_.get(), NULL, NULL,
277 std::vector<const BookmarkNode*>());
278 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
279 EXPECT_FALSE(
280 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
281 EXPECT_FALSE(
282 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
283 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
284 EXPECT_FALSE(
285 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
286 EXPECT_FALSE(
287 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
290 // Tests the enabled state of the menus when supplied a vector containing just
291 // the top-level bookmark bar node.
292 TEST_F(BookmarkContextMenuControllerTest, BookmarkBar) {
293 std::vector<const BookmarkNode*> nodes;
294 nodes.push_back(model_->bookmark_bar_node());
295 BookmarkContextMenuController controller(
296 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
297 EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
298 EXPECT_TRUE(
299 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
300 EXPECT_TRUE(
301 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
302 EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
303 EXPECT_TRUE(
304 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
305 EXPECT_TRUE(
306 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
309 TEST_F(BookmarkContextMenuControllerTest, CutCopyPasteNode) {
310 const BookmarkNode* bb_node = model_->bookmark_bar_node();
311 std::vector<const BookmarkNode*> nodes;
312 nodes.push_back(bb_node->GetChild(0));
313 scoped_ptr<BookmarkContextMenuController> controller(
314 new BookmarkContextMenuController(
315 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes));
316 EXPECT_TRUE(controller->IsCommandIdEnabled(IDC_COPY));
317 EXPECT_TRUE(controller->IsCommandIdEnabled(IDC_CUT));
319 // Copy the URL.
320 controller->ExecuteCommand(IDC_COPY, 0);
322 controller.reset(new BookmarkContextMenuController(
323 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes));
324 int old_count = bb_node->child_count();
325 controller->ExecuteCommand(IDC_PASTE, 0);
327 ASSERT_TRUE(bb_node->GetChild(1)->is_url());
328 ASSERT_EQ(old_count + 1, bb_node->child_count());
329 ASSERT_EQ(bb_node->GetChild(0)->url(), bb_node->GetChild(1)->url());
331 controller.reset(new BookmarkContextMenuController(
332 NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes));
333 // Cut the URL.
334 controller->ExecuteCommand(IDC_CUT, 0);
335 ASSERT_TRUE(bb_node->GetChild(0)->is_url());
336 ASSERT_TRUE(bb_node->GetChild(1)->is_folder());
337 ASSERT_EQ(old_count, bb_node->child_count());
340 TEST_F(BookmarkContextMenuControllerTest,
341 ManagedShowAppsShortcutInBookmarksBar) {
342 BookmarkContextMenuController controller(
343 NULL, NULL, NULL, profile_.get(), NULL, model_->bookmark_bar_node(),
344 std::vector<const BookmarkNode*>());
346 // By default, the pref is not managed and the command is enabled.
347 TestingPrefServiceSyncable* prefs = profile_->GetTestingPrefService();
348 EXPECT_FALSE(prefs->IsManagedPreference(
349 bookmarks::prefs::kShowAppsShortcutInBookmarkBar));
350 EXPECT_TRUE(
351 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_SHOW_APPS_SHORTCUT));
353 // Disabling the shorcut by policy disables the command.
354 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
355 new base::FundamentalValue(false));
356 EXPECT_FALSE(
357 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_SHOW_APPS_SHORTCUT));
359 // And enabling the shortcut by policy disables the command too.
360 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
361 new base::FundamentalValue(true));
362 EXPECT_FALSE(
363 controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_SHOW_APPS_SHORTCUT));