Chromium Code Reviews| Index: chrome/installer/gcapi/gcapi_last_run_test.cc |
| =================================================================== |
| --- chrome/installer/gcapi/gcapi_last_run_test.cc (revision 0) |
| +++ chrome/installer/gcapi/gcapi_last_run_test.cc (revision 0) |
| @@ -0,0 +1,114 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/stringprintf.h" |
| +#include "base/test/test_reg_util_win.h" |
| +#include "base/time.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "base/win/registry.h" |
| +#include "chrome/common/guid.h" |
| +#include "chrome/installer/gcapi/gcapi.h" |
| +#include "chrome/installer/util/google_update_constants.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using base::Time; |
| +using base::TimeDelta; |
| +using base::win::RegKey; |
| + |
| +namespace { |
| +const wchar_t kChromeRegClientStateKey[] = |
| + L"Software\\Google\\Update\\ClientState\\" |
| + L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; |
| +} |
| + |
| +class GCAPILastRunTest : public ::testing::Test { |
| + 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.
|
| + // Sets up the test fixture. |
| + static void SetUpTestCase() { |
| + std::wstring hklm_override = base::StringPrintf( |
| + L"hklm_override\\%ls", ASCIIToWide(guid::GenerateGUID())); |
| + 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.
|
| + |
| + std::wstring hkcu_override = base::StringPrintf( |
| + L"hkcu_override\\%ls", ASCIIToWide(guid::GenerateGUID())); |
| + override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, hkcu_override); |
| + } |
| + |
| + // Tears down the test fixture. |
| + static void TearDownTestCase() { |
| + 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.
|
| + } |
| + |
| + void SetUp() { |
| + // Create the client state keys in the right places. |
| + HKEY hives[] = {HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER}; |
| + for (int i = 0; i < arraysize(hives); ++i) { |
| + RegKey client_state(hives[i], |
| + kChromeRegClientStateKey, |
| + KEY_CREATE_SUB_KEY); |
| + ASSERT_TRUE(client_state.Valid()); |
| + } |
| + } |
| + |
| + void TearDown() { |
| + // Don't delete the state keys, allow the override manager to take care |
| + // of that. Do clear any 'lastrun' value left behind though. |
| + HKEY hives[] = {HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER}; |
| + for (int i = 0; i < arraysize(hives); ++i) { |
| + RegKey client_state(hives[i], kChromeRegClientStateKey, KEY_SET_VALUE); |
| + ASSERT_TRUE(client_state.Valid()); |
| + client_state.DeleteValue(google_update::kRegLastRunTimeField); |
| + } |
| + } |
| + |
| + bool SetLastRunTime(HKEY hive, int64 last_run_time) { |
| + RegKey client_state(hive, kChromeRegClientStateKey, KEY_SET_VALUE); |
| + 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
|
| + client_state.WriteValue( |
| + google_update::kRegLastRunTimeField, |
| + base::Int64ToString16(last_run_time).c_str()) == ERROR_SUCCESS); |
| + } |
| + |
| + 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.
|
| +}; |
| + |
| +registry_util::RegistryOverrideManager GCAPILastRunTest::override_manager_; |
| + |
| +TEST_F(GCAPILastRunTest, Basic_HKCU) { |
| + Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(10); |
| + EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, last_run.ToInternalValue())); |
| + |
| + int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| + EXPECT_EQ(10, days_since_last_run); |
| +} |
| + |
| +TEST_F(GCAPILastRunTest, Basic_HKLM) { |
| + Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(10); |
| + EXPECT_TRUE(SetLastRunTime(HKEY_LOCAL_MACHINE, last_run.ToInternalValue())); |
| + |
| + int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| + EXPECT_EQ(10, days_since_last_run); |
| +} |
| + |
| +TEST_F(GCAPILastRunTest, HKLM_and_HKCU) { |
| + Time hklm_last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(30); |
| + EXPECT_TRUE(SetLastRunTime(HKEY_LOCAL_MACHINE, |
| + hklm_last_run.ToInternalValue())); |
| + |
| + Time hkcu_last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(20); |
| + EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, |
| + hkcu_last_run.ToInternalValue())); |
| + |
| + int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| + EXPECT_EQ(20, days_since_last_run); |
| +} |
| + |
| +TEST_F(GCAPILastRunTest, NoLastRun) { |
| + int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| + EXPECT_EQ(-1, days_since_last_run); |
| +} |
|
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.
|
| Property changes on: chrome\installer\gcapi\gcapi_last_run_test.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |