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" |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 8 | #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" |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 15 | #include "components/signin/core/account_id/account_id.h" |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 16 | |
| 17 | namespace test { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | const char kTestAccount1[] = "[email protected]"; |
| 22 | const char kTestAccount2[] = "[email protected]"; |
| 23 | |
| 24 | } // namespace |
| 25 | |
| 26 | class 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_ = |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 40 | new chrome::MultiUserWindowManagerChromeOS(test_account_id1_); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 41 | 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 | |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 49 | AccountId test_account_id1_ = EmptyAccountId(); |
| 50 | AccountId test_account_id2_ = EmptyAccountId(); |
| 51 | |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 52 | private: |
| 53 | void SetUp() override { |
| 54 | profile_manager_.reset( |
| 55 | new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 56 | ASSERT_TRUE(profile_manager_->SetUp()); |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 57 | test_account_id1_ = AccountId::FromUserEmail(kTestAccount1); |
| 58 | test_account_id2_ = AccountId::FromUserEmail(kTestAccount2); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 59 | profile_manager_->SetLoggedIn(true); |
| 60 | chromeos::WallpaperManager::Initialize(); |
| 61 | BrowserWithTestWindowTest::SetUp(); |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 62 | second_profile_ = CreateMultiUserProfile(test_account_id2_.GetUserEmail()); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 63 | } |
| 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 { |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 76 | return CreateMultiUserProfile(test_account_id1_.GetUserEmail()); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 77 | } |
| 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 | |
| 90 | TEST_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, ¶ms)); |
| 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 | |
| 115 | TEST_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, ¶ms)); |
| 122 | GetUserWindowManager()->SetWindowOwner(browser->window()->GetNativeWindow(), |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 123 | test_account_id1_); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 124 | 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( |
alemate | cdf46dd | 2015-11-12 03:15:16 | [diff] [blame] | 134 | browser->window()->GetNativeWindow(), test_account_id2_); |
xdai | 69630960 | 2015-07-22 01:15:11 | [diff] [blame] | 135 | 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 |