Chromium Code Reviews| 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; |
|
Ilya Sherman
2012/03/15 21:00:41
nit: Please add a linebreak between this constant
GeorgeY
2012/03/17 00:36:16
Done.
| |
| 12 TEST(AutofillEntryTest, NoCulling) { | 13 TEST(AutofillEntryTest, NoCulling) { |
| 13 std::vector<base::Time> source, result; | 14 std::vector<base::Time> source, result; |
| 14 base::Time current = base::Time::Now(); | 15 base::Time current = base::Time::Now(); |
| 15 for (size_t i = 0; i < kMaxAutofillTimeStamps -1 ; ++i) { | 16 for (size_t i = 0; i < kMaxAutofillTimeStamps; ++i) |
| 16 source.push_back(current); | 17 source.push_back(current); |
| 17 } | |
| 18 | 18 |
| 19 EXPECT_FALSE(AutofillEntry::CullTimeStamps(source, &result)); | 19 EXPECT_FALSE(AutofillEntry::CullTimeStamps(source, &result)); |
| 20 EXPECT_EQ(result.size(), kMaxAutofillTimeStamps -1); | 20 EXPECT_EQ(result.size(), kMaxAutofillTimeStamps); |
| 21 for (std::vector<base::Time>::const_iterator it = result.begin(); | 21 for (std::vector<base::Time>::const_iterator it = result.begin(); |
| 22 it != result.end(); ++it) { | 22 it != result.end(); ++it) { |
| 23 EXPECT_EQ(*it, current); | 23 EXPECT_EQ(*it, current); |
| 24 } | 24 } |
| 25 } | 25 } |
| 26 | 26 |
| 27 TEST(AutofillEntryTest, Culling) { | 27 TEST(AutofillEntryTest, Culling) { |
| 28 std::vector<base::Time> source, result; | 28 std::vector<base::Time> source, result; |
| 29 base::Time current = base::Time::Now(); | 29 base::Time current = base::Time::Now(); |
| 30 const int offset = 10000; | 30 const int offset = 10000; |
| 31 | 31 |
| 32 int64 internal_value = current.ToInternalValue(); | 32 int64 internal_value = current.ToInternalValue(); |
| 33 for (size_t i = 0; i < kMaxAutofillTimeStamps * 2 ; ++i) { | 33 for (size_t i = 0; i < kMaxAutofillTimeStamps * 2 ; ++i) { |
| 34 source.push_back(base::Time::FromInternalValue( | 34 source.push_back(base::Time::FromInternalValue( |
| 35 internal_value + i * offset)); | 35 internal_value + i * offset)); |
| 36 } | 36 } |
| 37 std::sort(source.begin(), source.end()); | 37 std::sort(source.begin(), source.end()); |
| 38 EXPECT_TRUE(AutofillEntry::CullTimeStamps(source, &result)); | 38 EXPECT_TRUE(AutofillEntry::CullTimeStamps(source, &result)); |
| 39 | 39 |
| 40 EXPECT_EQ(result.size(), kMaxAutofillTimeStamps); | 40 EXPECT_EQ(result.size(), kMaxAutofillTimeStamps); |
| 41 int count = kMaxAutofillTimeStamps * 2 - 1; | 41 EXPECT_EQ(result.front(), base::Time::FromInternalValue(internal_value)); |
| 42 for (std::vector<base::Time>::const_iterator it = result.begin(); | 42 int last_offset = (kMaxAutofillTimeStamps * 2 - 1) * offset; |
| 43 it != result.end(); ++it) { | 43 EXPECT_EQ(result.back(), |
| 44 EXPECT_EQ(*it, base::Time::FromInternalValue( | 44 base::Time::FromInternalValue(last_offset + internal_value)); |
| 45 count*offset + internal_value)); | |
| 46 --count; | |
| 47 } | |
| 48 } | 45 } |
| 46 | |
| 47 TEST(AutofillEntryTest, CullByTime) { | |
| 48 base::Time current = base::Time::Now(); | |
| 49 base::TimeDelta one_day = base::TimeDelta::FromDays(1); | |
| 50 base::TimeDelta one_hour = base::TimeDelta::FromHours(1); | |
| 51 | |
| 52 std::vector<base::Time> timestamps; | |
| 53 base::Time cutoff_time = current - one_day; | |
| 54 | |
| 55 // Within the time limit. | |
| 56 timestamps.push_back(cutoff_time + one_hour); | |
| 57 | |
| 58 AutofillKey key(UTF8ToUTF16("test_key"), UTF8ToUTF16("test_value")); | |
| 59 | |
| 60 AutofillEntry entry_within_the_limits(key, timestamps); | |
| 61 EXPECT_FALSE(entry_within_the_limits.LastAccessOlder(cutoff_time)); | |
| 62 | |
| 63 // One within the time limit, one outside. | |
| 64 timestamps.push_back(cutoff_time - one_hour); | |
| 65 | |
| 66 AutofillEntry entry_partially_within_the_limits(key, timestamps); | |
| 67 EXPECT_TRUE( | |
| 68 entry_partially_within_the_limits.LastAccessOlder(cutoff_time)); | |
| 69 | |
| 70 // All outside the time limit. | |
| 71 timestamps.clear(); | |
| 72 timestamps.push_back(cutoff_time - one_hour); | |
| 73 timestamps.push_back(cutoff_time - one_hour * 2); | |
| 74 timestamps.push_back(cutoff_time - one_hour * 3); | |
| 75 | |
| 76 AutofillEntry entry_outside_the_limits(key, timestamps); | |
| 77 EXPECT_TRUE(entry_outside_the_limits.LastAccessOlder(cutoff_time)); | |
| 78 EXPECT_TRUE(entry_outside_the_limits.timestamps_culled()); | |
| 79 | |
| 80 // Timestamp is on the edge. | |
|
GeorgeY
2012/03/17 00:36:16
This is removed, as it is going to be flaky with t
| |
| 81 timestamps.clear(); | |
| 82 timestamps.push_back(cutoff_time); | |
| 83 AutofillEntry entry_on_the_edge(key, timestamps); | |
| 84 EXPECT_FALSE(entry_on_the_edge.LastAccessOlder(cutoff_time)); | |
| 85 EXPECT_FALSE(entry_on_the_edge.timestamps_culled()); | |
| 86 EXPECT_EQ(1U, entry_on_the_edge.timestamps().size()); | |
| 87 } | |
| OLD | NEW |