blob: c698c3dabda4f306740d5d7ac7aa241431df7db1 [file] [log] [blame]
James Lee530651d22023-12-26 16:54:551// Copyright 2023 The Chromium Authors
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/browser_process.h"
6#include "chrome/browser/ui/browser.h"
7#include "chrome/browser/ui/browser_list.h"
8#include "chrome/browser/ui/browser_list_observer.h"
9#include "chrome/test/supervised_user/supervision_mixin.h"
Tomasz Jurkiewicz1ac55b12025-04-08 11:10:3310#include "components/supervised_user/core/browser/supervised_user_preferences.h"
James Lee530651d22023-12-26 16:54:5511#include "content/public/test/browser_test.h"
12#include "content/public/test/test_utils.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
Tomasz Jurkiewicz1ac55b12025-04-08 11:10:3315namespace supervised_user {
James Lee530651d22023-12-26 16:54:5516namespace {
17
18class AllIncognitoBrowsersClosedWaiter : public BrowserListObserver {
19 public:
20 AllIncognitoBrowsersClosedWaiter() { BrowserList::AddObserver(this); }
21
22 AllIncognitoBrowsersClosedWaiter(const AllIncognitoBrowsersClosedWaiter&) =
23 delete;
24 AllIncognitoBrowsersClosedWaiter& operator=(
25 const AllIncognitoBrowsersClosedWaiter&) = delete;
26
27 ~AllIncognitoBrowsersClosedWaiter() override {
28 BrowserList::RemoveObserver(this);
29 }
30
31 void Wait() { run_loop_.Run(); }
32
33 // BrowserListObserver implementation.
34 void OnBrowserRemoved(Browser* browser) override {
35 if (BrowserList::GetInstance()->GetIncognitoBrowserCount() == 0u) {
36 run_loop_.Quit();
37 }
38 }
39
40 private:
41 base::RunLoop run_loop_;
42};
43
44class SupervisedUserIncognitoBrowserTest
Tomasz Jurkiewicz1ac55b12025-04-08 11:10:3345 : public MixinBasedInProcessBrowserTest {
James Lee530651d22023-12-26 16:54:5546 protected:
Tomasz Jurkiewicz1ac55b12025-04-08 11:10:3347 SupervisionMixin supervision_mixin{
James Lee530651d22023-12-26 16:54:5548 mixin_host_,
49 this,
James Lee56786ea72024-09-20 16:22:1950 embedded_test_server(),
Tomasz Jurkiewicz1ac55b12025-04-08 11:10:3351 {.sign_in_mode = SupervisionMixin::SignInMode::kSignedOut}};
James Lee530651d22023-12-26 16:54:5552};
53
54// ChromeOS Ash does not support the browser being signed out on a supervised
55// device.
Yuta Hijikataaabe6312025-03-06 02:03:0056#if !BUILDFLAG(IS_CHROMEOS)
James Lee530651d22023-12-26 16:54:5557IN_PROC_BROWSER_TEST_F(SupervisedUserIncognitoBrowserTest,
58 UnsupervisedSignInDoesNotCloseIncognito) {
59 BrowserList* browser_list = BrowserList::GetInstance();
60
61 // Create a new incognito windows (this is allowed as the user is not signed
62 // in).
63 CHECK_EQ(browser_list->GetIncognitoBrowserCount(), 0u);
64 CreateIncognitoBrowser();
65 CHECK_EQ(browser_list->GetIncognitoBrowserCount(), 1u);
66
67 // Sign in as a regular user.
Tomasz Jurkiewicz1ac55b12025-04-08 11:10:3368 supervision_mixin.SignIn(SupervisionMixin::SignInMode::kRegular);
James Lee530651d22023-12-26 16:54:5569
70 // Check the incognito window remains open.
71 base::RunLoop().RunUntilIdle();
72 CHECK_EQ(browser_list->GetIncognitoBrowserCount(), 1u);
73}
74
75IN_PROC_BROWSER_TEST_F(SupervisedUserIncognitoBrowserTest,
76 SupervisedSignInClosesIncognito) {
77 BrowserList* browser_list = BrowserList::GetInstance();
78
79 // Create a new incognito window (this is allowed as the user is not signed
80 // in).
81 CHECK_EQ(browser_list->GetIncognitoBrowserCount(), 0u);
82 CreateIncognitoBrowser();
83 CHECK_EQ(browser_list->GetIncognitoBrowserCount(), 1u);
84
85 AllIncognitoBrowsersClosedWaiter incognito_closed_waiter;
86
87 // Sign in as a supervised user.
Tomasz Jurkiewicz1ac55b12025-04-08 11:10:3388 supervision_mixin.SignIn(SupervisionMixin::SignInMode::kSupervised);
James Lee530651d22023-12-26 16:54:5589
90 // Check the incognito window remains open.
91 incognito_closed_waiter.Wait();
Tomasz Jurkiewicz1ac55b12025-04-08 11:10:3392 ASSERT_EQ(browser_list->GetIncognitoBrowserCount(), 0u);
James Lee530651d22023-12-26 16:54:5593}
Tomasz Jurkiewicz1ac55b12025-04-08 11:10:3394
95IN_PROC_BROWSER_TEST_F(SupervisedUserIncognitoBrowserTest,
96 BrowserContentFiltersCloseIncognito) {
97 BrowserList* browser_list = BrowserList::GetInstance();
98
99 // Create a new incognito window (this is allowed as the user is not signed
100 // in).
101 CHECK_EQ(browser_list->GetIncognitoBrowserCount(), 0u);
102 CreateIncognitoBrowser();
103 CHECK_EQ(browser_list->GetIncognitoBrowserCount(), 1u);
104
105 AllIncognitoBrowsersClosedWaiter incognito_closed_waiter;
106
107 // This filtering is account-agnostic (Family Link users do not have
108 // entrypoint here).
109 EnableBrowserContentFilters(*browser()->profile()->GetPrefs());
110
111 // Check the incognito window remains open.
112 incognito_closed_waiter.Wait();
113 ASSERT_EQ(browser_list->GetIncognitoBrowserCount(), 0u);
114 // CreateIncognitoBrowser() would crash here.
115
116 // This filtering is account-agnostic (Family Link users do not have
117 // entrypoint here).
118 DisableBrowserContentFilters(*browser()->profile()->GetPrefs());
119 CreateIncognitoBrowser();
120 CHECK_EQ(browser_list->GetIncognitoBrowserCount(), 1u);
121}
122
Yuta Hijikataaabe6312025-03-06 02:03:00123#endif // !BUILDFLAG(IS_CHROMEOS)
James Lee530651d22023-12-26 16:54:55124
125} // namespace
Tomasz Jurkiewicz1ac55b12025-04-08 11:10:33126} // namespace supervised_user