Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / predictors / autocomplete_action_predictor_unittest.cc
blob3a27a30654cc5d67f688b153c57c635d84bd3d40
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
3 // found in the LICENSE file.
5 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
7 #include "base/auto_reset.h"
8 #include "base/command_line.h"
9 #include "base/guid.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/history/history_service.h"
16 #include "chrome/browser/history/history_service_factory.h"
17 #include "chrome/browser/prerender/prerender_field_trial.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "components/history/core/browser/in_memory_database.h"
21 #include "components/history/core/browser/url_database.h"
22 #include "components/omnibox/autocomplete_match.h"
23 #include "content/public/test/test_browser_thread.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 using base::ASCIIToUTF16;
27 using content::BrowserThread;
28 using predictors::AutocompleteActionPredictor;
30 namespace {
32 struct TestUrlInfo {
33 GURL url;
34 base::string16 title;
35 int days_from_now;
36 base::string16 user_text;
37 int number_of_hits;
38 int number_of_misses;
39 AutocompleteActionPredictor::Action expected_action;
40 } test_url_db[] = {
41 { GURL("http://www.testsite.com/a.html"),
42 ASCIIToUTF16("Test - site - just a test"), 1,
43 ASCIIToUTF16("j"), 5, 0,
44 AutocompleteActionPredictor::ACTION_PRERENDER },
45 { GURL("http://www.testsite.com/b.html"),
46 ASCIIToUTF16("Test - site - just a test"), 1,
47 ASCIIToUTF16("ju"), 3, 0,
48 AutocompleteActionPredictor::ACTION_PRERENDER },
49 { GURL("http://www.testsite.com/c.html"),
50 ASCIIToUTF16("Test - site - just a test"), 5,
51 ASCIIToUTF16("just"), 3, 1,
52 AutocompleteActionPredictor::ACTION_PRECONNECT },
53 { GURL("http://www.testsite.com/d.html"),
54 ASCIIToUTF16("Test - site - just a test"), 5,
55 ASCIIToUTF16("just"), 3, 0,
56 AutocompleteActionPredictor::ACTION_PRERENDER },
57 { GURL("http://www.testsite.com/e.html"),
58 ASCIIToUTF16("Test - site - just a test"), 8,
59 ASCIIToUTF16("just"), 3, 1,
60 AutocompleteActionPredictor::ACTION_PRECONNECT },
61 { GURL("http://www.testsite.com/f.html"),
62 ASCIIToUTF16("Test - site - just a test"), 8,
63 ASCIIToUTF16("just"), 3, 0,
64 AutocompleteActionPredictor::ACTION_PRERENDER },
65 { GURL("http://www.testsite.com/g.html"),
66 ASCIIToUTF16("Test - site - just a test"), 12,
67 base::string16(), 5, 0,
68 AutocompleteActionPredictor::ACTION_NONE },
69 { GURL("http://www.testsite.com/h.html"),
70 ASCIIToUTF16("Test - site - just a test"), 21,
71 ASCIIToUTF16("just a test"), 2, 0,
72 AutocompleteActionPredictor::ACTION_NONE },
73 { GURL("http://www.testsite.com/i.html"),
74 ASCIIToUTF16("Test - site - just a test"), 28,
75 ASCIIToUTF16("just a test"), 2, 0,
76 AutocompleteActionPredictor::ACTION_NONE }
79 } // end namespace
81 namespace predictors {
83 class AutocompleteActionPredictorTest : public testing::Test {
84 public:
85 AutocompleteActionPredictorTest()
86 : ui_thread_(BrowserThread::UI, &loop_),
87 db_thread_(BrowserThread::DB, &loop_),
88 file_thread_(BrowserThread::FILE, &loop_),
89 profile_(new TestingProfile()),
90 predictor_(new AutocompleteActionPredictor(profile_.get())) {
93 ~AutocompleteActionPredictorTest() override {
94 predictor_.reset(NULL);
95 profile_.reset(NULL);
96 loop_.RunUntilIdle();
99 void SetUp() override {
100 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
101 switches::kPrerenderFromOmnibox,
102 switches::kPrerenderFromOmniboxSwitchValueEnabled);
104 predictor_->CreateLocalCachesFromDatabase();
105 ASSERT_TRUE(profile_->CreateHistoryService(true, false));
106 profile_->BlockUntilHistoryProcessesPendingRequests();
108 ASSERT_TRUE(predictor_->initialized_);
109 ASSERT_TRUE(db_cache()->empty());
110 ASSERT_TRUE(db_id_cache()->empty());
113 void TearDown() override {
114 profile_->DestroyHistoryService();
115 predictor_->Shutdown();
118 protected:
119 typedef AutocompleteActionPredictor::DBCacheKey DBCacheKey;
120 typedef AutocompleteActionPredictor::DBCacheValue DBCacheValue;
121 typedef AutocompleteActionPredictor::DBCacheMap DBCacheMap;
122 typedef AutocompleteActionPredictor::DBIdCacheMap DBIdCacheMap;
124 void AddAllRowsToHistory() {
125 for (size_t i = 0; i < arraysize(test_url_db); ++i)
126 ASSERT_TRUE(AddRowToHistory(test_url_db[i]));
129 history::URLID AddRowToHistory(const TestUrlInfo& test_row) {
130 HistoryService* history = HistoryServiceFactory::GetForProfile(
131 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS);
132 CHECK(history);
133 history::URLDatabase* url_db = history->InMemoryDatabase();
134 CHECK(url_db);
136 const base::Time visit_time =
137 base::Time::Now() - base::TimeDelta::FromDays(
138 test_row.days_from_now);
140 history::URLRow row(test_row.url);
141 row.set_title(test_row.title);
142 row.set_last_visit(visit_time);
144 return url_db->AddURL(row);
147 AutocompleteActionPredictorTable::Row CreateRowFromTestUrlInfo(
148 const TestUrlInfo& test_row) const {
149 AutocompleteActionPredictorTable::Row row;
150 row.id = base::GenerateGUID();
151 row.user_text = test_row.user_text;
152 row.url = test_row.url;
153 row.number_of_hits = test_row.number_of_hits;
154 row.number_of_misses = test_row.number_of_misses;
155 return row;
158 void AddAllRows() {
159 for (size_t i = 0; i < arraysize(test_url_db); ++i)
160 AddRow(test_url_db[i]);
163 std::string AddRow(const TestUrlInfo& test_row) {
164 AutocompleteActionPredictorTable::Row row =
165 CreateRowFromTestUrlInfo(test_row);
166 predictor_->AddAndUpdateRows(
167 AutocompleteActionPredictorTable::Rows(1, row),
168 AutocompleteActionPredictorTable::Rows());
170 return row.id;
173 void UpdateRow(const AutocompleteActionPredictorTable::Row& row) {
174 AutocompleteActionPredictor::DBCacheKey key = { row.user_text, row.url };
175 ASSERT_TRUE(db_cache()->find(key) != db_cache()->end());
176 predictor_->AddAndUpdateRows(
177 AutocompleteActionPredictorTable::Rows(),
178 AutocompleteActionPredictorTable::Rows(1, row));
181 void DeleteAllRows() {
182 predictor_->DeleteAllRows();
185 void DeleteRowsWithURLs(const history::URLRows& rows) {
186 predictor_->DeleteRowsWithURLs(rows);
189 void DeleteOldIdsFromCaches(
190 std::vector<AutocompleteActionPredictorTable::Row::Id>* id_list) {
191 HistoryService* history_service = HistoryServiceFactory::GetForProfile(
192 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS);
193 ASSERT_TRUE(history_service);
195 history::URLDatabase* url_db = history_service->InMemoryDatabase();
196 ASSERT_TRUE(url_db);
198 // Reset the predictor's |initialized_| flag for the life of this call,
199 // since outside of testing this function is only supposed to be reached
200 // before initialization is completed.
201 base::AutoReset<bool> initialized_reset(&predictor_->initialized_, false);
202 predictor_->DeleteOldIdsFromCaches(url_db, id_list);
205 AutocompleteActionPredictor* predictor() { return predictor_.get(); }
207 DBCacheMap* db_cache() { return &predictor_->db_cache_; }
208 DBIdCacheMap* db_id_cache() { return &predictor_->db_id_cache_; }
210 static int maximum_days_to_keep_entry() {
211 return AutocompleteActionPredictor::kMaximumDaysToKeepEntry;
214 private:
215 base::MessageLoop loop_;
216 content::TestBrowserThread ui_thread_;
217 content::TestBrowserThread db_thread_;
218 content::TestBrowserThread file_thread_;
219 scoped_ptr<TestingProfile> profile_;
220 scoped_ptr<AutocompleteActionPredictor> predictor_;
224 TEST_F(AutocompleteActionPredictorTest, AddRow) {
225 // Add a test entry to the predictor.
226 std::string guid = AddRow(test_url_db[0]);
228 // Get the data back out of the cache.
229 const DBCacheKey key = { test_url_db[0].user_text, test_url_db[0].url };
230 DBCacheMap::const_iterator it = db_cache()->find(key);
231 EXPECT_TRUE(it != db_cache()->end());
233 const DBCacheValue value = { test_url_db[0].number_of_hits,
234 test_url_db[0].number_of_misses };
235 EXPECT_EQ(value.number_of_hits, it->second.number_of_hits);
236 EXPECT_EQ(value.number_of_misses, it->second.number_of_misses);
238 DBIdCacheMap::const_iterator id_it = db_id_cache()->find(key);
239 EXPECT_TRUE(id_it != db_id_cache()->end());
240 EXPECT_EQ(guid, id_it->second);
243 TEST_F(AutocompleteActionPredictorTest, UpdateRow) {
244 ASSERT_NO_FATAL_FAILURE(AddAllRows());
246 EXPECT_EQ(arraysize(test_url_db), db_cache()->size());
247 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size());
249 // Get the data back out of the cache.
250 const DBCacheKey key = { test_url_db[0].user_text, test_url_db[0].url };
251 DBCacheMap::const_iterator it = db_cache()->find(key);
252 EXPECT_TRUE(it != db_cache()->end());
254 DBIdCacheMap::const_iterator id_it = db_id_cache()->find(key);
255 EXPECT_TRUE(id_it != db_id_cache()->end());
257 AutocompleteActionPredictorTable::Row update_row;
258 update_row.id = id_it->second;
259 update_row.user_text = key.user_text;
260 update_row.url = key.url;
261 update_row.number_of_hits = it->second.number_of_hits + 1;
262 update_row.number_of_misses = it->second.number_of_misses + 2;
264 UpdateRow(update_row);
266 // Get the updated version.
267 DBCacheMap::const_iterator update_it = db_cache()->find(key);
268 EXPECT_TRUE(update_it != db_cache()->end());
270 EXPECT_EQ(update_row.number_of_hits, update_it->second.number_of_hits);
271 EXPECT_EQ(update_row.number_of_misses, update_it->second.number_of_misses);
273 DBIdCacheMap::const_iterator update_id_it = db_id_cache()->find(key);
274 EXPECT_TRUE(update_id_it != db_id_cache()->end());
276 EXPECT_EQ(id_it->second, update_id_it->second);
279 TEST_F(AutocompleteActionPredictorTest, DeleteAllRows) {
280 ASSERT_NO_FATAL_FAILURE(AddAllRows());
282 EXPECT_EQ(arraysize(test_url_db), db_cache()->size());
283 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size());
285 DeleteAllRows();
287 EXPECT_TRUE(db_cache()->empty());
288 EXPECT_TRUE(db_id_cache()->empty());
291 TEST_F(AutocompleteActionPredictorTest, DeleteRowsWithURLs) {
292 ASSERT_NO_FATAL_FAILURE(AddAllRows());
294 EXPECT_EQ(arraysize(test_url_db), db_cache()->size());
295 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size());
297 history::URLRows rows;
298 for (size_t i = 0; i < 2; ++i)
299 rows.push_back(history::URLRow(test_url_db[i].url));
301 DeleteRowsWithURLs(rows);
303 EXPECT_EQ(arraysize(test_url_db) - 2, db_cache()->size());
304 EXPECT_EQ(arraysize(test_url_db) - 2, db_id_cache()->size());
306 for (size_t i = 0; i < arraysize(test_url_db); ++i) {
307 DBCacheKey key = { test_url_db[i].user_text, test_url_db[i].url };
309 bool deleted = (i < 2);
310 EXPECT_EQ(deleted, db_cache()->find(key) == db_cache()->end());
311 EXPECT_EQ(deleted, db_id_cache()->find(key) == db_id_cache()->end());
315 TEST_F(AutocompleteActionPredictorTest, DeleteOldIdsFromCaches) {
316 std::vector<AutocompleteActionPredictorTable::Row::Id> expected;
317 std::vector<AutocompleteActionPredictorTable::Row::Id> all_ids;
319 for (size_t i = 0; i < arraysize(test_url_db); ++i) {
320 std::string row_id = AddRow(test_url_db[i]);
321 all_ids.push_back(row_id);
323 bool exclude_url = StartsWithASCII(test_url_db[i].url.path(), "/d", true) ||
324 (test_url_db[i].days_from_now > maximum_days_to_keep_entry());
326 if (exclude_url)
327 expected.push_back(row_id);
328 else
329 ASSERT_TRUE(AddRowToHistory(test_url_db[i]));
332 std::vector<AutocompleteActionPredictorTable::Row::Id> id_list;
333 DeleteOldIdsFromCaches(&id_list);
334 EXPECT_EQ(expected.size(), id_list.size());
335 EXPECT_EQ(all_ids.size() - expected.size(), db_cache()->size());
336 EXPECT_EQ(all_ids.size() - expected.size(), db_id_cache()->size());
338 for (std::vector<AutocompleteActionPredictorTable::Row::Id>::iterator it =
339 all_ids.begin();
340 it != all_ids.end(); ++it) {
341 bool in_expected =
342 (std::find(expected.begin(), expected.end(), *it) != expected.end());
343 bool in_list =
344 (std::find(id_list.begin(), id_list.end(), *it) != id_list.end());
345 EXPECT_EQ(in_expected, in_list);
349 TEST_F(AutocompleteActionPredictorTest, RecommendActionURL) {
350 ASSERT_NO_FATAL_FAILURE(AddAllRows());
352 AutocompleteMatch match;
353 match.type = AutocompleteMatchType::HISTORY_URL;
355 for (size_t i = 0; i < arraysize(test_url_db); ++i) {
356 match.destination_url = GURL(test_url_db[i].url);
357 EXPECT_EQ(test_url_db[i].expected_action,
358 predictor()->RecommendAction(test_url_db[i].user_text, match))
359 << "Unexpected action for " << match.destination_url;
363 TEST_F(AutocompleteActionPredictorTest, RecommendActionSearch) {
364 ASSERT_NO_FATAL_FAILURE(AddAllRows());
366 AutocompleteMatch match;
367 match.type = AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED;
369 for (size_t i = 0; i < arraysize(test_url_db); ++i) {
370 match.destination_url = GURL(test_url_db[i].url);
371 AutocompleteActionPredictor::Action expected_action =
372 (test_url_db[i].expected_action ==
373 AutocompleteActionPredictor::ACTION_PRERENDER) ?
374 AutocompleteActionPredictor::ACTION_PRECONNECT :
375 test_url_db[i].expected_action;
376 EXPECT_EQ(expected_action,
377 predictor()->RecommendAction(test_url_db[i].user_text, match))
378 << "Unexpected action for " << match.destination_url;
382 } // namespace predictors