Index: chrome/browser/extensions/extension_management_test_util.h |
diff --git a/chrome/browser/extensions/extension_management_test_util.h b/chrome/browser/extensions/extension_management_test_util.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c306bb95716b871be7579e39d8ae1c2149970df9 |
--- /dev/null |
+++ b/chrome/browser/extensions/extension_management_test_util.h |
@@ -0,0 +1,149 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_ |
+#define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_ |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "base/values.h" |
+#include "chrome/browser/extensions/extension_management.h" |
+#include "components/crx_file/id_util.h" |
+#include "extensions/browser/pref_names.h" |
+#include "extensions/common/extension.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace extensions { |
+ |
+namespace schema = schema_constants; |
+ |
+// A helper class to manipulate the extension management preference in unit |
+// tests. It's a template class and |TestingPrefService| is suppored to be a |
+// subclass of TestingPrefServiceBase, which is another template class. |
+template <class TestingPrefService> |
+class ExtensionManagementPrefUpdater { |
+ public: |
+ explicit ExtensionManagementPrefUpdater(TestingPrefService* service) |
+ : service_(service) { |
+ const base::Value* pref_value = |
+ service_->GetManagedPref(pref_names::kExtensionManagement); |
+ if (pref_value) { |
+ const base::DictionaryValue* dict_value = NULL; |
+ pref_value->GetAsDictionary(&dict_value); |
+ pref_.reset(dict_value->DeepCopy()); |
+ } else { |
+ pref_.reset(new base::DictionaryValue); |
+ } |
+ } |
+ |
+ virtual ~ExtensionManagementPrefUpdater() { |
+ DCHECK(pref_.get()); |
+ service_->SetManagedPref(pref_names::kExtensionManagement, pref_.release()); |
Joao da Silva
2014/09/18 12:08:22
Break this class into 2:
class ExtensionManagemen
binjin
2014/09/18 14:37:27
Done.
|
+ } |
+ |
+ void SetBlacklistedByDefault(bool value) { |
+ pref_->SetString( |
+ make_path(schema::kAllOtherExtension, schema::kInstallationMode), |
+ value ? schema::kBlocked : schema::kAllowed); |
+ } |
+ |
+ void ClearInstallationModesForIndividualExtensions() { |
+ for (base::DictionaryValue::Iterator it(*pref_.get()); !it.IsAtEnd(); |
+ it.Advance()) { |
+ DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY)); |
+ if (it.key() != schema::kAllOtherExtension) { |
+ DCHECK(crx_file::id_util::IdIsValid(it.key())); |
+ pref_->Remove(make_path(it.key(), schema::kInstallationMode), NULL); |
+ pref_->Remove(make_path(it.key(), schema::kUpdateUrl), NULL); |
+ } |
+ } |
+ } |
+ |
+ void SetIndividualExtensionInstallationAllowed(const ExtensionId& id, |
+ bool allowed) { |
+ DCHECK(crx_file::id_util::IdIsValid(id)); |
+ pref_->SetString(make_path(id, schema::kInstallationMode), |
+ allowed ? schema::kAllowed : schema::kBlocked); |
+ pref_->Remove(make_path(id, schema::kUpdateUrl), NULL); |
+ } |
+ |
+ void SetIndividualExtensionAutoInstalled(const ExtensionId& id, |
+ const std::string& update_url, |
+ bool forced) { |
+ DCHECK(crx_file::id_util::IdIsValid(id)); |
+ pref_->SetString( |
+ make_path(id, schema::kInstallationMode), |
+ forced ? schema::kForceInstalled : schema::kNormalInstalled); |
+ pref_->SetString(make_path(id, schema::kUpdateUrl), update_url); |
+ } |
+ |
+ void UnsetInstallSources() { pref_->Remove(kInstallSourcesPath, NULL); } |
+ |
+ void ClearInstallSources() { ClearList(kInstallSourcesPath); } |
+ |
+ void AddInstallSource(const std::string& install_source) { |
+ AddStringToList(kInstallSourcesPath, install_source); |
+ } |
+ |
+ void RemoveInstallSource(const std::string& install_source) { |
+ RemoveStringFromList(kInstallSourcesPath, install_source); |
+ } |
+ |
+ void UnsetAllowedTypes() { pref_->Remove(kAllowedTypesPath, NULL); } |
+ |
+ void ClearAllowedTypes() { ClearList(kAllowedTypesPath); } |
+ |
+ void AddAllowedType(const std::string &allowed_type) { |
+ AddStringToList(kAllowedTypesPath, allowed_type); |
+ } |
+ |
+ void RemoveAllowedType(const std::string& allowd_type) { |
+ RemoveStringFromList(kAllowedTypesPath, allowd_type); |
+ } |
+ |
+ private: |
+ const std::string kInstallSourcesPath = |
+ make_path(schema::kAllOtherExtension, schema::kInstallSources); |
+ const std::string kAllowedTypesPath = |
+ make_path(schema::kAllOtherExtension, schema::kAllowedTypes); |
+ |
+ static std::string make_path(std::string a, std::string b) { |
+ return a + "." + b; |
+ } |
+ |
+ static std::string make_path3(std::string a, std::string b, std::string c) { |
+ return a + "." + b + "." + c; |
+ } |
+ |
+ void ClearList(const std::string& path) { |
+ base::ListValue* list_value = NULL; |
+ if (pref_->GetList(path, &list_value)) |
+ list_value->Clear(); |
+ else |
+ pref_->Set(path, new base::ListValue()); |
+ } |
+ |
+ void AddStringToList(const std::string& path, const std::string& str) { |
+ base::ListValue* list_value = NULL; |
+ if (!pref_->GetList(path, &list_value)) { |
+ list_value = new base::ListValue(); |
+ pref_->Set(path, list_value); |
+ } |
+ CHECK(list_value->AppendIfNotPresent(new base::StringValue(str))); |
+ } |
+ |
+ void RemoveStringFromList(const std::string& path, const std::string& str) { |
+ base::ListValue* list_value = NULL; |
+ if (pref_->GetList(path, &list_value)) |
+ CHECK(list_value->Remove(base::StringValue(str), NULL)); |
+ } |
+ |
+ TestingPrefService* service_; |
+ scoped_ptr<base::DictionaryValue> pref_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdater); |
+}; |
+ |
+} // namespace extensions |
+ |
+#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_ |