summaryrefslogtreecommitdiffstats
path: root/base/test
diff options
context:
space:
mode:
authorrogerta@chromium.org <rogerta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-24 20:02:42 +0000
committerrogerta@chromium.org <rogerta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-24 20:02:42 +0000
commita8f8588a7a24c9a0603e28fd67ea463b542a72ac (patch)
tree5130701e6d083f1f2b742fd155522ebd970ee331 /base/test
parent96de7910f611a4640bc9ade33abf981b6661680d (diff)
downloadchromium_src-a8f8588a7a24c9a0603e28fd67ea463b542a72ac.zip
chromium_src-a8f8588a7a24c9a0603e28fd67ea463b542a72ac.tar.gz
chromium_src-a8f8588a7a24c9a0603e28fd67ea463b542a72ac.tar.bz2
Tommi: I need an owner review for the chrome frame changes.
Moving the test helper class TempRegKeyOverride since I need to do something similar in some new RLZ tests, and I don't want to duplicate the code. Please suggest a better namespace name if needed, I was just following the same pattern found in test_file_util in the same directory. BUG=None TEST=No new tests, just refactoring test helpers Review URL: http://codereview.chromium.org/7669061 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98100 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/test')
-rw-r--r--base/test/test_reg_util_win.cc65
-rw-r--r--base/test/test_reg_util_win.h64
2 files changed, 129 insertions, 0 deletions
diff --git a/base/test/test_reg_util_win.cc b/base/test/test_reg_util_win.cc
new file mode 100644
index 0000000..e23c6e9
--- /dev/null
+++ b/base/test/test_reg_util_win.cc
@@ -0,0 +1,65 @@
+// Copyright (c) 2011 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.
+
+#include "base/test/test_reg_util_win.h"
+
+#include "base/logging.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace registry_util {
+
+const wchar_t RegistryOverrideManager::kTempTestKeyPath[] =
+ L"Software\\Chromium\\TempTestKeys";
+
+RegistryOverrideManager::ScopedRegistryKeyOverride::ScopedRegistryKeyOverride(
+ HKEY override,
+ const std::wstring& temp_name)
+ : override_(override),
+ temp_name_(temp_name) {
+ DCHECK(!temp_name_.empty());
+ std::wstring key_path(RegistryOverrideManager::kTempTestKeyPath);
+ key_path += L"\\" + temp_name_;
+ EXPECT_EQ(ERROR_SUCCESS,
+ temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS));
+ EXPECT_EQ(ERROR_SUCCESS,
+ ::RegOverridePredefKey(override_, temp_key_.Handle()));
+}
+
+RegistryOverrideManager::
+ ScopedRegistryKeyOverride::~ScopedRegistryKeyOverride() {
+ ::RegOverridePredefKey(override_, NULL);
+ // The temp key will be deleted via a call to DeleteAllTempKeys().
+}
+
+RegistryOverrideManager::RegistryOverrideManager() {
+ DeleteAllTempKeys();
+}
+
+RegistryOverrideManager::~RegistryOverrideManager() {
+ RemoveAllOverrides();
+}
+
+void RegistryOverrideManager::OverrideRegistry(HKEY override,
+ const std::wstring& temp_name) {
+ overrides_.push_back(new ScopedRegistryKeyOverride(override, temp_name));
+}
+
+void RegistryOverrideManager::RemoveAllOverrides() {
+ while (!overrides_.empty()) {
+ delete overrides_.back();
+ overrides_.pop_back();
+ }
+
+ DeleteAllTempKeys();
+}
+
+// static
+void RegistryOverrideManager::DeleteAllTempKeys() {
+ base::win::RegKey key;
+ if (key.Open(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS) == ERROR_SUCCESS) {
+ key.DeleteKey(kTempTestKeyPath);
+ }
+}
+
+} // namespace registry_util
diff --git a/base/test/test_reg_util_win.h b/base/test/test_reg_util_win.h
new file mode 100644
index 0000000..82c1a53
--- /dev/null
+++ b/base/test/test_reg_util_win.h
@@ -0,0 +1,64 @@
+// Copyright (c) 2011 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 BASE_TEST_TEST_REG_UTIL_H_
+#define BASE_TEST_TEST_REG_UTIL_H_
+#pragma once
+
+// Registry utility functions used only by tests.
+
+#include <string>
+#include <vector>
+
+#include "base/basictypes.h"
+#include "base/win/registry.h"
+
+namespace registry_util {
+
+// Allows a test to easily override registry hives so that it can start from a
+// known good state, or make sure to not leave any side effects once the test
+// completes.
+class RegistryOverrideManager {
+ public:
+ // All overridden hives will be descendents of this registry path under the
+ // main HKCU hive.
+ static const wchar_t kTempTestKeyPath[];
+
+ RegistryOverrideManager();
+ ~RegistryOverrideManager();
+
+ // Override the given registry hive using a temporary key named by temp_name
+ // under the temporary test key path.
+ void OverrideRegistry(HKEY override, const std::wstring& temp_name);
+
+ // Deletes all temporary test keys used by the overrides.
+ static void DeleteAllTempKeys();
+
+ // Removes all overrides and deletes all temporary test keys used by the
+ // overrides.
+ void RemoveAllOverrides();
+
+ private:
+ // Keeps track of one override.
+ class ScopedRegistryKeyOverride {
+ public:
+ ScopedRegistryKeyOverride(HKEY override, const std::wstring& temp_name);
+ ~ScopedRegistryKeyOverride();
+
+ private:
+ HKEY override_;
+ base::win::RegKey temp_key_;
+ std::wstring temp_name_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedRegistryKeyOverride);
+ };
+
+ std::vector<ScopedRegistryKeyOverride*> overrides_;
+
+ DISALLOW_COPY_AND_ASSIGN(RegistryOverrideManager);
+};
+
+} // namespace registry_util
+
+#endif // BASE_TEST_TEST_REG_UTIL_H_