Index: base/win/registry_unittest.cc |
diff --git a/base/win/registry_unittest.cc b/base/win/registry_unittest.cc |
index d2610efd48be372a27ec290174dffbc0cc012354..6548474c4facfca93497d8db149cfd2208b63af0 100644 |
--- a/base/win/registry_unittest.cc |
+++ b/base/win/registry_unittest.cc |
@@ -7,7 +7,10 @@ |
#include <cstring> |
#include <vector> |
+#include "base/bind.h" |
#include "base/compiler_specific.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
#include "base/stl_util.h" |
#include "base/win/windows_version.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -349,6 +352,68 @@ TEST_F(RegistryTest, OpenSubKey) { |
ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(L"foo")); |
} |
+class TestChangeDelegate { |
+ public: |
+ TestChangeDelegate() : called_(false) {} |
+ ~TestChangeDelegate() {} |
+ |
+ void OnKeyChanged() { |
+ MessageLoop::current()->QuitWhenIdle(); |
+ called_ = true; |
+ } |
+ |
+ bool WasCalled() { |
+ bool was_called = called_; |
+ called_ = false; |
+ return was_called; |
+ } |
+ |
+ private: |
+ bool called_; |
+}; |
+ |
+TEST_F(RegistryTest, ChangeCallback) { |
+ RegKey key; |
+ TestChangeDelegate delegate; |
+ MessageLoop message_loop; |
+ |
+ std::wstring foo_key(kRootKey); |
+ foo_key += L"\\Foo"; |
+ ASSERT_EQ(ERROR_SUCCESS, key.Create(HKEY_CURRENT_USER, foo_key.c_str(), |
+ KEY_READ)); |
+ |
+ ASSERT_TRUE(key.StartWatching(Bind(&TestChangeDelegate::OnKeyChanged, |
+ Unretained(&delegate)))); |
+ EXPECT_FALSE(delegate.WasCalled()); |
+ |
+ // Make some change. |
+ RegKey key2; |
+ ASSERT_EQ(ERROR_SUCCESS, key2.Open(HKEY_CURRENT_USER, foo_key.c_str(), |
+ KEY_READ | KEY_SET_VALUE)); |
+ ASSERT_TRUE(key2.Valid()); |
+ EXPECT_EQ(ERROR_SUCCESS, key2.WriteValue(L"name", L"data")); |
+ |
+ // Allow delivery of the notification. |
+ EXPECT_FALSE(delegate.WasCalled()); |
+ base::RunLoop().Run(); |
+ |
+ ASSERT_TRUE(delegate.WasCalled()); |
+ EXPECT_FALSE(delegate.WasCalled()); |
+ |
+ ASSERT_TRUE(key.StartWatching(Bind(&TestChangeDelegate::OnKeyChanged, |
+ Unretained(&delegate)))); |
+ |
+ // Change something else. |
+ EXPECT_EQ(ERROR_SUCCESS, key2.WriteValue(L"name2", L"data2")); |
+ base::RunLoop().Run(); |
+ ASSERT_TRUE(delegate.WasCalled()); |
+ |
+ ASSERT_TRUE(key.StartWatching(Bind(&TestChangeDelegate::OnKeyChanged, |
+ Unretained(&delegate)))); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_FALSE(delegate.WasCalled()); |
+} |
+ |
} // namespace |
} // namespace win |