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.
5 #include "chrome/browser/ui/browser_finder.h"
7 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
8 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
9 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
10 #include "chrome/test/base/browser_with_test_window_test.h"
11 #include "chrome/test/base/test_browser_window_aura.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/testing_profile_manager.h"
19 const char kTestAccount1
[] = "user1@test.com";
20 const char kTestAccount2
[] = "user2@test.com";
24 class BrowserFinderChromeOSTest
: public BrowserWithTestWindowTest
{
26 BrowserFinderChromeOSTest() : multi_user_window_manager_(nullptr) {}
28 TestingProfile
* CreateMultiUserProfile(const std::string
& user_email
) {
29 TestingProfile
* profile
=
30 profile_manager_
->CreateTestingProfile(user_email
);
31 GetUserWindowManager()->AddUser(profile
);
35 chrome::MultiUserWindowManagerChromeOS
* GetUserWindowManager() {
36 if (!multi_user_window_manager_
) {
37 multi_user_window_manager_
=
38 new chrome::MultiUserWindowManagerChromeOS(kTestAccount1
);
39 multi_user_window_manager_
->Init();
40 chrome::MultiUserWindowManager::SetInstanceForTest(
41 multi_user_window_manager_
,
42 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED
);
44 return multi_user_window_manager_
;
48 void SetUp() override
{
49 profile_manager_
.reset(
50 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
51 ASSERT_TRUE(profile_manager_
->SetUp());
52 profile_manager_
->SetLoggedIn(true);
53 chromeos::WallpaperManager::Initialize();
54 BrowserWithTestWindowTest::SetUp();
55 second_profile_
= CreateMultiUserProfile(kTestAccount2
);
58 void TearDown() override
{
59 chrome::MultiUserWindowManager::DeleteInstance();
60 BrowserWithTestWindowTest::TearDown();
61 chromeos::WallpaperManager::Shutdown();
62 if (second_profile_
) {
63 DestroyProfile(second_profile_
);
64 second_profile_
= nullptr;
68 TestingProfile
* CreateProfile() override
{
69 return CreateMultiUserProfile(kTestAccount1
);
72 void DestroyProfile(TestingProfile
* test_profile
) override
{
73 profile_manager_
->DeleteTestingProfile(test_profile
->GetProfileUserName());
76 TestingProfile
* second_profile_
;
77 scoped_ptr
<TestingProfileManager
> profile_manager_
;
78 chrome::MultiUserWindowManagerChromeOS
* multi_user_window_manager_
;
80 DISALLOW_COPY_AND_ASSIGN(BrowserFinderChromeOSTest
);
83 TEST_F(BrowserFinderChromeOSTest
, IncognitoBrowserMatchTest
) {
84 // GetBrowserCount() use kMatchAll to find all browser windows for profile().
86 chrome::GetBrowserCount(profile(), chrome::HOST_DESKTOP_TYPE_ASH
));
88 chrome::FindAnyBrowser(profile(), true, chrome::HOST_DESKTOP_TYPE_ASH
));
90 chrome::FindAnyBrowser(profile(), false, chrome::HOST_DESKTOP_TYPE_ASH
));
93 // Create an incognito browser.
94 Browser::CreateParams
params(profile()->GetOffTheRecordProfile(),
95 chrome::HOST_DESKTOP_TYPE_ASH
);
96 scoped_ptr
<Browser
> incognito_browser(
97 chrome::CreateBrowserWithAuraTestWindowForParams(nullptr, ¶ms
));
98 // Incognito windows are excluded in GetBrowserCount() because kMatchAll
99 // doesn't match original profile of the browser with the given profile.
101 chrome::GetBrowserCount(profile(), chrome::HOST_DESKTOP_TYPE_ASH
));
103 chrome::FindAnyBrowser(profile(), true, chrome::HOST_DESKTOP_TYPE_ASH
));
105 chrome::FindAnyBrowser(profile(), false, chrome::HOST_DESKTOP_TYPE_ASH
));
108 TEST_F(BrowserFinderChromeOSTest
, FindBrowserOwnedByAnotherProfile
) {
109 set_browser(nullptr);
111 Browser::CreateParams
params(profile()->GetOriginalProfile(),
112 chrome::HOST_DESKTOP_TYPE_ASH
);
113 scoped_ptr
<Browser
> browser(
114 chrome::CreateBrowserWithAuraTestWindowForParams(nullptr, ¶ms
));
115 GetUserWindowManager()->SetWindowOwner(browser
->window()->GetNativeWindow(),
118 chrome::GetBrowserCount(profile(), chrome::HOST_DESKTOP_TYPE_ASH
));
120 chrome::FindAnyBrowser(profile(), true, chrome::HOST_DESKTOP_TYPE_ASH
));
122 chrome::FindAnyBrowser(profile(), false, chrome::HOST_DESKTOP_TYPE_ASH
));
124 // Move the browser window to another user's desktop. Then no window should
125 // be available for the current profile.
126 GetUserWindowManager()->ShowWindowForUser(
127 browser
->window()->GetNativeWindow(), kTestAccount2
);
129 chrome::GetBrowserCount(profile(), chrome::HOST_DESKTOP_TYPE_ASH
));
131 chrome::FindAnyBrowser(profile(), true, chrome::HOST_DESKTOP_TYPE_ASH
));
133 chrome::FindAnyBrowser(profile(), false, chrome::HOST_DESKTOP_TYPE_ASH
));