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

Side by Side Diff: chrome/browser/ui/webui/browsing_history_handler_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
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/ui/webui/browsing_history_handler.h" 5 #include "chrome/browser/ui/webui/browsing_history_handler.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <memory> 8 #include <memory>
9 #include <set> 9 #include <set>
10 #include <utility>
tsergeant 2016/11/03 05:45:44 I might be wrong, but I don't see anything which n
10 11
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
13 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h" 15 #include "base/values.h"
15 #include "chrome/browser/history/web_history_service_factory.h" 16 #include "chrome/browser/history/web_history_service_factory.h"
16 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" 17 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
17 #include "chrome/browser/signin/fake_signin_manager_builder.h" 18 #include "chrome/browser/signin/fake_signin_manager_builder.h"
18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
19 #include "chrome/browser/signin/signin_manager_factory.h" 20 #include "chrome/browser/signin/signin_manager_factory.h"
20 #include "chrome/browser/sync/profile_sync_service_factory.h" 21 #include "chrome/browser/sync/profile_sync_service_factory.h"
21 #include "chrome/browser/sync/profile_sync_test_util.h" 22 #include "chrome/browser/sync/profile_sync_test_util.h"
23 #include "chrome/browser/ui/history_ui_service.h"
22 #include "chrome/test/base/testing_profile.h" 24 #include "chrome/test/base/testing_profile.h"
23 #include "components/browser_sync/test_profile_sync_service.h" 25 #include "components/browser_sync/test_profile_sync_service.h"
24 #include "components/history/core/test/fake_web_history_service.h" 26 #include "components/history/core/test/fake_web_history_service.h"
25 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" 27 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
26 #include "components/signin/core/browser/fake_signin_manager.h" 28 #include "components/signin/core/browser/fake_signin_manager.h"
27 #include "components/sync/base/model_type.h" 29 #include "components/sync/base/model_type.h"
28 #include "content/public/browser/browser_thread.h" 30 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/web_contents.h" 31 #include "content/public/browser/web_contents.h"
30 #include "content/public/test/test_browser_thread_bundle.h" 32 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "content/public/test/test_web_ui.h" 33 #include "content/public/test/test_web_ui.h"
32 #include "net/http/http_status_code.h" 34 #include "net/http/http_status_code.h"
33 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
34 #include "url/gurl.h" 36 #include "url/gurl.h"
35 37
36 #if !defined(OS_ANDROID) 38 #if !defined(OS_ANDROID)
37 #include "chrome/browser/ui/webui/md_history_ui.h" 39 #include "chrome/browser/ui/webui/md_history_ui.h"
38 #endif 40 #endif
39 41
40 namespace { 42 namespace {
41 43
42 struct TestResult {
43 std::string url;
44 int64_t hour_offset; // Visit time in hours past the baseline time.
45 };
46
47 // Duplicates on the same day in the local timezone are removed, so set a
48 // baseline time in local time.
49 const base::Time baseline_time = base::Time::UnixEpoch().LocalMidnight();
50
51 // For each item in |results|, create a new Value representing the visit, and
52 // insert it into |list_value|.
53 void AddQueryResults(
54 TestResult* test_results,
55 int test_results_size,
56 std::vector<BrowsingHistoryHandler::HistoryEntry>* results) {
57 for (int i = 0; i < test_results_size; ++i) {
58 BrowsingHistoryHandler::HistoryEntry entry;
59 entry.time = baseline_time +
60 base::TimeDelta::FromHours(test_results[i].hour_offset);
61 entry.url = GURL(test_results[i].url);
62 entry.all_timestamps.insert(entry.time.ToInternalValue());
63 results->push_back(entry);
64 }
65 }
66
67 // Returns true if |result| matches the test data given by |correct_result|,
68 // otherwise returns false.
69 bool ResultEquals(
70 const BrowsingHistoryHandler::HistoryEntry& result,
71 const TestResult& correct_result) {
72 base::Time correct_time =
73 baseline_time + base::TimeDelta::FromHours(correct_result.hour_offset);
74
75 return result.time == correct_time && result.url == GURL(correct_result.url);
76 }
77
78 void IgnoreBoolAndDoNothing(bool ignored_argument) {} 44 void IgnoreBoolAndDoNothing(bool ignored_argument) {}
79 45
80 class TestSyncService : public browser_sync::TestProfileSyncService { 46 class TestSyncService : public browser_sync::TestProfileSyncService {
81 public: 47 public:
82 explicit TestSyncService(Profile* profile) 48 explicit TestSyncService(Profile* profile)
83 : browser_sync::TestProfileSyncService( 49 : browser_sync::TestProfileSyncService(
84 CreateProfileSyncServiceParamsForTest(profile)), 50 CreateProfileSyncServiceParamsForTest(profile)),
85 sync_active_(true) {} 51 sync_active_(true) {}
86 52
87 bool IsSyncActive() const override { return sync_active_; } 53 bool IsSyncActive() const override { return sync_active_; }
(...skipping 10 matching lines...) Expand all
98 private: 64 private:
99 bool sync_active_; 65 bool sync_active_;
100 }; 66 };
101 67
102 class BrowsingHistoryHandlerWithWebUIForTesting 68 class BrowsingHistoryHandlerWithWebUIForTesting
103 : public BrowsingHistoryHandler { 69 : public BrowsingHistoryHandler {
104 public: 70 public:
105 explicit BrowsingHistoryHandlerWithWebUIForTesting(content::WebUI* web_ui) { 71 explicit BrowsingHistoryHandlerWithWebUIForTesting(content::WebUI* web_ui) {
106 set_web_ui(web_ui); 72 set_web_ui(web_ui);
107 } 73 }
108 using BrowsingHistoryHandler::QueryComplete;
109 }; 74 };
110 75
111 } // namespace 76 } // namespace
112 77
113 class BrowsingHistoryHandlerTest : public ::testing::Test { 78 class BrowsingHistoryHandlerTest : public ::testing::Test {
114 public: 79 public:
115 void SetUp() override { 80 void SetUp() override {
116 TestingProfile::Builder builder; 81 TestingProfile::Builder builder;
117 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), 82 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
118 &BuildFakeProfileOAuth2TokenService); 83 &BuildFakeProfileOAuth2TokenService);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 static std::unique_ptr<KeyedService> BuildFakeWebHistoryService( 124 static std::unique_ptr<KeyedService> BuildFakeWebHistoryService(
160 content::BrowserContext* context) { 125 content::BrowserContext* context) {
161 Profile* profile = static_cast<TestingProfile*>(context); 126 Profile* profile = static_cast<TestingProfile*>(context);
162 127
163 std::unique_ptr<history::FakeWebHistoryService> service = 128 std::unique_ptr<history::FakeWebHistoryService> service =
164 base::MakeUnique<history::FakeWebHistoryService>( 129 base::MakeUnique<history::FakeWebHistoryService>(
165 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), 130 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
166 SigninManagerFactory::GetForProfile(profile), 131 SigninManagerFactory::GetForProfile(profile),
167 profile->GetRequestContext()); 132 profile->GetRequestContext());
168 service->SetupFakeResponse(true /* success */, net::HTTP_OK); 133 service->SetupFakeResponse(true /* success */, net::HTTP_OK);
169 return std::move(service); 134 return std::move(service);
Dan Beam 2016/11/03 18:04:17 ^ this uses <utility>
170 } 135 }
171 136
172 content::TestBrowserThreadBundle thread_bundle_; 137 content::TestBrowserThreadBundle thread_bundle_;
173 std::unique_ptr<TestingProfile> profile_; 138 std::unique_ptr<TestingProfile> profile_;
174 TestSyncService* sync_service_; 139 TestSyncService* sync_service_;
175 history::FakeWebHistoryService* web_history_service_; 140 history::FakeWebHistoryService* web_history_service_;
176 std::unique_ptr<content::TestWebUI> web_ui_; 141 std::unique_ptr<content::TestWebUI> web_ui_;
177 std::unique_ptr<content::WebContents> web_contents_; 142 std::unique_ptr<content::WebContents> web_contents_;
178 }; 143 };
179 144
180 // Tests that the MergeDuplicateResults method correctly removes duplicate 145 // Tests that BrowsingHistoryHandler is informed about WebHistoryService
181 // visits to the same URL on the same day. 146 // deletions.
182 // Fails on Android. http://crbug.com/2345
183 #if defined(OS_ANDROID)
184 #define MAYBE_MergeDuplicateResults DISABLED_MergeDuplicateResults
185 #else
186 #define MAYBE_MergeDuplicateResults MergeDuplicateResults
187 #endif
188 TEST_F(BrowsingHistoryHandlerTest, MAYBE_MergeDuplicateResults) {
189 {
190 // Basic test that duplicates on the same day are removed.
191 TestResult test_data[] = {
192 { "http://google.com", 0 },
193 { "http://google.de", 1 },
194 { "http://google.com", 2 },
195 { "http://google.com", 3 } // Most recent.
196 };
197 std::vector<BrowsingHistoryHandler::HistoryEntry> results;
198 AddQueryResults(test_data, arraysize(test_data), &results);
199 BrowsingHistoryHandler::MergeDuplicateResults(&results);
200
201 ASSERT_EQ(2U, results.size());
202 EXPECT_TRUE(ResultEquals(results[0], test_data[3]));
203 EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
204 }
205
206 {
207 // Test that a duplicate URL on the next day is not removed.
208 TestResult test_data[] = {
209 { "http://google.com", 0 },
210 { "http://google.com", 23 },
211 { "http://google.com", 24 }, // Most recent.
212 };
213 std::vector<BrowsingHistoryHandler::HistoryEntry> results;
214 AddQueryResults(test_data, arraysize(test_data), &results);
215 BrowsingHistoryHandler::MergeDuplicateResults(&results);
216
217 ASSERT_EQ(2U, results.size());
218 EXPECT_TRUE(ResultEquals(results[0], test_data[2]));
219 EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
220 }
221
222 {
223 // Test multiple duplicates across multiple days.
224 TestResult test_data[] = {
225 // First day.
226 { "http://google.de", 0 },
227 { "http://google.com", 1 },
228 { "http://google.de", 2 },
229 { "http://google.com", 3 },
230
231 // Second day.
232 { "http://google.de", 24 },
233 { "http://google.com", 25 },
234 { "http://google.de", 26 },
235 { "http://google.com", 27 }, // Most recent.
236 };
237 std::vector<BrowsingHistoryHandler::HistoryEntry> results;
238 AddQueryResults(test_data, arraysize(test_data), &results);
239 BrowsingHistoryHandler::MergeDuplicateResults(&results);
240
241 ASSERT_EQ(4U, results.size());
242 EXPECT_TRUE(ResultEquals(results[0], test_data[7]));
243 EXPECT_TRUE(ResultEquals(results[1], test_data[6]));
244 EXPECT_TRUE(ResultEquals(results[2], test_data[3]));
245 EXPECT_TRUE(ResultEquals(results[3], test_data[2]));
246 }
247
248 {
249 // Test that timestamps for duplicates are properly saved.
250 TestResult test_data[] = {
251 { "http://google.com", 0 },
252 { "http://google.de", 1 },
253 { "http://google.com", 2 },
254 { "http://google.com", 3 } // Most recent.
255 };
256 std::vector<BrowsingHistoryHandler::HistoryEntry> results;
257 AddQueryResults(test_data, arraysize(test_data), &results);
258 BrowsingHistoryHandler::MergeDuplicateResults(&results);
259
260 ASSERT_EQ(2U, results.size());
261 EXPECT_TRUE(ResultEquals(results[0], test_data[3]));
262 EXPECT_TRUE(ResultEquals(results[1], test_data[1]));
263 EXPECT_EQ(3u, results[0].all_timestamps.size());
264 EXPECT_EQ(1u, results[1].all_timestamps.size());
265 }
266 }
267
268 // Tests that BrowsingHistoryHandler observes WebHistoryService deletions.
269 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) { 147 TEST_F(BrowsingHistoryHandlerTest, ObservingWebHistoryDeletions) {
270 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing); 148 base::Callback<void(bool)> callback = base::Bind(&IgnoreBoolAndDoNothing);
271 149
272 // BrowsingHistoryHandler listens to WebHistoryService history deletions. 150 // BrowsingHistoryHandler is informed about WebHistoryService history
151 // deletions.
273 { 152 {
274 sync_service()->SetSyncActive(true); 153 sync_service()->SetSyncActive(true);
275 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); 154 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui());
276 handler.RegisterMessages(); 155 handler.RegisterMessages();
277 156
278 web_history_service()->ExpireHistoryBetween(std::set<GURL>(), base::Time(), 157 web_history_service()->ExpireHistoryBetween(std::set<GURL>(), base::Time(),
279 base::Time::Max(), callback); 158 base::Time::Max(), callback);
280 159
281 EXPECT_EQ(1U, web_ui()->call_data().size()); 160 EXPECT_EQ(1U, web_ui()->call_data().size());
282 EXPECT_EQ("historyDeleted", web_ui()->call_data().back()->function_name()); 161 EXPECT_EQ("historyDeleted", web_ui()->call_data().back()->function_name());
283 } 162 }
284 163
285 // BrowsingHistoryHandler will listen to WebHistoryService deletions even if 164 // BrowsingHistoryHandler will be informed about WebHistoryService deletions
286 // history sync is activated later. 165 // even if history sync is activated later.
287 { 166 {
288 sync_service()->SetSyncActive(false); 167 sync_service()->SetSyncActive(false);
289 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); 168 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui());
290 handler.RegisterMessages(); 169 handler.RegisterMessages();
291 sync_service()->SetSyncActive(true); 170 sync_service()->SetSyncActive(true);
292 171
293 web_history_service()->ExpireHistoryBetween(std::set<GURL>(), base::Time(), 172 web_history_service()->ExpireHistoryBetween(std::set<GURL>(), base::Time(),
294 base::Time::Max(), callback); 173 base::Time::Max(), callback);
295 174
296 EXPECT_EQ(2U, web_ui()->call_data().size()); 175 EXPECT_EQ(2U, web_ui()->call_data().size());
(...skipping 27 matching lines...) Expand all
324 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" 203 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
325 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" 204 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
326 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" 205 "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
327 "ngurlislong.com"), base::Time()); 206 "ngurlislong.com"), base::Time());
328 ASSERT_GT(long_result.url().spec().size(), 300u); 207 ASSERT_GT(long_result.url().spec().size(), 300u);
329 208
330 history::QueryResults results; 209 history::QueryResults results;
331 results.AppendURLBySwapping(&long_result); 210 results.AppendURLBySwapping(&long_result);
332 211
333 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui()); 212 BrowsingHistoryHandlerWithWebUIForTesting handler(web_ui());
213 handler.RegisterMessages(); // Needed to create HistoryUiService.
334 web_ui()->ClearTrackedCalls(); 214 web_ui()->ClearTrackedCalls();
335 215
336 handler.QueryComplete(base::string16(), history::QueryOptions(), &results); 216 handler.history_ui_service_->QueryComplete(
217 base::string16(), history::QueryOptions(), &results);
337 ASSERT_FALSE(web_ui()->call_data().empty()); 218 ASSERT_FALSE(web_ui()->call_data().empty());
338 219
339 const base::ListValue* arg2; 220 const base::ListValue* arg2;
340 ASSERT_TRUE(web_ui()->call_data().front()->arg2()->GetAsList(&arg2)); 221 ASSERT_TRUE(web_ui()->call_data().front()->arg2()->GetAsList(&arg2));
341 222
342 const base::DictionaryValue* first_entry; 223 const base::DictionaryValue* first_entry;
343 ASSERT_TRUE(arg2->GetDictionary(0, &first_entry)); 224 ASSERT_TRUE(arg2->GetDictionary(0, &first_entry));
344 225
345 base::string16 title; 226 base::string16 title;
346 ASSERT_TRUE(first_entry->GetString("title", &title)); 227 ASSERT_TRUE(first_entry->GetString("title", &title));
347 228
348 ASSERT_EQ(0u, title.find(base::ASCIIToUTF16("http://loooo"))); 229 ASSERT_EQ(0u, title.find(base::ASCIIToUTF16("http://loooo")));
349 EXPECT_EQ(300u, title.size()); 230 EXPECT_EQ(300u, title.size());
350 } 231 }
351 #endif 232 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698