summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/install_time_util_unittest.mm
blob: e5cd15985c0aa437aebd19dcf24b1517e80807c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2013 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 "ios/chrome/browser/install_time_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"

TEST(InstallTimeUtilTest, ComputeInstallationTime) {
  const base::Time null_time = base::Time();
  const base::Time now = base::Time::Now();
  const base::Time one_month_ago = now - base::TimeDelta::FromDays(30);
  const base::Time sentinel =
      base::Time::FromTimeT(install_time_util::kUnknownInstallDate);

  base::Time install_time;
  base::TimeDelta delta_from_now;

  // Case 1: On first run, always set the install time to Now.
  install_time =
      install_time_util::ComputeInstallationTimeInternal(true, null_time);
  delta_from_now = install_time - now;
  EXPECT_FALSE(install_time.is_null());
  EXPECT_TRUE(delta_from_now.InSeconds() < 100);

  // Case 2: First run, but there was already an install time in NSUserDefaults.
  // Ignore the NSUserDefaults time and return Now.
  install_time =
      install_time_util::ComputeInstallationTimeInternal(true, one_month_ago);
  delta_from_now = install_time - now;
  EXPECT_FALSE(install_time.is_null());
  EXPECT_TRUE(delta_from_now.InSeconds() < 100);

  // Case 3: Not first run, and NSUserDefaults didn't have an install time.
  // Should return the sentinel value.
  install_time =
      install_time_util::ComputeInstallationTimeInternal(false, null_time);
  EXPECT_FALSE(install_time.is_null());
  EXPECT_EQ(sentinel, install_time);

  // Case 4: Not first run, and NSUserDefaults had an install time.  Should
  // migrate that to LocalState.
  install_time =
      install_time_util::ComputeInstallationTimeInternal(false, one_month_ago);
  EXPECT_FALSE(install_time.is_null());
  EXPECT_EQ(one_month_ago, install_time);
}