Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / installer / util / registry_key_backup_unittest.cc
blobdb33aa4af2f7d724f922e7772dd5ad5d097ec502
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <windows.h>
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/win/registry.h"
10 #include "chrome/installer/util/registry_key_backup.h"
11 #include "chrome/installer/util/registry_test_data.h"
12 #include "chrome/installer/util/work_item.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::win::RegKey;
17 class RegistryKeyBackupTest : public testing::Test {
18 protected:
19 static void TearDownTestCase() {
20 logging::CloseLogFile();
23 virtual void SetUp() {
24 ASSERT_TRUE(test_data_.Initialize(HKEY_CURRENT_USER, L"SOFTWARE\\TmpTmp"));
25 destination_path_.assign(test_data_.base_path()).append(L"\\Destination");
28 RegistryTestData test_data_;
29 std::wstring destination_path_;
32 // Test that writing an uninitialized backup does nothing.
33 TEST_F(RegistryKeyBackupTest, Uninitialized) {
34 RegistryKeyBackup backup;
36 EXPECT_TRUE(backup.WriteTo(test_data_.root_key(), destination_path_.c_str()));
37 EXPECT_FALSE(RegKey(test_data_.root_key(), destination_path_.c_str(),
38 KEY_READ).Valid());
41 // Test that initializing a backup with a non-existent key works, and that
42 // writing it back out does nothing.
43 TEST_F(RegistryKeyBackupTest, MissingKey) {
44 std::wstring non_existent_key_path(test_data_.base_path() + L"\\NoKeyHere");
45 RegistryKeyBackup backup;
47 EXPECT_TRUE(backup.Initialize(test_data_.root_key(),
48 non_existent_key_path.c_str()));
49 EXPECT_TRUE(backup.WriteTo(test_data_.root_key(), destination_path_.c_str()));
50 EXPECT_FALSE(RegKey(test_data_.root_key(), destination_path_.c_str(),
51 KEY_READ).Valid());
54 // Test that reading some data then writing it out does the right thing.
55 TEST_F(RegistryKeyBackupTest, ReadWrite) {
56 RegistryKeyBackup backup;
58 EXPECT_TRUE(backup.Initialize(test_data_.root_key(),
59 test_data_.non_empty_key_path().c_str()));
60 EXPECT_TRUE(backup.WriteTo(test_data_.root_key(), destination_path_.c_str()));
61 test_data_.ExpectMatchesNonEmptyKey(test_data_.root_key(),
62 destination_path_.c_str());
65 // Test that reading some data, swapping, then writing it out does the right
66 // thing.
67 TEST_F(RegistryKeyBackupTest, Swap) {
68 RegistryKeyBackup backup;
69 RegistryKeyBackup other_backup;
71 EXPECT_TRUE(backup.Initialize(test_data_.root_key(),
72 test_data_.non_empty_key_path().c_str()));
73 backup.swap(other_backup);
74 EXPECT_TRUE(other_backup.WriteTo(test_data_.root_key(),
75 destination_path_.c_str()));
77 // Now make sure the one we started with is truly empty.
78 EXPECT_EQ(ERROR_SUCCESS,
79 RegKey(test_data_.root_key(), L"", KEY_QUERY_VALUE)
80 .DeleteKey(destination_path_.c_str()));
81 EXPECT_TRUE(backup.WriteTo(test_data_.root_key(),
82 destination_path_.c_str()));
83 EXPECT_FALSE(RegKey(test_data_.root_key(), destination_path_.c_str(),
84 KEY_READ).Valid());