James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 1 | // 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 Jurkiewicz | 1ac55b1 | 2025-04-08 11:10:33 | [diff] [blame] | 10 | #include "components/supervised_user/core/browser/supervised_user_preferences.h" |
James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 11 | #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 Jurkiewicz | 1ac55b1 | 2025-04-08 11:10:33 | [diff] [blame] | 15 | namespace supervised_user { |
James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | class 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 | |
| 44 | class SupervisedUserIncognitoBrowserTest |
Tomasz Jurkiewicz | 1ac55b1 | 2025-04-08 11:10:33 | [diff] [blame] | 45 | : public MixinBasedInProcessBrowserTest { |
James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 46 | protected: |
Tomasz Jurkiewicz | 1ac55b1 | 2025-04-08 11:10:33 | [diff] [blame] | 47 | SupervisionMixin supervision_mixin{ |
James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 48 | mixin_host_, |
| 49 | this, |
James Lee | 56786ea7 | 2024-09-20 16:22:19 | [diff] [blame] | 50 | embedded_test_server(), |
Tomasz Jurkiewicz | 1ac55b1 | 2025-04-08 11:10:33 | [diff] [blame] | 51 | {.sign_in_mode = SupervisionMixin::SignInMode::kSignedOut}}; |
James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // ChromeOS Ash does not support the browser being signed out on a supervised |
| 55 | // device. |
Yuta Hijikata | aabe631 | 2025-03-06 02:03:00 | [diff] [blame] | 56 | #if !BUILDFLAG(IS_CHROMEOS) |
James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 57 | IN_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 Jurkiewicz | 1ac55b1 | 2025-04-08 11:10:33 | [diff] [blame] | 68 | supervision_mixin.SignIn(SupervisionMixin::SignInMode::kRegular); |
James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 69 | |
| 70 | // Check the incognito window remains open. |
| 71 | base::RunLoop().RunUntilIdle(); |
| 72 | CHECK_EQ(browser_list->GetIncognitoBrowserCount(), 1u); |
| 73 | } |
| 74 | |
| 75 | IN_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 Jurkiewicz | 1ac55b1 | 2025-04-08 11:10:33 | [diff] [blame] | 88 | supervision_mixin.SignIn(SupervisionMixin::SignInMode::kSupervised); |
James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 89 | |
| 90 | // Check the incognito window remains open. |
| 91 | incognito_closed_waiter.Wait(); |
Tomasz Jurkiewicz | 1ac55b1 | 2025-04-08 11:10:33 | [diff] [blame] | 92 | ASSERT_EQ(browser_list->GetIncognitoBrowserCount(), 0u); |
James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 93 | } |
Tomasz Jurkiewicz | 1ac55b1 | 2025-04-08 11:10:33 | [diff] [blame] | 94 | |
| 95 | IN_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 Hijikata | aabe631 | 2025-03-06 02:03:00 | [diff] [blame] | 123 | #endif // !BUILDFLAG(IS_CHROMEOS) |
James Lee | 530651d2 | 2023-12-26 16:54:55 | [diff] [blame] | 124 | |
| 125 | } // namespace |
Tomasz Jurkiewicz | 1ac55b1 | 2025-04-08 11:10:33 | [diff] [blame] | 126 | } // namespace supervised_user |