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" | |
11 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/run_loop.h" | |
14 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
15 #include "base/win/windows_version.h" | 12 #include "base/win/windows_version.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
17 | 14 |
18 namespace base { | 15 namespace base { |
19 namespace win { | 16 namespace win { |
20 | 17 |
21 namespace { | 18 namespace { |
22 | 19 |
23 class RegistryTest : public testing::Test { | 20 class RegistryTest : public testing::Test { |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 | 342 |
346 std::wstring foo_key(kRootKey); | 343 std::wstring foo_key(kRootKey); |
347 foo_key += L"\\Foo"; | 344 foo_key += L"\\Foo"; |
348 ASSERT_EQ(ERROR_SUCCESS, | 345 ASSERT_EQ(ERROR_SUCCESS, |
349 key.Open(HKEY_CURRENT_USER, foo_key.c_str(), KEY_READ)); | 346 key.Open(HKEY_CURRENT_USER, foo_key.c_str(), KEY_READ)); |
350 | 347 |
351 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kRootKey, KEY_WRITE)); | 348 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kRootKey, KEY_WRITE)); |
352 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(L"foo")); | 349 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(L"foo")); |
353 } | 350 } |
354 | 351 |
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 | |
417 } // namespace | 352 } // namespace |
418 | 353 |
419 } // namespace win | 354 } // namespace win |
420 } // namespace base | 355 } // namespace base |
OLD | NEW |