OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/history/in_memory_url_index_unittest_base.h" | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/file_util.h" | |
9 #include "base/path_service.h" | |
10 #include "base/string_split.h" | |
11 #include "chrome/browser/history/history.h" | |
12 #include "chrome/browser/history/history_backend.h" | |
13 #include "chrome/browser/history/history_service_factory.h" | |
14 #include "chrome/browser/history/in_memory_url_cache_database.h" | |
15 #include "chrome/browser/history/in_memory_url_index.h" | |
16 #include "chrome/browser/history/url_index_private_data.h" | |
17 #include "chrome/common/chrome_paths.h" | |
18 #include "chrome/test/base/ui_test_utils.h" | |
19 #include "sql/connection.h" | |
20 #include "sql/statement.h" | |
21 #include "sql/transaction.h" | |
22 | |
23 namespace history { | |
24 | |
25 InMemoryURLIndexTestBase::InMemoryURLIndexTestBase() | |
26 : ui_thread_(content::BrowserThread::UI, &message_loop_), | |
27 db_thread_(content::BrowserThread::DB) { | |
28 } | |
29 | |
30 InMemoryURLIndexTestBase::~InMemoryURLIndexTestBase() {} | |
31 | |
32 void InMemoryURLIndexTestBase::SetUp() { | |
33 if (!profile_.get()) | |
34 profile_.reset(new TestingProfile); | |
35 db_thread_.Start(); | |
36 // We cannot access the database until the backend has been loaded. | |
37 profile_->CreateHistoryService(true, false); | |
38 HistoryService* history_service = | |
39 HistoryServiceFactory::GetForProfile(profile_.get(), | |
40 Profile::EXPLICIT_ACCESS); | |
41 ASSERT_TRUE(history_service); | |
42 url_index_ = history_service->InMemoryIndex(); | |
43 BlockUntilIndexLoaded(); | |
44 DCHECK(url_index_->index_available()); | |
45 profile_->CreateBookmarkModel(true); | |
46 HistoryBackend* backend = history_service->get_history_backend_for_testing(); | |
47 history_database_ = backend->db(); | |
48 ui_test_utils::WaitForHistoryToLoad(history_service); | |
49 DCHECK(history_service->backend_loaded()); | |
50 | |
51 // Create and populate a working copy of the URL history database from the | |
52 // data contained in the file specified by the TestDBName() function. | |
53 // TODO(mrossetti): Adopt sqlite3_ functions for performing the database | |
54 // initialization. See http://crbug.com/137352. | |
55 FilePath test_file_path; | |
56 PathService::Get(chrome::DIR_TEST_DATA, &test_file_path); | |
57 test_file_path = test_file_path.Append(FILE_PATH_LITERAL("History")); | |
58 test_file_path = test_file_path.Append(TestDBName()); | |
59 EXPECT_TRUE(file_util::PathExists(test_file_path)); | |
60 | |
61 std::string sql_command_buffer; | |
62 file_util::ReadFileToString(test_file_path, &sql_command_buffer); | |
63 std::vector<std::string> sql_commands; | |
64 base::SplitStringDontTrim(sql_command_buffer, '\n', &sql_commands); | |
65 sql::Connection* db(history_database_->get_db_for_testing()); | |
66 for (std::vector<std::string>::const_iterator i = sql_commands.begin(); | |
67 i != sql_commands.end(); ++i) { | |
68 // We only process lines which begin with a upper-case letter. | |
69 // TODO(mrossetti): Can iswupper() be used here? | |
70 const std::string& sql_command(*i); | |
71 if (sql_command[0] >= 'A' && sql_command[0] <= 'Z') | |
72 EXPECT_TRUE(db->Execute(sql_command.c_str())); | |
73 } | |
74 | |
75 // Update the last_visit_time table column such that it represents a time | |
76 // relative to 'now'. | |
77 // TODO(mrossetti): Do an UPDATE to alter the days-ago. | |
78 // See http://crbug.com/137352. | |
79 sql::Statement statement(db->GetUniqueStatement( | |
80 "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls")); | |
81 EXPECT_TRUE(statement.is_valid()); | |
82 base::Time time_right_now = base::Time::NowFromSystemTime(); | |
83 base::TimeDelta day_delta = base::TimeDelta::FromDays(1); | |
84 while (statement.Step()) { | |
85 URLRow row; | |
86 history_database_->FillURLRow(statement, &row); | |
87 row.set_last_visit(time_right_now - | |
88 day_delta * row.last_visit().ToInternalValue()); | |
89 history_database_->UpdateURLRow(row.id(), row); | |
90 } | |
91 } | |
92 | |
93 void InMemoryURLIndexTestBase::TearDown() { | |
94 MessageLoop::current()->RunAllPending(); | |
95 message_loop_.RunAllPending(); | |
96 db_thread_.Stop(); | |
97 } | |
98 | |
99 void InMemoryURLIndexTestBase::LoadIndex() { | |
100 url_index_->ScheduleRebuildFromHistory(); | |
101 BlockUntilIndexLoaded(); | |
102 } | |
103 | |
104 void InMemoryURLIndexTestBase::BlockUntilIndexLoaded() { | |
105 if (url_index_->index_available()) | |
106 return; | |
107 InMemoryURLIndex::Observer observer(url_index_); | |
108 MessageLoop::current()->Run(); | |
109 } | |
110 | |
111 URLIndexPrivateData* InMemoryURLIndexTestBase::GetPrivateData() { | |
112 return url_index_->private_data(); | |
113 } | |
114 | |
115 bool InMemoryURLIndexTestBase::UpdateURL(const URLRow& row) { | |
116 bool success = GetPrivateData()->UpdateURL(row); | |
117 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
118 return success; | |
119 } | |
120 | |
121 bool InMemoryURLIndexTestBase::DeleteURL(const GURL& url) { | |
122 bool success = GetPrivateData()->DeleteURL(url); | |
123 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
124 return success; | |
125 } | |
126 | |
127 bool InMemoryURLIndexTestBase::GetCacheDBPath(FilePath* file_path) { | |
128 return GetPrivateData()->GetCacheDBPath(file_path); | |
129 } | |
130 | |
131 } // namespace history | |
OLD | NEW |