File: browser_navigator_browsertest_chromeos.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (316 lines) | stat: -rw-r--r-- 13,213 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/browser_navigator_browsertest.h"

#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/multi_user_window_manager.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/wm/window_pin_util.h"
#include "base/command_line.h"
#include "chrome/browser/ash/login/chrome_restart_request.h"
#include "chrome/browser/ash/login/test/device_state_mixin.h"
#include "chrome/browser/ash/login/test/login_manager_mixin.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_helper.h"
#include "chrome/browser/ui/ash/session/session_controller_client_impl.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_helper.h"
#include "components/account_id/account_id.h"
#include "components/account_id/account_id_literal.h"
#include "components/prefs/pref_service.h"
#include "components/session_manager/core/session.h"
#include "components/session_manager/core/session_manager.h"
#include "components/signin/public/identity_manager/account_managed_status_finder.h"
#include "components/user_manager/user_manager_pref_names.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "ui/aura/window.h"

namespace {

using BrowserNavigatorTestChromeOS = BrowserNavigatorTest;

GURL GetGoogleURL() {
  return GURL("http://www.google.com/");
}

// Verifies that new browser is not opened for Signin profile.
IN_PROC_BROWSER_TEST_F(BrowserNavigatorTestChromeOS, RestrictSigninProfile) {
  EXPECT_EQ(chrome::GetTotalBrowserCount(), 1u);

  EXPECT_EQ(Browser::CreationStatus::kErrorProfileUnsuitable,
            Browser::GetCreationStatusForProfile(
                ash::ProfileHelper::GetSigninProfile()));
}

// Verify that page navigation is blocked in locked fullscreen mode.
IN_PROC_BROWSER_TEST_F(BrowserNavigatorTestChromeOS,
                       NavigationBlockedInLockedFullscreen) {
  // Set locked fullscreen state.
  aura::Window* window = browser()->window()->GetNativeWindow();
  PinWindow(window, /*trusted=*/true);

  // Navigate to a page.
  auto url = GURL(chrome::kChromeUIVersionURL);
  NavigateParams params(MakeNavigateParams(browser()));
  params.disposition = WindowOpenDisposition::NEW_WINDOW;
  params.url = url;
  params.window_action = NavigateParams::SHOW_WINDOW;
  Navigate(&params);

  // The page should not be opened, and the browser should still sit at the
  // default about:blank page.
  EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());
  EXPECT_EQ(GURL(url::kAboutBlankURL),
            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());

  // As a sanity check unset the locked fullscreen state and make sure that the
  // navigation happens (the following EXPECTs fail if the next line isn't
  // executed).
  UnpinWindow(window);

  Navigate(&params);

  // The original browser should still be at the same page, but the newly
  // opened browser should sit on the chrome:version page.
  EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());
  EXPECT_EQ(GURL(url::kAboutBlankURL),
            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
  EXPECT_EQ(1, params.browser->tab_strip_model()->count());
  EXPECT_EQ(
      GURL(chrome::kChromeUIVersionURL),
      params.browser->tab_strip_model()->GetActiveWebContents()->GetURL());
}

// Verify that page navigation is allowed in locked fullscreen mode when locked
// for OnTask. Only applicable for non-web browser scenarios.
IN_PROC_BROWSER_TEST_F(BrowserNavigatorTestChromeOS,
                       NavigationAllowedInLockedFullscreenWhenLockedForOnTask) {
  // Set locked fullscreen state.
  aura::Window* const window = browser()->window()->GetNativeWindow();
  PinWindow(window, /*trusted=*/true);
  browser()->SetLockedForOnTask(true);

  // Navigate to a page.
  const GURL kUrl(chrome::kChromeUIVersionURL);
  NavigateParams params(MakeNavigateParams(browser()));
  params.disposition = WindowOpenDisposition::NEW_WINDOW;
  params.url = kUrl;
  params.window_action = NavigateParams::SHOW_WINDOW;
  Navigate(&params);

  // The original browser should still be at the same page, but the newly
  // opened browser should sit on the chrome:version page.
  ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
  ASSERT_EQ(1, browser()->tab_strip_model()->count());
  EXPECT_EQ(GURL(url::kAboutBlankURL),
            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
  ASSERT_EQ(1, params.browser->tab_strip_model()->count());
  EXPECT_EQ(
      kUrl,
      params.browser->tab_strip_model()->GetActiveWebContents()->GetURL());
}

// Subclass that tests navigation while in the Guest session.
class BrowserGuestSessionNavigatorTest : public BrowserNavigatorTest {
 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    base::CommandLine command_line_copy = *command_line;
    command_line_copy.AppendSwitchASCII(ash::switches::kLoginProfile, "user");
    command_line_copy.AppendSwitch(ash::switches::kGuestSession);
    ash::GetOffTheRecordCommandLine(GetGoogleURL(), command_line_copy,
                                    command_line);
  }
};

