1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Copyright 2014 The Chromium Authors. All rights reserved.
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Use of this source code is governed by a BSD-style license that can be
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// found in the LICENSE file.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <string>
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <vector>
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "base/json/json_writer.h"
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "base/prefs/pref_service.h"
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "base/strings/stringprintf.h"
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "base/values.h"
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "chrome/browser/extensions/extension_apitest.h"
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "chrome/common/pref_names.h"
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "extensions/test/result_catcher.h"
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// API tests for chrome.accessibilityFeatures API.
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Note that the API is implemented using preference API infrastructure.
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// See preference_api.cc for the list of accessibility features exposed by the
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// API and the related preferences.
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace extensions {
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace {
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Keys for data in the test config argument that will be set for the test app
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// to use.
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The test that the app should run.
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kTestNameKey[] = "testName";
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Key for list of features enabled when the test is initialized.
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kEnabledFeaturesKey[] = "enabled";
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Key for list fo features disabled when the test is initialized.
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kDisabledFeaturesKey[] = "disabled";
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A test extension path. The extension has only |accessibilityFeatures.read|
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// permission.
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kTestExtensionPathReadPermission[] =
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    "accessibility_features/read_permission/";
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A test extension path. The extension has only |accessibilityFeatures.modify|
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// permission.
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kTestExtensionPathMofifyPermission[] =
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    "accessibility_features/modify_permission/";
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Accessibility features API test.
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Tests are parameterized by whether the test extension is write-only (the
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// parameter value is true) or read-only (the parameter value is false).
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass AccessibilityFeaturesApiTest : public ExtensionApiTest,
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                     public testing::WithParamInterface<bool> {
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org public:
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  AccessibilityFeaturesApiTest() {}
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  virtual ~AccessibilityFeaturesApiTest() {}
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org protected:
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // Returns pref service to be used to initialize and later verify
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // accessibility preference values.
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  PrefService* GetPrefs() { return browser()->profile()->GetPrefs(); }
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // Returns the path of the extension that should be used in a parameterized
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // test.
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  std::string GetTestExtensionPath() const {
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    if (GetParam())
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return kTestExtensionPathMofifyPermission;
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    return kTestExtensionPathReadPermission;
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  }
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // Whether a parameterized test should have been able to modify accessibility
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // preferences (i.e. whether the test extension had modify permission).
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  bool ShouldModifyingFeatureSucceed() const { return GetParam(); }
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // Returns preference path for accessibility features as defined by the API.
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  const char* const GetPrefForFeature(const std::string& feature) {
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    if (feature == "spokenFeedback")
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return prefs::kAccessibilitySpokenFeedbackEnabled;
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    if (feature == "largeCursor")
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return prefs::kAccessibilityLargeCursorEnabled;
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    if (feature == "stickyKeys")
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return prefs::kAccessibilityStickyKeysEnabled;
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    if (feature == "highContrast")
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return prefs::kAccessibilityHighContrastEnabled;
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    if (feature == "screenMagnifier")
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return prefs::kAccessibilityScreenMagnifierEnabled;
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    if (feature == "autoclick")
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return prefs::kAccessibilityAutoclickEnabled;
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    if (feature == "virtualKeyboard")
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return prefs::kAccessibilityVirtualKeyboardEnabled;
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    return NULL;
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  }
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // Initializes preferences before running the test extension.
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // |prefs| Pref service which should be initializzed.
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // |enabled_features| List of boolean preference whose value should be set to
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  //     true.
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // |disabled_features| List of boolean preferences whose value should be set
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  //     to false.
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  bool InitPrefServiceForTest(
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      PrefService* prefs,
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const std::vector<std::string>& enabled_features,
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const std::vector<std::string>& disabled_features) {
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    for (size_t i = 0; i < enabled_features.size(); ++i) {
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const char* const pref_name = GetPrefForFeature(enabled_features[i]);
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      EXPECT_TRUE(pref_name) << "Invalid feature " << enabled_features[i];
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!pref_name)
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        return false;
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      prefs->SetBoolean(pref_name, true);
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    }
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    for (size_t i = 0; i < disabled_features.size(); ++i) {
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const char* const pref_name = GetPrefForFeature(disabled_features[i]);
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      EXPECT_TRUE(pref_name) << "Invalid feature " << disabled_features[i];
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!pref_name)
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        return false;
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      prefs->SetBoolean(pref_name, false);
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    }
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    return true;
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  }
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // Verifies that preferences have the expected value.
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // |prefs| The pref service to be verified.
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // |enabled_features| The list of boolean preferences whose value should be
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  //     true.
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // |disabled_features| The list of boolean preferences whose value should be
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  //     false.
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  void VerifyPrefServiceState(
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      PrefService* prefs,
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const std::vector<std::string>& enabled_features,
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const std::vector<std::string>& disabled_features) {
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    for (size_t i = 0; i < enabled_features.size(); ++i) {
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const char* const pref_name = GetPrefForFeature(enabled_features[i]);
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ASSERT_TRUE(pref_name) << "Invalid feature " << enabled_features[i];
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ASSERT_TRUE(prefs->GetBoolean(pref_name));
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    }
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    for (size_t i = 0; i < disabled_features.size(); ++i) {
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const char* const pref_name = GetPrefForFeature(disabled_features[i]);
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ASSERT_TRUE(pref_name) << "Invalid feature " << disabled_features[i];
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ASSERT_FALSE(prefs->GetBoolean(pref_name));
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    }
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  }
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // Given the test name and list of enabled and disabled features, generates
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // and sets the JSON string that should be given to the test extension as
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // test configuration.
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // The result is saved to |result|. The return value is whether the test
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // argument was successfully generated.
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  bool GenerateTestArg(const std::string& test_name,
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       const std::vector<std::string>& enabled_features,
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       const std::vector<std::string>& disabled_features,
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       std::string* result) {
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    base::DictionaryValue test_arg;
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    test_arg.SetString(kTestNameKey, test_name);
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    scoped_ptr<base::ListValue> enabled_list(new base::ListValue);
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    for (size_t i = 0; i < enabled_features.size(); ++i)
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      enabled_list->AppendString(enabled_features[i]);
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    test_arg.Set(kEnabledFeaturesKey, enabled_list.release());
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    scoped_ptr<base::ListValue> disabled_list(new base::ListValue);
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    for (size_t i = 0; i < disabled_features.size(); ++i)
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      disabled_list->AppendString(disabled_features[i]);
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    test_arg.Set(kDisabledFeaturesKey, disabled_list.release());
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    return base::JSONWriter::Write(&test_arg, result);
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  }
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgINSTANTIATE_TEST_CASE_P(AccessibilityFeatureaApiTestInstantiatePermission,
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        AccessibilityFeaturesApiTest,
167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        testing::Bool());
168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Tests that an extension with read permission can read accessibility features
170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// state, while an extension that doesn't have the permission cannot.
171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgIN_PROC_BROWSER_TEST_P(AccessibilityFeaturesApiTest, Get) {
172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // WARNING: Make sure that spoken feedback is not among enabled_features
173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // (see |Set| test for the reason).
174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  std::vector<std::string> enabled_features;
175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  enabled_features.push_back("largeCursor");
176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  enabled_features.push_back("stickyKeys");
177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  enabled_features.push_back("highContrast");
178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  std::vector<std::string> disabled_features;
180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  disabled_features.push_back("spokenFeedback");
181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  disabled_features.push_back("screenMagnifier");
182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  disabled_features.push_back("autoclick");
183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  disabled_features.push_back("virtualKeyboard");
184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ASSERT_TRUE(
186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      InitPrefServiceForTest(GetPrefs(), enabled_features, disabled_features));
187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  std::string test_arg;
189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ASSERT_TRUE(GenerateTestArg(
190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      "getterTest", enabled_features, disabled_features, &test_arg));
191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  EXPECT_TRUE(
192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      RunPlatformAppTestWithArg(GetTestExtensionPath(), test_arg.c_str()))
193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      << message_;
194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Tests that an extension with modify permission can modify accessibility
197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// features, while an extension that doesn't have the permission can't.
198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgIN_PROC_BROWSER_TEST_P(AccessibilityFeaturesApiTest, Set) {
199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // WARNING: Make sure that spoken feedback does not get enabled at this point
200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // (before the test app is loaded), as that may break the test:
201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // |RunPlatformAppTestWithArg| waits for the test extension to load by
202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // waiting for EXTENSION_LOADED notification to be observed. It also assumes
203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // that there is only one extension being loaded during this time (it finishes
204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // when the first notification is seen). Enabling spoken feedback here would
205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // break this assumption as it would induce loading of ChromeVox extension.
206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  std::vector<std::string> enabled_features;
207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  enabled_features.push_back("stickyKeys");
208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  enabled_features.push_back("autoclick");
209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  enabled_features.push_back("virtualKeyboard");
210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  std::vector<std::string> disabled_features;
212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  disabled_features.push_back("spokenFeedback");
213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  disabled_features.push_back("largeCursor");
214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  disabled_features.push_back("highContrast");
215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  disabled_features.push_back("screenMagnifier");
216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ASSERT_TRUE(
218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      InitPrefServiceForTest(GetPrefs(), enabled_features, disabled_features));
219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  std::string test_arg;
221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ASSERT_TRUE(GenerateTestArg(
222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      "setterTest", enabled_features, disabled_features, &test_arg));
223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // The test extension attempts to flip all feature values.
225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ASSERT_TRUE(
226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      RunPlatformAppTestWithArg(GetTestExtensionPath(), test_arg.c_str()))
227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      << message_;
228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // The test tries to flip the feature states.
230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  if (ShouldModifyingFeatureSucceed()) {
231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    VerifyPrefServiceState(GetPrefs(), disabled_features, enabled_features);
232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  } else {
233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    VerifyPrefServiceState(GetPrefs(), enabled_features, disabled_features);
234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  }
235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Tests that an extension with read permission is notified when accessibility
238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// features change.
239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgIN_PROC_BROWSER_TEST_F(AccessibilityFeaturesApiTest, ObserveFeatures) {
240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // WARNING: Make sure that spoken feedback is not among enabled_features
241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // (see |Set| test for the reason).
242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  std::vector<std::string> enabled_features;
243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  enabled_features.push_back("largeCursor");
244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  enabled_features.push_back("stickyKeys");
245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  enabled_features.push_back("highContrast");
246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  std::vector<std::string> disabled_features;
248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  disabled_features.push_back("screenMagnifier");
249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ASSERT_TRUE(
251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      InitPrefServiceForTest(GetPrefs(), enabled_features, disabled_features));
252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  std::string test_arg;
254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ASSERT_TRUE(GenerateTestArg(
255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      "observerTest", enabled_features, disabled_features, &test_arg));
256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // The test extension is supposed to report result twice when runnign this
258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // test. First time when in initializes it's feature listeners, and second
259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // time, when gets all expected events. This is done so the extension is
260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // running when the accessibility features are flipped; oterwise, the
261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // extension may not see events.
262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ASSERT_TRUE(RunPlatformAppTestWithArg(kTestExtensionPathReadPermission,
263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                        test_arg.c_str()))
264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      << message_;
265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // This should flip all features.
267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ASSERT_TRUE(
268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      InitPrefServiceForTest(GetPrefs(), disabled_features, enabled_features));
269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  // Catch the second result notification sent by the test extension.
271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ResultCatcher result_catcher;
272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  ASSERT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}  // namespace
276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}  // namespace extensions
278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org