blob: df69f5e7d04f7ff80ac929b0f46d0b3018c437e3 [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"
xdai696309602015-07-22 01:15:118#include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
9#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
10#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
11#include "chrome/test/base/browser_with_test_window_test.h"
12#include "chrome/test/base/test_browser_window_aura.h"
13#include "chrome/test/base/testing_browser_process.h"
14#include "chrome/test/base/testing_profile_manager.h"
alematecdf46dd2015-11-12 03:15:1615#include "components/signin/core/account_id/account_id.h"
xdai696309602015-07-22 01:15:1116
17namespace test {
18
19namespace {
20
21const char kTestAccount1[] = "[email protected]";
22const char kTestAccount2[] = "[email protected]";
23
24} // namespace
25
26class BrowserFinderChromeOSTest : public BrowserWithTestWindowTest {
27 protected:
28 BrowserFinderChromeOSTest() : multi_user_window_manager_(nullptr) {}
29
30 TestingProfile* CreateMultiUserProfile(const std::string& user_email) {
31 TestingProfile* profile =
32 profile_manager_->CreateTestingProfile(user_email);
33 GetUserWindowManager()->AddUser(profile);
34 return profile;
35 }
36
37 chrome::MultiUserWindowManagerChromeOS* GetUserWindowManager() {
38 if (!multi_user_window_manager_) {
39 multi_user_window_manager_ =
alematecdf46dd2015-11-12 03:15:1640 new chrome::MultiUserWindowManagerChromeOS(test_account_id1_);
xdai696309602015-07-22 01:15:1141 multi_user_window_manager_->Init();
42 chrome::MultiUserWindowManager::SetInstanceForTest(
43 multi_user_window_manager_,
44 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED);
45 }
46 return multi_user_window_manager_;
47 }
48
alematecdf46dd2015-11-12 03:15:1649 AccountId test_account_id1_ = EmptyAccountId();
50 AccountId test_account_id2_ = EmptyAccountId();
51
xdai696309602015-07-22 01:15:1152 private:
53 void SetUp() override {
54 profile_manager_.reset(
55 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
56 ASSERT_TRUE(profile_manager_->SetUp());
alematecdf46dd2015-11-12 03:15:1657 test_account_id1_ = AccountId::FromUserEmail(kTestAccount1);
58 test_account_id2_ = AccountId::FromUserEmail(kTestAccount2);
xdai696309602015-07-22 01:15:1159 profile_manager_->SetLoggedIn(true);
60 chromeos::WallpaperManager::Initialize();
61 BrowserWithTestWindowTest::SetUp();
alematecdf46dd2015-11-12 03:15:1662 second_profile_ = CreateMultiUserProfile(test_account_id2_.GetUserEmail());
xdai696309602015-07-22 01:15:1163 }
64
65 void TearDown() override {
66 chrome::MultiUserWindowManager::DeleteInstance();
67 BrowserWithTestWindowTest::TearDown();
68 chromeos::WallpaperManager::Shutdown();
69 if (second_profile_) {
70 DestroyProfile(second_profile_);
71 second_profile_ = nullptr;
72 }
73 }
74
75 TestingProfile* CreateProfile() override {
alematecdf46dd2015-11-12 03:15:1676 return CreateMultiUserProfile(test_account_id1_.GetUserEmail());
xdai696309602015-07-22 01:15:1177 }
78
79 void DestroyProfile(TestingProfile* test_profile) override {
80 profile_manager_->DeleteTestingProfile(test_profile->GetProfileUserName());
81 }
82
83 TestingProfile* second_profile_;
84 scoped_ptr<TestingProfileManager> profile_manager_;
85 chrome::MultiUserWindowManagerChromeOS* multi_user_window_manager_;
86
87 DISALLOW_COPY_AND_ASSIGN(BrowserFinderChromeOSTest);
88};
89
90TEST_F(BrowserFinderChromeOSTest, IncognitoBrowserMatchTest) {
91 // GetBrowserCount() use kMatchAll to find all browser windows for profile().
92 EXPECT_EQ(1u,
93 chrome::GetBrowserCount(profile(), chrome::HOST_DESKTOP_TYPE_ASH));
94 EXPECT_TRUE(
95 chrome::FindAnyBrowser(profile(), true, chrome::HOST_DESKTOP_TYPE_ASH));
96 EXPECT_TRUE(
97 chrome::FindAnyBrowser(profile(), false, chrome::HOST_DESKTOP_TYPE_ASH));
98 set_browser(nullptr);
99
100 // Create an incognito browser.
101 Browser::CreateParams params(profile()->GetOffTheRecordProfile(),
102 chrome::HOST_DESKTOP_TYPE_ASH);
103 scoped_ptr<Browser> incognito_browser(
104 chrome::CreateBrowserWithAuraTestWindowForParams(nullptr, &params));
105 // Incognito windows are excluded in GetBrowserCount() because kMatchAll
106 // doesn't match original profile of the browser with the given profile.
107 EXPECT_EQ(0u,
108 chrome::GetBrowserCount(profile(), chrome::HOST_DESKTOP_TYPE_ASH));
109 EXPECT_TRUE(
110 chrome::FindAnyBrowser(profile(), true, chrome::HOST_DESKTOP_TYPE_ASH));
111 EXPECT_FALSE(
112 chrome::FindAnyBrowser(profile(), false, chrome::HOST_DESKTOP_TYPE_ASH));
113}
114
115TEST_F(BrowserFinderChromeOSTest, FindBrowserOwnedByAnotherProfile) {
116 set_browser(nullptr);
117
118 Browser::CreateParams params(profile()->GetOriginalProfile(),
119 chrome::HOST_DESKTOP_TYPE_ASH);
120 scoped_ptr<Browser> browser(
121 chrome::CreateBrowserWithAuraTestWindowForParams(nullptr, &params));
122 GetUserWindowManager()->SetWindowOwner(browser->window()->GetNativeWindow(),
alematecdf46dd2015-11-12 03:15:16123 test_account_id1_);
xdai696309602015-07-22 01:15:11124 EXPECT_EQ(1u,
125 chrome::GetBrowserCount(profile(), chrome::HOST_DESKTOP_TYPE_ASH));
126 EXPECT_TRUE(
127 chrome::FindAnyBrowser(profile(), true, chrome::HOST_DESKTOP_TYPE_ASH));
128 EXPECT_TRUE(
129 chrome::FindAnyBrowser(profile(), false, chrome::HOST_DESKTOP_TYPE_ASH));
130
131 // Move the browser window to another user's desktop. Then no window should
132 // be available for the current profile.
133 GetUserWindowManager()->ShowWindowForUser(
alematecdf46dd2015-11-12 03:15:16134 browser->window()->GetNativeWindow(), test_account_id2_);
xdai696309602015-07-22 01:15:11135 EXPECT_EQ(0u,
136 chrome::GetBrowserCount(profile(), chrome::HOST_DESKTOP_TYPE_ASH));
137 EXPECT_FALSE(
138 chrome::FindAnyBrowser(profile(), true, chrome::HOST_DESKTOP_TYPE_ASH));
139 EXPECT_FALSE(
140 chrome::FindAnyBrowser(profile(), false, chrome::HOST_DESKTOP_TYPE_ASH));
141}
142
143} // namespace test