Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bookmark toggle on NTP whether bookmark is empty or not #2563

Merged
merged 1 commit into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions browser/ui/bookmark/bookmark_tab_helper_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <vector>

#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "url/gurl.h"

using BookmarkTabHelperBrowserTest = InProcessBrowserTest;

namespace {

bool IsNTP(content::WebContents* web_contents) {
// Use the committed entry so the bookmarks bar disappears at the same time
// the page does.
content::NavigationEntry* entry =
web_contents->GetController().GetLastCommittedEntry();
if (!entry)
entry = web_contents->GetController().GetVisibleEntry();
return (entry && NewTabUI::IsNewTab(entry->GetURL())) ||
search::NavEntryIsInstantNTP(web_contents, entry);
}

} // namespace

IN_PROC_BROWSER_TEST_F(BookmarkTabHelperBrowserTest,
BookmarkBarOnNTPToggleTest) {
auto* contents = browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(content::NavigateToURL(contents,
GURL(chrome::kChromeUINewTabURL)));
EXPECT_TRUE(IsNTP(contents));
chrome::ToggleBookmarkBar(browser());
EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());

const GURL url = GURL("https://www.brave.com");
bookmarks::BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForBrowserContext(browser()->profile());

std::vector<const bookmarks::BookmarkNode*> nodes;
bookmark_model->GetNodesByURL(url, &nodes);
EXPECT_EQ(0UL, nodes.size());

bookmarks::AddIfNotBookmarked(bookmark_model, url, base::string16());
bookmark_model->GetNodesByURL(url, &nodes);
EXPECT_EQ(1UL, nodes.size());

chrome::ToggleBookmarkBar(browser());
EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/chrome/browser/ui/bookmarks/bookmark_tab_helper.cc b/chrome/browser/ui/bookmarks/bookmark_tab_helper.cc
index f6ef61a0ac86ae55e8f9e80ece0758253af48520..888d656cf7080ebc32e90fc492e70a9b394d7026 100644
--- a/chrome/browser/ui/bookmarks/bookmark_tab_helper.cc
+++ b/chrome/browser/ui/bookmarks/bookmark_tab_helper.cc
@@ -67,6 +67,7 @@ bool BookmarkTabHelper::ShouldShowBookmarkBar() const {
!prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar))
return false;

+ return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was expecting to see something like return !IsNTP(web_contents()) here. Is it safe to just return false here without further checks?

Copy link
Member Author

@simonhong simonhong May 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emerick I think this method is used for preventing bookmark bar visible or hidden opposite to the kShowBookmarkBar prefs value.

When kShowBookmarkBar is set to hidden, this method only returns true when active tab is NTP and bookmark bar isn't empty if all above conditions aren't met. So, we can hide bookmark bar on NTP by returning false here in this case.

When kShowBookmarkBar is set to visible, this method isn't called except fullscreen condition.
According to the comment of Browser::ShouldHideUIForFullscreen(), browser controls UI can be visible on MacOS fullscreen mode by sliding down. When Browser::ShouldHideUIForFullscreen() returns true, this method will be called and we should also return false to hide bookmark bar.

So, I think returning false in here is safe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, got it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emerick Thanks for quick review!

// The bookmark bar is only shown on the NTP if the user
// has added something to it.
return IsNTP(web_contents()) && bookmark_model_ &&
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ test("brave_browser_tests") {
"//brave/browser/renderer_context_menu/brave_mock_render_view_context_menu.h",
"//brave/browser/renderer_context_menu/brave_spelling_menu_observer_browsertest.cc",
"//brave/browser/search_engines/search_engine_provider_service_browsertest.cc",
"//brave/browser/ui/bookmark/bookmark_tab_helper_browsertest.cc",
"//brave/browser/ui/brave_dark_mode_observer_browsertest_mac.mm",
"//brave/browser/ui/content_settings/brave_autoplay_blocked_image_model_browsertest.cc",
"//brave/browser/ui/views/brave_actions/brave_actions_container_browsertest.cc",
Expand Down