Home | History | Annotate | Download | only in test
      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.
      4 
      5 #include "chrome_frame/registry_watcher.h"
      6 
      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"
     11 
     12 using base::win::RegKey;
     13 
     14 const wchar_t kTestRoot[] = L"CFRegistryWatcherTest";
     15 const wchar_t kTestWindowClass[] = L"TestWndClass";
     16 const wchar_t kTestWindowName[] = L"TestWndName";
     17 
     18 // Give notifications 30 seconds to stick. Hopefully long enough to avoid
     19 // flakiness.
     20 const int64 kWaitSeconds = 30;
     21 
     22 class RegistryWatcherUnittest : public testing::Test {
     23  public:
     24   void SetUp() {
     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,
     29                                             kTestRoot,
     30                                             KEY_READ));
     31     ASSERT_EQ(ERROR_SUCCESS,
     32         temp_key_.Create(HKEY_CURRENT_USER, kTestRoot, KEY_READ));
     33 
     34     reg_change_count_ = 0;
     35   }
     36 
     37   void TearDown() {
     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));
     41     temp_key_.Close();
     42 
     43     reg_change_count_ = 0;
     44   }
     45 
     46   static void WaitCallback() {
     47     reg_change_count_++;
     48     event_.Signal();
     49   }
     50 
     51  protected:
     52   RegKey temp_key_;
     53   static unsigned int reg_change_count_;
     54   static base::WaitableEvent event_;
     55 };
     56 
     57 unsigned int RegistryWatcherUnittest::reg_change_count_ = 0;
     58 base::WaitableEvent RegistryWatcherUnittest::event_(
     59     false,   // auto reset
     60     false);  // initially unsignalled
     61 
     62 TEST_F(RegistryWatcherUnittest, Basic) {
     63   RegistryWatcher watcher(HKEY_CURRENT_USER,
     64                           kTestRoot,
     65                           &RegistryWatcherUnittest::WaitCallback);
     66   ASSERT_TRUE(watcher.StartWatching());
     67   EXPECT_EQ(0, reg_change_count_);
     68 
     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_);
     72 }
     73