Home | History | Annotate | Download | only in util
      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 #ifndef CHROME_INSTALLER_UTIL_REGISTRY_TEST_DATA_H_
      6 #define CHROME_INSTALLER_UTIL_REGISTRY_TEST_DATA_H_
      7 
      8 #include <windows.h>
      9 
     10 #include <string>
     11 
     12 #include "base/basictypes.h"
     13 
     14 // A helper class for use by unit tests that need some registry space and data.
     15 // BEWARE: Instances of this class irrevocably and recursively delete keys and
     16 // values from the registry.  Carefully read the comments for Initialize and
     17 // Reset before use.
     18 class RegistryTestData {
     19  public:
     20   RegistryTestData();
     21   // Invokes Reset() on its way out.
     22   ~RegistryTestData();
     23 
     24   // Resets this instance, deletes the key rooted at |base_path|, and then
     25   // populates |base_path| with:
     26   // \EmptyKey
     27   // \NonEmptyKey (default value = "|base_path|\NonEmptyKey")
     28   // \NonEmptyKey\Subkey ("SomeValue" = DWORD 1)
     29   bool Initialize(HKEY root_key, const wchar_t* base_path);
     30 
     31   // Deletes the key rooted at base_path and clears all state.
     32   void Reset();
     33 
     34   // Fires Google Test expectations in the hopes that |path| contains the same
     35   // data as originally placed in |non_empty_key| by Initialize().
     36   void ExpectMatchesNonEmptyKey(HKEY root_key, const wchar_t* path);
     37 
     38   HKEY root_key() const { return root_key_; }
     39   const std::wstring& base_path() const { return base_path_; }
     40   const std::wstring& empty_key_path() const { return empty_key_path_; }
     41   const std::wstring& non_empty_key_path() const { return non_empty_key_path_; }
     42 
     43   // Fires Google Test expectations in the hopes that |path| is an empty key
     44   // (exists but has no values or subkeys).
     45   static void ExpectEmptyKey(HKEY root_key, const wchar_t* path);
     46 
     47  private:
     48   static bool DeleteKey(HKEY root_key, const wchar_t* path);
     49 
     50   HKEY root_key_;
     51   std::wstring base_path_;
     52   std::wstring empty_key_path_;
     53   std::wstring non_empty_key_path_;
     54 
     55   DISALLOW_COPY_AND_ASSIGN(RegistryTestData);
     56 };
     57 
     58 #endif  // CHROME_INSTALLER_UTIL_REGISTRY_TEST_DATA_H_
     59