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_frame/registry_watcher.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "base/time/time.h"
9 #include "base/win/registry.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using base::win::RegKey
;
14 const wchar_t kTestRoot
[] = L
"CFRegistryWatcherTest";
15 const wchar_t kTestWindowClass
[] = L
"TestWndClass";
16 const wchar_t kTestWindowName
[] = L
"TestWndName";
18 // Give notifications 30 seconds to stick. Hopefully long enough to avoid
20 const int64 kWaitSeconds
= 30;
22 class RegistryWatcherUnittest
: public testing::Test
{
25 // Create a temporary key for testing
26 temp_key_
.Open(HKEY_CURRENT_USER
, L
"", KEY_QUERY_VALUE
);
27 temp_key_
.DeleteKey(kTestRoot
);
28 ASSERT_NE(ERROR_SUCCESS
, temp_key_
.Open(HKEY_CURRENT_USER
,
31 ASSERT_EQ(ERROR_SUCCESS
,
32 temp_key_
.Create(HKEY_CURRENT_USER
, kTestRoot
, KEY_READ
));
34 reg_change_count_
= 0;
38 // Clean up the temporary key
39 temp_key_
.Open(HKEY_CURRENT_USER
, L
"", KEY_QUERY_VALUE
);
40 ASSERT_EQ(ERROR_SUCCESS
, temp_key_
.DeleteKey(kTestRoot
));
43 reg_change_count_
= 0;
46 static void WaitCallback() {
53 static unsigned int reg_change_count_
;
54 static base::WaitableEvent event_
;
57 unsigned int RegistryWatcherUnittest::reg_change_count_
= 0;
58 base::WaitableEvent
RegistryWatcherUnittest::event_(
60 false); // initially unsignalled
62 TEST_F(RegistryWatcherUnittest
, Basic
) {
63 RegistryWatcher
watcher(HKEY_CURRENT_USER
,
65 &RegistryWatcherUnittest::WaitCallback
);
66 ASSERT_TRUE(watcher
.StartWatching());
67 EXPECT_EQ(0, reg_change_count_
);
69 EXPECT_EQ(ERROR_SUCCESS
, temp_key_
.CreateKey(L
"foo", KEY_ALL_ACCESS
));
70 EXPECT_TRUE(event_
.TimedWait(base::TimeDelta::FromSeconds(kWaitSeconds
)));
71 EXPECT_EQ(1, reg_change_count_
);