text: // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  // Use of this source code is governed by a BSD-style license that can be
  // found in the LICENSE file.
  
  #include "chrome/browser/ui/bookmarks/bookmark_context_menu_controller.h"
  
  #include <string>
  
  #include "base/memory/scoped_ptr.h"
  #include "base/message_loop/message_loop.h"
  #include "base/strings/utf_string_conversions.h"
  #include "base/values.h"
  #include "chrome/app/chrome_command_ids.h"
  #include "chrome/browser/bookmarks/bookmark_model_factory.h"
  #include "chrome/browser/profiles/profile.h"
  #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
  #include "chrome/common/pref_names.h"
  #include "chrome/test/base/testing_pref_service_syncable.h"
  #include "chrome/test/base/testing_profile.h"
  #include "components/bookmarks/browser/bookmark_model.h"
  #include "components/bookmarks/browser/bookmark_node.h"
  #include "components/bookmarks/test/bookmark_test_helpers.h"
  #include "content/public/browser/page_navigator.h"
  #include "content/public/test/test_browser_thread.h"
  #include "grit/generated_resources.h"
  #include "testing/gtest/include/gtest/gtest.h"
  #include "ui/base/clipboard/clipboard.h"
  
  using base::ASCIIToUTF16;
  using content::BrowserThread;
  using content::OpenURLParams;
  using content::PageNavigator;
  using content::WebContents;
  
  // PageNavigator implementation that records the URL.
  class TestingPageNavigator : public PageNavigator {
   public:
    virtual WebContents* OpenURL(const OpenURLParams& params) OVERRIDE {
      urls_.push_back(params.url);
      return NULL;
    }
  
    std::vector<GURL> urls_;
  };
  
  class BookmarkContextMenuControllerTest : public testing::Test {
   public:
    BookmarkContextMenuControllerTest()
        : ui_thread_(BrowserThread::UI, &message_loop_),
          file_thread_(BrowserThread::FILE, &message_loop_),
          model_(NULL) {
    }
  
    virtual void SetUp() OVERRIDE {
      TestingProfile::Builder builder;
      profile_ = builder.Build();
      profile_->CreateBookmarkModel(true);
      model_ = BookmarkModelFactory::GetForProfile(profile_.get());
      test::WaitForBookmarkModelToLoad(model_);
      AddTestData(model_);
    }
  
    virtual void TearDown() OVERRIDE {
      ui::Clipboard::DestroyClipboardForCurrentThread();
  
      // Flush the message loop to make application verifiers happy.
      message_loop_.RunUntilIdle();
    }
  
    // Creates the following structure:
    // a
    // F1
    //  f1a
    //  F11
    //   f11a
    // F2
    // F3
    // F4
    //   f4a
    static void AddTestData(BookmarkModel* model) {
      const BookmarkNode* bb_node = model->bookmark_bar_node();
      std::string test_base = "file:///c:/tmp/";
      model->AddURL(bb_node, 0, ASCIIToUTF16("a"), GURL(test_base + "a"));
      const BookmarkNode* f1 = model->AddFolder(bb_node, 1, ASCIIToUTF16("F1"));
      model->AddURL(f1, 0, ASCIIToUTF16("f1a"), GURL(test_base + "f1a"));
      const BookmarkNode* f11 = model->AddFolder(f1, 1, ASCIIToUTF16("F11"));
      model->AddURL(f11, 0, ASCIIToUTF16("f11a"), GURL(test_base + "f11a"));
      model->AddFolder(bb_node, 2, ASCIIToUTF16("F2"));
      model->AddFolder(bb_node, 3, ASCIIToUTF16("F3"));
      const BookmarkNode* f4 = model->AddFolder(bb_node, 4, ASCIIToUTF16("F4"));
      model->AddURL(f4, 0, ASCIIToUTF16("f4a"), GURL(test_base + "f4a"));
    }
  
   protected:
    base::MessageLoopForUI message_loop_;
    content::TestBrowserThread ui_thread_;
    content::TestBrowserThread file_thread_;
    scoped_ptr<TestingProfile> profile_;
    BookmarkModel* model_;
    TestingPageNavigator navigator_;
  };
  
  // Tests Deleting from the menu.
  TEST_F(BookmarkContextMenuControllerTest, DeleteURL) {
    std::vector<const BookmarkNode*> nodes;
    nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
    GURL url = model_->bookmark_bar_node()->GetChild(0)->url();
    ASSERT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
    // Delete the URL.
    controller.ExecuteCommand(IDC_BOOKMARK_BAR_REMOVE, 0);
    // Model shouldn't have URL anymore.
    ASSERT_FALSE(model_->IsBookmarked(url));
  }
  
  // Tests open all on a folder with a couple of bookmarks.
  TEST_F(BookmarkContextMenuControllerTest, OpenAll) {
    const BookmarkNode* folder = model_->bookmark_bar_node()->GetChild(1);
    chrome::OpenAll(NULL, &navigator_, folder, NEW_FOREGROUND_TAB, NULL);
  
    // Should have navigated to F1's child, but not F11's child.
    ASSERT_EQ(static_cast<size_t>(1), navigator_.urls_.size());
    ASSERT_TRUE(folder->GetChild(0)->url() == navigator_.urls_[0]);
  }
  
  // Tests the enabled state of the menus when supplied an empty vector.
  TEST_F(BookmarkContextMenuControllerTest, EmptyNodes) {
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, model_->other_node(),
        std::vector<const BookmarkNode*>());
    EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
    EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
  }
  
  // Tests the enabled state of the menus when supplied a vector with a single
  // url.
  TEST_F(BookmarkContextMenuControllerTest, SingleURL) {
    std::vector<const BookmarkNode*> nodes;
    nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
    EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
    EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
  }
  
  // Tests the enabled state of the menus when supplied a vector with multiple
  // urls.
  TEST_F(BookmarkContextMenuControllerTest, MultipleURLs) {
    std::vector<const BookmarkNode*> nodes;
    nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
    nodes.push_back(model_->bookmark_bar_node()->GetChild(1)->GetChild(0));
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
    EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
    EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
  }
  
  // Tests the enabled state of the menus when supplied an vector with a single
  // folder.
  TEST_F(BookmarkContextMenuControllerTest, SingleFolder) {
    std::vector<const BookmarkNode*> nodes;
    nodes.push_back(model_->bookmark_bar_node()->GetChild(2));
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
    EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
    EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
  }
  
  // Tests the enabled state of the menus when supplied a vector with multiple
  // folders, all of which are empty.
  TEST_F(BookmarkContextMenuControllerTest, MultipleEmptyFolders) {
    std::vector<const BookmarkNode*> nodes;
    nodes.push_back(model_->bookmark_bar_node()->GetChild(2));
    nodes.push_back(model_->bookmark_bar_node()->GetChild(3));
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
    EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
    EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
  }
  
  // Tests the enabled state of the menus when supplied a vector with multiple
  // folders, some of which contain URLs.
  TEST_F(BookmarkContextMenuControllerTest, MultipleFoldersWithURLs) {
    std::vector<const BookmarkNode*> nodes;
    nodes.push_back(model_->bookmark_bar_node()->GetChild(3));
    nodes.push_back(model_->bookmark_bar_node()->GetChild(4));
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
    EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
    EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
  }
  
  // Tests the enabled state of open incognito.
  TEST_F(BookmarkContextMenuControllerTest, DisableIncognito) {
    // Create an incognito Profile. It must be associated with the original
    // Profile, so that GetOriginalProfile() works as expected.
    TestingProfile::Builder builder;
    builder.SetIncognito();
    scoped_ptr<TestingProfile> testing_incognito = builder.Build();
    testing_incognito->SetOriginalProfile(profile_.get());
    TestingProfile* incognito = testing_incognito.get();
    profile_->SetOffTheRecordProfile(testing_incognito.PassAs<Profile>());
  
    incognito->CreateBookmarkModel(true);
    BookmarkModel* model = BookmarkModelFactory::GetForProfile(incognito);
    test::WaitForBookmarkModelToLoad(model);
    AddTestData(model);
  
    std::vector<const BookmarkNode*> nodes;
    nodes.push_back(model->bookmark_bar_node()->GetChild(0));
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, incognito, NULL, nodes[0]->parent(), nodes);
    EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_INCOGNITO));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
  }
  
  // Tests that you can't remove/edit when showing the other node.
  TEST_F(BookmarkContextMenuControllerTest, DisabledItemsWithOtherNode) {
    std::vector<const BookmarkNode*> nodes;
    nodes.push_back(model_->other_node());
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, nodes[0], nodes);
    EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_EDIT));
    EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
  }
  
  // Tests the enabled state of the menus when supplied an empty vector and null
  // parent.
  TEST_F(BookmarkContextMenuControllerTest, EmptyNodesNullParent) {
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, NULL,
        std::vector<const BookmarkNode*>());
    EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
    EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
  }
  
  // Tests the enabled state of the menus when supplied a vector containing just
  // the top-level bookmark bar node.
  TEST_F(BookmarkContextMenuControllerTest, BookmarkBar) {
    std::vector<const BookmarkNode*> nodes;
    nodes.push_back(model_->bookmark_bar_node());
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes);
    EXPECT_TRUE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
    EXPECT_FALSE(controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER));
  }
  
  TEST_F(BookmarkContextMenuControllerTest, CutCopyPasteNode) {
    const BookmarkNode* bb_node = model_->bookmark_bar_node();
    std::vector<const BookmarkNode*> nodes;
    nodes.push_back(bb_node->GetChild(0));
    scoped_ptr<BookmarkContextMenuController> controller(
        new BookmarkContextMenuController(
            NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes));
    EXPECT_TRUE(controller->IsCommandIdEnabled(IDC_COPY));
    EXPECT_TRUE(controller->IsCommandIdEnabled(IDC_CUT));
  
    // Copy the URL.
    controller->ExecuteCommand(IDC_COPY, 0);
  
    controller.reset(new BookmarkContextMenuController(
        NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes));
    int old_count = bb_node->child_count();
    controller->ExecuteCommand(IDC_PASTE, 0);
  
    ASSERT_TRUE(bb_node->GetChild(1)->is_url());
    ASSERT_EQ(old_count + 1, bb_node->child_count());
    ASSERT_EQ(bb_node->GetChild(0)->url(), bb_node->GetChild(1)->url());
  
    controller.reset(new BookmarkContextMenuController(
        NULL, NULL, NULL, profile_.get(), NULL, nodes[0]->parent(), nodes));
    // Cut the URL.
    controller->ExecuteCommand(IDC_CUT, 0);
    ASSERT_TRUE(bb_node->GetChild(0)->is_url());
    ASSERT_TRUE(bb_node->GetChild(1)->is_folder());
    ASSERT_EQ(old_count, bb_node->child_count());
  }
  
  TEST_F(BookmarkContextMenuControllerTest,
         ManagedShowAppsShortcutInBookmarksBar) {
    BookmarkContextMenuController controller(
        NULL, NULL, NULL, profile_.get(), NULL, model_->bookmark_bar_node(),
        std::vector<const BookmarkNode*>());
  
    // By default, the pref is not managed and the command is enabled.
    TestingPrefServiceSyncable* prefs = profile_->GetTestingPrefService();
    EXPECT_FALSE(
        prefs->IsManagedPreference(prefs::kShowAppsShortcutInBookmarkBar));
    EXPECT_TRUE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_SHOW_APPS_SHORTCUT));
  
    // Disabling the shorcut by policy disables the command.
    prefs->SetManagedPref(prefs::kShowAppsShortcutInBookmarkBar,
                          new base::FundamentalValue(false));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_SHOW_APPS_SHORTCUT));
  
    // And enabling the shortcut by policy disables the command too.
    prefs->SetManagedPref(prefs::kShowAppsShortcutInBookmarkBar,
                          new base::FundamentalValue(true));
    EXPECT_FALSE(
        controller.IsCommandIdEnabled(IDC_BOOKMARK_BAR_SHOW_APPS_SHORTCUT));
  }
  