summaryrefslogtreecommitdiffstats
path: root/chrome/browser/webdata/autofill_entry_unittest.cc
blob: 66a6178e8fe4dc57906611ea889dc922c8521eef (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2012 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 <algorithm>

#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "chrome/browser/webdata/autofill_entry.h"

const unsigned int kMaxAutofillTimeStamps = 2;

TEST(AutofillEntryTest, NoCulling) {
  std::vector<base::Time> source, result;
  base::Time current = base::Time::Now();
  for (size_t i = 0; i < kMaxAutofillTimeStamps; ++i)
    source.push_back(current);

  EXPECT_FALSE(AutofillEntry::CullTimeStamps(source, &result));
  EXPECT_EQ(result.size(), kMaxAutofillTimeStamps);
  for (std::vector<base::Time>::const_iterator it = result.begin();
       it != result.end(); ++it) {
    EXPECT_EQ(*it, current);
  }
}

TEST(AutofillEntryTest, Culling) {
  std::vector<base::Time> source, result;
  base::Time current = base::Time::Now();
  const int offset = 10000;

  int64 internal_value = current.ToInternalValue();
  for (size_t i = 0; i < kMaxAutofillTimeStamps * 2 ; ++i) {
    source.push_back(base::Time::FromInternalValue(
        internal_value + i * offset));
  }
  std::sort(source.begin(), source.end());
  EXPECT_TRUE(AutofillEntry::CullTimeStamps(source, &result));

  EXPECT_EQ(result.size(), kMaxAutofillTimeStamps);
  EXPECT_EQ(result.front(), base::Time::FromInternalValue(internal_value));
  int last_offset = (kMaxAutofillTimeStamps * 2 - 1) * offset;
  EXPECT_EQ(result.back(),
            base::Time::FromInternalValue(last_offset + internal_value));
}

TEST(AutofillEntryTest, CullByTime) {
  base::TimeDelta one_hour = base::TimeDelta::FromHours(1);

  std::vector<base::Time> timestamps;
  base::Time cutoff_time = AutofillEntry::ExpirationTime();

  // Within the time limit.
  timestamps.push_back(cutoff_time + one_hour);

  AutofillKey key(UTF8ToUTF16("test_key"), UTF8ToUTF16("test_value"));

  AutofillEntry entry_within_the_limits(key, timestamps);
  EXPECT_FALSE(entry_within_the_limits.IsExpired());

  // One within the time limit, one outside.
  timestamps.push_back(cutoff_time - one_hour);

  AutofillEntry entry_partially_within_the_limits(key, timestamps);
  EXPECT_TRUE(
      entry_partially_within_the_limits.IsExpired());

  // All outside the time limit.
  timestamps.clear();
  timestamps.push_back(cutoff_time - one_hour);
  timestamps.push_back(cutoff_time - one_hour * 2);
  timestamps.push_back(cutoff_time - one_hour * 3);

  AutofillEntry entry_outside_the_limits(key, timestamps);
  EXPECT_TRUE(entry_outside_the_limits.IsExpired());
  EXPECT_TRUE(entry_outside_the_limits.timestamps_culled());
}