1 // Copyright 2013 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.
5 #include "ios/chrome/browser/install_time_util.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "testing/gtest_mac.h"
9 TEST(InstallTimeUtilTest, ComputeInstallationTime) {
10 const base::Time null_time = base::Time();
11 const base::Time now = base::Time::Now();
12 const base::Time one_month_ago = now - base::TimeDelta::FromDays(30);
13 const base::Time sentinel =
14 base::Time::FromTimeT(install_time_util::kUnknownInstallDate);
16 base::Time install_time;
17 base::TimeDelta delta_from_now;
19 // Case 1: On first run, always set the install time to Now.
21 install_time_util::ComputeInstallationTimeInternal(true, null_time);
22 delta_from_now = install_time - now;
23 EXPECT_FALSE(install_time.is_null());
24 EXPECT_TRUE(delta_from_now.InSeconds() < 100);
26 // Case 2: First run, but there was already an install time in NSUserDefaults.
27 // Ignore the NSUserDefaults time and return Now.
29 install_time_util::ComputeInstallationTimeInternal(true, one_month_ago);
30 delta_from_now = install_time - now;
31 EXPECT_FALSE(install_time.is_null());
32 EXPECT_TRUE(delta_from_now.InSeconds() < 100);
34 // Case 3: Not first run, and NSUserDefaults didn't have an install time.
35 // Should return the sentinel value.
37 install_time_util::ComputeInstallationTimeInternal(false, null_time);
38 EXPECT_FALSE(install_time.is_null());
39 EXPECT_EQ(sentinel, install_time);
41 // Case 4: Not first run, and NSUserDefaults had an install time. Should
42 // migrate that to LocalState.
44 install_time_util::ComputeInstallationTimeInternal(false, one_month_ago);
45 EXPECT_FALSE(install_time.is_null());
46 EXPECT_EQ(one_month_ago, install_time);