Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: chrome/browser/ui/history_ui_service_unittest.cc

Issue 2450453002: Refactor BrowsingHistoryHandler, create BrowsingHistoryService (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "chrome/browser/ui/history_ui_service.h"
6
7 #include <stdint.h>
8 #include <memory>
9
10 #include "base/macros.h"
11 #include "base/values.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "url/gurl.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(
29 TestResult* test_results,
30 int test_results_size,
31 std::vector<HistoryUiService::HistoryEntry>* results) {
32 for (int i = 0; i < test_results_size; ++i) {
33 HistoryUiService::HistoryEntry entry;
34 entry.time = baseline_time +
35 base::TimeDelta::FromHours(test_results[i].hour_offset);
36 entry.url = GURL(test_results[i].url);
37 entry.all_timestamps.insert(entry.time.ToInternalValue());
38 results->push_back(entry);
39 }
40 }
41
42 // Returns true if |result| matches the test data given by |correct_result|,
43 // otherwise returns false.
44 bool ResultEquals(
45 const HistoryUiService::HistoryEntry& result,
46 const TestResult& correct_result) {
47 base::Time correct_time =
48 baseline_time + base::TimeDelta::FromHours(correct_result.hour_offset);
49
50 return result.time == correct_time && result.url == GURL(correct_result.url);
51 }
52
53 } // namespace
54
55 class HistoryUiServiceTest : public ::testing::Test {
56 public:
57 HistoryUiServiceTest() {}
58 ~HistoryUiServiceTest() override {}
59 };
60
61 // Tests that the MergeDuplicateResults method correctly removes duplicate
62 // visits to the same URL on the same day.
63 // Fails on Android. http://crbug.com/2345
64 #if defined(OS_ANDROID)
65 #define MAYBE_MergeDuplicateResults DISABLED_MergeDuplicateResults
66 #else
67 #define MAYBE_MergeDuplicateResults MergeDuplicateResults
68 #endif
69 TEST_F(HistoryUiServiceTest, MAYBE_MergeDuplicateResults) {
70 {
71 // Basic test that duplicates on the same day are removed.
72 TestResult test_data[] = {
73 { "http://google.com", 0 },
74 { "http://google.de", 1 },
75 { "http://google.com", 2 },
76 { "http://google.com", 3 } // Most recent.
77 };
78 std::vector<HistoryUiService::HistoryEntry> results;
79 AddQueryResults(test_data, arraysize(test_data), &results);
80 HistoryUiService::MergeDuplicateResults(&results);
81
82 ASSERT_EQ(2U, results.size());
83 EXPECT_TRUE(ResultEquals(results[0], test_data[3]));
84 EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
85 }
86
87 {
88 // Test that a duplicate URL on the next day is not removed.
89 TestResult test_data[] = {
90 { "http://google.com", 0 },
91 { "http://google.com", 23 },
92 { "http://google.com", 24 }, // Most recent.
93 };
94 std::vector<HistoryUiService::HistoryEntry> results;
95 AddQueryResults(test_data, arraysize(test_data), &results);
96 HistoryUiService::MergeDuplicateResults(&results);
97
98 ASSERT_EQ(2U, results.size());
99 EXPECT_TRUE(ResultEquals(results[0], test_data[2]));
100 EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
101 }
102
103 {
104 // Test multiple duplicates across multiple days.
105 TestResult test_data[] = {
106 // First day.
107 { "http://google.de", 0 },
108 { "http://google.com", 1 },
109 { "http://google.de", 2 },
110 { "http://google.com", 3 },
111
112 // Second day.
113 { "http://google.de", 24 },
114 { "http://google.com", 25 },
115 { "http://google.de", 26 },
116 { "http://google.com", 27 }, // Most recent.
117 };
118 std::vector<HistoryUiService::HistoryEntry> results;
119 AddQueryResults(test_data, arraysize(test_data), &results);
120 HistoryUiService::MergeDuplicateResults(&results);
121
122 ASSERT_EQ(4U, results.size());
123 EXPECT_TRUE(ResultEquals(results[0], test_data[7]));
124 EXPECT_TRUE(ResultEquals(results[1], test_data[6]));
125 EXPECT_TRUE(ResultEquals(results[2], test_data[3]));
126 EXPECT_TRUE(ResultEquals(results[3], test_data[2]));
127 }
128
129 {
130 // Test that timestamps for duplicates are properly saved.
131 TestResult test_data[] = {
132 { "http://google.com", 0 },
133 { "http://google.de", 1 },
134 { "http://google.com", 2 },
135 { "http://google.com", 3 } // Most recent.
136 };
137 std::vector<HistoryUiService::HistoryEntry> results;
138 AddQueryResults(test_data, arraysize(test_data), &results);
139 HistoryUiService::MergeDuplicateResults(&results);
140
141 ASSERT_EQ(2U, results.size());
142 EXPECT_TRUE(ResultEquals(results[0], test_data[3]));
143 EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
144 EXPECT_EQ(3u, results[0].all_timestamps.size());
145 EXPECT_EQ(1u, results[1].all_timestamps.size());
146 }
147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698