-
Notifications
You must be signed in to change notification settings - Fork 921
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
Toolbar: hide the profile button if there is only 1 user profile #3097
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
browser/ui/views/toolbar/brave_toolbar_view_browsertest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright 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 "chrome/browser/browser_process.h" | ||
#include "chrome/browser/chrome_notification_types.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/profiles/profile_manager.h" | ||
#include "chrome/browser/profiles/profile_window.h" | ||
#include "chrome/browser/search_engines/template_url_service_factory.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/browser/ui/browser_finder.h" | ||
#include "chrome/browser/ui/browser_list.h" | ||
#include "chrome/browser/ui/views/frame/browser_view.h" | ||
#include "chrome/browser/ui/views/toolbar/toolbar_view.h" | ||
#include "chrome/test/base/in_process_browser_test.h" | ||
#include "chrome/test/base/search_test_utils.h" | ||
#include "content/public/browser/notification_service.h" | ||
#include "content/public/test/test_utils.h" | ||
#include "ui/views/view.h" | ||
|
||
// An observer that returns back to test code after a new profile is | ||
// initialized. | ||
void OnUnblockOnProfileCreation(base::RunLoop* run_loop, | ||
Profile* profile, | ||
Profile::CreateStatus status) { | ||
if (status == Profile::CREATE_STATUS_INITIALIZED) | ||
run_loop->Quit(); | ||
} | ||
|
||
class BraveToolbarViewTest : public InProcessBrowserTest { | ||
public: | ||
BraveToolbarViewTest() = default; | ||
~BraveToolbarViewTest() override = default; | ||
|
||
void SetUpOnMainThread() override { | ||
Init(browser()); | ||
} | ||
|
||
void Init(Browser* browser) { | ||
BrowserView* browser_view = | ||
BrowserView::GetBrowserViewForBrowser(browser); | ||
ASSERT_NE(browser_view, nullptr); | ||
toolbar_view_ = browser_view->toolbar(); | ||
ASSERT_NE(toolbar_view_, nullptr); | ||
} | ||
|
||
protected: | ||
bool is_avatar_button_shown() { | ||
views::View* button = toolbar_view_->GetAvatarToolbarButton(); | ||
DCHECK(button); | ||
return button->GetVisible(); | ||
} | ||
|
||
private: | ||
ToolbarView* toolbar_view_; | ||
DISALLOW_COPY_AND_ASSIGN(BraveToolbarViewTest); | ||
}; | ||
|
||
IN_PROC_BROWSER_TEST_F(BraveToolbarViewTest, | ||
AvatarButtonNotShownSingleProfile) { | ||
EXPECT_EQ(false, is_avatar_button_shown()); | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F(BraveToolbarViewTest, AvatarButtonIsShownGuestProfile) { | ||
// Open a Guest window. | ||
EXPECT_EQ(1U, BrowserList::GetInstance()->size()); | ||
content::WindowedNotificationObserver browser_creation_observer( | ||
chrome::NOTIFICATION_BROWSER_OPENED, | ||
content::NotificationService::AllSources()); | ||
profiles::SwitchToGuestProfile(ProfileManager::CreateCallback()); | ||
base::RunLoop().RunUntilIdle(); | ||
browser_creation_observer.Wait(); | ||
EXPECT_EQ(2U, BrowserList::GetInstance()->size()); | ||
|
||
// Retrieve the new Guest profile. | ||
Profile* guest = g_browser_process->profile_manager()->GetProfileByPath( | ||
ProfileManager::GetGuestProfilePath()); | ||
|
||
// Access the browser with the Guest profile and re-init test for it. | ||
Browser* browser = chrome::FindAnyBrowser(guest, true); | ||
EXPECT_TRUE(browser); | ||
Init(browser); | ||
EXPECT_EQ(true, is_avatar_button_shown()); | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F(BraveToolbarViewTest, | ||
AvatarButtonIsShownMultipleProfiles) { | ||
// Should not be shown in first profile, at first | ||
EXPECT_EQ(false, is_avatar_button_shown()); | ||
|
||
// Create an additional profile. | ||
ProfileManager* profile_manager = g_browser_process->profile_manager(); | ||
ProfileAttributesStorage& storage = | ||
profile_manager->GetProfileAttributesStorage(); | ||
base::FilePath current_profile_path = browser()->profile()->GetPath(); | ||
base::FilePath new_path = profile_manager->GenerateNextProfileDirectoryPath(); | ||
base::RunLoop run_loop; | ||
profile_manager->CreateProfileAsync( | ||
new_path, base::Bind(&OnUnblockOnProfileCreation, &run_loop), | ||
base::string16(), std::string()); | ||
run_loop.Run(); | ||
ASSERT_EQ(2u, storage.GetNumberOfProfiles()); | ||
Profile* new_profile = profile_manager->GetProfileByPath(new_path); | ||
|
||
// check it's now shown in first profile | ||
EXPECT_EQ(true, is_avatar_button_shown()); | ||
|
||
// Open the new profile | ||
EXPECT_EQ(1U, BrowserList::GetInstance()->size()); | ||
content::WindowedNotificationObserver browser_creation_observer( | ||
chrome::NOTIFICATION_BROWSER_OPENED, | ||
content::NotificationService::AllSources()); | ||
profiles::OpenBrowserWindowForProfile( | ||
ProfileManager::CreateCallback(), | ||
false, true, true, | ||
new_profile, | ||
Profile::CREATE_STATUS_INITIALIZED); | ||
base::RunLoop().RunUntilIdle(); | ||
browser_creation_observer.Wait(); | ||
EXPECT_EQ(2U, BrowserList::GetInstance()->size()); | ||
|
||
// Check it's shown in second profile | ||
Browser* browser = chrome::FindAnyBrowser(new_profile, true); | ||
EXPECT_TRUE(browser); | ||
Init(browser); | ||
EXPECT_EQ(true, is_avatar_button_shown()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this callback only called only when new normal profile is created?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@simonhong I don't see this getting called when starting / opening a guest or tor profile. Are there any other non-normal profiles I should check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't need to check about tor/guest profile add/delete, I think current impl is fine.
FYI, we can get all profile creation by
NOTIFICATION_PROFILE_XXX
notifications.