1 // Copyright (c) 2012 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_navigator_browsertest.h"
9 #include "base/command_line.h"
10 #include "chrome/browser/chromeos/login/chrome_restart_request.h"
11 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
12 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/browser_navigator.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "chromeos/chromeos_switches.h"
21 #include "content/public/browser/web_contents.h"
25 // This is a test implementation of a MultiUserWindowManager which allows to
26 // test a visiting window on another desktop. It will install and remove itself
27 // from the system upon creation / destruction.
28 // The creation function gets a |browser| which is shown on |desktop_owner|'s
30 class TestMultiUserWindowManager
: public chrome::MultiUserWindowManager
{
32 TestMultiUserWindowManager(
33 Browser
* visiting_browser
,
34 const std::string
& desktop_owner
);
35 virtual ~TestMultiUserWindowManager();
37 aura::Window
* created_window() { return created_window_
; }
39 // MultiUserWindowManager overrides:
40 virtual void SetWindowOwner(
41 aura::Window
* window
, const std::string
& user_id
) OVERRIDE
;
42 virtual const std::string
& GetWindowOwner(aura::Window
* window
) OVERRIDE
;
43 virtual void ShowWindowForUser(
44 aura::Window
* window
, const std::string
& user_id
) OVERRIDE
;
45 virtual bool AreWindowsSharedAmongUsers() OVERRIDE
;
46 virtual void GetOwnersOfVisibleWindows(
47 std::set
<std::string
>* user_ids
) OVERRIDE
;
48 virtual bool IsWindowOnDesktopOfUser(aura::Window
* window
,
49 const std::string
& user_id
) OVERRIDE
;
50 virtual const std::string
& GetUserPresentingWindow(
51 aura::Window
* window
) OVERRIDE
;
52 virtual void AddUser(Profile
* profile
) OVERRIDE
;
53 virtual void AddObserver(Observer
* observer
) OVERRIDE
;
54 virtual void RemoveObserver(Observer
* observer
) OVERRIDE
;
57 // The window of the visiting browser.
58 aura::Window
* browser_window_
;
59 // The owner of the visiting browser.
60 std::string browser_owner_
;
61 // The owner of the currently shown desktop.
62 std::string desktop_owner_
;
63 // The created window.
64 aura::Window
* created_window_
;
65 // The location of the window.
66 std::string created_window_shown_for_
;
68 DISALLOW_COPY_AND_ASSIGN(TestMultiUserWindowManager
);
71 TestMultiUserWindowManager::TestMultiUserWindowManager(
72 Browser
* visiting_browser
,
73 const std::string
& desktop_owner
)
74 : browser_window_(visiting_browser
->window()->GetNativeWindow()),
75 browser_owner_(multi_user_util::GetUserIDFromProfile(
76 visiting_browser
->profile())),
77 desktop_owner_(desktop_owner
),
78 created_window_(NULL
),
79 created_window_shown_for_(browser_owner_
) {
80 // Create a window manager for a visiting user.
81 chrome::MultiUserWindowManager::SetInstanceForTest(
83 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED
);
86 TestMultiUserWindowManager::~TestMultiUserWindowManager() {
87 // This object is owned by the MultiUserWindowManager since the
88 // SetInstanceForTest call. As such no uninstall is required.
91 // MultiUserWindowManager overrides:
92 void TestMultiUserWindowManager::SetWindowOwner(
93 aura::Window
* window
, const std::string
& user_id
) {
97 const std::string
& TestMultiUserWindowManager::GetWindowOwner(
98 aura::Window
* window
) {
99 // No matter which window will get queried - all browsers belong to the
100 // original browser's user.
101 return browser_owner_
;
104 void TestMultiUserWindowManager::ShowWindowForUser(
105 aura::Window
* window
,
106 const std::string
& user_id
) {
107 // This class is only able to handle one additional window <-> user
108 // association beside the creation parameters.
109 // If no association has yet been requested remember it now.
110 DCHECK(!created_window_
);
111 created_window_
= window
;
112 created_window_shown_for_
= user_id
;
115 bool TestMultiUserWindowManager::AreWindowsSharedAmongUsers() {
116 return browser_owner_
!= desktop_owner_
;
119 void TestMultiUserWindowManager::GetOwnersOfVisibleWindows(
120 std::set
<std::string
>* user_ids
) {
123 bool TestMultiUserWindowManager::IsWindowOnDesktopOfUser(
124 aura::Window
* window
,
125 const std::string
& user_id
) {
126 return GetUserPresentingWindow(window
) == user_id
;
129 const std::string
& TestMultiUserWindowManager::GetUserPresentingWindow(
130 aura::Window
* window
) {
131 if (window
== browser_window_
)
132 return desktop_owner_
;
133 if (created_window_
&& window
== created_window_
)
134 return created_window_shown_for_
;
135 // We can come here before the window gets registered.
136 return browser_owner_
;
139 void TestMultiUserWindowManager::AddUser(Profile
* profile
) {
142 void TestMultiUserWindowManager::AddObserver(Observer
* observer
) {
145 void TestMultiUserWindowManager::RemoveObserver(Observer
* observer
) {
148 GURL
GetGoogleURL() {
149 return GURL("http://www.google.com/");
152 // Subclass that tests navigation while in the Guest session.
153 class BrowserGuestSessionNavigatorTest
: public BrowserNavigatorTest
{
155 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
156 CommandLine command_line_copy
= *command_line
;
157 command_line_copy
.AppendSwitchASCII(
158 chromeos::switches::kLoginProfile
, "user");
159 chromeos::GetOffTheRecordCommandLine(GetGoogleURL(),
165 // This test verifies that the settings page is opened in the incognito window
166 // in Guest Session (as well as all other windows in Guest session).
167 IN_PROC_BROWSER_TEST_F(BrowserGuestSessionNavigatorTest
,
168 Disposition_Settings_UseIncognitoWindow
) {
169 Browser
* incognito_browser
= CreateIncognitoBrowser();
171 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
172 EXPECT_EQ(1, browser()->tab_strip_model()->count());
173 EXPECT_EQ(1, incognito_browser
->tab_strip_model()->count());
175 // Navigate to the settings page.
176 chrome::NavigateParams
params(MakeNavigateParams(incognito_browser
));
177 params
.disposition
= SINGLETON_TAB
;
178 params
.url
= GURL("chrome://chrome/settings");
179 params
.window_action
= chrome::NavigateParams::SHOW_WINDOW
;
180 params
.path_behavior
= chrome::NavigateParams::IGNORE_AND_NAVIGATE
;
181 chrome::Navigate(¶ms
);
183 // Settings page should be opened in incognito window.
184 EXPECT_NE(browser(), params
.browser
);
185 EXPECT_EQ(incognito_browser
, params
.browser
);
186 EXPECT_EQ(2, incognito_browser
->tab_strip_model()->count());
187 EXPECT_EQ(GURL("chrome://chrome/settings"),
188 incognito_browser
->tab_strip_model()->GetActiveWebContents()->
192 // Test that in multi user environments a newly created browser gets created
193 // on the same desktop as the browser is shown on.
194 IN_PROC_BROWSER_TEST_F(BrowserGuestSessionNavigatorTest
,
195 Browser_Gets_Created_On_Visiting_Desktop
) {
196 // Test 1: Test that a browser created from a visiting browser will be on the
197 // same visiting desktop.
199 const std::string desktop_user_id
= "desktop_user_id@fake.com";
200 TestMultiUserWindowManager
* manager
=
201 new TestMultiUserWindowManager(browser(), desktop_user_id
);
203 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
205 // Navigate to the settings page.
206 chrome::NavigateParams
params(MakeNavigateParams(browser()));
207 params
.disposition
= NEW_POPUP
;
208 params
.url
= GURL("chrome://chrome/settings");
209 params
.window_action
= chrome::NavigateParams::SHOW_WINDOW
;
210 params
.path_behavior
= chrome::NavigateParams::IGNORE_AND_NAVIGATE
;
211 params
.browser
= browser();
212 chrome::Navigate(¶ms
);
214 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
216 aura::Window
* created_window
= manager
->created_window();
217 ASSERT_TRUE(created_window
);
218 EXPECT_TRUE(manager
->IsWindowOnDesktopOfUser(created_window
,
221 // Test 2: Test that a window which is not visiting does not cause an owner
222 // assignment of a newly created browser.
224 std::string browser_owner
=
225 multi_user_util::GetUserIDFromProfile(browser()->profile());
226 TestMultiUserWindowManager
* manager
=
227 new TestMultiUserWindowManager(browser(), browser_owner
);
229 // Navigate to the settings page.
230 chrome::NavigateParams
params(MakeNavigateParams(browser()));
231 params
.disposition
= NEW_POPUP
;
232 params
.url
= GURL("chrome://chrome/settings");
233 params
.window_action
= chrome::NavigateParams::SHOW_WINDOW
;
234 params
.path_behavior
= chrome::NavigateParams::IGNORE_AND_NAVIGATE
;
235 params
.browser
= browser();
236 chrome::Navigate(¶ms
);
238 EXPECT_EQ(3u, chrome::GetTotalBrowserCount());
240 // The ShowWindowForUser should not have been called since the window is
241 // already on the correct desktop.
242 ASSERT_FALSE(manager
->created_window());