Hide Tor settings only when Tor is disabled and managed (#32162)

Disable Tor settings only when Tor is disabled and managed
This commit is contained in:
Brian R. Bondy
2025-11-01 08:13:36 -04:00
committed by GitHub
parent d6747425fc
commit 059995b375
3 changed files with 80 additions and 1 deletions
@@ -12,11 +12,14 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
using testing::_;
using testing::Return;
@@ -84,6 +87,53 @@ IN_PROC_BROWSER_TEST_F(TorEnabledPolicyBrowserTest, TorDisabledPrefValueTest) {
TorProfileServiceFactory::SetTorDisabled(true);
EXPECT_FALSE(TorProfileServiceFactory::IsTorDisabled(browser()->profile()));
}
// Test that Tor settings are hidden in chrome://settings when disabled by
// policy
IN_PROC_BROWSER_TEST_F(TorDisabledPolicyBrowserTest,
TorSettingsHiddenByPolicy) {
// Verify Tor is disabled by policy
ASSERT_TRUE(TorProfileServiceFactory::IsTorDisabled(browser()->profile()));
ASSERT_TRUE(TorProfileServiceFactory::IsTorManaged(browser()->profile()));
// Navigate to settings page
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL("chrome://settings/")));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(web_contents);
// Check that braveTorDisabledByPolicy is true when disabled by policy
std::string script = R"(
loadTimeData.getBoolean('braveTorDisabledByPolicy');
)";
EXPECT_EQ(true, content::EvalJs(web_contents, script));
}
// Test that Tor settings are visible when enabled by policy
IN_PROC_BROWSER_TEST_F(TorEnabledPolicyBrowserTest,
TorSettingsVisibleWhenEnabledByPolicy) {
// Verify Tor is enabled (not disabled) by policy
EXPECT_FALSE(TorProfileServiceFactory::IsTorDisabled(browser()->profile()));
EXPECT_TRUE(TorProfileServiceFactory::IsTorManaged(browser()->profile()));
// Navigate to settings page
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL("chrome://settings/")));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(web_contents);
// Check that braveTorDisabledByPolicy is false when enabled by policy
std::string script = R"(
loadTimeData.getBoolean('braveTorDisabledByPolicy');
)";
EXPECT_EQ(false, content::EvalJs(web_contents, script));
}
#endif
template <bool enable>
+28
View File
@@ -552,3 +552,31 @@ IN_PROC_BROWSER_TEST_P(BraveTorBrowserTest_EnableTorHttpsOnlyFlag,
INSTANTIATE_TEST_SUITE_P(BraveTorBrowserTest_EnableTorHttpsOnlyFlag,
BraveTorBrowserTest_EnableTorHttpsOnlyFlag,
::testing::Bool());
// Test that Tor settings are visible when Tor is disabled by user preference
// but not by admin policy.
IN_PROC_BROWSER_TEST_F(BraveTorBrowserTest,
TorSettingsVisibleWhenUserDisabled) {
// Disable Tor via user preference (not policy)
TorProfileServiceFactory::SetTorDisabled(true);
// Verify Tor is disabled but NOT managed by policy
EXPECT_TRUE(TorProfileServiceFactory::IsTorDisabled(browser()->profile()));
EXPECT_FALSE(TorProfileServiceFactory::IsTorManaged(browser()->profile()));
// Navigate to settings page
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL("chrome://settings/")));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(web_contents);
// Check that braveTorDisabledByPolicy is false when disabled by user (not
// policy)
std::string script = R"(
loadTimeData.getBoolean('braveTorDisabledByPolicy');
)";
EXPECT_EQ(false, content::EvalJs(web_contents, script));
}
@@ -1225,7 +1225,8 @@ void BraveAddLocalizedStrings(content::WebUIDataSource* html_source,
#if BUILDFLAG(ENABLE_TOR)
html_source->AddBoolean("braveTorDisabledByPolicy",
TorProfileServiceFactory::IsTorDisabled(profile));
TorProfileServiceFactory::IsTorDisabled(profile) &&
TorProfileServiceFactory::IsTorManaged(profile));
#endif
if (base::FeatureList::IsEnabled(