File: kiosk_settings_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (325 lines) | stat: -rw-r--r-- 11,470 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
317
318
319
320
321
322
323
324
325
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cstddef>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/check_deref.h"
#include "base/strings/strcat.h"
#include "chrome/browser/ash/app_mode/kiosk_chrome_app_manager.h"
#include "chrome/browser/ash/app_mode/kiosk_controller.h"
#include "chrome/browser/ash/app_mode/kiosk_system_session.h"
#include "chrome/browser/ash/app_mode/test/kiosk_mixin.h"
#include "chrome/browser/ash/app_mode/test/kiosk_test_utils.h"
#include "chrome/browser/chromeos/app_mode/kiosk_settings_navigation_throttle.h"
#include "chrome/browser/ui/ash/login/login_display_host.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/window_open_disposition.h"

namespace ash {

using chromeos::KioskSettingsNavigationThrottle;
using kiosk::test::WaitKioskLaunched;

namespace {

constexpr std::string_view kSettingsUrl = "https://settings.com";

using kiosk::test::CurrentProfile;
using kiosk::test::DidKioskCloseNewWindow;

KioskSystemSession& GetKioskSystemSession() {
  return CHECK_DEREF(KioskController::Get().GetKioskSystemSession());
}

content::WebContents& ActiveWebContents(Browser& browser) {
  content::WebContents& web_contents =
      CHECK_DEREF(browser.tab_strip_model()->GetActiveWebContents());
  return web_contents;
}

NavigateParams NavigateAndReturnParams(const GURL& url,
                                       WindowOpenDisposition disposition) {
  auto& profile = CurrentProfile();
  NavigateParams params(&profile, url, ui::PAGE_TRANSITION_AUTO_BOOKMARK);
  params.disposition = disposition;
  params.window_action = NavigateParams::SHOW_WINDOW;
  Navigate(&params);
  return params;
}

// Opens a popup at `url` and returns true if the window wasn't force closed.
bool OpenPopup(const GURL& url) {
  NavigateAndReturnParams(url, WindowOpenDisposition::NEW_POPUP);
  return !DidKioskCloseNewWindow();
}

// Navigates to `url` in the current tab, and returns the browser.
Browser& NavigateInCurrentTab(const GURL& url) {
  auto params =
      NavigateAndReturnParams(url, WindowOpenDisposition::CURRENT_TAB);
  return CHECK_DEREF(params.browser.get());
}

GURL NavigateInBrowser(Browser& browser, const GURL& url) {
  auto& web_contents = ActiveWebContents(browser);
  NavigateToURLBlockUntilNavigationsComplete(
      /*web_contents=*/&web_contents, url,
      /*number_of_navigations=*/1,
      /*ignore_uncommitted_navigations=*/false);
  return web_contents.GetLastCommittedURL();
}

GURL NextCommittedUrl(Browser& browser) {
  auto& web_contents = ActiveWebContents(browser);
  content::TestNavigationObserver(&web_contents,
                                  /*expected_number_of_navigations=*/1)
      .Wait();
  return web_contents.GetLastCommittedURL();
}

// Helper for tests to override the list of settings pages.
class ScopedSettingsPages {
 public:
  explicit ScopedSettingsPages(
      std::vector<chromeos::KioskSettingsNavigationThrottle::SettingsPage>
          pages)
      : pages_(std::move(pages)) {
    chromeos::KioskSettingsNavigationThrottle::SetSettingPagesForTesting(
        &pages_);
  }

  ScopedSettingsPages(const ScopedSettingsPages&) = delete;
  ScopedSettingsPages& operator=(const ScopedSettingsPages&) = delete;

  ~ScopedSettingsPages() {
    chromeos::KioskSettingsNavigationThrottle::SetSettingPagesForTesting(
        nullptr);
  }

