blob: 9a23568a6c535250d16c58f49c73abd438822afc [file] [log] [blame]
xdai696309602015-07-22 01:15:111// 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
avi655876a2015-12-25 07:18:157#include "base/macros.h"
Xiyuan Xiadfe3a9f2017-11-13 21:46:268#include "base/memory/ptr_util.h"
yoshiki18898fb2016-11-18 15:59:489#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
xdai8a874a32016-05-16 19:22:1710#include "chrome/browser/chromeos/profiles/profile_helper.h"
xdai696309602015-07-22 01:15:1111#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"
xdai696309602015-07-22 01:15:1115#include "chrome/test/base/testing_profile_manager.h"
Alexander Alekseev3f09a8f2018-05-03 02:52:1016#include "components/account_id/account_id.h"
Xiyuan Xiadfe3a9f2017-11-13 21:46:2617#include "components/user_manager/scoped_user_manager.h"
xdai8a874a32016-05-16 19:22:1718#include "components/user_manager/user.h"
xdai696309602015-07-22 01:15:1119
20namespace test {
21
22namespace {
23
24const char kTestAccount1[] = "[email protected]";
25const char kTestAccount2[] = "[email protected]";
26
27} // namespace
28
29class BrowserFinderChromeOSTest : public BrowserWithTestWindowTest {
30 protected:
xdai8a874a32016-05-16 19:22:1731 BrowserFinderChromeOSTest()
32 : multi_user_window_manager_(nullptr),
yoshiki18898fb2016-11-18 15:59:4833 fake_user_manager_(new chromeos::FakeChromeUserManager),
Xiyuan Xiadfe3a9f2017-11-13 21:46:2634 user_manager_enabler_(base::WrapUnique(fake_user_manager_)) {}
xdai696309602015-07-22 01:15:1135
xdai957c25f02016-03-03 18:37:1836 TestingProfile* CreateMultiUserProfile(const AccountId& account_id) {
xdai696309602015-07-22 01:15:1137 TestingProfile* profile =
Evan Stadeef1543f2017-11-17 20:20:1338 profile_manager()->CreateTestingProfile(account_id.GetUserEmail());
xdai8a874a32016-05-16 19:22:1739 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));
xdai8a874a32016-05-16 19:22:1744 GetUserWindowManager()->AddUser(profile);
xdai696309602015-07-22 01:15:1145 return profile;
46 }
47
cm.sanchia0d45852017-11-23 11:19:3448 MultiUserWindowManagerChromeOS* GetUserWindowManager() {
xdai696309602015-07-22 01:15:1149 if (!multi_user_window_manager_) {
50 multi_user_window_manager_ =
cm.sanchia0d45852017-11-23 11:19:3451 new MultiUserWindowManagerChromeOS(test_account_id1_);
xdai696309602015-07-22 01:15:1152 multi_user_window_manager_->Init();
cm.sanchia0d45852017-11-23 11:19:3453 MultiUserWindowManager::SetInstanceForTest(multi_user_window_manager_);
xdai696309602015-07-22 01:15:1154 }
55 return multi_user_window_manager_;
56 }
57
alematecdf46dd2015-11-12 03:15:1658 AccountId test_account_id1_ = EmptyAccountId();
59 AccountId test_account_id2_ = EmptyAccountId();
60
xdai696309602015-07-22 01:15:1161 private:
62 void SetUp() override {
alematecdf46dd2015-11-12 03:15:1663 test_account_id1_ = AccountId::FromUserEmail(kTestAccount1);
64 test_account_id2_ = AccountId::FromUserEmail(kTestAccount2);
xdai696309602015-07-22 01:15:1165 BrowserWithTestWindowTest::SetUp();
Evan Stadeef1543f2017-11-17 20:20:1366 profile_manager()->SetLoggedIn(true);
xdai957c25f02016-03-03 18:37:1867 second_profile_ = CreateMultiUserProfile(test_account_id2_);
xdai696309602015-07-22 01:15:1168 }
69
70 void TearDown() override {
cm.sanchia0d45852017-11-23 11:19:3471 MultiUserWindowManager::DeleteInstance();
xdai696309602015-07-22 01:15:1172 BrowserWithTestWindowTest::TearDown();
xdai696309602015-07-22 01:15:1173 }
74
75 TestingProfile* CreateProfile() override {
xdai957c25f02016-03-03 18:37:1876 return CreateMultiUserProfile(test_account_id1_);
xdai696309602015-07-22 01:15:1177 }
78
xdai696309602015-07-22 01:15:1179 TestingProfile* second_profile_;
cm.sanchia0d45852017-11-23 11:19:3480 MultiUserWindowManagerChromeOS* multi_user_window_manager_;
yoshiki18898fb2016-11-18 15:59:4881
82 // |fake_user_manager_| is owned by |user_manager_enabler_|
83 chromeos::FakeChromeUserManager* fake_user_manager_;
Xiyuan Xiadfe3a9f2017-11-13 21:46:2684 user_manager::ScopedUserManager user_manager_enabler_;
xdai696309602015-07-22 01:15:1185
xdai696309602015-07-22 01:15:1186 DISALLOW_COPY_AND_ASSIGN(BrowserFinderChromeOSTest);
87};
88
89TEST_F(BrowserFinderChromeOSTest, IncognitoBrowserMatchTest) {
90 // GetBrowserCount() use kMatchAll to find all browser windows for profile().
scottmg34c5dd882016-02-03 05:21:5491 EXPECT_EQ(1u, chrome::GetBrowserCount(profile()));
92 EXPECT_TRUE(chrome::FindAnyBrowser(profile(), true));
93 EXPECT_TRUE(chrome::FindAnyBrowser(profile(), false));
xdai696309602015-07-22 01:15:1194 set_browser(nullptr);
95
96 // Create an incognito browser.
erg7b01d692017-02-22 21:57:3597 Browser::CreateParams params(profile()->GetOffTheRecordProfile(), true);
dcheng9603ab92016-04-08 04:17:3298 std::unique_ptr<Browser> incognito_browser(
xdai696309602015-07-22 01:15:1199 chrome::CreateBrowserWithAuraTestWindowForParams(nullptr, &params));
100 // Incognito windows are excluded in GetBrowserCount() because kMatchAll
101 // doesn't match original profile of the browser with the given profile.
scottmg34c5dd882016-02-03 05:21:54102 EXPECT_EQ(0u, chrome::GetBrowserCount(profile()));
103 EXPECT_TRUE(chrome::FindAnyBrowser(profile(), true));
104 EXPECT_FALSE(chrome::FindAnyBrowser(profile(), false));
xdai696309602015-07-22 01:15:11105}
106
107TEST_F(BrowserFinderChromeOSTest, FindBrowserOwnedByAnotherProfile) {
108 set_browser(nullptr);
109
erg7b01d692017-02-22 21:57:35110 Browser::CreateParams params(profile()->GetOriginalProfile(), true);
dcheng9603ab92016-04-08 04:17:32111 std::unique_ptr<Browser> browser(
xdai696309602015-07-22 01:15:11112 chrome::CreateBrowserWithAuraTestWindowForParams(nullptr, &params));
113 GetUserWindowManager()->SetWindowOwner(browser->window()->GetNativeWindow(),
alematecdf46dd2015-11-12 03:15:16114 test_account_id1_);
scottmg34c5dd882016-02-03 05:21:54115 EXPECT_EQ(1u, chrome::GetBrowserCount(profile()));
116 EXPECT_TRUE(chrome::FindAnyBrowser(profile(), true));
117 EXPECT_TRUE(chrome::FindAnyBrowser(profile(), false));
xdai696309602015-07-22 01:15:11118
119 // Move the browser window to another user's desktop. Then no window should
120 // be available for the current profile.
121 GetUserWindowManager()->ShowWindowForUser(
alematecdf46dd2015-11-12 03:15:16122 browser->window()->GetNativeWindow(), test_account_id2_);
scottmg34c5dd882016-02-03 05:21:54123 EXPECT_EQ(0u, chrome::GetBrowserCount(profile()));
124 EXPECT_FALSE(chrome::FindAnyBrowser(profile(), true));
125 EXPECT_FALSE(chrome::FindAnyBrowser(profile(), false));
xdai696309602015-07-22 01:15:11126}
127
128} // namespace test