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/autocomplete/shortcuts_database.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/format_macros.h"
9 #include "base/path_service.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/common/chrome_constants.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "components/omnibox/autocomplete_match_type.h"
17 #include "sql/statement.h"
18 #include "sql/test/test_helpers.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/base/page_transition_types.h"
22 using base::ASCIIToUTF16
;
24 // Helpers --------------------------------------------------------------------
28 struct ShortcutsDatabaseTestInfo
{
31 std::string fill_into_edit
;
32 std::string destination_url
;
34 std::string contents_class
;
35 std::string description
;
36 std::string description_class
;
37 ui::PageTransition transition
;
38 AutocompleteMatchType::Type type
;
42 } shortcut_test_db
[] = {
43 { "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", "goog", "www.google.com",
44 "http://www.google.com/", "Google", "0,1,4,0", "Google", "0,1",
45 ui::PAGE_TRANSITION_GENERATED
, AutocompleteMatchType::SEARCH_HISTORY
,
46 "google.com", 1, 100, },
47 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "slash", "slashdot.org",
48 "http://slashdot.org/", "slashdot.org", "0,1",
49 "Slashdot - News for nerds, stuff that matters", "0,0",
50 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 0,
52 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "news", "slashdot.org",
53 "http://slashdot.org/", "slashdot.org", "0,1",
54 "Slashdot - News for nerds, stuff that matters", "0,0",
55 ui::PAGE_TRANSITION_LINK
, AutocompleteMatchType::HISTORY_TITLE
, "", 0,
59 typedef testing::Test ShortcutsDatabaseMigrationTest
;
61 // Checks that the database at |db| has the version 2 columns iff |is_v2|.
62 void CheckV2ColumnExistence(const base::FilePath
& db_path
, bool is_v2
) {
63 sql::Connection connection
;
64 ASSERT_TRUE(connection
.Open(db_path
));
65 EXPECT_EQ(is_v2
, connection
.DoesColumnExist("omni_box_shortcuts",
67 EXPECT_EQ(is_v2
, connection
.DoesColumnExist("omni_box_shortcuts",
69 EXPECT_EQ(is_v2
, connection
.DoesColumnExist("omni_box_shortcuts", "type"));
70 EXPECT_EQ(is_v2
, connection
.DoesColumnExist("omni_box_shortcuts", "keyword"));
75 // ShortcutsDatabaseTest ------------------------------------------------------
77 class ShortcutsDatabaseTest
: public testing::Test
{
79 void SetUp() override
;
80 void TearDown() override
;
83 size_t CountRecords() const;
85 ShortcutsDatabase::Shortcut
ShortcutFromTestInfo(
86 const ShortcutsDatabaseTestInfo
& info
);
90 scoped_ptr
<TestingProfile
> profile_
;
91 scoped_refptr
<ShortcutsDatabase
> db_
;
94 void ShortcutsDatabaseTest::SetUp() {
95 profile_
.reset(new TestingProfile());
96 db_
= new ShortcutsDatabase(
97 profile_
->GetPath().Append(chrome::kShortcutsDatabaseName
));
98 ASSERT_TRUE(db_
->Init());
102 void ShortcutsDatabaseTest::TearDown() {
106 void ShortcutsDatabaseTest::ClearDB() {
108 s(db_
->db_
.GetUniqueStatement("DELETE FROM omni_box_shortcuts"));
109 EXPECT_TRUE(s
.Run());
112 size_t ShortcutsDatabaseTest::CountRecords() const {
113 sql::Statement
s(db_
->db_
.GetUniqueStatement(
114 "SELECT count(*) FROM omni_box_shortcuts"));
115 EXPECT_TRUE(s
.Step());
116 return static_cast<size_t>(s
.ColumnInt(0));
119 ShortcutsDatabase::Shortcut
ShortcutsDatabaseTest::ShortcutFromTestInfo(
120 const ShortcutsDatabaseTestInfo
& info
) {
121 return ShortcutsDatabase::Shortcut(
122 info
.guid
, ASCIIToUTF16(info
.text
),
123 ShortcutsDatabase::Shortcut::MatchCore(
124 ASCIIToUTF16(info
.fill_into_edit
), GURL(info
.destination_url
),
125 ASCIIToUTF16(info
.contents
), info
.contents_class
,
126 ASCIIToUTF16(info
.description
), info
.description_class
,
127 info
.transition
, info
.type
, ASCIIToUTF16(info
.keyword
)),
128 base::Time::Now() - base::TimeDelta::FromDays(info
.days_from_now
),
129 info
.number_of_hits
);
132 void ShortcutsDatabaseTest::AddAll() {
134 for (size_t i
= 0; i
< arraysize(shortcut_test_db
); ++i
)
135 db_
->AddShortcut(ShortcutFromTestInfo(shortcut_test_db
[i
]));
136 EXPECT_EQ(arraysize(shortcut_test_db
), CountRecords());
140 // Actual tests ---------------------------------------------------------------
142 TEST_F(ShortcutsDatabaseTest
, AddShortcut
) {
144 EXPECT_EQ(0U, CountRecords());
145 EXPECT_TRUE(db_
->AddShortcut(ShortcutFromTestInfo(shortcut_test_db
[0])));
146 EXPECT_EQ(1U, CountRecords());
147 EXPECT_TRUE(db_
->AddShortcut(ShortcutFromTestInfo(shortcut_test_db
[1])));
148 EXPECT_EQ(2U, CountRecords());
149 EXPECT_TRUE(db_
->AddShortcut(ShortcutFromTestInfo(shortcut_test_db
[2])));
150 EXPECT_EQ(3U, CountRecords());
153 TEST_F(ShortcutsDatabaseTest
, UpdateShortcut
) {
155 ShortcutsDatabase::Shortcut
shortcut(
156 ShortcutFromTestInfo(shortcut_test_db
[1]));
157 shortcut
.match_core
.contents
= ASCIIToUTF16("gro.todhsals");
158 EXPECT_TRUE(db_
->UpdateShortcut(shortcut
));
159 ShortcutsDatabase::GuidToShortcutMap shortcuts
;
160 db_
->LoadShortcuts(&shortcuts
);
161 ShortcutsDatabase::GuidToShortcutMap::const_iterator
it(
162 shortcuts
.find(shortcut
.id
));
163 EXPECT_TRUE(it
!= shortcuts
.end());
164 EXPECT_TRUE(it
->second
.match_core
.contents
== shortcut
.match_core
.contents
);
167 TEST_F(ShortcutsDatabaseTest
, DeleteShortcutsWithIds
) {
169 std::vector
<std::string
> shortcut_ids
;
170 shortcut_ids
.push_back(shortcut_test_db
[0].guid
);
171 shortcut_ids
.push_back(shortcut_test_db
[2].guid
);
172 EXPECT_TRUE(db_
->DeleteShortcutsWithIDs(shortcut_ids
));
173 EXPECT_EQ(arraysize(shortcut_test_db
) - 2, CountRecords());
175 ShortcutsDatabase::GuidToShortcutMap shortcuts
;
176 db_
->LoadShortcuts(&shortcuts
);
178 ShortcutsDatabase::GuidToShortcutMap::iterator it
=
179 shortcuts
.find(shortcut_test_db
[0].guid
);
180 EXPECT_TRUE(it
== shortcuts
.end());
182 it
= shortcuts
.find(shortcut_test_db
[1].guid
);
183 EXPECT_TRUE(it
!= shortcuts
.end());
185 it
= shortcuts
.find(shortcut_test_db
[2].guid
);
186 EXPECT_TRUE(it
== shortcuts
.end());
189 TEST_F(ShortcutsDatabaseTest
, DeleteShortcutsWithURL
) {
192 EXPECT_TRUE(db_
->DeleteShortcutsWithURL("http://slashdot.org/"));
193 EXPECT_EQ(arraysize(shortcut_test_db
) - 2, CountRecords());
195 ShortcutsDatabase::GuidToShortcutMap shortcuts
;
196 db_
->LoadShortcuts(&shortcuts
);
198 ShortcutsDatabase::GuidToShortcutMap::iterator it
=
199 shortcuts
.find(shortcut_test_db
[0].guid
);
200 EXPECT_TRUE(it
!= shortcuts
.end());
202 it
= shortcuts
.find(shortcut_test_db
[1].guid
);
203 EXPECT_TRUE(it
== shortcuts
.end());
205 it
= shortcuts
.find(shortcut_test_db
[2].guid
);
206 EXPECT_TRUE(it
== shortcuts
.end());
210 TEST_F(ShortcutsDatabaseTest
, DeleteAllShortcuts
) {
212 ShortcutsDatabase::GuidToShortcutMap shortcuts
;
213 db_
->LoadShortcuts(&shortcuts
);
214 EXPECT_EQ(arraysize(shortcut_test_db
), shortcuts
.size());
215 EXPECT_TRUE(db_
->DeleteAllShortcuts());
216 db_
->LoadShortcuts(&shortcuts
);
217 EXPECT_EQ(0U, shortcuts
.size());
220 TEST(ShortcutsDatabaseMigrationTest
, MigrateTableAddFillIntoEdit
) {
221 // Use the pre-v0 test file to create a test database in a temp dir.
222 base::FilePath sql_path
;
223 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &sql_path
));
224 sql_path
= sql_path
.AppendASCII("History").AppendASCII(
225 #if defined(OS_ANDROID)
228 "Shortcuts.no_fill_into_edit.sql");
230 base::ScopedTempDir temp_dir
;
231 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
232 base::FilePath
db_path(temp_dir
.path().AppendASCII("TestShortcuts1.db"));
233 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path
, sql_path
));
235 CheckV2ColumnExistence(db_path
, false);
237 // Create a ShortcutsDatabase from the test database, which will migrate the
238 // test database to the current version.
240 scoped_refptr
<ShortcutsDatabase
> db(new ShortcutsDatabase(db_path
));
244 CheckV2ColumnExistence(db_path
, true);
246 // Check the values in each of the new columns.
247 sql::Connection connection
;
248 ASSERT_TRUE(connection
.Open(db_path
));
249 sql::Statement
statement(connection
.GetUniqueStatement(
250 "SELECT fill_into_edit, url, transition, type, keyword "
251 "FROM omni_box_shortcuts"));
252 ASSERT_TRUE(statement
.is_valid());
253 while (statement
.Step()) {
254 // |fill_into_edit| should have been copied from the |url|.
255 EXPECT_EQ(statement
.ColumnString(1), statement
.ColumnString(0));
257 // The other three columns have default values.
258 EXPECT_EQ(ui::PAGE_TRANSITION_TYPED
,
259 ui::PageTransitionFromInt(statement
.ColumnInt(2)));
260 EXPECT_EQ(AutocompleteMatchType::HISTORY_TITLE
,
261 static_cast<AutocompleteMatchType::Type
>(statement
.ColumnInt(3)));
262 EXPECT_TRUE(statement
.ColumnString(4).empty());
264 EXPECT_TRUE(statement
.Succeeded());
266 EXPECT_TRUE(temp_dir
.Delete());
270 TEST(ShortcutsDatabaseMigrationTest
, MigrateV0ToV1
) {
271 // Use the v0 test file to create a test database in a temp dir.
272 base::FilePath sql_path
;
273 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &sql_path
));
274 sql_path
= sql_path
.AppendASCII("History").AppendASCII("Shortcuts.v0.sql");
275 base::ScopedTempDir temp_dir
;
276 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
277 base::FilePath
db_path(temp_dir
.path().AppendASCII("TestShortcuts2.db"));
278 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path
, sql_path
));
280 // Create a ShortcutsDatabase from the test database, which will migrate the
281 // test database to the current version.
283 scoped_refptr
<ShortcutsDatabase
> db(new ShortcutsDatabase(db_path
));
287 // Check that all the old type values got converted to new values.
288 sql::Connection connection
;
289 ASSERT_TRUE(connection
.Open(db_path
));
290 sql::Statement
statement(connection
.GetUniqueStatement(
291 "SELECT count(1) FROM omni_box_shortcuts WHERE type in (9, 10, 11, 12)"));
292 ASSERT_TRUE(statement
.is_valid());
293 while (statement
.Step())
294 EXPECT_EQ(0, statement
.ColumnInt(0));
295 EXPECT_TRUE(statement
.Succeeded());
297 EXPECT_TRUE(temp_dir
.Delete());