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