// This test verifies that the settings page is opened in the incognito window
// in Guest Session (as well as all other windows in Guest session).
IN_PROC_BROWSER_TEST_F(BrowserGuestSessionNavigatorTest,
                       Disposition_Settings_UseIncognitoWindow) {
  Browser* incognito_browser = CreateIncognitoBrowser();

  EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());
  EXPECT_EQ(1, incognito_browser->tab_strip_model()->count());

  // Navigate to the settings page.
  NavigateParams params(MakeNavigateParams(incognito_browser));
  params.disposition = WindowOpenDisposition::SINGLETON_TAB;
  params.url = GURL("chrome://settings");
  params.window_action = NavigateParams::SHOW_WINDOW;
  params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
  Navigate(&params);

  // Settings page should be opened in incognito window.
  EXPECT_NE(browser(), params.browser);
  EXPECT_EQ(incognito_browser, params.browser);
  EXPECT_EQ(2, incognito_browser->tab_strip_model()->count());
  EXPECT_EQ(
      GURL("chrome://settings"),
      incognito_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
}

class BrowserNavigatorMultiUserTestChromeOS
    : public InProcessBrowserTestMixinHostSupport<
          BrowserNavigatorTestChromeOS> {
 public:
  static constexpr inline auto kPrimaryAccountId =
      AccountId::Literal::FromUserEmailGaiaId("primary@test",
                                              GaiaId::Literal("12345"));
  static constexpr inline auto kSecondaryAccountId =
      AccountId::Literal::FromUserEmailGaiaId("secondary@test",
                                              GaiaId::Literal("67890"));

  BrowserNavigatorMultiUserTestChromeOS() = default;
  ~BrowserNavigatorMultiUserTestChromeOS() override = default;

  void SetUp() override {
    set_exit_when_last_browser_closes(false);
    signin::AccountManagedStatusFinder::SetNonEnterpriseDomainForTesting(
        "test");
    InProcessBrowserTestMixinHostSupport<BrowserNavigatorTestChromeOS>::SetUp();
  }

  void TearDown() override {
    InProcessBrowserTestMixinHostSupport<
        BrowserNavigatorTestChromeOS>::TearDown();
    signin::AccountManagedStatusFinder::SetNonEnterpriseDomainForTesting(
        nullptr);
  }

  void LogIn(const AccountId& account_id) {
    // If there's already a session, i.e. if this is multi user sign in,
    // first, launch user selection flow.
    if (auto* primary_session =
            session_manager::SessionManager::Get()->GetPrimarySession()) {
      // Mark the acknowledge for the multi-user sign-in.
      user_manager::User* primary_user =
          user_manager::UserManager::Get()->FindUserAndModify(
              primary_session->account_id());
      primary_user->GetProfilePrefs()->SetBoolean(
          user_manager::prefs::kMultiProfileNeverShowIntro, true);
      SessionControllerClientImpl::Get()->ShowMultiProfileLogin();
    }

    login_manager_mixin_.LoginWithDefaultContext(
        ash::LoginManagerMixin::TestUserInfo(account_id));
  }

 private:
  ash::DeviceStateMixin device_state_{
      &mixin_host_,
      ash::DeviceStateMixin::State::OOBE_COMPLETED_PERMANENTLY_UNOWNED};
  ash::LoginManagerMixin login_manager_mixin_{
      &mixin_host_,
      {ash::LoginManagerMixin::TestUserInfo(kPrimaryAccountId),
       ash::LoginManagerMixin::TestUserInfo(kSecondaryAccountId)}};
};

