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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <string>
#include <vector>
#include "ash/constants/ash_pref_names.h"
#include "base/json/json_writer.h"
#include "base/values.h"
#include "chrome/browser/ash/accessibility/accessibility_test_utils.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "extensions/test/result_catcher.h"
// API tests for chrome.accessibilityFeatures API.
// Note that the API is implemented using preference API infrastructure.
// See preference_api.cc for the list of accessibility features exposed by the
// API and the related preferences.
namespace extensions {
namespace {
// Keys for data in the test config argument that will be set for the test app
// to use.
// The test that the app should run.
const char kTestNameKey[] = "testName";
// Key for list of features enabled when the test is initialized.
const char kEnabledFeaturesKey[] = "enabled";
// Key for list of features disabled when the test is initialized.
const char kDisabledFeaturesKey[] = "disabled";
// A test extension path. The extension has only |accessibilityFeatures.read|
// permission.
const char kTestExtensionPathReadPermission[] =
"accessibility_features/read_permission/";
// A test extension path. The extension has only |accessibilityFeatures.read|
// permission and has manifest v3.
const char kTestExtensionPathReadPermissionV3[] =
"accessibility_features/mv3/read_permission/";
// A test extension path. The extension has only |accessibilityFeatures.modify|
// permission.
const char kTestExtensionPathModifyPermission[] =
"accessibility_features/modify_permission/";
// A test extension path. The extension has only |accessibilityFeatures.modify|
// permission and has manifest v3.
const char kTestExtensionPathModifyPermissionV3[] =
"accessibility_features/mv3/modify_permission/";
using ManifestVersion = ash::ManifestVersion;
enum class Permission { kWriteOnly, kReadOnly };
// A class used to define the parameters of a test case.
struct TestConfig {
Permission permission;
ManifestVersion version;
};
// Accessibility features API test.
// Tests are parameterized by the permission (write-only or read-only), as well
// as the manifest version (v2 or v3).
class AccessibilityFeaturesApiTest
: public ExtensionApiTest,
public testing::WithParamInterface<TestConfig> {
public:
AccessibilityFeaturesApiTest() = default;
virtual ~AccessibilityFeaturesApiTest() = default;
protected:
// Returns pref service to be used to initialize and later verify
// accessibility preference values.
PrefService* GetPrefs() { return browser()->profile()->GetPrefs(); }
// Returns the path of the extension that should be used in a parameterized
// test.
const char* GetTestExtensionPath() const {
Permission permission = GetParam().permission;
ManifestVersion version = GetParam().version;
if (version == ManifestVersion::kTwo &&
permission == Permission::kWriteOnly) {
return kTestExtensionPathModifyPermission;
} else if (version == ManifestVersion::kTwo &&
permission == Permission::kReadOnly) {
return kTestExtensionPathReadPermission;
} else if (version == ManifestVersion::kThree &&
permission == Permission::kWriteOnly) {
return kTestExtensionPathModifyPermissionV3;
} else if (version == ManifestVersion::kThree &&
permission == Permission::kReadOnly) {
return kTestExtensionPathReadPermissionV3;
}
NOTREACHED();
}
// Whether a parameterized test should have been able to modify accessibility
// preferences (i.e. whether the test extension had modify permission).
bool ShouldModifyingFeatureSucceed() const {
return GetParam().permission == Permission::kWriteOnly;
}
// Returns preference path for accessibility features as defined by the API.
const char* GetPrefForFeature(const std::string& feature) {
if (feature == "spokenFeedback")
return ash::prefs::kAccessibilitySpokenFeedbackEnabled;
if (feature == "largeCursor")
return ash::prefs::kAccessibilityLargeCursorEnabled;
if (feature == "stickyKeys")
return ash::prefs::kAccessibilityStickyKeysEnabled;
if (feature == "highContrast")
return ash::prefs::kAccessibilityHighContrastEnabled;
if (feature == "screenMagnifier")
return ash::prefs::kAccessibilityScreenMagnifierEnabled;
if (feature == "autoclick")
return ash::prefs::kAccessibilityAutoclickEnabled;
if (feature == "virtualKeyboard")
return ash::prefs::kAccessibilityVirtualKeyboardEnabled;
if (feature == "caretHighlight")
return ash::prefs::kAccessibilityCaretHighlightEnabled;
if (feature == "cursorHighlight")
return ash::prefs::kAccessibilityCursorHighlightEnabled;
if (feature == "focusHighlight")
return ash::prefs::kAccessibilityFocusHighlightEnabled;
if (feature == "selectToSpeak")
return ash::prefs::kAccessibilitySelectToSpeakEnabled;
if (feature == "switchAccess")
return ash::prefs::kAccessibilitySwitchAccessEnabled;
if (feature == "cursorColor")
return ash::prefs::kAccessibilityCursorColorEnabled;
if (feature == "dockedMagnifier")
return ash::prefs::kDockedMagnifierEnabled;
if (feature == "dictation")
return ash::prefs::kAccessibilityDictationEnabled;
return nullptr;
}
// Initializes preferences before running the test extension.
// |prefs| Pref service which should be initialized.
// |enabled_features| List of boolean preference whose value should be set to
// true.
// |disabled_features| List of boolean preferences whose value should be set
// to false.
bool InitPrefServiceForTest(
PrefService* prefs,
const std::vector<std::string>& enabled_features,
const std::vector<std::string>& disabled_features) {
for (const auto& feature : enabled_features) {
const char* const pref_name = GetPrefForFeature(feature);
EXPECT_TRUE(pref_name) << "Invalid feature " << feature;
if (!pref_name)
return false;
prefs->SetBoolean(pref_name, true);
}
for (const auto& feature : disabled_features) {
const char* const pref_name = GetPrefForFeature(feature);
EXPECT_TRUE(pref_name) << "Invalid feature " << feature;
if (!pref_name)
return false;
prefs->SetBoolean(pref_name, false);
}
return true;
}
// Verifies that preferences have the expected value.
// |prefs| The pref service to be verified.
// |enabled_features| The list of boolean preferences whose value should be
// true.
// |disabled_features| The list of boolean preferences whose value should be
// false.
void VerifyPrefServiceState(
PrefService* prefs,
const std::vector<std::string>& enabled_features,
const std::vector<std::string>& disabled_features) {
for (const auto& feature : enabled_features) {
const char* const pref_name = GetPrefForFeature(feature);
ASSERT_TRUE(pref_name) << "Invalid feature " << feature;
ASSERT_TRUE(prefs->GetBoolean(pref_name));
}
for (const auto& feature : disabled_features) {
const char* const pref_name = GetPrefForFeature(feature);
ASSERT_TRUE(pref_name) << "Invalid feature " << feature;
ASSERT_FALSE(prefs->GetBoolean(pref_name));
}
}
// Given the test name and list of enabled and disabled features, generates
// and sets the JSON string that should be given to the test extension as
// test configuration.
// The result is saved to |result|. The return value is whether the test
// argument was successfully generated.
bool GenerateTestArg(const std::string& test_name,
const std::vector<std::string>& enabled_features,
const std::vector<std::string>& disabled_features,
std::string* result) {
base::Value::Dict test_arg;
test_arg.Set(kTestNameKey, test_name);
base::Value::List enabled_list;
for (const auto& feature : enabled_features)
enabled_list.Append(feature);
test_arg.Set(kEnabledFeaturesKey, std::move(enabled_list));
base::Value::List disabled_list;
for (const auto& feature : disabled_features)
disabled_list.Append(feature);
test_arg.Set(kDisabledFeaturesKey, std::move(disabled_list));
return base::JSONWriter::Write(test_arg, result);
}
};
INSTANTIATE_TEST_SUITE_P(AccessibilityFeaturesApiTestWritePermission,
AccessibilityFeaturesApiTest,
::testing::Values(TestConfig{Permission::kWriteOnly,
ManifestVersion::kTwo}));
INSTANTIATE_TEST_SUITE_P(AccessibilityFeaturesApiTestReadPermission,
AccessibilityFeaturesApiTest,
::testing::Values(TestConfig{Permission::kReadOnly,
ManifestVersion::kTwo}));
INSTANTIATE_TEST_SUITE_P(AccessibilityFeaturesApiTestWritePermissionV3,
AccessibilityFeaturesApiTest,
::testing::Values(TestConfig{
Permission::kWriteOnly, ManifestVersion::kThree}));
INSTANTIATE_TEST_SUITE_P(AccessibilityFeaturesApiTestReadPermissionV3,
AccessibilityFeaturesApiTest,
::testing::Values(TestConfig{
Permission::kReadOnly, ManifestVersion::kThree}));
// Tests that an extension with read permission can read accessibility features
// state, while an extension that doesn't have the permission cannot.
IN_PROC_BROWSER_TEST_P(AccessibilityFeaturesApiTest, Get) {
// WARNING: Make sure that features which load Chrome extension are not among
// enabled_features (see |Set| test for the reason).
std::vector<std::string> enabled_features = {
"cursorColor", "cursorHighlight", "highContrast",
"largeCursor", "stickyKeys",
};
std::vector<std::string> disabled_features = {
"autoclick",
"caretHighlight",
"dockedMagnifier",
"focusHighlight",
"screenMagnifier",
"selectToSpeak",
"spokenFeedback",
"switchAccess",
"virtualKeyboard",
};
ASSERT_TRUE(
InitPrefServiceForTest(GetPrefs(), enabled_features, disabled_features));
std::string test_arg;
ASSERT_TRUE(GenerateTestArg("getterTest", enabled_features, disabled_features,
&test_arg));
bool is_mv2 = GetParam().version == ManifestVersion::kTwo;
EXPECT_TRUE(RunExtensionTest(
GetTestExtensionPath(),
{.custom_arg = test_arg.c_str(), .launch_as_platform_app = is_mv2}))
<< message_;
}
IN_PROC_BROWSER_TEST_P(AccessibilityFeaturesApiTest, PRE_Get_ComponentApp) {
bool is_mv2 = GetParam().version == ManifestVersion::kTwo;
EXPECT_FALSE(
RunExtensionTest(GetTestExtensionPath(),
{.custom_arg = "{}", .launch_as_platform_app = is_mv2},
{.load_as_component = is_mv2}))
<< message_;
}
// A regression test for https://crbug.com/454513. Ensure that loading a
// component extension with the same version as has previously loaded, correctly
// sets up access to accessibility prefs. Otherwise,this is the same as the
// |Get| test.
IN_PROC_BROWSER_TEST_P(AccessibilityFeaturesApiTest, Get_ComponentApp) {
// WARNING: Make sure that features which load Chrome extension are not among
// enabled_features (see |Set| test for the reason).
std::vector<std::string> enabled_features = {
"cursorHighlight",
"dockedMagnifier",
"highContrast",
"largeCursor",
"stickyKeys",
};
std::vector<std::string> disabled_features = {
"autoclick", "caretHighlight", "cursorColor",
"focusHighlight", "screenMagnifier", "selectToSpeak",
"spokenFeedback", "switchAccess", "virtualKeyboard",
};
ASSERT_TRUE(
InitPrefServiceForTest(GetPrefs(), enabled_features, disabled_features));
std::string test_arg;
ASSERT_TRUE(GenerateTestArg("getterTest", enabled_features, disabled_features,
&test_arg));
bool is_mv2 = GetParam().version == ManifestVersion::kTwo;
EXPECT_TRUE(RunExtensionTest(
GetTestExtensionPath(),
{.custom_arg = test_arg.c_str(), .launch_as_platform_app = is_mv2},
{.load_as_component = is_mv2}))
<< message_;
}
// Tests that an extension with modify permission can modify accessibility
// features, while an extension that doesn't have the permission can't.
IN_PROC_BROWSER_TEST_P(AccessibilityFeaturesApiTest, Set) {
// WARNING: Make sure that features which load Chrome extension are not
// enabled at this point (before the test app is loaded), as that may break
// the test:
// |RunPlatformAppTestWithArg| waits for the test extension to load by
// waiting for EXTENSION_LOADED notification to be observed. It also assumes
// that there is only one extension being loaded during this time (it finishes
// when the first notification is seen). Enabling spoken feedback, select to
// speak, autoclick, or switch access here would break this assumption as it
// would induce loading of Chrome extension.
std::vector<std::string> enabled_features = {
"caretHighlight",
"cursorColor",
"focusHighlight",
"stickyKeys",
};
std::vector<std::string> disabled_features = {
"autoclick",
"cursorHighlight",
"dockedMagnifier",
"highContrast",
"largeCursor",
"screenMagnifier",
"selectToSpeak",
"spokenFeedback",
"switchAccess",
"virtualKeyboard",
};
ASSERT_TRUE(
InitPrefServiceForTest(GetPrefs(), enabled_features, disabled_features));
std::string test_arg;
ASSERT_TRUE(GenerateTestArg("setterTest", enabled_features, disabled_features,
&test_arg));
bool is_mv2 = GetParam().version == ManifestVersion::kTwo;
// The test extension attempts to flip all feature values.
ASSERT_TRUE(RunExtensionTest(
GetTestExtensionPath(),
{.custom_arg = test_arg.c_str(), .launch_as_platform_app = is_mv2}))
<< message_;
// The test tries to flip the feature states.
if (ShouldModifyingFeatureSucceed()) {
VerifyPrefServiceState(GetPrefs(), disabled_features, enabled_features);
} else {
VerifyPrefServiceState(GetPrefs(), enabled_features, disabled_features);
}
}
// Tests that an extension with read permission is notified when accessibility
// features change.
IN_PROC_BROWSER_TEST_P(AccessibilityFeaturesApiTest, ObserveFeatures) {
// WARNING: Make sure that features which load Chrome extension are not among
// enabled_features (see |Set| test for the reason).
std::vector<std::string> enabled_features = {
"caretHighlight",
"cursorColor",
"focusHighlight",
"stickyKeys",
};
std::vector<std::string> disabled_features = {
"autoclick",
"cursorHighlight",
"dockedMagnifier",
"highContrast",
"largeCursor",
"screenMagnifier",
"selectToSpeak",
"spokenFeedback",
"switchAccess",
"virtualKeyboard",
};
ASSERT_TRUE(
InitPrefServiceForTest(GetPrefs(), enabled_features, disabled_features));
std::string test_arg;
ASSERT_TRUE(GenerateTestArg("observerTest", enabled_features,
disabled_features, &test_arg));
// The test extension is supposed to report result twice when running this
// test. First time when in initializes it's feature listeners, and second
// time, when gets all expected events. This is done so the extension is
// running when the accessibility features are flipped; otherwise, the
// extension may not see events.
bool is_mv2 = GetParam().version == ManifestVersion::kTwo;
const char* extension_path = is_mv2 ? kTestExtensionPathReadPermission
: kTestExtensionPathReadPermissionV3;
ASSERT_TRUE(RunExtensionTest(
extension_path,
{.custom_arg = test_arg.c_str(), .launch_as_platform_app = is_mv2}))
<< message_;
// This should flip all features.
ASSERT_TRUE(
InitPrefServiceForTest(GetPrefs(), disabled_features, enabled_features));
// Catch the second result notification sent by the test extension.
ResultCatcher result_catcher;
ASSERT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
}
} // namespace
} // namespace extensions
|