Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2871)

Unified Diff: chrome/installer/gcapi/gcapi_last_run_test.cc

Issue 8508060: Add functionality to the GCAPI to detect when Chrome was last run. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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,130 @@
+// 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 <limits>
+#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}";
+const wchar_t kChromeRegClientStateMediumKey[] =
+ L"Software\\Google\\Update\\ClientStateMedium\\"
+ L"{8A69D345-D564-463c-AFF1-A69D9E530F96}";
+}
+
+class GCAPILastRunTest : public ::testing::Test {
+ protected:
+ void SetUp() {
+ std::wstring hklm_override = base::StringPrintf(
+ L"hklm_override\\%ls", ASCIIToWide(guid::GenerateGUID()));
+ override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, hklm_override);
+
+ std::wstring hkcu_override = base::StringPrintf(
+ L"hkcu_override\\%ls", ASCIIToWide(guid::GenerateGUID()));
+ override_manager_.OverrideRegistry(HKEY_CURRENT_USER, hkcu_override);
+
+ // Create the client state keys in the right places.
+ struct {
+ HKEY hive;
+ const wchar_t* path;
+ } reg_data[] = {
+ { HKEY_LOCAL_MACHINE, kChromeRegClientStateMediumKey },
+ { HKEY_CURRENT_USER, kChromeRegClientStateKey }
+ };
+ for (int i = 0; i < arraysize(reg_data); ++i) {
+ RegKey client_state(reg_data[i].hive,
+ reg_data[i].path,
+ KEY_CREATE_SUB_KEY);
+ ASSERT_TRUE(client_state.Valid());
+ }
+ }
+
+ void TearDown() {
grt (UTC plus 2) 2011/11/14 14:24:09 remove this since override_manager_'s dtor will do
robertshield 2011/11/14 15:13:49 Done.
+ override_manager_.RemoveAllOverrides();
+ }
+
+ bool SetLastRunTime(HKEY hive, int64 last_run_time) {
+ return SetLastRunTimeString(hive, base::Int64ToString16(last_run_time));
+ }
+
+ bool SetLastRunTimeString(HKEY hive, const string16& last_run_time_string) {
+ const wchar_t* path =
+ (hive == HKEY_LOCAL_MACHINE) ? kChromeRegClientStateMediumKey :
+ kChromeRegClientStateKey;
+
+ RegKey client_state(hive, path, KEY_SET_VALUE);
+ return (client_state.Valid() &&
+ client_state.WriteValue(
+ google_update::kRegLastRunTimeField,
+ last_run_time_string.c_str()) == ERROR_SUCCESS);
+ }
+
+ private:
+ registry_util::RegistryOverrideManager 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);
+}
+
+TEST_F(GCAPILastRunTest, InvalidLastRun) {
+ EXPECT_TRUE(SetLastRunTimeString(HKEY_CURRENT_USER, L"Hi Mum!"));
+ int days_since_last_run = GoogleChromeDaysSinceLastRun();
+ EXPECT_EQ(-1, days_since_last_run);
+}
+
+TEST_F(GCAPILastRunTest, OutOfRangeLastRun) {
+ Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(-42);
+ EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, last_run.ToInternalValue()));
+
+ int days_since_last_run = GoogleChromeDaysSinceLastRun();
+ EXPECT_EQ(-1, days_since_last_run);
+}
Property changes on: chrome\installer\gcapi\gcapi_last_run_test.cc
___________________________________________________________________
Added: svn:eol-style
+ LF

Powered by Google App Engine
This is Rietveld 408576698