 private:
  std::vector<chromeos::KioskSettingsNavigationThrottle::SettingsPage> pages_;
};

}  // namespace

// Verifies settings pages work correctly in Kiosk.
class KioskSettingsTest
    : public MixinBasedInProcessBrowserTest,
      public testing::WithParamInterface<KioskMixin::Config> {
 public:
  KioskSettingsTest() = default;

  KioskSettingsTest(const KioskSettingsTest&) = delete;
  KioskSettingsTest& operator=(const KioskSettingsTest&) = delete;

  ~KioskSettingsTest() override = default;

  void SetUpOnMainThread() override {
    MixinBasedInProcessBrowserTest::SetUpOnMainThread();
    ASSERT_TRUE(WaitKioskLaunched());
  }

  KioskMixin kiosk_{&mixin_host_,
                    /*cached_configuration=*/GetParam()};
};

IN_PROC_BROWSER_TEST_P(KioskSettingsTest, CanNavigateToSettingsUrl) {
  const GURL settings_url(kSettingsUrl);

  ScopedSettingsPages scoped_pages({
      {/*url=*/settings_url.spec().c_str(), /*allow_subpages=*/false},
  });

  ASSERT_TRUE(OpenPopup(settings_url));

  auto& session = GetKioskSystemSession();
  Browser& settings = CHECK_DEREF(session.GetSettingsBrowserForTesting());
  ASSERT_EQ(NextCommittedUrl(settings), settings_url);
}

IN_PROC_BROWSER_TEST_P(KioskSettingsTest, CanNavigateToSettingsSubUrl) {
  const GURL settings_url(kSettingsUrl);
  const GURL settings_suburl(base::StrCat({kSettingsUrl, "/suburl"}));

  ScopedSettingsPages scoped_pages({
      {/*url=*/settings_url.spec().c_str(), /*allow_subpages=*/true},
  });

  ASSERT_TRUE(OpenPopup(settings_suburl));

  auto& session = GetKioskSystemSession();
  Browser& settings = CHECK_DEREF(session.GetSettingsBrowserForTesting());
  EXPECT_EQ(NextCommittedUrl(settings), settings_suburl);
}

IN_PROC_BROWSER_TEST_P(KioskSettingsTest, CannotNavigateToNonSettingsUrl) {
  const GURL settings_url(kSettingsUrl);
  const GURL other_url("https://not-settings.com/");

  ScopedSettingsPages scoped_pages({
      {/*url=*/settings_url.spec().c_str(), /*allow_subpages=*/false},
  });

  // A new window directly to a disallowed page gets closed.
  ASSERT_FALSE(OpenPopup(other_url));

  // Navigating away from settings in an existing settings window doesn't work.
  ASSERT_TRUE(OpenPopup(settings_url));

  auto& session = GetKioskSystemSession();
  Browser& settings = CHECK_DEREF(session.GetSettingsBrowserForTesting());
  ASSERT_EQ(NextCommittedUrl(settings), settings_url);

  const GURL committed_url = NavigateInBrowser(settings, other_url);
  EXPECT_EQ(committed_url, settings_url);
}

IN_PROC_BROWSER_TEST_P(KioskSettingsTest, CannotNavigateToDisallowedSubUrl) {
  const GURL settings_url(kSettingsUrl);
  const GURL settings_suburl(
      base::StrCat({kSettingsUrl, "/disallowed-suburl"}));

  ScopedSettingsPages scoped_pages({
      {/*url=*/settings_url.spec().c_str(), /*allow_subpages=*/false},
  });

  // A new window directly to a non settings URL gets closed.
  ASSERT_FALSE(OpenPopup(settings_suburl));

  // Navigating away from settings in an existing settings window doesn't work.
  ASSERT_TRUE(OpenPopup(settings_url));
  auto& session = GetKioskSystemSession();
  Browser& settings = CHECK_DEREF(session.GetSettingsBrowserForTesting());
  ASSERT_EQ(NextCommittedUrl(settings), settings_url);

  const GURL committed_url = NavigateInBrowser(settings, settings_suburl);
  EXPECT_EQ(committed_url, settings_url);
}

IN_PROC_BROWSER_TEST_P(KioskSettingsTest, DoesNotOpenTwoSettingsBrowsers) {
  const GURL settings_url_1("https://settings-one.com/");
  const GURL settings_url_2("https://settings-two.com/");

  ScopedSettingsPages scoped_pages({
      {/*url=*/settings_url_1.spec().c_str(), /*allow_subpages=*/false},
      {/*url=*/settings_url_2.spec().c_str(), /*allow_subpages=*/false},
  });

  ASSERT_TRUE(OpenPopup(settings_url_1));

  auto& session = GetKioskSystemSession();
  Browser& first_settings = CHECK_DEREF(session.GetSettingsBrowserForTesting());
  ASSERT_EQ(NextCommittedUrl(first_settings), settings_url_1);

  ASSERT_TRUE(OpenPopup(settings_url_2));

  Browser& second_settings =
      CHECK_DEREF(session.GetSettingsBrowserForTesting());
  ASSERT_EQ(NextCommittedUrl(second_settings), settings_url_2);

  EXPECT_EQ(&first_settings, &second_settings);
}

IN_PROC_BROWSER_TEST_P(KioskSettingsTest,
                       NavigatingToSettingsInAppCreatesNewBrowser) {
  const GURL settings_url(kSettingsUrl);

  ScopedSettingsPages scoped_pages({
      {/*url=*/settings_url.spec().c_str(), /*allow_subpages=*/false},
  });

  // Navigation in the current tab creates a new browser of app type, and closes
  // the non-app one.
  Browser& browser = NavigateInCurrentTab(settings_url);
  EXPECT_FALSE(DidKioskCloseNewWindow());
  EXPECT_FALSE(DidKioskCloseNewWindow());

  Browser* settings = GetKioskSystemSession().GetSettingsBrowserForTesting();
  ASSERT_NE(settings, nullptr);
  EXPECT_NE(&browser, settings);
}

IN_PROC_BROWSER_TEST_P(KioskSettingsTest,
                       SettingsBrowserIsNullWhenSettingsIsClosed) {
  const GURL settings_url(kSettingsUrl);

  ScopedSettingsPages scoped_pages({
      {/*url=*/settings_url.spec().c_str(), /*allow_subpages=*/false},
  });

  // Settings browser is initially null since there are no settings windows.
  auto& session = GetKioskSystemSession();
  ASSERT_EQ(session.GetSettingsBrowserForTesting(), nullptr);

  // Settings browser is no longer null once a settings window opens.
  ASSERT_TRUE(OpenPopup(settings_url));
  Browser* settings = session.GetSettingsBrowserForTesting();
  ASSERT_NE(settings, nullptr);

  // Settings browser becomes null when the settings window closes.
  CloseBrowserSynchronously(settings);
  ASSERT_EQ(session.GetSettingsBrowserForTesting(), nullptr);
}

// Covers crbug.com/245088137, the settings could not reopen after losing focus.
IN_PROC_BROWSER_TEST_P(KioskSettingsTest, CanRefocusSettings) {
  const auto& pages = KioskSettingsNavigationThrottle::DefaultSettingsPages();
  ASSERT_GT(pages.size(), 1UL);

  ASSERT_TRUE(OpenPopup(GURL(pages[0].url)));

  auto& session = GetKioskSystemSession();
  Browser& settings = CHECK_DEREF(session.GetSettingsBrowserForTesting());

  // The settings browser is focused.
  EXPECT_TRUE(settings.window()->IsActive());

  // Simulate a focus switch.
  settings.window()->Deactivate();
  EXPECT_FALSE(settings.window()->IsActive());

  // Verify focus can switch to any other settings page.
  for (size_t i = 1; i < pages.size(); i++) {
    const GURL other_settings_page(pages[i].url);

    // Open another settings browser and expect navigation in the old window.
    auto& web_contents = ActiveWebContents(settings);
    content::TestNavigationObserver settings_navigation_observer(&web_contents,
                                                                 1);
    ASSERT_TRUE(OpenPopup(other_settings_page));
    // Wait for navigation to finish.
    settings_navigation_observer.Wait();

    // The settings browser should not have changed.
    ASSERT_EQ(&settings, session.GetSettingsBrowserForTesting());
    EXPECT_EQ(web_contents.GetLastCommittedURL(), other_settings_page);

    // The settings browser should be focused again.
    EXPECT_TRUE(settings.window()->IsActive());
  }
}

INSTANTIATE_TEST_SUITE_P(
    All,
    KioskSettingsTest,
    testing::ValuesIn(KioskMixin::ConfigsToAutoLaunchEachAppType()),
    KioskMixin::ConfigName);

}  // namespace ash