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