#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/ash/accessibility/magnification_manager.h"
#include "chrome/browser/ash/accessibility/magnifier_type.h"
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_controller_client.h"
#include "chrome/browser/ui/browser.h"
#include "components/prefs/pref_service.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <tuple>
#include "ui/accessibility/accessibility_features.h"
#include "ui/accessibility/platform/ax_platform.h"
#endif
namespace policy {
#if BUILDFLAG(IS_CHROMEOS_ASH)
using ::ash::AccessibilityManager;
using ::ash::MagnificationManager;
using ::ash::MagnifierType;
namespace {
void SetEnableFlag(const keyboard::KeyboardEnableFlag& flag) {
auto* keyboard_client = ChromeKeyboardControllerClient::Get();
keyboard_client->SetEnableFlag(flag);
}
void ClearEnableFlag(const keyboard::KeyboardEnableFlag& flag) {
auto* keyboard_client = ChromeKeyboardControllerClient::Get();
keyboard_client->ClearEnableFlag(flag);
}
}
class AccessibilityPolicyTest : public PolicyTest {};
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, LargeCursorEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
accessibility_manager->EnableLargeCursor(true);
EXPECT_TRUE(accessibility_manager->IsLargeCursorEnabled());
PolicyMap policies;
policies.Set(key::kLargeCursorEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsLargeCursorEnabled());
accessibility_manager->EnableLargeCursor(true);
EXPECT_FALSE(accessibility_manager->IsLargeCursorEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, SpokenFeedbackEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
accessibility_manager->EnableSpokenFeedback(true);
EXPECT_TRUE(accessibility_manager->IsSpokenFeedbackEnabled());
PolicyMap policies;
policies.Set(key::kSpokenFeedbackEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsSpokenFeedbackEnabled());
accessibility_manager->EnableSpokenFeedback(true);
EXPECT_FALSE(accessibility_manager->IsSpokenFeedbackEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, HighContrastEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
accessibility_manager->EnableHighContrast(true);
EXPECT_TRUE(accessibility_manager->IsHighContrastEnabled());
PolicyMap policies;
policies.Set(key::kHighContrastEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsHighContrastEnabled());
accessibility_manager->EnableHighContrast(true);
EXPECT_FALSE(accessibility_manager->IsHighContrastEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, ScreenMagnifierTypeNone) {
MagnificationManager* magnification_manager = MagnificationManager::Get();
magnification_manager->SetMagnifierEnabled(true);
EXPECT_TRUE(magnification_manager->IsMagnifierEnabled());
PolicyMap policies;
policies.Set(key::kScreenMagnifierType, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(0), nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(magnification_manager->IsMagnifierEnabled());
magnification_manager->SetMagnifierEnabled(true);
EXPECT_FALSE(magnification_manager->IsMagnifierEnabled());
magnification_manager->SetDockedMagnifierEnabled(true);
EXPECT_FALSE(magnification_manager->IsDockedMagnifierEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, ScreenMagnifierTypeFull) {
MagnificationManager* magnification_manager = MagnificationManager::Get();
EXPECT_FALSE(magnification_manager->IsMagnifierEnabled());
PolicyMap policies;
policies.Set(key::kScreenMagnifierType, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
base::Value(static_cast<int>(MagnifierType::kFull)), nullptr);
UpdateProviderPolicy(policies);
EXPECT_TRUE(magnification_manager->IsMagnifierEnabled());
magnification_manager->SetMagnifierEnabled(false);
EXPECT_TRUE(magnification_manager->IsMagnifierEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, ScreenMagnifierTypeDocked) {
MagnificationManager* magnification_manager = MagnificationManager::Get();
EXPECT_FALSE(magnification_manager->IsDockedMagnifierEnabled());
PolicyMap policies;
policies.Set(key::kScreenMagnifierType, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
base::Value(static_cast<int>(MagnifierType::kDocked)), nullptr);
UpdateProviderPolicy(policies);
EXPECT_TRUE(magnification_manager->IsDockedMagnifierEnabled());
magnification_manager->SetDockedMagnifierEnabled(false);
EXPECT_TRUE(magnification_manager->IsDockedMagnifierEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest,
AccessibilityVirtualKeyboardEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
accessibility_manager->EnableVirtualKeyboard(true);
EXPECT_TRUE(accessibility_manager->IsVirtualKeyboardEnabled());
PolicyMap policies;
policies.Set(key::kVirtualKeyboardEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsVirtualKeyboardEnabled());
accessibility_manager->EnableVirtualKeyboard(true);
EXPECT_FALSE(accessibility_manager->IsVirtualKeyboardEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, StickyKeysEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
EXPECT_FALSE(accessibility_manager->IsStickyKeysEnabled());
accessibility_manager->EnableStickyKeys(true);
EXPECT_TRUE(accessibility_manager->IsStickyKeysEnabled());
PolicyMap policies;
policies.Set(key::kStickyKeysEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsStickyKeysEnabled());
accessibility_manager->EnableStickyKeys(true);
EXPECT_FALSE(accessibility_manager->IsStickyKeysEnabled());
}
class AccessibilityPolicyTouchVirtualKeyboardEnabledTest
: public AccessibilityPolicyTest,
public testing::WithParamInterface<bool> {
public:
AccessibilityPolicyTouchVirtualKeyboardEnabledTest() {
feature_list_.InitWithFeatureState(
ash::features::kTouchVirtualKeyboardPolicyListenPrefsAtLogin,
GetParam());
}
private:
base::test::ScopedFeatureList feature_list_;
};
INSTANTIATE_TEST_SUITE_P(,
AccessibilityPolicyTouchVirtualKeyboardEnabledTest,
::testing::Values(true, false));
IN_PROC_BROWSER_TEST_P(AccessibilityPolicyTouchVirtualKeyboardEnabledTest,
TouchVirtualKeyboardEnabledDefault) {
auto* keyboard_client = ChromeKeyboardControllerClient::Get();
ASSERT_TRUE(keyboard_client);
EXPECT_FALSE(keyboard_client->is_keyboard_enabled());
SetEnableFlag(keyboard::KeyboardEnableFlag::kTouchEnabled);
EXPECT_TRUE(keyboard_client->is_keyboard_enabled());
ClearEnableFlag(keyboard::KeyboardEnableFlag::kTouchEnabled);
EXPECT_FALSE(keyboard_client->is_keyboard_enabled());
}
IN_PROC_BROWSER_TEST_P(AccessibilityPolicyTouchVirtualKeyboardEnabledTest,
TouchVirtualKeyboardEnabledTrueEnablesVirtualKeyboard) {
auto* keyboard_client = ChromeKeyboardControllerClient::Get();
ASSERT_TRUE(keyboard_client);
PolicyMap policies;
policies.Set(key::kTouchVirtualKeyboardEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_TRUE(keyboard_client->is_keyboard_enabled());
ClearEnableFlag(keyboard::KeyboardEnableFlag::kTouchEnabled);
EXPECT_TRUE(keyboard_client->is_keyboard_enabled());
}
IN_PROC_BROWSER_TEST_P(
AccessibilityPolicyTouchVirtualKeyboardEnabledTest,
TouchVirtualKeyboardEnabledFalseDisablesVirtualKeyboard) {
auto* keyboard_client = ChromeKeyboardControllerClient::Get();
ASSERT_TRUE(keyboard_client);
PolicyMap policies;
policies.Set(key::kTouchVirtualKeyboardEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(keyboard_client->is_keyboard_enabled());
SetEnableFlag(keyboard::KeyboardEnableFlag::kTouchEnabled);
EXPECT_FALSE(keyboard_client->is_keyboard_enabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, SelectToSpeakEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
EXPECT_FALSE(accessibility_manager->IsSelectToSpeakEnabled());
accessibility_manager->SetSelectToSpeakEnabled(true);
EXPECT_TRUE(accessibility_manager->IsSelectToSpeakEnabled());
PolicyMap policies;
policies.Set(key::kSelectToSpeakEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsSelectToSpeakEnabled());
accessibility_manager->SetSelectToSpeakEnabled(true);
EXPECT_FALSE(accessibility_manager->IsSelectToSpeakEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, DictationEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
PrefService* prefs = browser()->profile()->GetPrefs();
EXPECT_FALSE(accessibility_manager->IsDictationEnabled());
prefs->SetBoolean(ash::prefs::kAccessibilityDictationEnabled, true);
EXPECT_TRUE(accessibility_manager->IsDictationEnabled());
PolicyMap policies;
policies.Set(key::kDictationEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsDictationEnabled());
prefs->SetBoolean(ash::prefs::kAccessibilityDictationEnabled, true);
EXPECT_FALSE(accessibility_manager->IsDictationEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, KeyboardFocusHighlightEnabled) {
AccessibilityManager* const accessibility_manager =
AccessibilityManager::Get();
EXPECT_FALSE(accessibility_manager->IsFocusHighlightEnabled());
accessibility_manager->SetFocusHighlightEnabled(true);
EXPECT_TRUE(accessibility_manager->IsFocusHighlightEnabled());
PolicyMap policies;
policies.Set(key::kKeyboardFocusHighlightEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsFocusHighlightEnabled());
accessibility_manager->SetFocusHighlightEnabled(true);
EXPECT_FALSE(accessibility_manager->IsFocusHighlightEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, CursorHighlightEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
EXPECT_FALSE(accessibility_manager->IsCursorHighlightEnabled());
accessibility_manager->SetCursorHighlightEnabled(true);
EXPECT_TRUE(accessibility_manager->IsCursorHighlightEnabled());
PolicyMap policies;
policies.Set(key::kCursorHighlightEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsCursorHighlightEnabled());
accessibility_manager->SetCursorHighlightEnabled(true);
EXPECT_FALSE(accessibility_manager->IsCursorHighlightEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, CaretHighlightEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
EXPECT_FALSE(accessibility_manager->IsCaretHighlightEnabled());
accessibility_manager->SetCaretHighlightEnabled(true);
EXPECT_TRUE(accessibility_manager->IsCaretHighlightEnabled());
PolicyMap policies;
policies.Set(key::kCaretHighlightEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsCaretHighlightEnabled());
accessibility_manager->SetCaretHighlightEnabled(true);
EXPECT_FALSE(accessibility_manager->IsCaretHighlightEnabled());
policies.Set(key::kCaretHighlightEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_TRUE(accessibility_manager->IsCaretHighlightEnabled());
accessibility_manager->SetCaretHighlightEnabled(false);
EXPECT_TRUE(accessibility_manager->IsCaretHighlightEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, MonoAudioEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
accessibility_manager->EnableMonoAudio(false);
EXPECT_FALSE(accessibility_manager->IsMonoAudioEnabled());
accessibility_manager->EnableMonoAudio(true);
EXPECT_TRUE(accessibility_manager->IsMonoAudioEnabled());
PolicyMap policies;
policies.Set(key::kMonoAudioEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsMonoAudioEnabled());
accessibility_manager->EnableMonoAudio(true);
EXPECT_FALSE(accessibility_manager->IsMonoAudioEnabled());
policies.Set(key::kMonoAudioEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_TRUE(accessibility_manager->IsMonoAudioEnabled());
accessibility_manager->EnableMonoAudio(false);
EXPECT_TRUE(accessibility_manager->IsMonoAudioEnabled());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, AutoclickEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
accessibility_manager->EnableAutoclick(false);
EXPECT_FALSE(accessibility_manager->IsAutoclickEnabled());
accessibility_manager->EnableAutoclick(true);
EXPECT_TRUE(accessibility_manager->IsAutoclickEnabled());
PolicyMap policies;
policies.Set(key::kAutoclickEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsAutoclickEnabled());
accessibility_manager->EnableAutoclick(true);
EXPECT_FALSE(accessibility_manager->IsAutoclickEnabled());
policies.Set(key::kAutoclickEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_TRUE(accessibility_manager->IsAutoclickEnabled());
accessibility_manager->EnableAutoclick(false);
EXPECT_TRUE(accessibility_manager->IsAutoclickEnabled());
EXPECT_FALSE(accessibility_manager->IsDisableAutoclickDialogVisibleForTest());
}
IN_PROC_BROWSER_TEST_F(AccessibilityPolicyTest, ColorCorrectionEnabled) {
AccessibilityManager* accessibility_manager = AccessibilityManager::Get();
accessibility_manager->SetColorCorrectionEnabled(false);
EXPECT_FALSE(accessibility_manager->IsColorCorrectionEnabled());
accessibility_manager->SetColorCorrectionEnabled(true);
EXPECT_TRUE(accessibility_manager->IsColorCorrectionEnabled());
PolicyMap policies;
policies.Set(key::kColorCorrectionEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsColorCorrectionEnabled());
accessibility_manager->SetColorCorrectionEnabled(true);
EXPECT_FALSE(accessibility_manager->IsColorCorrectionEnabled());
policies.Set(key::kColorCorrectionEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
nullptr);
UpdateProviderPolicy(policies);
EXPECT_TRUE(accessibility_manager->IsColorCorrectionEnabled());
accessibility_manager->SetColorCorrectionEnabled(false);
EXPECT_TRUE(accessibility_manager->IsColorCorrectionEnabled());
}
#endif
#if BUILDFLAG(IS_WIN)
class UiAutomationProviderPolicyTest
: public PolicyTest,
public ::testing::WithParamInterface<
std::tuple<PolicyTest::BooleanPolicy, bool>> {
protected:
static PolicyTest::BooleanPolicy GetBooleanPolicyParam() {
return std::get<0>(GetParam());
}
static bool GetFeatureEnabledParam() { return std::get<1>(GetParam()); }
UiAutomationProviderPolicyTest() {
feature_list_.InitWithFeatureState(::features::kUiaProvider,
GetFeatureEnabledParam());
}
void SetUpInProcessBrowserTestFixture() override {
PolicyTest::SetUpInProcessBrowserTestFixture();
if (const auto boolean_policy = GetBooleanPolicyParam();
boolean_policy != BooleanPolicy::kNotConfigured) {
PolicyMap policy_map;
SetPolicy(&policy_map, key::kUiAutomationProviderEnabled,
base::Value(boolean_policy == BooleanPolicy::kTrue));
UpdateProviderPolicy(policy_map);
}
}
private:
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_P(UiAutomationProviderPolicyTest, IsUiaProviderEnabled) {
if (const auto boolean_policy = GetBooleanPolicyParam();
boolean_policy == BooleanPolicy::kNotConfigured) {
ASSERT_EQ(::ui::AXPlatform::GetInstance().IsUiaProviderEnabled(),
GetFeatureEnabledParam());
} else {
ASSERT_EQ(::ui::AXPlatform::GetInstance().IsUiaProviderEnabled(),
boolean_policy == BooleanPolicy::kTrue);
}
}
INSTANTIATE_TEST_SUITE_P(
All,
UiAutomationProviderPolicyTest,
::testing::Combine(
::testing::Values(PolicyTest::BooleanPolicy::kNotConfigured,
PolicyTest::BooleanPolicy::kFalse,
PolicyTest::BooleanPolicy::kTrue),
::testing::Bool()));
#endif
}