// Test that in multi user environments a newly created browser gets created
// on the same desktop as the browser is shown on.
IN_PROC_BROWSER_TEST_F(BrowserNavigatorMultiUserTestChromeOS,
                       Browser_Gets_Created_On_Visiting_Desktop) {
  // Set up primary user.
  LogIn(kPrimaryAccountId);
  Profile* primary_user_profile = Profile::FromBrowserContext(
      ash::BrowserContextHelper::Get()->GetBrowserContextByAccountId(
          kPrimaryAccountId));
  ASSERT_TRUE(primary_user_profile);

  // Create a browser window with the primary user.
  ash::NewWindowDelegate::GetInstance()->NewWindow(
      /*incognito=*/false, /*should_trigger_session_restore=*/false);

  ASSERT_EQ(1u, chrome::GetTotalBrowserCount());
  Browser* browser = chrome::FindBrowserWithProfile(primary_user_profile);
  ASSERT_TRUE(browser);

  // Start multi-user sign-in.
  LogIn(kSecondaryAccountId);

  auto* window_manager = MultiUserWindowManagerHelper::GetWindowManager();

  // Test 1: Test that a browser created from a visiting browser will be on the
  // same visiting desktop.
  {
    // Make sure the current active user is the secondary.
    ASSERT_EQ(session_manager::SessionManager::Get()
                  ->GetActiveSession()
                  ->account_id(),
              kSecondaryAccountId);

    // Teleport the primary user's browser window to the secondary user's
    // desktop.
    window_manager->ShowWindowForUser(
        browser->window()->GetNativeWindow()->GetToplevelWindow(),
        kSecondaryAccountId);
    EXPECT_EQ(1u, chrome::GetTotalBrowserCount());

    // Navigate to the settings page from the primary user's browser.
    NavigateParams params(MakeNavigateParams(browser));
    params.disposition = WindowOpenDisposition::NEW_POPUP;
    params.url = GURL("chrome://settings");
    params.window_action = NavigateParams::SHOW_WINDOW;
    params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
    params.browser = browser;
    auto navigated = Navigate(&params);
    EXPECT_EQ(2u, chrome::GetTotalBrowserCount());

    // Verify the created window is shown on the secondary user's desktop.
    aura::Window* created_window =
        navigated->GetWebContents()->GetTopLevelNativeWindow();
    ASSERT_TRUE(created_window);
    EXPECT_TRUE(
        MultiUserWindowManagerHelper::GetInstance()->IsWindowOnDesktopOfUser(
            created_window, kSecondaryAccountId));
  }

  // Test 2: Test that a window which is not visiting does not cause an owner
  // assignment of a newly created browser.
  {
    // Move the browser window back to the primary user's desktop.
    window_manager->ShowWindowForUser(
        browser->window()->GetNativeWindow()->GetToplevelWindow(),
        kPrimaryAccountId);
    // Teleporting the window also triggers to switch the active session.
    ASSERT_EQ(session_manager::SessionManager::Get()
                  ->GetActiveSession()
                  ->account_id(),
              kPrimaryAccountId);

    // Navigate to the settings page.
    NavigateParams params(MakeNavigateParams(browser));
    params.disposition = WindowOpenDisposition::NEW_POPUP;
    params.url = GURL("chrome://settings");
    params.window_action = NavigateParams::SHOW_WINDOW;
    params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
    params.browser = browser;
    auto navigated = Navigate(&params);
    EXPECT_EQ(3u, chrome::GetTotalBrowserCount());

    // The created window should be at the primary user's desktop now.
    aura::Window* created_window =
        navigated->GetWebContents()->GetTopLevelNativeWindow();
    ASSERT_TRUE(created_window);
    EXPECT_TRUE(
        MultiUserWindowManagerHelper::GetInstance()->IsWindowOnDesktopOfUser(
            created_window, kPrimaryAccountId));
  }
}

}  // namespace