3939#include < vector>
4040
4141#include " absl/log/check.h"
42- #include " base/singleton.h"
4342#include " base/win32/win_api_test_helper.h"
4443#include " base/win32/win_util.h"
4544#include " testing/gunit.h"
@@ -71,12 +70,11 @@ bool IsEqualInLowercase(const std::wstring& lhs, const std::wstring& rhs) {
7170 return WinUtil::SystemEqualString (lhs, rhs, true );
7271}
7372
74- // Win32 registry emulator for unit testing. To separate internal state,
75- // set unique id at the template parameter.
76- template < int Id>
77- class RegistryEmulator {
73+ // Win32 registry emulator for unit testing. Can be safely used only in a
74+ // single thread test scenario. At most one instance can be created at the same
75+ // time in the same process.
76+ class SingleThreadedRegistryEmulator {
7877 public:
79- template <int Id>
8078 class PropertySelector {
8179 public:
8280 PropertySelector ()
@@ -131,9 +129,10 @@ class RegistryEmulator {
131129 std::wstring installer_result_ui_string_;
132130 };
133131
134- typedef PropertySelector<Id> Property;
132+ typedef PropertySelector Property;
135133
136- RegistryEmulator () {
134+ SingleThreadedRegistryEmulator () {
135+ current_ = &property_;
137136 std::vector<WinAPITestHelper::HookRequest> requests;
138137 requests.push_back (
139138 DEFINE_HOOK (" advapi32.dll" , RegCreateKeyExW, TestRegCreateKeyExW));
@@ -151,12 +150,13 @@ class RegistryEmulator {
151150 WinAPITestHelper::DoHook (::GetModuleHandle (nullptr ), requests);
152151 }
153152
154- ~RegistryEmulator () {
153+ ~SingleThreadedRegistryEmulator () {
155154 WinAPITestHelper::RestoreHook (restore_info_);
156155 restore_info_ = nullptr ;
156+ current_ = nullptr ;
157157 }
158158
159- static Property* property () { return Singleton<Property>:: get () ; }
159+ static Property* property () { return current_ ; }
160160
161161 static HKEY GetClientStateKey (REGSAM regsam) {
162162 const REGSAM kReadWrite = (KEY_WRITE | KEY_READ);
@@ -434,15 +434,17 @@ class RegistryEmulator {
434434 return ERROR_SUCCESS;
435435 }
436436
437+ Property property_;
437438 WinAPITestHelper::RestoreInfoHandle restore_info_;
439+ inline static Property* current_ = nullptr ;
438440};
439441
440442} // namespace
441443
442444#if defined(GOOGLE_JAPANESE_INPUT_BUILD)
443445
444446TEST (OmahaUtilTestOn64bitMachine, ReadWriteClearChannel) {
445- RegistryEmulator<__COUNTER__> test;
447+ SingleThreadedRegistryEmulator test;
446448
447449 // ClientStateKey does not exist.
448450 test.property ()->Clear ();
@@ -484,7 +486,7 @@ TEST(OmahaUtilTestOn64bitMachine, ReadWriteClearChannel) {
484486}
485487
486488TEST (OmahaUtilTestOn64bitMachine, WriteClearOmahaError) {
487- RegistryEmulator<__COUNTER__> test;
489+ SingleThreadedRegistryEmulator test;
488490
489491 // ClientStateKey does not exist.
490492 test.property ()->Clear ();
@@ -509,7 +511,7 @@ TEST(OmahaUtilTestOn64bitMachine, WriteClearOmahaError) {
509511#else // !GOOGLE_JAPANESE_INPUT_BUILD
510512
511513TEST (OmahaUtilTestOn64bitMachine, ReadWriteClearChannel) {
512- RegistryEmulator<__COUNTER__> test;
514+ SingleThreadedRegistryEmulator test;
513515
514516 // ClientStateKey does not exist.
515517 test.property ()->Clear ();
@@ -524,7 +526,7 @@ TEST(OmahaUtilTestOn64bitMachine, ReadWriteClearChannel) {
524526}
525527
526528TEST (OmahaUtilTestOn64bitMachine, WriteClearOmahaError) {
527- RegistryEmulator<__COUNTER__> test;
529+ SingleThreadedRegistryEmulator test;
528530
529531 // ClientStateKey does not exist.
530532 test.property ()->Clear ();
0 commit comments