WebKit roll 138552:138617
[chromium-blink-merge.git] / rlz / test / rlz_test_helpers.cc
blob32719e0a24bcfe4dfe31ce1b122902fabc4fbe73
1 // Copyright (c) 2012 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 // Main entry point for all unit tests.
7 #include "rlz_test_helpers.h"
9 #include "rlz/lib/rlz_lib.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 #if defined(OS_WIN)
13 #include <shlwapi.h>
14 #include "base/win/registry.h"
15 #include "rlz/win/lib/rlz_lib.h"
16 #elif defined(OS_POSIX)
17 #include "base/file_path.h"
18 #include "rlz/lib/rlz_value_store.h"
19 #endif
21 #if defined(OS_WIN)
22 namespace {
24 const wchar_t* kHKCUReplacement = L"Software\\Google\\RlzUtilUnittest\\HKCU";
25 const wchar_t* kHKLMReplacement = L"Software\\Google\\RlzUtilUnittest\\HKLM";
27 void OverrideRegistryHives() {
28 // Wipe the keys we redirect to.
29 // This gives us a stable run, even in the presence of previous
30 // crashes or failures.
31 LSTATUS err = SHDeleteKey(HKEY_CURRENT_USER, kHKCUReplacement);
32 EXPECT_TRUE(err == ERROR_SUCCESS || err == ERROR_FILE_NOT_FOUND);
33 err = SHDeleteKey(HKEY_CURRENT_USER, kHKLMReplacement);
34 EXPECT_TRUE(err == ERROR_SUCCESS || err == ERROR_FILE_NOT_FOUND);
36 // Create the keys we're redirecting HKCU and HKLM to.
37 base::win::RegKey hkcu;
38 base::win::RegKey hklm;
39 ASSERT_EQ(ERROR_SUCCESS,
40 hkcu.Create(HKEY_CURRENT_USER, kHKCUReplacement, KEY_READ));
41 ASSERT_EQ(ERROR_SUCCESS,
42 hklm.Create(HKEY_CURRENT_USER, kHKLMReplacement, KEY_READ));
44 rlz_lib::InitializeTempHivesForTesting(hklm, hkcu);
46 // And do the switcharoo.
47 ASSERT_EQ(ERROR_SUCCESS,
48 ::RegOverridePredefKey(HKEY_CURRENT_USER, hkcu.Handle()));
49 ASSERT_EQ(ERROR_SUCCESS,
50 ::RegOverridePredefKey(HKEY_LOCAL_MACHINE, hklm.Handle()));
53 void UndoOverrideRegistryHives() {
54 // Undo the redirection.
55 EXPECT_EQ(ERROR_SUCCESS, ::RegOverridePredefKey(HKEY_CURRENT_USER, NULL));
56 EXPECT_EQ(ERROR_SUCCESS, ::RegOverridePredefKey(HKEY_LOCAL_MACHINE, NULL));
59 } // namespace
60 #endif // defined(OS_WIN)
62 void RlzLibTestNoMachineState::SetUp() {
63 #if defined(OS_WIN)
64 OverrideRegistryHives();
65 #elif defined(OS_MACOSX)
66 base::mac::ScopedNSAutoreleasePool pool;
67 #endif // defined(OS_WIN)
68 #if defined(OS_POSIX)
69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
70 rlz_lib::testing::SetRlzStoreDirectory(temp_dir_.path());
71 #endif // defined(OS_POSIX)
74 void RlzLibTestNoMachineState::TearDown() {
75 #if defined(OS_WIN)
76 UndoOverrideRegistryHives();
77 #elif defined(OS_POSIX)
78 rlz_lib::testing::SetRlzStoreDirectory(FilePath());
79 #endif // defined(OS_WIN)
82 void RlzLibTestBase::SetUp() {
83 RlzLibTestNoMachineState::SetUp();
84 #if defined(OS_WIN)
85 rlz_lib::CreateMachineState();
86 #endif // defined(OS_WIN)