OLD | NEW |
---|---|
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/message_loop/message_loop.h" | |
9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "base/time/time.h" | 13 #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" | 14 #include "components/omnibox/browser/autocomplete_match_type.h" |
16 #include "components/omnibox/browser/shortcuts_constants.h" | 15 #include "components/omnibox/browser/shortcuts_constants.h" |
17 #include "content/public/test/test_browser_thread_bundle.h" | |
18 #include "sql/statement.h" | 16 #include "sql/statement.h" |
19 #include "sql/test/test_helpers.h" | 17 #include "sql/test/test_helpers.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "ui/base/page_transition_types.h" | 19 #include "ui/base/page_transition_types.h" |
22 | 20 |
23 using base::ASCIIToUTF16; | 21 using base::ASCIIToUTF16; |
24 | 22 |
25 // Helpers -------------------------------------------------------------------- | 23 // Helpers -------------------------------------------------------------------- |
26 | 24 |
27 namespace { | 25 namespace { |
(...skipping 28 matching lines...) Expand all Loading... | |
56 ui::PAGE_TRANSITION_LINK, AutocompleteMatchType::HISTORY_TITLE, "", 0, | 54 ui::PAGE_TRANSITION_LINK, AutocompleteMatchType::HISTORY_TITLE, "", 0, |
57 5}, | 55 5}, |
58 }; | 56 }; |
59 | 57 |
60 typedef testing::Test ShortcutsDatabaseMigrationTest; | 58 typedef testing::Test ShortcutsDatabaseMigrationTest; |
61 | 59 |
62 // Checks that the database at |db| has the version 2 columns iff |is_v2|. | 60 // 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) { | 61 void CheckV2ColumnExistence(const base::FilePath& db_path, bool is_v2) { |
64 sql::Connection connection; | 62 sql::Connection connection; |
65 ASSERT_TRUE(connection.Open(db_path)); | 63 ASSERT_TRUE(connection.Open(db_path)); |
66 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", | 64 EXPECT_EQ(is_v2, |
67 "fill_into_edit")); | 65 connection.DoesColumnExist("omni_box_shortcuts", "fill_into_edit")); |
68 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", | 66 EXPECT_EQ(is_v2, |
69 "transition")); | 67 connection.DoesColumnExist("omni_box_shortcuts", "transition")); |
70 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "type")); | 68 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "type")); |
71 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "keyword")); | 69 EXPECT_EQ(is_v2, connection.DoesColumnExist("omni_box_shortcuts", "keyword")); |
72 } | 70 } |
73 | 71 |
72 const base::FilePath& GetTestDataDir() { | |
73 CR_DEFINE_STATIC_LOCAL(base::FilePath, dir, ()); | |
74 if (dir.empty()) { | |
75 PathService::Get(base::DIR_SOURCE_ROOT, &dir); | |
76 dir = dir.AppendASCII("components"); | |
77 dir = dir.AppendASCII("test"); | |
78 dir = dir.AppendASCII("data"); | |
79 dir = dir.AppendASCII("omnibox"); | |
80 } | |
81 return dir; | |
droger
2015/12/11 12:08:23
I don't think the static local is useful here.
I
Abhishek
2015/12/11 15:54:11
Done.
| |
82 } | |
83 | |
74 } // namespace | 84 } // namespace |
75 | 85 |
76 // ShortcutsDatabaseTest ------------------------------------------------------ | 86 // ShortcutsDatabaseTest ------------------------------------------------------ |
77 | 87 |
78 class ShortcutsDatabaseTest : public testing::Test { | 88 class ShortcutsDatabaseTest : public testing::Test { |
79 public: | 89 public: |
80 void SetUp() override; | 90 void SetUp() override; |
81 void TearDown() override; | 91 void TearDown() override; |
82 | 92 |
83 void ClearDB(); | 93 void ClearDB(); |
84 size_t CountRecords() const; | 94 size_t CountRecords() const; |
85 | 95 |
86 ShortcutsDatabase::Shortcut ShortcutFromTestInfo( | 96 ShortcutsDatabase::Shortcut ShortcutFromTestInfo( |
87 const ShortcutsDatabaseTestInfo& info); | 97 const ShortcutsDatabaseTestInfo& info); |
88 | 98 |
89 void AddAll(); | 99 void AddAll(); |
90 | 100 |
91 content::TestBrowserThreadBundle thread_bundle_; | 101 base::MessageLoop message_loop_; |
droger
2015/12/11 12:08:23
Is this even needed?
Abhishek
2015/12/11 15:54:11
Yeah! We don't require it now.
I've removed it.
| |
92 scoped_ptr<TestingProfile> profile_; | |
93 scoped_refptr<ShortcutsDatabase> db_; | 102 scoped_refptr<ShortcutsDatabase> db_; |
94 }; | 103 }; |
95 | 104 |
96 void ShortcutsDatabaseTest::SetUp() { | 105 void ShortcutsDatabaseTest::SetUp() { |
97 profile_.reset(new TestingProfile()); | 106 base::ScopedTempDir temp_dir; |
98 db_ = new ShortcutsDatabase( | 107 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
99 profile_->GetPath().Append(kShortcutsDatabaseName)); | 108 base::FilePath db_path(temp_dir.path().AppendASCII(kShortcutsDatabaseName)); |
droger
2015/12/11 12:08:23
This does not compile on windows, can you try usin
Abhishek
2015/12/11 15:54:11
Done.
| |
109 db_ = new ShortcutsDatabase(db_path); | |
100 ASSERT_TRUE(db_->Init()); | 110 ASSERT_TRUE(db_->Init()); |
101 ClearDB(); | 111 ClearDB(); |
102 } | 112 } |
103 | 113 |
104 void ShortcutsDatabaseTest::TearDown() { | 114 void ShortcutsDatabaseTest::TearDown() { |
105 db_ = NULL; | 115 db_ = NULL; |
106 } | 116 } |
107 | 117 |
108 void ShortcutsDatabaseTest::ClearDB() { | 118 void ShortcutsDatabaseTest::ClearDB() { |
109 sql::Statement | 119 sql::Statement s( |
110 s(db_->db_.GetUniqueStatement("DELETE FROM omni_box_shortcuts")); | 120 db_->db_.GetUniqueStatement("DELETE FROM omni_box_shortcuts")); |
111 EXPECT_TRUE(s.Run()); | 121 EXPECT_TRUE(s.Run()); |
112 } | 122 } |
113 | 123 |
114 size_t ShortcutsDatabaseTest::CountRecords() const { | 124 size_t ShortcutsDatabaseTest::CountRecords() const { |
115 sql::Statement s(db_->db_.GetUniqueStatement( | 125 sql::Statement s( |
116 "SELECT count(*) FROM omni_box_shortcuts")); | 126 db_->db_.GetUniqueStatement("SELECT count(*) FROM omni_box_shortcuts")); |
117 EXPECT_TRUE(s.Step()); | 127 EXPECT_TRUE(s.Step()); |
118 return static_cast<size_t>(s.ColumnInt(0)); | 128 return static_cast<size_t>(s.ColumnInt(0)); |
119 } | 129 } |
120 | 130 |
121 ShortcutsDatabase::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo( | 131 ShortcutsDatabase::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo( |
122 const ShortcutsDatabaseTestInfo& info) { | 132 const ShortcutsDatabaseTestInfo& info) { |
123 return ShortcutsDatabase::Shortcut( | 133 return ShortcutsDatabase::Shortcut( |
124 info.guid, ASCIIToUTF16(info.text), | 134 info.guid, ASCIIToUTF16(info.text), |
125 ShortcutsDatabase::Shortcut::MatchCore( | 135 ShortcutsDatabase::Shortcut::MatchCore( |
126 ASCIIToUTF16(info.fill_into_edit), GURL(info.destination_url), | 136 ASCIIToUTF16(info.fill_into_edit), GURL(info.destination_url), |
127 ASCIIToUTF16(info.contents), info.contents_class, | 137 ASCIIToUTF16(info.contents), info.contents_class, |
128 ASCIIToUTF16(info.description), info.description_class, | 138 ASCIIToUTF16(info.description), info.description_class, |
129 info.transition, info.type, ASCIIToUTF16(info.keyword)), | 139 info.transition, info.type, ASCIIToUTF16(info.keyword)), |
130 base::Time::Now() - base::TimeDelta::FromDays(info.days_from_now), | 140 base::Time::Now() - base::TimeDelta::FromDays(info.days_from_now), |
131 info.number_of_hits); | 141 info.number_of_hits); |
132 } | 142 } |
133 | 143 |
134 void ShortcutsDatabaseTest::AddAll() { | 144 void ShortcutsDatabaseTest::AddAll() { |
135 ClearDB(); | 145 ClearDB(); |
136 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i) | 146 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i) |
137 db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[i])); | 147 db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[i])); |
138 EXPECT_EQ(arraysize(shortcut_test_db), CountRecords()); | 148 EXPECT_EQ(arraysize(shortcut_test_db), CountRecords()); |
139 } | 149 } |
140 | 150 |
141 | |
142 // Actual tests --------------------------------------------------------------- | 151 // Actual tests --------------------------------------------------------------- |
143 | 152 |
144 TEST_F(ShortcutsDatabaseTest, AddShortcut) { | 153 TEST_F(ShortcutsDatabaseTest, AddShortcut) { |
145 ClearDB(); | 154 ClearDB(); |
146 EXPECT_EQ(0U, CountRecords()); | 155 EXPECT_EQ(0U, CountRecords()); |
147 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[0]))); | 156 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[0]))); |
148 EXPECT_EQ(1U, CountRecords()); | 157 EXPECT_EQ(1U, CountRecords()); |
149 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[1]))); | 158 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[1]))); |
150 EXPECT_EQ(2U, CountRecords()); | 159 EXPECT_EQ(2U, CountRecords()); |
151 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[2]))); | 160 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[2]))); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 shortcuts.find(shortcut_test_db[0].guid); | 210 shortcuts.find(shortcut_test_db[0].guid); |
202 EXPECT_TRUE(it != shortcuts.end()); | 211 EXPECT_TRUE(it != shortcuts.end()); |
203 | 212 |
204 it = shortcuts.find(shortcut_test_db[1].guid); | 213 it = shortcuts.find(shortcut_test_db[1].guid); |
205 EXPECT_TRUE(it == shortcuts.end()); | 214 EXPECT_TRUE(it == shortcuts.end()); |
206 | 215 |
207 it = shortcuts.find(shortcut_test_db[2].guid); | 216 it = shortcuts.find(shortcut_test_db[2].guid); |
208 EXPECT_TRUE(it == shortcuts.end()); | 217 EXPECT_TRUE(it == shortcuts.end()); |
209 } | 218 } |
210 | 219 |
211 | |
212 TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) { | 220 TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) { |
213 AddAll(); | 221 AddAll(); |
214 ShortcutsDatabase::GuidToShortcutMap shortcuts; | 222 ShortcutsDatabase::GuidToShortcutMap shortcuts; |
215 db_->LoadShortcuts(&shortcuts); | 223 db_->LoadShortcuts(&shortcuts); |
216 EXPECT_EQ(arraysize(shortcut_test_db), shortcuts.size()); | 224 EXPECT_EQ(arraysize(shortcut_test_db), shortcuts.size()); |
217 EXPECT_TRUE(db_->DeleteAllShortcuts()); | 225 EXPECT_TRUE(db_->DeleteAllShortcuts()); |
218 db_->LoadShortcuts(&shortcuts); | 226 db_->LoadShortcuts(&shortcuts); |
219 EXPECT_EQ(0U, shortcuts.size()); | 227 EXPECT_EQ(0U, shortcuts.size()); |
220 } | 228 } |
221 | 229 |
222 TEST(ShortcutsDatabaseMigrationTest, MigrateTableAddFillIntoEdit) { | 230 TEST(ShortcutsDatabaseMigrationTest, MigrateTableAddFillIntoEdit) { |
223 // Use the pre-v0 test file to create a test database in a temp dir. | 231 // Use the pre-v0 test file to create a test database in a temp dir. |
224 base::FilePath sql_path; | 232 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) | 233 #if defined(OS_ANDROID) |
228 "Shortcuts.v1.sql"); | 234 "Shortcuts.v1.sql"); |
229 #else | 235 #else |
230 "Shortcuts.no_fill_into_edit.sql"); | 236 "Shortcuts.no_fill_into_edit.sql"); |
231 #endif | 237 #endif |
232 base::ScopedTempDir temp_dir; | 238 base::ScopedTempDir temp_dir; |
233 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 239 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
234 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts1.db")); | 240 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts1.db")); |
235 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path)); | 241 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path)); |
236 | 242 |
(...skipping 27 matching lines...) Expand all Loading... | |
264 EXPECT_TRUE(statement.ColumnString(4).empty()); | 270 EXPECT_TRUE(statement.ColumnString(4).empty()); |
265 } | 271 } |
266 EXPECT_TRUE(statement.Succeeded()); | 272 EXPECT_TRUE(statement.Succeeded()); |
267 #if !defined(OS_WIN) | 273 #if !defined(OS_WIN) |
268 EXPECT_TRUE(temp_dir.Delete()); | 274 EXPECT_TRUE(temp_dir.Delete()); |
269 #endif | 275 #endif |
270 } | 276 } |
271 | 277 |
272 TEST(ShortcutsDatabaseMigrationTest, MigrateV0ToV1) { | 278 TEST(ShortcutsDatabaseMigrationTest, MigrateV0ToV1) { |
273 // Use the v0 test file to create a test database in a temp dir. | 279 // Use the v0 test file to create a test database in a temp dir. |
274 base::FilePath sql_path; | 280 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; | 281 base::ScopedTempDir temp_dir; |
278 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 282 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
279 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts2.db")); | 283 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts2.db")); |
280 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path)); | 284 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path)); |
281 | 285 |
282 // Create a ShortcutsDatabase from the test database, which will migrate the | 286 // Create a ShortcutsDatabase from the test database, which will migrate the |
283 // test database to the current version. | 287 // test database to the current version. |
284 { | 288 { |
285 scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path)); | 289 scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path)); |
286 db->Init(); | 290 db->Init(); |
287 } | 291 } |
288 | 292 |
289 // Check that all the old type values got converted to new values. | 293 // Check that all the old type values got converted to new values. |
290 sql::Connection connection; | 294 sql::Connection connection; |
291 ASSERT_TRUE(connection.Open(db_path)); | 295 ASSERT_TRUE(connection.Open(db_path)); |
292 sql::Statement statement(connection.GetUniqueStatement( | 296 sql::Statement statement(connection.GetUniqueStatement( |
293 "SELECT count(1) FROM omni_box_shortcuts WHERE type in (9, 10, 11, 12)")); | 297 "SELECT count(1) FROM omni_box_shortcuts WHERE type in (9, 10, 11, 12)")); |
294 ASSERT_TRUE(statement.is_valid()); | 298 ASSERT_TRUE(statement.is_valid()); |
295 while (statement.Step()) | 299 while (statement.Step()) |
296 EXPECT_EQ(0, statement.ColumnInt(0)); | 300 EXPECT_EQ(0, statement.ColumnInt(0)); |
297 EXPECT_TRUE(statement.Succeeded()); | 301 EXPECT_TRUE(statement.Succeeded()); |
298 #if !defined(OS_WIN) | 302 #if !defined(OS_WIN) |
299 EXPECT_TRUE(temp_dir.Delete()); | 303 EXPECT_TRUE(temp_dir.Delete()); |
300 #endif | 304 #endif |
301 } | 305 } |
OLD | NEW |