xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 1 | // Copyright (c) 2015 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. |
| 4 | |
| 5 | #include "chrome/browser/ui/browser_finder.h" |
| 6 | |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 7 | #include "base/macros.h" |
Xiyuan Xia | dfe3a9f | 2017-11-13 21:46:26 | [diff] [blame] | 8 | #include "base/memory/ptr_util.h" |
yoshiki | 18898fb | 2016-11-18 15:59:48 | [diff] [blame] | 9 | #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
xdai | 8a874a3 | 2016-05-16 19:22:17 | [diff] [blame] | 10 | #include "chrome/browser/chromeos/profiles/profile_helper.h" |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 11 | #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" |
| 12 | #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h" |
| 13 | #include "chrome/test/base/browser_with_test_window_test.h" |
| 14 | #include "chrome/test/base/test_browser_window_aura.h" |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 15 | #include "chrome/test/base/testing_profile_manager.h" |
Alexander Alekseev | 3f09a8f | 2018-05-03 02:52:10 | [diff] [blame] | 16 | #include "components/account_id/account_id.h" |
Xiyuan Xia | dfe3a9f | 2017-11-13 21:46:26 | [diff] [blame] | 17 | #include "components/user_manager/scoped_user_manager.h" |
xdai | 8a874a3 | 2016-05-16 19:22:17 | [diff] [blame] | 18 | #include "components/user_manager/user.h" |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 19 | |
| 20 | namespace test { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | const char kTestAccount1[] = "[email protected]"; |
| 25 | const char kTestAccount2[] = "[email protected]"; |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | class BrowserFinderChromeOSTest : public BrowserWithTestWindowTest { |
| 30 | protected: |
xdai | 8a874a3 | 2016-05-16 19:22:17 | [diff] [blame] | 31 | BrowserFinderChromeOSTest() |
| 32 | : multi_user_window_manager_(nullptr), |
yoshiki | 18898fb | 2016-11-18 15:59:48 | [diff] [blame] | 33 | fake_user_manager_(new chromeos::FakeChromeUserManager), |
Xiyuan Xia | dfe3a9f | 2017-11-13 21:46:26 | [diff] [blame] | 34 | user_manager_enabler_(base::WrapUnique(fake_user_manager_)) {} |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 35 | |
xdai | 957c25f0 | 2016-03-03 18:37:18 | [diff] [blame] | 36 | TestingProfile* CreateMultiUserProfile(const AccountId& account_id) { |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 37 | TestingProfile* profile = |
Evan Stade | ef1543f | 2017-11-17 20:20:13 | [diff] [blame] | 38 | profile_manager()->CreateTestingProfile(account_id.GetUserEmail()); |
xdai | 8a874a3 | 2016-05-16 19:22:17 | [diff] [blame] | 39 | const user_manager::User* user = fake_user_manager_->AddUser(account_id); |
| 40 | chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting( |
| 41 | const_cast<user_manager::User*>(user), profile); |
| 42 | chromeos::ProfileHelper::Get()->SetProfileToUserMappingForTesting( |
| 43 | const_cast<user_manager::User*>(user)); |
xdai | 8a874a3 | 2016-05-16 19:22:17 | [diff] [blame] | 44 | GetUserWindowManager()->AddUser(profile); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 45 | return profile; |
| 46 | } |
| 47 | |
cm.sanchi | a0d4585 | 2017-11-23 11:19:34 | [diff] [blame] | 48 | MultiUserWindowManagerChromeOS* GetUserWindowManager() { |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 49 | if (!multi_user_window_manager_) { |
| 50 | multi_user_window_manager_ = |
cm.sanchi | a0d4585 | 2017-11-23 11:19:34 | [diff] [blame] | 51 | new MultiUserWindowManagerChromeOS(test_account_id1_); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 52 | multi_user_window_manager_->Init(); |
cm.sanchi | a0d4585 | 2017-11-23 11:19:34 | [diff] [blame] | 53 | MultiUserWindowManager::SetInstanceForTest(multi_user_window_manager_); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 54 | } |
| 55 | return multi_user_window_manager_; |
| 56 | } |
| 57 | |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 58 | AccountId test_account_id1_ = EmptyAccountId(); |
| 59 | AccountId test_account_id2_ = EmptyAccountId(); |
| 60 | |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 61 | private: |
| 62 | void SetUp() override { |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 63 | test_account_id1_ = AccountId::FromUserEmail(kTestAccount1); |
| 64 | test_account_id2_ = AccountId::FromUserEmail(kTestAccount2); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 65 | BrowserWithTestWindowTest::SetUp(); |
Evan Stade | ef1543f | 2017-11-17 20:20:13 | [diff] [blame] | 66 | profile_manager()->SetLoggedIn(true); |
xdai | 957c25f0 | 2016-03-03 18:37:18 | [diff] [blame] | 67 | second_profile_ = CreateMultiUserProfile(test_account_id2_); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void TearDown() override { |
cm.sanchi | a0d4585 | 2017-11-23 11:19:34 | [diff] [blame] | 71 | MultiUserWindowManager::DeleteInstance(); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 72 | BrowserWithTestWindowTest::TearDown(); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TestingProfile* CreateProfile() override { |
xdai | 957c25f0 | 2016-03-03 18:37:18 | [diff] [blame] | 76 | return CreateMultiUserProfile(test_account_id1_); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 77 | } |
| 78 | |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 79 | TestingProfile* second_profile_; |
cm.sanchi | a0d4585 | 2017-11-23 11:19:34 | [diff] [blame] | 80 | MultiUserWindowManagerChromeOS* multi_user_window_manager_; |
yoshiki | 18898fb | 2016-11-18 15:59:48 | [diff] [blame] | 81 | |
| 82 | // |fake_user_manager_| is owned by |user_manager_enabler_| |
| 83 | chromeos::FakeChromeUserManager* fake_user_manager_; |
Xiyuan Xia | dfe3a9f | 2017-11-13 21:46:26 | [diff] [blame] | 84 | user_manager::ScopedUserManager user_manager_enabler_; |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 85 | |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 86 | DISALLOW_COPY_AND_ASSIGN(BrowserFinderChromeOSTest); |
| 87 | }; |
| 88 | |
| 89 | TEST_F(BrowserFinderChromeOSTest, IncognitoBrowserMatchTest) { |
| 90 | // GetBrowserCount() use kMatchAll to find all browser windows for profile(). |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 91 | EXPECT_EQ(1u, chrome::GetBrowserCount(profile())); |
| 92 | EXPECT_TRUE(chrome::FindAnyBrowser(profile(), true)); |
| 93 | EXPECT_TRUE(chrome::FindAnyBrowser(profile(), false)); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 94 | set_browser(nullptr); |
| 95 | |
| 96 | // Create an incognito browser. |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 97 | Browser::CreateParams params(profile()->GetOffTheRecordProfile(), true); |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 98 | std::unique_ptr<Browser> incognito_browser( |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 99 | chrome::CreateBrowserWithAuraTestWindowForParams(nullptr, ¶ms)); |
| 100 | // Incognito windows are excluded in GetBrowserCount() because kMatchAll |
| 101 | // doesn't match original profile of the browser with the given profile. |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 102 | EXPECT_EQ(0u, chrome::GetBrowserCount(profile())); |
| 103 | EXPECT_TRUE(chrome::FindAnyBrowser(profile(), true)); |
| 104 | EXPECT_FALSE(chrome::FindAnyBrowser(profile(), false)); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | TEST_F(BrowserFinderChromeOSTest, FindBrowserOwnedByAnotherProfile) { |
| 108 | set_browser(nullptr); |
| 109 | |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 110 | Browser::CreateParams params(profile()->GetOriginalProfile(), true); |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 111 | std::unique_ptr<Browser> browser( |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 112 | chrome::CreateBrowserWithAuraTestWindowForParams(nullptr, ¶ms)); |
| 113 | GetUserWindowManager()->SetWindowOwner(browser->window()->GetNativeWindow(), |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 114 | test_account_id1_); |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 115 | EXPECT_EQ(1u, chrome::GetBrowserCount(profile())); |
| 116 | EXPECT_TRUE(chrome::FindAnyBrowser(profile(), true)); |
| 117 | EXPECT_TRUE(chrome::FindAnyBrowser(profile(), false)); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 118 | |
| 119 | // Move the browser window to another user's desktop. Then no window should |
| 120 | // be available for the current profile. |
| 121 | GetUserWindowManager()->ShowWindowForUser( |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 122 | browser->window()->GetNativeWindow(), test_account_id2_); |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 123 | EXPECT_EQ(0u, chrome::GetBrowserCount(profile())); |
| 124 | EXPECT_FALSE(chrome::FindAnyBrowser(profile(), true)); |
| 125 | EXPECT_FALSE(chrome::FindAnyBrowser(profile(), false)); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } // namespace test |