| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/time.h" | 7 #include "base/time.h" |
| 8 #include "base/utf_string_conversions.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "chrome/browser/webdata/autofill_entry.h" | 10 #include "chrome/browser/webdata/autofill_entry.h" |
| 10 | 11 |
| 11 extern const unsigned int kMaxAutofillTimeStamps; | 12 const unsigned int kMaxAutofillTimeStamps = 2; |
| 13 |
| 12 TEST(AutofillEntryTest, NoCulling) { | 14 TEST(AutofillEntryTest, NoCulling) { |
| 13 std::vector<base::Time> source, result; | 15 std::vector<base::Time> source, result; |
| 14 base::Time current = base::Time::Now(); | 16 base::Time current = base::Time::Now(); |
| 15 for (size_t i = 0; i < kMaxAutofillTimeStamps -1 ; ++i) { | 17 for (size_t i = 0; i < kMaxAutofillTimeStamps; ++i) |
| 16 source.push_back(current); | 18 source.push_back(current); |
| 17 } | |
| 18 | 19 |
| 19 EXPECT_FALSE(AutofillEntry::CullTimeStamps(source, &result)); | 20 EXPECT_FALSE(AutofillEntry::CullTimeStamps(source, &result)); |
| 20 EXPECT_EQ(result.size(), kMaxAutofillTimeStamps -1); | 21 EXPECT_EQ(result.size(), kMaxAutofillTimeStamps); |
| 21 for (std::vector<base::Time>::const_iterator it = result.begin(); | 22 for (std::vector<base::Time>::const_iterator it = result.begin(); |
| 22 it != result.end(); ++it) { | 23 it != result.end(); ++it) { |
| 23 EXPECT_EQ(*it, current); | 24 EXPECT_EQ(*it, current); |
| 24 } | 25 } |
| 25 } | 26 } |
| 26 | 27 |
| 27 TEST(AutofillEntryTest, Culling) { | 28 TEST(AutofillEntryTest, Culling) { |
| 28 std::vector<base::Time> source, result; | 29 std::vector<base::Time> source, result; |
| 29 base::Time current = base::Time::Now(); | 30 base::Time current = base::Time::Now(); |
| 30 const int offset = 10000; | 31 const int offset = 10000; |
| 31 | 32 |
| 32 int64 internal_value = current.ToInternalValue(); | 33 int64 internal_value = current.ToInternalValue(); |
| 33 for (size_t i = 0; i < kMaxAutofillTimeStamps * 2 ; ++i) { | 34 for (size_t i = 0; i < kMaxAutofillTimeStamps * 2 ; ++i) { |
| 34 source.push_back(base::Time::FromInternalValue( | 35 source.push_back(base::Time::FromInternalValue( |
| 35 internal_value + i * offset)); | 36 internal_value + i * offset)); |
| 36 } | 37 } |
| 37 std::sort(source.begin(), source.end()); | 38 std::sort(source.begin(), source.end()); |
| 38 EXPECT_TRUE(AutofillEntry::CullTimeStamps(source, &result)); | 39 EXPECT_TRUE(AutofillEntry::CullTimeStamps(source, &result)); |
| 39 | 40 |
| 40 EXPECT_EQ(result.size(), kMaxAutofillTimeStamps); | 41 EXPECT_EQ(result.size(), kMaxAutofillTimeStamps); |
| 41 int count = kMaxAutofillTimeStamps * 2 - 1; | 42 EXPECT_EQ(result.front(), base::Time::FromInternalValue(internal_value)); |
| 42 for (std::vector<base::Time>::const_iterator it = result.begin(); | 43 int last_offset = (kMaxAutofillTimeStamps * 2 - 1) * offset; |
| 43 it != result.end(); ++it) { | 44 EXPECT_EQ(result.back(), |
| 44 EXPECT_EQ(*it, base::Time::FromInternalValue( | 45 base::Time::FromInternalValue(last_offset + internal_value)); |
| 45 count*offset + internal_value)); | |
| 46 --count; | |
| 47 } | |
| 48 } | 46 } |
| 47 |
| 48 TEST(AutofillEntryTest, CullByTime) { |
| 49 base::TimeDelta one_hour = base::TimeDelta::FromHours(1); |
| 50 |
| 51 std::vector<base::Time> timestamps; |
| 52 base::Time cutoff_time = AutofillEntry::ExpirationTime(); |
| 53 |
| 54 // Within the time limit. |
| 55 timestamps.push_back(cutoff_time + one_hour); |
| 56 |
| 57 AutofillKey key(UTF8ToUTF16("test_key"), UTF8ToUTF16("test_value")); |
| 58 |
| 59 AutofillEntry entry_within_the_limits(key, timestamps); |
| 60 EXPECT_FALSE(entry_within_the_limits.IsExpired()); |
| 61 |
| 62 // One within the time limit, one outside. |
| 63 timestamps.push_back(cutoff_time - one_hour); |
| 64 |
| 65 AutofillEntry entry_partially_within_the_limits(key, timestamps); |
| 66 EXPECT_TRUE( |
| 67 entry_partially_within_the_limits.IsExpired()); |
| 68 |
| 69 // All outside the time limit. |
| 70 timestamps.clear(); |
| 71 timestamps.push_back(cutoff_time - one_hour); |
| 72 timestamps.push_back(cutoff_time - one_hour * 2); |
| 73 timestamps.push_back(cutoff_time - one_hour * 3); |
| 74 |
| 75 AutofillEntry entry_outside_the_limits(key, timestamps); |
| 76 EXPECT_TRUE(entry_outside_the_limits.IsExpired()); |
| 77 EXPECT_TRUE(entry_outside_the_limits.timestamps_culled()); |
| 78 } |
| OLD | NEW |