OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "ios/chrome/browser/ui/history/history_util.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/time/time.h" |
| 12 #include "ios/chrome/browser/ui/history/history_entry.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 struct TestResult { |
| 18 std::string url; |
| 19 int64_t hour_offset; // Visit time in hours past the baseline time. |
| 20 }; |
| 21 |
| 22 // Duplicates on the same day in the local timezone are removed, so set a |
| 23 // baseline time in local time. |
| 24 const base::Time baseline_time = base::Time::UnixEpoch().LocalMidnight(); |
| 25 |
| 26 // For each item in |results|, create a new Value representing the visit, and |
| 27 // insert it into |list_value|. |
| 28 void AddQueryResults(TestResult* test_results, |
| 29 int test_results_size, |
| 30 std::vector<history::HistoryEntry>* results) { |
| 31 for (int i = 0; i < test_results_size; ++i) { |
| 32 history::HistoryEntry entry; |
| 33 entry.time = |
| 34 baseline_time + base::TimeDelta::FromHours(test_results[i].hour_offset); |
| 35 entry.url = GURL(test_results[i].url); |
| 36 entry.all_timestamps.insert(entry.time.ToInternalValue()); |
| 37 results->push_back(entry); |
| 38 } |
| 39 } |
| 40 |
| 41 // Returns true if |result| matches the test data given by |correct_result|, |
| 42 // otherwise returns false. |
| 43 bool ResultEquals(const history::HistoryEntry& result, |
| 44 const TestResult& correct_result) { |
| 45 base::Time correct_time = |
| 46 baseline_time + base::TimeDelta::FromHours(correct_result.hour_offset); |
| 47 |
| 48 return result.time == correct_time && result.url == GURL(correct_result.url); |
| 49 } |
| 50 |
| 51 } // namespace |
| 52 |
| 53 // Tests that the MergeDuplicateResults method correctly removes duplicate |
| 54 // visits to the same URL on the same day. |
| 55 TEST(HistoryUtilTest, MergeDuplicateResults) { |
| 56 { |
| 57 // Basic test that duplicates on the same day are removed. |
| 58 TestResult test_data[] = { |
| 59 {"http://testurl.com", 0}, |
| 60 {"http://testurl.de", 1}, |
| 61 {"http://testurl.com", 2}, |
| 62 {"http://testurl.com", 3} // Most recent. |
| 63 }; |
| 64 std::vector<history::HistoryEntry> results; |
| 65 AddQueryResults(test_data, arraysize(test_data), &results); |
| 66 history::MergeDuplicateHistoryEntries(&results); |
| 67 |
| 68 ASSERT_EQ(2U, results.size()); |
| 69 EXPECT_TRUE(ResultEquals(results[0], test_data[3])); |
| 70 EXPECT_TRUE(ResultEquals(results[1], test_data[1])); |
| 71 } |
| 72 |
| 73 { |
| 74 // Test that a duplicate URL on the next day is not removed. |
| 75 TestResult test_data[] = { |
| 76 {"http://testurl.com", 0}, |
| 77 {"http://testurl.com", 23}, |
| 78 {"http://testurl.com", 24}, // Most recent. |
| 79 }; |
| 80 std::vector<history::HistoryEntry> results; |
| 81 AddQueryResults(test_data, arraysize(test_data), &results); |
| 82 history::MergeDuplicateHistoryEntries(&results); |
| 83 |
| 84 ASSERT_EQ(2U, results.size()); |
| 85 EXPECT_TRUE(ResultEquals(results[0], test_data[2])); |
| 86 EXPECT_TRUE(ResultEquals(results[1], test_data[1])); |
| 87 } |
| 88 |
| 89 { |
| 90 // Test multiple duplicates across multiple days. |
| 91 TestResult test_data[] = { |
| 92 // First day. |
| 93 {"http://testurl.de", 0}, |
| 94 {"http://testurl.com", 1}, |
| 95 {"http://testurl.de", 2}, |
| 96 {"http://testurl.com", 3}, |
| 97 |
| 98 // Second day. |
| 99 {"http://testurl.de", 24}, |
| 100 {"http://testurl.com", 25}, |
| 101 {"http://testurl.de", 26}, |
| 102 {"http://testurl.com", 27}, // Most recent. |
| 103 }; |
| 104 std::vector<history::HistoryEntry> results; |
| 105 AddQueryResults(test_data, arraysize(test_data), &results); |
| 106 history::MergeDuplicateHistoryEntries(&results); |
| 107 |
| 108 ASSERT_EQ(4U, results.size()); |
| 109 EXPECT_TRUE(ResultEquals(results[0], test_data[7])); |
| 110 EXPECT_TRUE(ResultEquals(results[1], test_data[6])); |
| 111 EXPECT_TRUE(ResultEquals(results[2], test_data[3])); |
| 112 EXPECT_TRUE(ResultEquals(results[3], test_data[2])); |
| 113 } |
| 114 |
| 115 { |
| 116 // Test that timestamps for duplicates are properly saved. |
| 117 TestResult test_data[] = { |
| 118 {"http://testurl.com", 0}, |
| 119 {"http://testurl.de", 1}, |
| 120 {"http://testurl.com", 2}, |
| 121 {"http://testurl.com", 3} // Most recent. |
| 122 }; |
| 123 std::vector<history::HistoryEntry> results; |
| 124 AddQueryResults(test_data, arraysize(test_data), &results); |
| 125 history::MergeDuplicateHistoryEntries(&results); |
| 126 |
| 127 ASSERT_EQ(2U, results.size()); |
| 128 EXPECT_TRUE(ResultEquals(results[0], test_data[3])); |
| 129 EXPECT_TRUE(ResultEquals(results[1], test_data[1])); |
| 130 EXPECT_EQ(3u, results[0].all_timestamps.size()); |
| 131 EXPECT_EQ(1u, results[1].all_timestamps.size()); |
| 132 } |
| 133 } |
OLD | NEW |