Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / browser_navigator_browsertest_chromeos.cc
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.
4
5 #include "chrome/browser/ui/browser_navigator_browsertest.h"
6
7 #include <string>
8
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"
22
23 namespace {
24
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();
36
37   aura::Window* created_window() { return created_window_; }
38
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
54  private:
55   // The window of the visiting browser.
56   aura::Window* browser_window_;
57   // The owner of the visiting browser.
58   std::string browser_owner_;
59   // The owner of the currently shown desktop.
60   std::string desktop_owner_;
61   // The created window.
62   aura::Window* created_window_;
63   // The location of the window.
64   std::string created_window_shown_for_;
65
66   DISALLOW_COPY_AND_ASSIGN(TestMultiUserWindowManager);
67 };
68
69 TestMultiUserWindowManager::TestMultiUserWindowManager(
70     Browser* visiting_browser,
71     const std::string& desktop_owner)
72     : browser_window_(visiting_browser->window()->GetNativeWindow()),
73       browser_owner_(multi_user_util::GetUserIDFromProfile(
74           visiting_browser->profile())),
75       desktop_owner_(desktop_owner),
76       created_window_(NULL),
77       created_window_shown_for_(browser_owner_) {
78   // Create a window manager for a visiting user.
79   chrome::MultiUserWindowManager::SetInstanceForTest(
80       this,
81       chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED);
82 }
83
84 TestMultiUserWindowManager::~TestMultiUserWindowManager() {
85   // This object is owned by the MultiUserWindowManager since the
86   // SetInstanceForTest call. As such no uninstall is required.
87 }
88
89 // MultiUserWindowManager overrides:
90 void TestMultiUserWindowManager::SetWindowOwner(
91     aura::Window* window, const std::string& user_id) {
92   NOTREACHED();
93 }
94
95 const std::string& TestMultiUserWindowManager::GetWindowOwner(
96     aura::Window* window) {
97   // No matter which window will get queried - all browsers belong to the
98   // original browser's user.
99   return browser_owner_;
100 }
101
102 void TestMultiUserWindowManager::ShowWindowForUser(
103     aura::Window* window,
104     const std::string& user_id) {
105   // This class is only able to handle one additional window <-> user
106   // association beside the creation parameters.
107   // If no association has yet been requested remember it now.
108   DCHECK(!created_window_);
109   created_window_ = window;
110   created_window_shown_for_ = user_id;
111 }
112
113 bool TestMultiUserWindowManager::AreWindowsSharedAmongUsers() {
114   return browser_owner_ != desktop_owner_;
115 }
116
117 void TestMultiUserWindowManager::GetOwnersOfVisibleWindows(
118     std::set<std::string>* user_ids) {
119 }
120
121 bool TestMultiUserWindowManager::IsWindowOnDesktopOfUser(
122     aura::Window* window,
123     const std::string& user_id) {
124   return GetUserPresentingWindow(window) == user_id;
125 }
126
127 const std::string& TestMultiUserWindowManager::GetUserPresentingWindow(
128     aura::Window* window) {
129   if (window == browser_window_)
130     return desktop_owner_;
131   if (created_window_ && window == created_window_)
132     return created_window_shown_for_;
133   // We can come here before the window gets registered.
134   return browser_owner_;
135 }
136
137 void TestMultiUserWindowManager::AddUser(Profile* profile) {
138 }
139
140 GURL GetGoogleURL() {
141   return GURL("http://www.google.com/");
142 }
143
144 // Subclass that tests navigation while in the Guest session.
145 class BrowserGuestSessionNavigatorTest: public BrowserNavigatorTest {
146  protected:
147   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
148     CommandLine command_line_copy = *command_line;
149     command_line_copy.AppendSwitchASCII(
150         chromeos::switches::kLoginProfile, "user");
151     chromeos::GetOffTheRecordCommandLine(GetGoogleURL(),
152                                          command_line_copy,
153                                          command_line);
154   }
155 };
156
157 // This test verifies that the settings page is opened in the incognito window
158 // in Guest Session (as well as all other windows in Guest session).
159 IN_PROC_BROWSER_TEST_F(BrowserGuestSessionNavigatorTest,
160                        Disposition_Settings_UseIncognitoWindow) {
161   Browser* incognito_browser = CreateIncognitoBrowser();
162
163   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
164   EXPECT_EQ(1, browser()->tab_strip_model()->count());
165   EXPECT_EQ(1, incognito_browser->tab_strip_model()->count());
166
167   // Navigate to the settings page.
168   chrome::NavigateParams params(MakeNavigateParams(incognito_browser));
169   params.disposition = SINGLETON_TAB;
170   params.url = GURL("chrome://chrome/settings");
171   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
172   params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
173   chrome::Navigate(&params);
174
175   // Settings page should be opened in incognito window.
176   EXPECT_NE(browser(), params.browser);
177   EXPECT_EQ(incognito_browser, params.browser);
178   EXPECT_EQ(2, incognito_browser->tab_strip_model()->count());
179   EXPECT_EQ(GURL("chrome://chrome/settings"),
180             incognito_browser->tab_strip_model()->GetActiveWebContents()->
181                 GetURL());
182 }
183
184 // Test that in multi user environments a newly created browser gets created
185 // on the same desktop as the browser is shown on.
186 IN_PROC_BROWSER_TEST_F(BrowserGuestSessionNavigatorTest,
187                        Browser_Gets_Created_On_Visiting_Desktop) {
188   // Test 1: Test that a browser created from a visiting browser will be on the
189   // same visiting desktop.
190   {
191     const std::string desktop_user_id = "desktop_user_id@fake.com";
192     TestMultiUserWindowManager* manager =
193         new TestMultiUserWindowManager(browser(), desktop_user_id);
194
195     EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
196
197     // Navigate to the settings page.
198     chrome::NavigateParams params(MakeNavigateParams(browser()));
199     params.disposition = NEW_POPUP;
200     params.url = GURL("chrome://chrome/settings");
201     params.window_action = chrome::NavigateParams::SHOW_WINDOW;
202     params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
203     params.browser = browser();
204     chrome::Navigate(&params);
205
206     EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
207
208     aura::Window* created_window = manager->created_window();
209     ASSERT_TRUE(created_window);
210     EXPECT_TRUE(manager->IsWindowOnDesktopOfUser(created_window,
211                                                  desktop_user_id));
212   }
213   // Test 2: Test that a window which is not visiting does not cause an owner
214   // assignment of a newly created browser.
215   {
216     std::string browser_owner =
217         multi_user_util::GetUserIDFromProfile(browser()->profile());
218     TestMultiUserWindowManager* manager =
219         new TestMultiUserWindowManager(browser(), browser_owner);
220
221     // Navigate to the settings page.
222     chrome::NavigateParams params(MakeNavigateParams(browser()));
223     params.disposition = NEW_POPUP;
224     params.url = GURL("chrome://chrome/settings");
225     params.window_action = chrome::NavigateParams::SHOW_WINDOW;
226     params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
227     params.browser = browser();
228     chrome::Navigate(&params);
229
230     EXPECT_EQ(3u, chrome::GetTotalBrowserCount());
231
232     // The ShowWindowForUser should not have been called since the window is
233     // already on the correct desktop.
234     ASSERT_FALSE(manager->created_window());
235   }
236 }
237
238 }  // namespace