Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / chrome / browser / autocomplete / shortcuts_database_unittest.cc
blobba8f685e36230d2c23028ff3095e4ad9b7bb5eb1
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 --------------------------------------------------------------------
26 namespace {
28 struct ShortcutsDatabaseTestInfo {
29 std::string guid;
30 std::string text;
31 std::string fill_into_edit;
32 std::string destination_url;
33 std::string contents;
34 std::string contents_class;
35 std::string description;
36 std::string description_class;
37 ui::PageTransition transition;
38 AutocompleteMatchType::Type type;
39 std::string keyword;
40 int days_from_now;
41 int number_of_hits;
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,
51 100},
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,
56 5},
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",
66 "fill_into_edit"));
67 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts",
68 "transition"));
69 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "type"));
70 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "keyword"));
73 } // namespace
75 namespace history {
78 // ShortcutsDatabaseTest ------------------------------------------------------
80 class ShortcutsDatabaseTest : public testing::Test {
81 public:
82 void SetUp() override;
83 void TearDown() override;
85 void ClearDB();
86 size_t CountRecords() const;
88 ShortcutsDatabase::Shortcut ShortcutFromTestInfo(
89 const ShortcutsDatabaseTestInfo& info);
91 void AddAll();
93 scoped_ptr<TestingProfile> profile_;
94 scoped_refptr<ShortcutsDatabase> db_;
97 void ShortcutsDatabaseTest::SetUp() {
98 profile_.reset(new TestingProfile());
99 db_ = new ShortcutsDatabase(
100 profile_->GetPath().Append(chrome::kShortcutsDatabaseName));
101 ASSERT_TRUE(db_->Init());
102 ClearDB();
105 void ShortcutsDatabaseTest::TearDown() {
106 db_ = NULL;
109 void ShortcutsDatabaseTest::ClearDB() {
110 sql::Statement
111 s(db_->db_.GetUniqueStatement("DELETE FROM omni_box_shortcuts"));
112 EXPECT_TRUE(s.Run());
115 size_t ShortcutsDatabaseTest::CountRecords() const {
116 sql::Statement s(db_->db_.GetUniqueStatement(
117 "SELECT count(*) FROM omni_box_shortcuts"));
118 EXPECT_TRUE(s.Step());
119 return static_cast<size_t>(s.ColumnInt(0));
122 ShortcutsDatabase::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo(
123 const ShortcutsDatabaseTestInfo& info) {
124 return ShortcutsDatabase::Shortcut(
125 info.guid, ASCIIToUTF16(info.text),
126 ShortcutsDatabase::Shortcut::MatchCore(
127 ASCIIToUTF16(info.fill_into_edit), GURL(info.destination_url),
128 ASCIIToUTF16(info.contents), info.contents_class,
129 ASCIIToUTF16(info.description), info.description_class,
130 info.transition, info.type, ASCIIToUTF16(info.keyword)),
131 base::Time::Now() - base::TimeDelta::FromDays(info.days_from_now),
132 info.number_of_hits);
135 void ShortcutsDatabaseTest::AddAll() {
136 ClearDB();
137 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i)
138 db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[i]));
139 EXPECT_EQ(arraysize(shortcut_test_db), CountRecords());
143 // Actual tests ---------------------------------------------------------------
145 TEST_F(ShortcutsDatabaseTest, AddShortcut) {
146 ClearDB();
147 EXPECT_EQ(0U, CountRecords());
148 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[0])));
149 EXPECT_EQ(1U, CountRecords());
150 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[1])));
151 EXPECT_EQ(2U, CountRecords());
152 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[2])));
153 EXPECT_EQ(3U, CountRecords());
156 TEST_F(ShortcutsDatabaseTest, UpdateShortcut) {
157 AddAll();
158 ShortcutsDatabase::Shortcut shortcut(
159 ShortcutFromTestInfo(shortcut_test_db[1]));
160 shortcut.match_core.contents = ASCIIToUTF16("gro.todhsals");
161 EXPECT_TRUE(db_->UpdateShortcut(shortcut));
162 ShortcutsDatabase::GuidToShortcutMap shortcuts;
163 db_->LoadShortcuts(&shortcuts);
164 ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
165 shortcuts.find(shortcut.id));
166 EXPECT_TRUE(it != shortcuts.end());
167 EXPECT_TRUE(it->second.match_core.contents == shortcut.match_core.contents);
170 TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithIds) {
171 AddAll();
172 std::vector<std::string> shortcut_ids;
173 shortcut_ids.push_back(shortcut_test_db[0].guid);
174 shortcut_ids.push_back(shortcut_test_db[2].guid);
175 EXPECT_TRUE(db_->DeleteShortcutsWithIDs(shortcut_ids));
176 EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords());
178 ShortcutsDatabase::GuidToShortcutMap shortcuts;
179 db_->LoadShortcuts(&shortcuts);
181 ShortcutsDatabase::GuidToShortcutMap::iterator it =
182 shortcuts.find(shortcut_test_db[0].guid);
183 EXPECT_TRUE(it == shortcuts.end());
185 it = shortcuts.find(shortcut_test_db[1].guid);
186 EXPECT_TRUE(it != shortcuts.end());
188 it = shortcuts.find(shortcut_test_db[2].guid);
189 EXPECT_TRUE(it == shortcuts.end());
192 TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithURL) {
193 AddAll();
195 EXPECT_TRUE(db_->DeleteShortcutsWithURL("http://slashdot.org/"));
196 EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords());
198 ShortcutsDatabase::GuidToShortcutMap shortcuts;
199 db_->LoadShortcuts(&shortcuts);
201 ShortcutsDatabase::GuidToShortcutMap::iterator it =
202 shortcuts.find(shortcut_test_db[0].guid);
203 EXPECT_TRUE(it != shortcuts.end());
205 it = shortcuts.find(shortcut_test_db[1].guid);
206 EXPECT_TRUE(it == shortcuts.end());
208 it = shortcuts.find(shortcut_test_db[2].guid);
209 EXPECT_TRUE(it == shortcuts.end());
213 TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) {
214 AddAll();
215 ShortcutsDatabase::GuidToShortcutMap shortcuts;
216 db_->LoadShortcuts(&shortcuts);
217 EXPECT_EQ(arraysize(shortcut_test_db), shortcuts.size());
218 EXPECT_TRUE(db_->DeleteAllShortcuts());
219 db_->LoadShortcuts(&shortcuts);
220 EXPECT_EQ(0U, shortcuts.size());
223 TEST(ShortcutsDatabaseMigrationTest, MigrateTableAddFillIntoEdit) {
224 // Use the pre-v0 test file to create a test database in a temp dir.
225 base::FilePath sql_path;
226 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &sql_path));
227 sql_path = sql_path.AppendASCII("History").AppendASCII(
228 #if defined(OS_ANDROID)
229 "Shortcuts.v1.sql");
230 #else
231 "Shortcuts.no_fill_into_edit.sql");
232 #endif
233 base::ScopedTempDir temp_dir;
234 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
235 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts1.db"));
236 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path));
238 CheckV2ColumnExistence(db_path, false);
240 // Create a ShortcutsDatabase from the test database, which will migrate the
241 // test database to the current version.
243 scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path));
244 db->Init();
247 CheckV2ColumnExistence(db_path, true);
249 // Check the values in each of the new columns.
250 sql::Connection connection;
251 ASSERT_TRUE(connection.Open(db_path));
252 sql::Statement statement(connection.GetUniqueStatement(
253 "SELECT fill_into_edit, url, transition, type, keyword "
254 "FROM omni_box_shortcuts"));
255 ASSERT_TRUE(statement.is_valid());
256 while (statement.Step()) {
257 // |fill_into_edit| should have been copied from the |url|.
258 EXPECT_EQ(statement.ColumnString(1), statement.ColumnString(0));
260 // The other three columns have default values.
261 EXPECT_EQ(ui::PAGE_TRANSITION_TYPED,
262 ui::PageTransitionFromInt(statement.ColumnInt(2)));
263 EXPECT_EQ(AutocompleteMatchType::HISTORY_TITLE,
264 static_cast<AutocompleteMatchType::Type>(statement.ColumnInt(3)));
265 EXPECT_TRUE(statement.ColumnString(4).empty());
267 EXPECT_TRUE(statement.Succeeded());
268 #if !defined(OS_WIN)
269 EXPECT_TRUE(temp_dir.Delete());
270 #endif
273 TEST(ShortcutsDatabaseMigrationTest, MigrateV0ToV1) {
274 // Use the v0 test file to create a test database in a temp dir.
275 base::FilePath sql_path;
276 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &sql_path));
277 sql_path = sql_path.AppendASCII("History").AppendASCII("Shortcuts.v0.sql");
278 base::ScopedTempDir temp_dir;
279 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
280 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts2.db"));
281 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path));
283 // Create a ShortcutsDatabase from the test database, which will migrate the
284 // test database to the current version.
286 scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path));
287 db->Init();
290 // Check that all the old type values got converted to new values.
291 sql::Connection connection;
292 ASSERT_TRUE(connection.Open(db_path));
293 sql::Statement statement(connection.GetUniqueStatement(
294 "SELECT count(1) FROM omni_box_shortcuts WHERE type in (9, 10, 11, 12)"));
295 ASSERT_TRUE(statement.is_valid());
296 while (statement.Step())
297 EXPECT_EQ(0, statement.ColumnInt(0));
298 EXPECT_TRUE(statement.Succeeded());
299 #if !defined(OS_WIN)
300 EXPECT_TRUE(temp_dir.Delete());
301 #endif
304 } // namespace history