1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/shortcuts_database.h"
#include <stddef.h>
#include "base/files/scoped_temp_dir.h"
#include "base/format_macros.h"
#include "base/macros.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/omnibox/browser/autocomplete_match_type.h"
#include "components/omnibox/browser/shortcuts_constants.h"
#include "sql/statement.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/page_transition_types.h"
using base::ASCIIToUTF16;
// Helpers --------------------------------------------------------------------
namespace {
struct ShortcutsDatabaseTestInfo {
std::string guid;
std::string text;
std::string fill_into_edit;
std::string destination_url;
std::string contents;
std::string contents_class;
std::string description;
std::string description_class;
ui::PageTransition transition;
AutocompleteMatchType::Type type;
std::string keyword;
int days_from_now;
int number_of_hits;
} shortcut_test_db[] = {
{ "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", "goog", "www.google.com",
"http://www.google.com/", "Google", "0,1,4,0", "Google", "0,1",
ui::PAGE_TRANSITION_GENERATED, AutocompleteMatchType::SEARCH_HISTORY,
"google.com", 1, 100, },
{ "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "slash", "slashdot.org",
"http://slashdot.org/", "slashdot.org", "0,1",
"Slashdot - News for nerds, stuff that matters", "0,0",
ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 0,
100},
{ "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "news", "slashdot.org",
"http://slashdot.org/", "slashdot.org", "0,1",
"Slashdot - News for nerds, stuff that matters", "0,0",
ui::PAGE_TRANSITION_LINK, AutocompleteMatchType::HISTORY_TITLE, "", 0,
5},
};
typedef testing::Test ShortcutsDatabaseMigrationTest;
// Checks that the database at |db| has the version 2 columns iff |is_v2|.
void CheckV2ColumnExistence(const base::FilePath& db_path, bool is_v2) {
sql::Connection connection;
ASSERT_TRUE(connection.Open(db_path));
EXPECT_EQ(is_v2,
connection.DoesColumnExist("omni_box_shortcuts", "fill_into_edit"));
EXPECT_EQ(is_v2,
connection.DoesColumnExist("omni_box_shortcuts", "transition"));
EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "type"));
EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "keyword"));
}
const base::FilePath GetTestDataDir() {
base::FilePath path;
PathService::Get(base::DIR_SOURCE_ROOT, &path);
return path.AppendASCII("components/test/data/omnibox");
}
} // namespace
// ShortcutsDatabaseTest ------------------------------------------------------
class ShortcutsDatabaseTest : public testing::Test {
public:
void SetUp() override;
void TearDown() override;
void ClearDB();
size_t CountRecords() const;
ShortcutsDatabase::Shortcut ShortcutFromTestInfo(
const ShortcutsDatabaseTestInfo& info);
void AddAll();
scoped_refptr<ShortcutsDatabase> db_;
};
void ShortcutsDatabaseTest::SetUp() {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath db_path(temp_dir.path().Append(kShortcutsDatabaseName));
db_ = new ShortcutsDatabase(db_path);
ASSERT_TRUE(db_->Init());
ClearDB();
}
void ShortcutsDatabaseTest::TearDown() {
db_ = NULL;
}
void ShortcutsDatabaseTest::ClearDB() {
sql::Statement s(
db_->db_.GetUniqueStatement("DELETE FROM omni_box_shortcuts"));
EXPECT_TRUE(s.Run());
}
size_t ShortcutsDatabaseTest::CountRecords() const {
sql::Statement s(
db_->db_.GetUniqueStatement("SELECT count(*) FROM omni_box_shortcuts"));
EXPECT_TRUE(s.Step());
return static_cast<size_t>(s.ColumnInt(0));
}
ShortcutsDatabase::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo(
const ShortcutsDatabaseTestInfo& info) {
return ShortcutsDatabase::Shortcut(
info.guid, ASCIIToUTF16(info.text),
ShortcutsDatabase::Shortcut::MatchCore(
ASCIIToUTF16(info.fill_into_edit), GURL(info.destination_url),
ASCIIToUTF16(info.contents), info.contents_class,
ASCIIToUTF16(info.description), info.description_class,
info.transition, info.type, ASCIIToUTF16(info.keyword)),
base::Time::Now() - base::TimeDelta::FromDays(info.days_from_now),
info.number_of_hits);
}
void ShortcutsDatabaseTest::AddAll() {
ClearDB();
for (size_t i = 0; i < arraysize(shortcut_test_db); ++i)
db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[i]));
EXPECT_EQ(arraysize(shortcut_test_db), CountRecords());
}
// Actual tests ---------------------------------------------------------------
TEST_F(ShortcutsDatabaseTest, AddShortcut) {
ClearDB();
EXPECT_EQ(0U, CountRecords());
EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[0])));
EXPECT_EQ(1U, CountRecords());
EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[1])));
EXPECT_EQ(2U, CountRecords());
EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[2])));
EXPECT_EQ(3U, CountRecords());
}
TEST_F(ShortcutsDatabaseTest, UpdateShortcut) {
AddAll();
ShortcutsDatabase::Shortcut shortcut(
ShortcutFromTestInfo(shortcut_test_db[1]));
shortcut.match_core.contents = ASCIIToUTF16("gro.todhsals");
EXPECT_TRUE(db_->UpdateShortcut(shortcut));
ShortcutsDatabase::GuidToShortcutMap shortcuts;
db_->LoadShortcuts(&shortcuts);
ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
shortcuts.find(shortcut.id));
EXPECT_TRUE(it != shortcuts.end());
EXPECT_TRUE(it->second.match_core.contents == shortcut.match_core.contents);
}
TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithIds) {
AddAll();
std::vector<std::string> shortcut_ids;
shortcut_ids.push_back(shortcut_test_db[0].guid);
shortcut_ids.push_back(shortcut_test_db[2].guid);
EXPECT_TRUE(db_->DeleteShortcutsWithIDs(shortcut_ids));
EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords());
ShortcutsDatabase::GuidToShortcutMap shortcuts;
db_->LoadShortcuts(&shortcuts);
ShortcutsDatabase::GuidToShortcutMap::iterator it =
shortcuts.find(shortcut_test_db[0].guid);
EXPECT_TRUE(it == shortcuts.end());
it = shortcuts.find(shortcut_test_db[1].guid);
EXPECT_TRUE(it != shortcuts.end());
it = shortcuts.find(shortcut_test_db[2].guid);
EXPECT_TRUE(it == shortcuts.end());
}
TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithURL) {
AddAll();
EXPECT_TRUE(db_->DeleteShortcutsWithURL("http://slashdot.org/"));
EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords());
ShortcutsDatabase::GuidToShortcutMap shortcuts;
db_->LoadShortcuts(&shortcuts);
ShortcutsDatabase::GuidToShortcutMap::iterator it =
shortcuts.find(shortcut_test_db[0].guid);
EXPECT_TRUE(it != shortcuts.end());
it = shortcuts.find(shortcut_test_db[1].guid);
EXPECT_TRUE(it == shortcuts.end());
it = shortcuts.find(shortcut_test_db[2].guid);
EXPECT_TRUE(it == shortcuts.end());
}
TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) {
AddAll();
ShortcutsDatabase::GuidToShortcutMap shortcuts;
db_->LoadShortcuts(&shortcuts);
EXPECT_EQ(arraysize(shortcut_test_db), shortcuts.size());
EXPECT_TRUE(db_->DeleteAllShortcuts());
db_->LoadShortcuts(&shortcuts);
EXPECT_EQ(0U, shortcuts.size());
}
TEST(ShortcutsDatabaseMigrationTest, MigrateTableAddFillIntoEdit) {
// Use the pre-v0 test file to create a test database in a temp dir.
base::FilePath sql_path = GetTestDataDir().AppendASCII(
#if defined(OS_ANDROID)
"Shortcuts.v1.sql");
#else
"Shortcuts.no_fill_into_edit.sql");
#endif
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts1.db"));
ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path));
CheckV2ColumnExistence(db_path, false);
// Create a ShortcutsDatabase from the test database, which will migrate the
// test database to the current version.
{
scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path));
db->Init();
}
CheckV2ColumnExistence(db_path, true);
// Check the values in each of the new columns.
sql::Connection connection;
ASSERT_TRUE(connection.Open(db_path));
sql::Statement statement(connection.GetUniqueStatement(
"SELECT fill_into_edit, url, transition, type, keyword "
"FROM omni_box_shortcuts"));
ASSERT_TRUE(statement.is_valid());
while (statement.Step()) {
// |fill_into_edit| should have been copied from the |url|.
EXPECT_EQ(statement.ColumnString(1), statement.ColumnString(0));
// The other three columns have default values.
EXPECT_EQ(ui::PAGE_TRANSITION_TYPED,
ui::PageTransitionFromInt(statement.ColumnInt(2)));
EXPECT_EQ(AutocompleteMatchType::HISTORY_TITLE,
static_cast<AutocompleteMatchType::Type>(statement.ColumnInt(3)));
EXPECT_TRUE(statement.ColumnString(4).empty());
}
EXPECT_TRUE(statement.Succeeded());
#if !defined(OS_WIN)
EXPECT_TRUE(temp_dir.Delete());
#endif
}
TEST(ShortcutsDatabaseMigrationTest, MigrateV0ToV1) {
// Use the v0 test file to create a test database in a temp dir.
base::FilePath sql_path = GetTestDataDir().AppendASCII("Shortcuts.v0.sql");
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts2.db"));
ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path));
// Create a ShortcutsDatabase from the test database, which will migrate the
// test database to the current version.
{
scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path));
db->Init();
}
// Check that all the old type values got converted to new values.
sql::Connection connection;
ASSERT_TRUE(connection.Open(db_path));
sql::Statement statement(connection.GetUniqueStatement(
"SELECT count(1) FROM omni_box_shortcuts WHERE type in (9, 10, 11, 12)"));
ASSERT_TRUE(statement.is_valid());
while (statement.Step())
EXPECT_EQ(0, statement.ColumnInt(0));
EXPECT_TRUE(statement.Succeeded());
#if !defined(OS_WIN)
EXPECT_TRUE(temp_dir.Delete());
#endif
}
|