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.
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/time/time.h"
9 #include "components/autofill/core/browser/webdata/autofill_entry.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 const unsigned int kMaxAutofillTimeStamps = 2;
16 TEST(AutofillEntryTest, NoCulling) {
17 std::vector<base::Time> source, result;
18 base::Time current = base::Time::Now();
19 for (size_t i = 0; i < kMaxAutofillTimeStamps; ++i)
20 source.push_back(current);
22 EXPECT_FALSE(AutofillEntry::CullTimeStamps(source, &result));
23 EXPECT_EQ(result.size(), kMaxAutofillTimeStamps);
24 for (std::vector<base::Time>::const_iterator it = result.begin();
25 it != result.end(); ++it) {
26 EXPECT_EQ(*it, current);
30 TEST(AutofillEntryTest, Culling) {
31 std::vector<base::Time> source, result;
32 base::Time current = base::Time::Now();
33 const int offset = 10000;
35 int64 internal_value = current.ToInternalValue();
36 for (size_t i = 0; i < kMaxAutofillTimeStamps * 2 ; ++i) {
37 source.push_back(base::Time::FromInternalValue(
38 internal_value + i * offset));
40 std::sort(source.begin(), source.end());
41 EXPECT_TRUE(AutofillEntry::CullTimeStamps(source, &result));
43 EXPECT_EQ(result.size(), kMaxAutofillTimeStamps);
44 EXPECT_EQ(result.front(), base::Time::FromInternalValue(internal_value));
45 int last_offset = (kMaxAutofillTimeStamps * 2 - 1) * offset;
46 EXPECT_EQ(result.back(),
47 base::Time::FromInternalValue(last_offset + internal_value));
50 TEST(AutofillEntryTest, CullByTime) {
51 base::TimeDelta one_hour = base::TimeDelta::FromHours(1);
53 std::vector<base::Time> timestamps;
54 base::Time cutoff_time = AutofillEntry::ExpirationTime();
56 // Within the time limit.
57 timestamps.push_back(cutoff_time + one_hour);
59 AutofillKey key(UTF8ToUTF16("test_key"), UTF8ToUTF16("test_value"));
61 AutofillEntry entry_within_the_limits(key, timestamps);
62 EXPECT_FALSE(entry_within_the_limits.IsExpired());
64 // One within the time limit, one outside.
65 timestamps.push_back(cutoff_time - one_hour);
67 AutofillEntry entry_partially_within_the_limits(key, timestamps);
69 entry_partially_within_the_limits.IsExpired());
71 // All outside the time limit.
73 timestamps.push_back(cutoff_time - one_hour);
74 timestamps.push_back(cutoff_time - one_hour * 2);
75 timestamps.push_back(cutoff_time - one_hour * 3);
77 AutofillEntry entry_outside_the_limits(key, timestamps);
78 EXPECT_TRUE(entry_outside_the_limits.IsExpired());
79 EXPECT_TRUE(entry_outside_the_limits.timestamps_culled());
82 } // namespace autofill