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. 4 5 #include <algorithm> 6 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" 11 12 namespace autofill { 13 14 const unsigned int kMaxAutofillTimeStamps = 2; 15 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); 21 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); 27 } 28 } 29 30 TEST(AutofillEntryTest, Culling) { 31 std::vector<base::Time> source, result; 32 base::Time current = base::Time::Now(); 33 const int offset = 10000; 34 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)); 39 } 40 std::sort(source.begin(), source.end()); 41 EXPECT_TRUE(AutofillEntry::CullTimeStamps(source, &result)); 42 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)); 48 } 49 50 TEST(AutofillEntryTest, CullByTime) { 51 base::TimeDelta one_hour = base::TimeDelta::FromHours(1); 52 53 std::vector<base::Time> timestamps; 54 base::Time cutoff_time = AutofillEntry::ExpirationTime(); 55 56 // Within the time limit. 57 timestamps.push_back(cutoff_time + one_hour); 58 59 AutofillKey key(UTF8ToUTF16("test_key"), UTF8ToUTF16("test_value")); 60 61 AutofillEntry entry_within_the_limits(key, timestamps); 62 EXPECT_FALSE(entry_within_the_limits.IsExpired()); 63 64 // One within the time limit, one outside. 65 timestamps.push_back(cutoff_time - one_hour); 66 67 AutofillEntry entry_partially_within_the_limits(key, timestamps); 68 EXPECT_TRUE( 69 entry_partially_within_the_limits.IsExpired()); 70 71 // All outside the time limit. 72 timestamps.clear(); 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); 76 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()); 80 } 81 82 } // namespace autofill 83