Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: components/omnibox/browser/shortcuts_database_unittest.cc

Issue 1515853002: Componentize shortcuts_database_unittest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: updated Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/omnibox/browser/shortcuts_database.h" 5 #include "components/omnibox/browser/shortcuts_database.h"
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/format_macros.h" 8 #include "base/format_macros.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/omnibox/browser/autocomplete_match_type.h" 13 #include "components/omnibox/browser/autocomplete_match_type.h"
16 #include "components/omnibox/browser/shortcuts_constants.h" 14 #include "components/omnibox/browser/shortcuts_constants.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "sql/statement.h" 15 #include "sql/statement.h"
19 #include "sql/test/test_helpers.h" 16 #include "sql/test/test_helpers.h"
20 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/base/page_transition_types.h" 18 #include "ui/base/page_transition_types.h"
22 19
23 using base::ASCIIToUTF16; 20 using base::ASCIIToUTF16;
24 21
25 // Helpers -------------------------------------------------------------------- 22 // Helpers --------------------------------------------------------------------
26 23
27 namespace { 24 namespace {
(...skipping 28 matching lines...) Expand all
56 ui::PAGE_TRANSITION_LINK, AutocompleteMatchType::HISTORY_TITLE, "", 0, 53 ui::PAGE_TRANSITION_LINK, AutocompleteMatchType::HISTORY_TITLE, "", 0,
57 5}, 54 5},
58 }; 55 };
59 56
60 typedef testing::Test ShortcutsDatabaseMigrationTest; 57 typedef testing::Test ShortcutsDatabaseMigrationTest;
61 58
62 // Checks that the database at |db| has the version 2 columns iff |is_v2|. 59 // Checks that the database at |db| has the version 2 columns iff |is_v2|.
63 void CheckV2ColumnExistence(const base::FilePath& db_path, bool is_v2) { 60 void CheckV2ColumnExistence(const base::FilePath& db_path, bool is_v2) {
64 sql::Connection connection; 61 sql::Connection connection;
65 ASSERT_TRUE(connection.Open(db_path)); 62 ASSERT_TRUE(connection.Open(db_path));
66 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", 63 EXPECT_EQ(is_v2,
67 "fill_into_edit")); 64 connection.DoesColumnExist("omni_box_shortcuts", "fill_into_edit"));
68 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", 65 EXPECT_EQ(is_v2,
69 "transition")); 66 connection.DoesColumnExist("omni_box_shortcuts", "transition"));
70 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "type")); 67 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "type"));
71 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "keyword")); 68 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "keyword"));
72 } 69 }
73 70
71 const base::FilePath GetTestDataDir() {
72 base::FilePath path;
73 PathService::Get(base::DIR_SOURCE_ROOT, &path);
74 return path.AppendASCII("components/test/data/omnibox");
75 }
76
74 } // namespace 77 } // namespace
75 78
76 // ShortcutsDatabaseTest ------------------------------------------------------ 79 // ShortcutsDatabaseTest ------------------------------------------------------
77 80
78 class ShortcutsDatabaseTest : public testing::Test { 81 class ShortcutsDatabaseTest : public testing::Test {
79 public: 82 public:
80 void SetUp() override; 83 void SetUp() override;
81 void TearDown() override; 84 void TearDown() override;
82 85
83 void ClearDB(); 86 void ClearDB();
84 size_t CountRecords() const; 87 size_t CountRecords() const;
85 88
86 ShortcutsDatabase::Shortcut ShortcutFromTestInfo( 89 ShortcutsDatabase::Shortcut ShortcutFromTestInfo(
87 const ShortcutsDatabaseTestInfo& info); 90 const ShortcutsDatabaseTestInfo& info);
88 91
89 void AddAll(); 92 void AddAll();
90 93
91 content::TestBrowserThreadBundle thread_bundle_;
92 scoped_ptr<TestingProfile> profile_;
93 scoped_refptr<ShortcutsDatabase> db_; 94 scoped_refptr<ShortcutsDatabase> db_;
94 }; 95 };
95 96
96 void ShortcutsDatabaseTest::SetUp() { 97 void ShortcutsDatabaseTest::SetUp() {
97 profile_.reset(new TestingProfile()); 98 base::ScopedTempDir temp_dir;
98 db_ = new ShortcutsDatabase( 99 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
99 profile_->GetPath().Append(kShortcutsDatabaseName)); 100 base::FilePath db_path(temp_dir.path().Append(kShortcutsDatabaseName));
101 db_ = new ShortcutsDatabase(db_path);
100 ASSERT_TRUE(db_->Init()); 102 ASSERT_TRUE(db_->Init());
101 ClearDB(); 103 ClearDB();
102 } 104 }
103 105
104 void ShortcutsDatabaseTest::TearDown() { 106 void ShortcutsDatabaseTest::TearDown() {
105 db_ = NULL; 107 db_ = NULL;
106 } 108 }
107 109
108 void ShortcutsDatabaseTest::ClearDB() { 110 void ShortcutsDatabaseTest::ClearDB() {
109 sql::Statement 111 sql::Statement s(
110 s(db_->db_.GetUniqueStatement("DELETE FROM omni_box_shortcuts")); 112 db_->db_.GetUniqueStatement("DELETE FROM omni_box_shortcuts"));
111 EXPECT_TRUE(s.Run()); 113 EXPECT_TRUE(s.Run());
112 } 114 }
113 115
114 size_t ShortcutsDatabaseTest::CountRecords() const { 116 size_t ShortcutsDatabaseTest::CountRecords() const {
115 sql::Statement s(db_->db_.GetUniqueStatement( 117 sql::Statement s(
116 "SELECT count(*) FROM omni_box_shortcuts")); 118 db_->db_.GetUniqueStatement("SELECT count(*) FROM omni_box_shortcuts"));
117 EXPECT_TRUE(s.Step()); 119 EXPECT_TRUE(s.Step());
118 return static_cast<size_t>(s.ColumnInt(0)); 120 return static_cast<size_t>(s.ColumnInt(0));
119 } 121 }
120 122
121 ShortcutsDatabase::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo( 123 ShortcutsDatabase::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo(
122 const ShortcutsDatabaseTestInfo& info) { 124 const ShortcutsDatabaseTestInfo& info) {
123 return ShortcutsDatabase::Shortcut( 125 return ShortcutsDatabase::Shortcut(
124 info.guid, ASCIIToUTF16(info.text), 126 info.guid, ASCIIToUTF16(info.text),
125 ShortcutsDatabase::Shortcut::MatchCore( 127 ShortcutsDatabase::Shortcut::MatchCore(
126 ASCIIToUTF16(info.fill_into_edit), GURL(info.destination_url), 128 ASCIIToUTF16(info.fill_into_edit), GURL(info.destination_url),
127 ASCIIToUTF16(info.contents), info.contents_class, 129 ASCIIToUTF16(info.contents), info.contents_class,
128 ASCIIToUTF16(info.description), info.description_class, 130 ASCIIToUTF16(info.description), info.description_class,
129 info.transition, info.type, ASCIIToUTF16(info.keyword)), 131 info.transition, info.type, ASCIIToUTF16(info.keyword)),
130 base::Time::Now() - base::TimeDelta::FromDays(info.days_from_now), 132 base::Time::Now() - base::TimeDelta::FromDays(info.days_from_now),
131 info.number_of_hits); 133 info.number_of_hits);
132 } 134 }
133 135
134 void ShortcutsDatabaseTest::AddAll() { 136 void ShortcutsDatabaseTest::AddAll() {
135 ClearDB(); 137 ClearDB();
136 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i) 138 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i)
137 db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[i])); 139 db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[i]));
138 EXPECT_EQ(arraysize(shortcut_test_db), CountRecords()); 140 EXPECT_EQ(arraysize(shortcut_test_db), CountRecords());
139 } 141 }
140 142
141
142 // Actual tests --------------------------------------------------------------- 143 // Actual tests ---------------------------------------------------------------
143 144
144 TEST_F(ShortcutsDatabaseTest, AddShortcut) { 145 TEST_F(ShortcutsDatabaseTest, AddShortcut) {
145 ClearDB(); 146 ClearDB();
146 EXPECT_EQ(0U, CountRecords()); 147 EXPECT_EQ(0U, CountRecords());
147 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[0]))); 148 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[0])));
148 EXPECT_EQ(1U, CountRecords()); 149 EXPECT_EQ(1U, CountRecords());
149 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[1]))); 150 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[1])));
150 EXPECT_EQ(2U, CountRecords()); 151 EXPECT_EQ(2U, CountRecords());
151 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[2]))); 152 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[2])));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 shortcuts.find(shortcut_test_db[0].guid); 202 shortcuts.find(shortcut_test_db[0].guid);
202 EXPECT_TRUE(it != shortcuts.end()); 203 EXPECT_TRUE(it != shortcuts.end());
203 204
204 it = shortcuts.find(shortcut_test_db[1].guid); 205 it = shortcuts.find(shortcut_test_db[1].guid);
205 EXPECT_TRUE(it == shortcuts.end()); 206 EXPECT_TRUE(it == shortcuts.end());
206 207
207 it = shortcuts.find(shortcut_test_db[2].guid); 208 it = shortcuts.find(shortcut_test_db[2].guid);
208 EXPECT_TRUE(it == shortcuts.end()); 209 EXPECT_TRUE(it == shortcuts.end());
209 } 210 }
210 211
211
212 TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) { 212 TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) {
213 AddAll(); 213 AddAll();
214 ShortcutsDatabase::GuidToShortcutMap shortcuts; 214 ShortcutsDatabase::GuidToShortcutMap shortcuts;
215 db_->LoadShortcuts(&shortcuts); 215 db_->LoadShortcuts(&shortcuts);
216 EXPECT_EQ(arraysize(shortcut_test_db), shortcuts.size()); 216 EXPECT_EQ(arraysize(shortcut_test_db), shortcuts.size());
217 EXPECT_TRUE(db_->DeleteAllShortcuts()); 217 EXPECT_TRUE(db_->DeleteAllShortcuts());
218 db_->LoadShortcuts(&shortcuts); 218 db_->LoadShortcuts(&shortcuts);
219 EXPECT_EQ(0U, shortcuts.size()); 219 EXPECT_EQ(0U, shortcuts.size());
220 } 220 }
221 221
222 TEST(ShortcutsDatabaseMigrationTest, MigrateTableAddFillIntoEdit) { 222 TEST(ShortcutsDatabaseMigrationTest, MigrateTableAddFillIntoEdit) {
223 // Use the pre-v0 test file to create a test database in a temp dir. 223 // Use the pre-v0 test file to create a test database in a temp dir.
224 base::FilePath sql_path; 224 base::FilePath sql_path = GetTestDataDir().AppendASCII(
225 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &sql_path));
226 sql_path = sql_path.AppendASCII("History").AppendASCII(
227 #if defined(OS_ANDROID) 225 #if defined(OS_ANDROID)
228 "Shortcuts.v1.sql"); 226 "Shortcuts.v1.sql");
229 #else 227 #else
230 "Shortcuts.no_fill_into_edit.sql"); 228 "Shortcuts.no_fill_into_edit.sql");
231 #endif 229 #endif
232 base::ScopedTempDir temp_dir; 230 base::ScopedTempDir temp_dir;
233 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 231 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
234 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts1.db")); 232 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts1.db"));
235 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path)); 233 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path));
236 234
(...skipping 27 matching lines...) Expand all
264 EXPECT_TRUE(statement.ColumnString(4).empty()); 262 EXPECT_TRUE(statement.ColumnString(4).empty());
265 } 263 }
266 EXPECT_TRUE(statement.Succeeded()); 264 EXPECT_TRUE(statement.Succeeded());
267 #if !defined(OS_WIN) 265 #if !defined(OS_WIN)
268 EXPECT_TRUE(temp_dir.Delete()); 266 EXPECT_TRUE(temp_dir.Delete());
269 #endif 267 #endif
270 } 268 }
271 269
272 TEST(ShortcutsDatabaseMigrationTest, MigrateV0ToV1) { 270 TEST(ShortcutsDatabaseMigrationTest, MigrateV0ToV1) {
273 // Use the v0 test file to create a test database in a temp dir. 271 // Use the v0 test file to create a test database in a temp dir.
274 base::FilePath sql_path; 272 base::FilePath sql_path = GetTestDataDir().AppendASCII("Shortcuts.v0.sql");
275 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &sql_path));
276 sql_path = sql_path.AppendASCII("History").AppendASCII("Shortcuts.v0.sql");
277 base::ScopedTempDir temp_dir; 273 base::ScopedTempDir temp_dir;
278 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 274 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
279 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts2.db")); 275 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts2.db"));
280 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path)); 276 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path));
281 277
282 // Create a ShortcutsDatabase from the test database, which will migrate the 278 // Create a ShortcutsDatabase from the test database, which will migrate the
283 // test database to the current version. 279 // test database to the current version.
284 { 280 {
285 scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path)); 281 scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path));
286 db->Init(); 282 db->Init();
287 } 283 }
288 284
289 // Check that all the old type values got converted to new values. 285 // Check that all the old type values got converted to new values.
290 sql::Connection connection; 286 sql::Connection connection;
291 ASSERT_TRUE(connection.Open(db_path)); 287 ASSERT_TRUE(connection.Open(db_path));
292 sql::Statement statement(connection.GetUniqueStatement( 288 sql::Statement statement(connection.GetUniqueStatement(
293 "SELECT count(1) FROM omni_box_shortcuts WHERE type in (9, 10, 11, 12)")); 289 "SELECT count(1) FROM omni_box_shortcuts WHERE type in (9, 10, 11, 12)"));
294 ASSERT_TRUE(statement.is_valid()); 290 ASSERT_TRUE(statement.is_valid());
295 while (statement.Step()) 291 while (statement.Step())
296 EXPECT_EQ(0, statement.ColumnInt(0)); 292 EXPECT_EQ(0, statement.ColumnInt(0));
297 EXPECT_TRUE(statement.Succeeded()); 293 EXPECT_TRUE(statement.Succeeded());
298 #if !defined(OS_WIN) 294 #if !defined(OS_WIN)
299 EXPECT_TRUE(temp_dir.Delete()); 295 EXPECT_TRUE(temp_dir.Delete());
300 #endif 296 #endif
301 } 297 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/BUILD.gn ('k') | components/test/data/omnibox/Shortcuts.no_fill_into_edit.sql » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698