OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/win/registry.h" | 5 #include "base/win/registry.h" |
6 | 6 |
7 #include <cstring> | 7 #include <cstring> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" |
11 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
12 #include "base/win/windows_version.h" | 15 #include "base/win/windows_version.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
14 | 17 |
15 namespace base { | 18 namespace base { |
16 namespace win { | 19 namespace win { |
17 | 20 |
18 namespace { | 21 namespace { |
19 | 22 |
20 class RegistryTest : public testing::Test { | 23 class RegistryTest : public testing::Test { |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 | 345 |
343 std::wstring foo_key(kRootKey); | 346 std::wstring foo_key(kRootKey); |
344 foo_key += L"\\Foo"; | 347 foo_key += L"\\Foo"; |
345 ASSERT_EQ(ERROR_SUCCESS, | 348 ASSERT_EQ(ERROR_SUCCESS, |
346 key.Open(HKEY_CURRENT_USER, foo_key.c_str(), KEY_READ)); | 349 key.Open(HKEY_CURRENT_USER, foo_key.c_str(), KEY_READ)); |
347 | 350 |
348 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kRootKey, KEY_WRITE)); | 351 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kRootKey, KEY_WRITE)); |
349 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(L"foo")); | 352 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(L"foo")); |
350 } | 353 } |
351 | 354 |
| 355 class TestChangeDelegate { |
| 356 public: |
| 357 TestChangeDelegate() : called_(false) {} |
| 358 ~TestChangeDelegate() {} |
| 359 |
| 360 void OnKeyChanged() { |
| 361 MessageLoop::current()->QuitWhenIdle(); |
| 362 called_ = true; |
| 363 } |
| 364 |
| 365 bool WasCalled() { |
| 366 bool was_called = called_; |
| 367 called_ = false; |
| 368 return was_called; |
| 369 } |
| 370 |
| 371 private: |
| 372 bool called_; |
| 373 }; |
| 374 |
| 375 TEST_F(RegistryTest, ChangeCallback) { |
| 376 RegKey key; |
| 377 TestChangeDelegate delegate; |
| 378 MessageLoop message_loop; |
| 379 |
| 380 std::wstring foo_key(kRootKey); |
| 381 foo_key += L"\\Foo"; |
| 382 ASSERT_EQ(ERROR_SUCCESS, key.Create(HKEY_CURRENT_USER, foo_key.c_str(), |
| 383 KEY_READ)); |
| 384 |
| 385 ASSERT_TRUE(key.StartWatching(Bind(&TestChangeDelegate::OnKeyChanged, |
| 386 Unretained(&delegate)))); |
| 387 EXPECT_FALSE(delegate.WasCalled()); |
| 388 |
| 389 // Make some change. |
| 390 RegKey key2; |
| 391 ASSERT_EQ(ERROR_SUCCESS, key2.Open(HKEY_CURRENT_USER, foo_key.c_str(), |
| 392 KEY_READ | KEY_SET_VALUE)); |
| 393 ASSERT_TRUE(key2.Valid()); |
| 394 EXPECT_EQ(ERROR_SUCCESS, key2.WriteValue(L"name", L"data")); |
| 395 |
| 396 // Allow delivery of the notification. |
| 397 EXPECT_FALSE(delegate.WasCalled()); |
| 398 base::RunLoop().Run(); |
| 399 |
| 400 ASSERT_TRUE(delegate.WasCalled()); |
| 401 EXPECT_FALSE(delegate.WasCalled()); |
| 402 |
| 403 ASSERT_TRUE(key.StartWatching(Bind(&TestChangeDelegate::OnKeyChanged, |
| 404 Unretained(&delegate)))); |
| 405 |
| 406 // Change something else. |
| 407 EXPECT_EQ(ERROR_SUCCESS, key2.WriteValue(L"name2", L"data2")); |
| 408 base::RunLoop().Run(); |
| 409 ASSERT_TRUE(delegate.WasCalled()); |
| 410 |
| 411 ASSERT_TRUE(key.StartWatching(Bind(&TestChangeDelegate::OnKeyChanged, |
| 412 Unretained(&delegate)))); |
| 413 base::RunLoop().RunUntilIdle(); |
| 414 EXPECT_FALSE(delegate.WasCalled()); |
| 415 } |
| 416 |
352 } // namespace | 417 } // namespace |
353 | 418 |
354 } // namespace win | 419 } // namespace win |
355 } // namespace base | 420 } // namespace base |
OLD | NEW |