Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / ui / browser_navigator_browsertest_chromeos.cc
blob98052b0054758f9d163067d1462be9de9afec298
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"
7 #include <string>
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"
23 namespace {
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
29 // desktop.
30 class TestMultiUserWindowManager : public chrome::MultiUserWindowManager {
31 public:
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(
43 aura::Window* window) const OVERRIDE;
44 virtual void ShowWindowForUser(
45 aura::Window* window, const std::string& user_id) OVERRIDE;
46 virtual bool AreWindowsSharedAmongUsers() const OVERRIDE;
47 virtual void GetOwnersOfVisibleWindows(
48 std::set<std::string>* user_ids) const OVERRIDE;
49 virtual bool IsWindowOnDesktopOfUser(
50 aura::Window* window,
51 const std::string& user_id) const OVERRIDE;
52 virtual const std::string& GetUserPresentingWindow(
53 aura::Window* window) const OVERRIDE;
54 virtual void AddUser(content::BrowserContext* profile) OVERRIDE;
55 virtual void AddObserver(Observer* observer) OVERRIDE;
56 virtual void RemoveObserver(Observer* observer) OVERRIDE;
58 private:
59 // The window of the visiting browser.
60 aura::Window* browser_window_;
61 // The owner of the visiting browser.
62 std::string browser_owner_;
63 // The owner of the currently shown desktop.
64 std::string desktop_owner_;
65 // The created window.
66 aura::Window* created_window_;
67 // The location of the window.
68 std::string created_window_shown_for_;
70 DISALLOW_COPY_AND_ASSIGN(TestMultiUserWindowManager);
73 TestMultiUserWindowManager::TestMultiUserWindowManager(
74 Browser* visiting_browser,
75 const std::string& desktop_owner)
76 : browser_window_(visiting_browser->window()->GetNativeWindow()),
77 browser_owner_(multi_user_util::GetUserIDFromProfile(
78 visiting_browser->profile())),
79 desktop_owner_(desktop_owner),
80 created_window_(NULL),
81 created_window_shown_for_(browser_owner_) {
82 // Create a window manager for a visiting user.
83 chrome::MultiUserWindowManager::SetInstanceForTest(
84 this,
85 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED);
88 TestMultiUserWindowManager::~TestMultiUserWindowManager() {
89 // This object is owned by the MultiUserWindowManager since the
90 // SetInstanceForTest call. As such no uninstall is required.
93 // MultiUserWindowManager overrides:
94 void TestMultiUserWindowManager::SetWindowOwner(
95 aura::Window* window, const std::string& user_id) {
96 NOTREACHED();
99 const std::string& TestMultiUserWindowManager::GetWindowOwner(
100 aura::Window* window) const {
101 // No matter which window will get queried - all browsers belong to the
102 // original browser's user.
103 return browser_owner_;
106 void TestMultiUserWindowManager::ShowWindowForUser(
107 aura::Window* window,
108 const std::string& user_id) {
109 // This class is only able to handle one additional window <-> user
110 // association beside the creation parameters.
111 // If no association has yet been requested remember it now.
112 DCHECK(!created_window_);
113 created_window_ = window;
114 created_window_shown_for_ = user_id;
117 bool TestMultiUserWindowManager::AreWindowsSharedAmongUsers() const {
118 return browser_owner_ != desktop_owner_;
121 void TestMultiUserWindowManager::GetOwnersOfVisibleWindows(
122 std::set<std::string>* user_ids) const {
125 bool TestMultiUserWindowManager::IsWindowOnDesktopOfUser(
126 aura::Window* window,
127 const std::string& user_id) const {
128 return GetUserPresentingWindow(window) == user_id;
131 const std::string& TestMultiUserWindowManager::GetUserPresentingWindow(
132 aura::Window* window) const {
133 if (window == browser_window_)
134 return desktop_owner_;
135 if (created_window_ && window == created_window_)
136 return created_window_shown_for_;
137 // We can come here before the window gets registered.
138 return browser_owner_;
141 void TestMultiUserWindowManager::AddUser(content::BrowserContext* profile) {
144 void TestMultiUserWindowManager::AddObserver(Observer* observer) {
147 void TestMultiUserWindowManager::RemoveObserver(Observer* observer) {
150 GURL GetGoogleURL() {
151 return GURL("http://www.google.com/");
154 // Subclass that tests navigation while in the Guest session.
155 class BrowserGuestSessionNavigatorTest: public BrowserNavigatorTest {
156 protected:
157 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
158 CommandLine command_line_copy = *command_line;
159 command_line_copy.AppendSwitchASCII(
160 chromeos::switches::kLoginProfile, "user");
161 chromeos::GetOffTheRecordCommandLine(GetGoogleURL(),
162 true,
163 command_line_copy,
164 command_line);
168 // This test verifies that the settings page is opened in the incognito window
169 // in Guest Session (as well as all other windows in Guest session).
170 IN_PROC_BROWSER_TEST_F(BrowserGuestSessionNavigatorTest,
171 Disposition_Settings_UseIncognitoWindow) {
172 Browser* incognito_browser = CreateIncognitoBrowser();
174 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
175 EXPECT_EQ(1, browser()->tab_strip_model()->count());
176 EXPECT_EQ(1, incognito_browser->tab_strip_model()->count());
178 // Navigate to the settings page.
179 chrome::NavigateParams params(MakeNavigateParams(incognito_browser));
180 params.disposition = SINGLETON_TAB;
181 params.url = GURL("chrome://chrome/settings");
182 params.window_action = chrome::NavigateParams::SHOW_WINDOW;
183 params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
184 chrome::Navigate(&params);
186 // Settings page should be opened in incognito window.
187 EXPECT_NE(browser(), params.browser);
188 EXPECT_EQ(incognito_browser, params.browser);
189 EXPECT_EQ(2, incognito_browser->tab_strip_model()->count());
190 EXPECT_EQ(GURL("chrome://chrome/settings"),
191 incognito_browser->tab_strip_model()->GetActiveWebContents()->
192 GetURL());
195 // Test that in multi user environments a newly created browser gets created
196 // on the same desktop as the browser is shown on.
197 IN_PROC_BROWSER_TEST_F(BrowserGuestSessionNavigatorTest,
198 Browser_Gets_Created_On_Visiting_Desktop) {
199 // Test 1: Test that a browser created from a visiting browser will be on the
200 // same visiting desktop.
202 const std::string desktop_user_id = "desktop_user_id@fake.com";
203 TestMultiUserWindowManager* manager =
204 new TestMultiUserWindowManager(browser(), desktop_user_id);
206 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
208 // Navigate to the settings page.
209 chrome::NavigateParams params(MakeNavigateParams(browser()));
210 params.disposition = NEW_POPUP;
211 params.url = GURL("chrome://chrome/settings");
212 params.window_action = chrome::NavigateParams::SHOW_WINDOW;
213 params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
214 params.browser = browser();
215 chrome::Navigate(&params);
217 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
219 aura::Window* created_window = manager->created_window();
220 ASSERT_TRUE(created_window);
221 EXPECT_TRUE(manager->IsWindowOnDesktopOfUser(created_window,
222 desktop_user_id));
224 // Test 2: Test that a window which is not visiting does not cause an owner
225 // assignment of a newly created browser.
227 std::string browser_owner =
228 multi_user_util::GetUserIDFromProfile(browser()->profile());
229 TestMultiUserWindowManager* manager =
230 new TestMultiUserWindowManager(browser(), browser_owner);
232 // Navigate to the settings page.
233 chrome::NavigateParams params(MakeNavigateParams(browser()));
234 params.disposition = NEW_POPUP;
235 params.url = GURL("chrome://chrome/settings");
236 params.window_action = chrome::NavigateParams::SHOW_WINDOW;
237 params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
238 params.browser = browser();
239 chrome::Navigate(&params);
241 EXPECT_EQ(3u, chrome::GetTotalBrowserCount());
243 // The ShowWindowForUser should not have been called since the window is
244 // already on the correct desktop.
245 ASSERT_FALSE(manager->created_window());
249 } // namespace