Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 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 <string> | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/string_number_conversions.h" | |
| 9 #include "base/stringprintf.h" | |
| 10 #include "base/test/test_reg_util_win.h" | |
| 11 #include "base/time.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 13 #include "base/win/registry.h" | |
| 14 #include "chrome/common/guid.h" | |
| 15 #include "chrome/installer/gcapi/gcapi.h" | |
| 16 #include "chrome/installer/util/google_update_constants.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 using base::Time; | |
| 20 using base::TimeDelta; | |
| 21 using base::win::RegKey; | |
| 22 | |
| 23 namespace { | |
| 24 const wchar_t kChromeRegClientStateKey[] = | |
| 25 L"Software\\Google\\Update\\ClientState\\" | |
| 26 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | |
| 27 } | |
| 28 | |
| 29 class GCAPILastRunTest : public ::testing::Test { | |
| 30 public: | |
|
grt (UTC plus 2)
2011/11/11 17:01:01
the pattern i've seen most often is for all of thi
robertshield
2011/11/12 05:25:38
Done.
| |
| 31 // Sets up the test fixture. | |
| 32 static void SetUpTestCase() { | |
| 33 std::wstring hklm_override = base::StringPrintf( | |
| 34 L"hklm_override\\%ls", ASCIIToWide(guid::GenerateGUID())); | |
| 35 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, hklm_override); | |
|
grt (UTC plus 2)
2011/11/11 17:01:01
seems simpler to me to move these two calls to Ove
robertshield
2011/11/12 05:25:38
Done.
| |
| 36 | |
| 37 std::wstring hkcu_override = base::StringPrintf( | |
| 38 L"hkcu_override\\%ls", ASCIIToWide(guid::GenerateGUID())); | |
| 39 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, hkcu_override); | |
| 40 } | |
| 41 | |
| 42 // Tears down the test fixture. | |
| 43 static void TearDownTestCase() { | |
| 44 override_manager_.RemoveAllOverrides(); | |
|
grt (UTC plus 2)
2011/11/11 17:01:01
remove this (well, replace it with a delete) if yo
robertshield
2011/11/12 05:25:38
removed.
| |
| 45 } | |
| 46 | |
| 47 void SetUp() { | |
| 48 // Create the client state keys in the right places. | |
| 49 HKEY hives[] = {HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER}; | |
| 50 for (int i = 0; i < arraysize(hives); ++i) { | |
| 51 RegKey client_state(hives[i], | |
| 52 kChromeRegClientStateKey, | |
| 53 KEY_CREATE_SUB_KEY); | |
| 54 ASSERT_TRUE(client_state.Valid()); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 void TearDown() { | |
| 59 // Don't delete the state keys, allow the override manager to take care | |
| 60 // of that. Do clear any 'lastrun' value left behind though. | |
| 61 HKEY hives[] = {HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER}; | |
| 62 for (int i = 0; i < arraysize(hives); ++i) { | |
| 63 RegKey client_state(hives[i], kChromeRegClientStateKey, KEY_SET_VALUE); | |
| 64 ASSERT_TRUE(client_state.Valid()); | |
| 65 client_state.DeleteValue(google_update::kRegLastRunTimeField); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 bool SetLastRunTime(HKEY hive, int64 last_run_time) { | |
| 70 RegKey client_state(hive, kChromeRegClientStateKey, KEY_SET_VALUE); | |
| 71 return (client_state.Valid() && | |
|
grt (UTC plus 2)
2011/11/11 17:01:01
"client_state.Valid() &&" serves no purpose, remov
robertshield
2011/11/12 05:25:38
it avoided a teeny bit of extra work, but ok.
grt (UTC plus 2)
2011/11/14 14:24:09
it added a teeny bit of extra work for the common
| |
| 72 client_state.WriteValue( | |
| 73 google_update::kRegLastRunTimeField, | |
| 74 base::Int64ToString16(last_run_time).c_str()) == ERROR_SUCCESS); | |
| 75 } | |
| 76 | |
| 77 static registry_util::RegistryOverrideManager override_manager_; | |
|
grt (UTC plus 2)
2011/11/11 17:01:01
make this a pointer and new/delete in SUTC and TDT
robertshield
2011/11/12 05:25:38
Made it non-static instead.
| |
| 78 }; | |
| 79 | |
| 80 registry_util::RegistryOverrideManager GCAPILastRunTest::override_manager_; | |
| 81 | |
| 82 TEST_F(GCAPILastRunTest, Basic_HKCU) { | |
| 83 Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(10); | |
| 84 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, last_run.ToInternalValue())); | |
| 85 | |
| 86 int days_since_last_run = GoogleChromeDaysSinceLastRun(); | |
| 87 EXPECT_EQ(10, days_since_last_run); | |
| 88 } | |
| 89 | |
| 90 TEST_F(GCAPILastRunTest, Basic_HKLM) { | |
| 91 Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(10); | |
| 92 EXPECT_TRUE(SetLastRunTime(HKEY_LOCAL_MACHINE, last_run.ToInternalValue())); | |
| 93 | |
| 94 int days_since_last_run = GoogleChromeDaysSinceLastRun(); | |
| 95 EXPECT_EQ(10, days_since_last_run); | |
| 96 } | |
| 97 | |
| 98 TEST_F(GCAPILastRunTest, HKLM_and_HKCU) { | |
| 99 Time hklm_last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(30); | |
| 100 EXPECT_TRUE(SetLastRunTime(HKEY_LOCAL_MACHINE, | |
| 101 hklm_last_run.ToInternalValue())); | |
| 102 | |
| 103 Time hkcu_last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(20); | |
| 104 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, | |
| 105 hkcu_last_run.ToInternalValue())); | |
| 106 | |
| 107 int days_since_last_run = GoogleChromeDaysSinceLastRun(); | |
| 108 EXPECT_EQ(20, days_since_last_run); | |
| 109 } | |
| 110 | |
| 111 TEST_F(GCAPILastRunTest, NoLastRun) { | |
| 112 int days_since_last_run = GoogleChromeDaysSinceLastRun(); | |
| 113 EXPECT_EQ(-1, days_since_last_run); | |
| 114 } | |
|
grt (UTC plus 2)
2011/11/11 17:01:01
add tests for bad data in the registry:
- the valu
robertshield
2011/11/12 05:25:38
Done.
grt (UTC plus 2)
2011/11/14 14:24:09
looks good.
| |
| OLD | NEW |