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 "chrome/installer/util/registry_test_data.h"
7 #include "base/logging.h"
8 #include "base/win/registry.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using base::win::RegKey
;
13 RegistryTestData::RegistryTestData()
17 RegistryTestData::~RegistryTestData() {
22 bool RegistryTestData::DeleteKey(HKEY root_key
, const wchar_t* path
) {
23 LONG result
= ERROR_SUCCESS
;
24 if (root_key
!= NULL
&& path
!= NULL
&& *path
!= L
'\0') {
25 result
= RegKey(root_key
, L
"", KEY_QUERY_VALUE
).DeleteKey(path
);
26 if (result
== ERROR_FILE_NOT_FOUND
) {
27 result
= ERROR_SUCCESS
;
29 DLOG_IF(DFATAL
, result
!= ERROR_SUCCESS
)
30 << "Failed to delete registry key " << path
<< ", result: " << result
;
33 return result
== ERROR_SUCCESS
;
36 void RegistryTestData::Reset() {
37 // Clean up behind a previous use (ignore failures).
38 DeleteKey(root_key_
, base_path_
.c_str());
43 empty_key_path_
.clear();
44 non_empty_key_path_
.clear();
47 bool RegistryTestData::Initialize(HKEY root_key
, const wchar_t* base_path
) {
48 LONG result
= ERROR_SUCCESS
;
52 // Take over the new registry location.
53 if (DeleteKey(root_key
, base_path
)) {
54 // Take on the new values.
56 base_path_
.assign(base_path
);
59 empty_key_path_
.assign(base_path_
).append(L
"\\EmptyKey");
60 non_empty_key_path_
.assign(base_path_
).append(L
"\\NonEmptyKey");
64 result
= key
.Create(root_key_
, empty_key_path_
.c_str(), KEY_QUERY_VALUE
);
65 if (result
== ERROR_SUCCESS
)
66 result
= key
.Create(root_key_
, non_empty_key_path_
.c_str(), KEY_WRITE
);
67 if (result
== ERROR_SUCCESS
)
68 result
= key
.WriteValue(NULL
, non_empty_key_path_
.c_str());
69 if (result
== ERROR_SUCCESS
)
70 result
= key
.CreateKey(L
"SubKey", KEY_WRITE
);
71 if (result
== ERROR_SUCCESS
)
72 result
= key
.WriteValue(L
"SomeValue", 1UL);
73 DLOG_IF(DFATAL
, result
!= ERROR_SUCCESS
)
74 << "Failed to create test registry data based at " << base_path_
75 << ", result: " << result
;
77 result
= ERROR_INVALID_PARAMETER
;
80 return result
== ERROR_SUCCESS
;
83 void RegistryTestData::ExpectMatchesNonEmptyKey(HKEY root_key
,
84 const wchar_t* path
) {
87 EXPECT_EQ(ERROR_SUCCESS
, key
.Open(root_key
, path
, KEY_READ
));
88 std::wstring str_value
;
89 EXPECT_EQ(ERROR_SUCCESS
, key
.ReadValue(NULL
, &str_value
));
90 EXPECT_EQ(non_empty_key_path_
, str_value
);
91 EXPECT_EQ(ERROR_SUCCESS
, key
.OpenKey(L
"Subkey", KEY_READ
));
93 EXPECT_EQ(ERROR_SUCCESS
, key
.ReadValueDW(L
"SomeValue", &dw_value
));
94 EXPECT_EQ(1UL, dw_value
);
98 void RegistryTestData::ExpectEmptyKey(HKEY root_key
, const wchar_t* path
) {
99 DWORD num_subkeys
= 0;
100 DWORD num_values
= 0;
102 EXPECT_EQ(ERROR_SUCCESS
, key
.Open(root_key
, path
, KEY_READ
));
103 EXPECT_EQ(ERROR_SUCCESS
,
104 RegQueryInfoKey(key
.Handle(), NULL
, NULL
, NULL
, &num_subkeys
,
105 NULL
, NULL
, &num_values
, NULL
, NULL
, NULL
, NULL
));
106 EXPECT_EQ(0UL, num_subkeys
);
107 EXPECT_EQ(0UL, num_values
);