This source file includes following definitions.
- GetPrefs
- GetTestExtensionPath
- ShouldModifyingFeatureSucceed
- GetPrefForFeature
- InitPrefServiceForTest
- VerifyPrefServiceState
- GenerateTestArg
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_F
#include <string>
#include <vector>
#include "base/json/json_writer.h"
#include "base/prefs/pref_service.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/common/pref_names.h"
namespace extensions {
namespace {
const char kTestNameKey[] = "testName";
const char kEnabledFeaturesKey[] = "enabled";
const char kDisabledFeaturesKey[] = "disabled";
const char kTestExtensionPathReadPermission[] =
"accessibility_features/read_permission/";
const char kTestExtensionPathMofifyPermission[] =
"accessibility_features/modify_permission/";
class AccessibilityFeaturesApiTest : public ExtensionApiTest,
public testing::WithParamInterface<bool> {
public:
AccessibilityFeaturesApiTest() {}
virtual ~AccessibilityFeaturesApiTest() {}
protected:
PrefService* GetPrefs() { return browser()->profile()->GetPrefs(); }
std::string GetTestExtensionPath() const {
if (GetParam())
return kTestExtensionPathMofifyPermission;
return kTestExtensionPathReadPermission;
}
bool ShouldModifyingFeatureSucceed() const { return GetParam(); }
const char* const GetPrefForFeature(const std::string& feature) {
if (feature == "spokenFeedback")
return prefs::kSpokenFeedbackEnabled;
if (feature == "largeCursor")
return prefs::kLargeCursorEnabled;
if (feature == "stickyKeys")
return prefs::kStickyKeysEnabled;
if (feature == "highContrast")
return prefs::kHighContrastEnabled;
if (feature == "screenMagnifier")
return prefs::kScreenMagnifierEnabled;
if (feature == "autoclick")
return prefs::kAutoclickEnabled;
if (feature == "virtualKeyboard")
return prefs::kVirtualKeyboardEnabled;
return NULL;
}
bool InitPrefServiceForTest(
PrefService* prefs,
const std::vector<std::string>& enabled_features,
const std::vector<std::string>& disabled_features) {
for (size_t i = 0; i < enabled_features.size(); ++i) {
const char* const pref_name = GetPrefForFeature(enabled_features[i]);
EXPECT_TRUE(pref_name) << "Invalid feature " << enabled_features[i];
if (!pref_name)
return false;
prefs->SetBoolean(pref_name, true);
}
for (size_t i = 0; i < disabled_features.size(); ++i) {
const char* const pref_name = GetPrefForFeature(disabled_features[i]);
EXPECT_TRUE(pref_name) << "Invalid feature " << disabled_features[i];
if (!pref_name)
return false;
prefs->SetBoolean(pref_name, false);
}
return true;
}
void VerifyPrefServiceState(
PrefService* prefs,
const std::vector<std::string>& enabled_features,
const std::vector<std::string>& disabled_features) {
for (size_t i = 0; i < enabled_features.size(); ++i) {
const char* const pref_name = GetPrefForFeature(enabled_features[i]);
ASSERT_TRUE(pref_name) << "Invalid feature " << enabled_features[i];
ASSERT_TRUE(prefs->GetBoolean(pref_name));
}
for (size_t i = 0; i < disabled_features.size(); ++i) {
const char* const pref_name = GetPrefForFeature(disabled_features[i]);
ASSERT_TRUE(pref_name) << "Invalid feature " << disabled_features[i];
ASSERT_FALSE(prefs->GetBoolean(pref_name));
}
}
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::DictionaryValue test_arg;
test_arg.SetString(kTestNameKey, test_name);
scoped_ptr<base::ListValue> enabled_list(new base::ListValue);
for (size_t i = 0; i < enabled_features.size(); ++i)
enabled_list->AppendString(enabled_features[i]);
test_arg.Set(kEnabledFeaturesKey, enabled_list.release());
scoped_ptr<base::ListValue> disabled_list(new base::ListValue);
for (size_t i = 0; i < disabled_features.size(); ++i)
disabled_list->AppendString(disabled_features[i]);
test_arg.Set(kDisabledFeaturesKey, disabled_list.release());
return base::JSONWriter::Write(&test_arg, result);
}
};
INSTANTIATE_TEST_CASE_P(AccessibilityFeatureaApiTestInstantiatePermission,
AccessibilityFeaturesApiTest,
testing::Bool());
IN_PROC_BROWSER_TEST_P(AccessibilityFeaturesApiTest, Get) {
std::vector<std::string> enabled_features;
enabled_features.push_back("largeCursor");
enabled_features.push_back("stickyKeys");
enabled_features.push_back("highContrast");
std::vector<std::string> disabled_features;
disabled_features.push_back("spokenFeedback");
disabled_features.push_back("screenMagnifier");
disabled_features.push_back("autoclick");
disabled_features.push_back("virtualKeyboard");
ASSERT_TRUE(
InitPrefServiceForTest(GetPrefs(), enabled_features, disabled_features));
std::string test_arg;
ASSERT_TRUE(GenerateTestArg(
"getterTest", enabled_features, disabled_features, &test_arg));
EXPECT_TRUE(
RunPlatformAppTestWithArg(GetTestExtensionPath(), test_arg.c_str()));
}
IN_PROC_BROWSER_TEST_P(AccessibilityFeaturesApiTest, Set) {
std::vector<std::string> enabled_features;
enabled_features.push_back("spokenFeedback");
enabled_features.push_back("stickyKeys");
enabled_features.push_back("autoclick");
enabled_features.push_back("virtualKeyboard");
std::vector<std::string> disabled_features;
disabled_features.push_back("largeCursor");
disabled_features.push_back("highContrast");
disabled_features.push_back("screenMagnifier");
ASSERT_TRUE(
InitPrefServiceForTest(GetPrefs(), enabled_features, disabled_features));
std::string test_arg;
ASSERT_TRUE(GenerateTestArg(
"setterTest", enabled_features, disabled_features, &test_arg));
ASSERT_TRUE(
RunPlatformAppTestWithArg(GetTestExtensionPath(), test_arg.c_str()));
if (ShouldModifyingFeatureSucceed()) {
VerifyPrefServiceState(GetPrefs(), disabled_features, enabled_features);
} else {
VerifyPrefServiceState(GetPrefs(), enabled_features, disabled_features);
}
}
IN_PROC_BROWSER_TEST_F(AccessibilityFeaturesApiTest, ObserveFeatures) {
std::vector<std::string> enabled_features;
enabled_features.push_back("largeCursor");
enabled_features.push_back("stickyKeys");
enabled_features.push_back("highContrast");
std::vector<std::string> disabled_features;
disabled_features.push_back("screenMagnifier");
ASSERT_TRUE(
InitPrefServiceForTest(GetPrefs(), enabled_features, disabled_features));
std::string test_arg;
ASSERT_TRUE(GenerateTestArg(
"observerTest", enabled_features, disabled_features, &test_arg));
ASSERT_TRUE(RunPlatformAppTestWithArg(kTestExtensionPathReadPermission,
test_arg.c_str()));
ASSERT_TRUE(
InitPrefServiceForTest(GetPrefs(), disabled_features, enabled_features));
ResultCatcher result_catcher;
ASSERT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
}
}
}