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 "base/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
6 #include "base/format_macros.h" | 6 #include "base/format_macros.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "chrome/browser/history/shortcuts_database.h" | 11 #include "chrome/browser/history/shortcuts_database.h" |
| 12 #include "chrome/common/autocomplete_match_type.h" |
12 #include "chrome/common/chrome_constants.h" | 13 #include "chrome/common/chrome_constants.h" |
13 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
14 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "content/public/common/page_transition_types.h" |
15 #include "sql/statement.h" | 17 #include "sql/statement.h" |
16 #include "sql/test/test_helpers.h" | 18 #include "sql/test/test_helpers.h" |
17 | 19 |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
20 using base::ASCIIToUTF16; | 22 using base::ASCIIToUTF16; |
21 | 23 |
22 // Helpers -------------------------------------------------------------------- | 24 // Helpers -------------------------------------------------------------------- |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 struct ShortcutsDatabaseTestInfo { | 28 struct ShortcutsDatabaseTestInfo { |
27 std::string guid; | 29 std::string guid; |
28 std::string text; | 30 std::string text; |
29 std::string fill_into_edit; | 31 std::string fill_into_edit; |
30 std::string destination_url; | 32 std::string destination_url; |
31 std::string contents; | 33 std::string contents; |
32 std::string contents_class; | 34 std::string contents_class; |
33 std::string stripped_contents_class; // |contents_class| with matches removed | |
34 std::string description; | 35 std::string description; |
35 std::string description_class; | 36 std::string description_class; |
36 std::string stripped_description_class; | |
37 content::PageTransition transition; | 37 content::PageTransition transition; |
38 AutocompleteMatch::Type type; | 38 AutocompleteMatchType::Type type; |
39 std::string keyword; | 39 std::string keyword; |
40 int days_from_now; | 40 int days_from_now; |
41 int number_of_hits; | 41 int number_of_hits; |
42 } shortcut_test_db[] = { | 42 } shortcut_test_db[] = { |
43 { "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", "goog", "www.google.com", | 43 { "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", "goog", "www.google.com", |
44 "http://www.google.com/", "Google", "0,1,4,0", "0,1,4,0", "Google", | 44 "http://www.google.com/", "Google", "0,1,4,0", "Google", "0,1", |
45 "0,3,4,1", "0,1", content::PAGE_TRANSITION_GENERATED, | 45 content::PAGE_TRANSITION_GENERATED, AutocompleteMatchType::SEARCH_HISTORY, |
46 AutocompleteMatchType::SEARCH_HISTORY, "google.com", 1, 100, }, | 46 "google.com", 1, 100, }, |
47 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "slash", "slashdot.org", | 47 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "slash", "slashdot.org", |
48 "http://slashdot.org/", "slashdot.org", "0,3,5,1", "0,1", | 48 "http://slashdot.org/", "slashdot.org", "0,1", |
49 "Slashdot - News for nerds, stuff that matters", "0,2,5,0", "0,0", | 49 "Slashdot - News for nerds, stuff that matters", "0,0", |
50 content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 0, | 50 content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 0, |
51 100}, | 51 100}, |
52 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "news", "slashdot.org", | 52 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "news", "slashdot.org", |
53 "http://slashdot.org/", "slashdot.org", "0,1", "0,1", | 53 "http://slashdot.org/", "slashdot.org", "0,1", |
54 "Slashdot - News for nerds, stuff that matters", "0,0,11,2,15,0", "0,0", | 54 "Slashdot - News for nerds, stuff that matters", "0,0", |
55 content::PAGE_TRANSITION_LINK, AutocompleteMatchType::HISTORY_TITLE, "", 0, | 55 content::PAGE_TRANSITION_LINK, AutocompleteMatchType::HISTORY_TITLE, "", 0, |
56 5}, | 56 5}, |
57 }; | 57 }; |
58 | 58 |
59 typedef testing::Test ShortcutsDatabaseMigrationTest; | 59 typedef testing::Test ShortcutsDatabaseMigrationTest; |
60 | 60 |
61 // Checks that the database at |db| has the version 2 columns iff |is_v2|. | 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) { | 62 void CheckV2ColumnExistence(const base::FilePath& db_path, bool is_v2) { |
63 sql::Connection connection; | 63 sql::Connection connection; |
64 ASSERT_TRUE(connection.Open(db_path)); | 64 ASSERT_TRUE(connection.Open(db_path)); |
(...skipping 13 matching lines...) Expand all Loading... |
78 // ShortcutsDatabaseTest ------------------------------------------------------ | 78 // ShortcutsDatabaseTest ------------------------------------------------------ |
79 | 79 |
80 class ShortcutsDatabaseTest : public testing::Test { | 80 class ShortcutsDatabaseTest : public testing::Test { |
81 public: | 81 public: |
82 virtual void SetUp(); | 82 virtual void SetUp(); |
83 virtual void TearDown(); | 83 virtual void TearDown(); |
84 | 84 |
85 void ClearDB(); | 85 void ClearDB(); |
86 size_t CountRecords() const; | 86 size_t CountRecords() const; |
87 | 87 |
88 ShortcutsBackend::Shortcut ShortcutFromTestInfo( | 88 ShortcutsDatabase::Shortcut ShortcutFromTestInfo( |
89 const ShortcutsDatabaseTestInfo& info); | 89 const ShortcutsDatabaseTestInfo& info); |
90 | 90 |
91 void AddAll(); | 91 void AddAll(); |
92 | 92 |
93 scoped_ptr<TestingProfile> profile_; | 93 scoped_ptr<TestingProfile> profile_; |
94 scoped_refptr<ShortcutsDatabase> db_; | 94 scoped_refptr<ShortcutsDatabase> db_; |
95 }; | 95 }; |
96 | 96 |
97 void ShortcutsDatabaseTest::SetUp() { | 97 void ShortcutsDatabaseTest::SetUp() { |
98 profile_.reset(new TestingProfile()); | 98 profile_.reset(new TestingProfile()); |
(...skipping 13 matching lines...) Expand all Loading... |
112 EXPECT_TRUE(s.Run()); | 112 EXPECT_TRUE(s.Run()); |
113 } | 113 } |
114 | 114 |
115 size_t ShortcutsDatabaseTest::CountRecords() const { | 115 size_t ShortcutsDatabaseTest::CountRecords() const { |
116 sql::Statement s(db_->db_.GetUniqueStatement( | 116 sql::Statement s(db_->db_.GetUniqueStatement( |
117 "SELECT count(*) FROM omni_box_shortcuts")); | 117 "SELECT count(*) FROM omni_box_shortcuts")); |
118 EXPECT_TRUE(s.Step()); | 118 EXPECT_TRUE(s.Step()); |
119 return static_cast<size_t>(s.ColumnInt(0)); | 119 return static_cast<size_t>(s.ColumnInt(0)); |
120 } | 120 } |
121 | 121 |
122 ShortcutsBackend::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo( | 122 ShortcutsDatabase::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo( |
123 const ShortcutsDatabaseTestInfo& info) { | 123 const ShortcutsDatabaseTestInfo& info) { |
124 return ShortcutsBackend::Shortcut( | 124 return ShortcutsDatabase::Shortcut( |
125 info.guid, ASCIIToUTF16(info.text), | 125 info.guid, ASCIIToUTF16(info.text), |
126 ShortcutsBackend::Shortcut::MatchCore( | 126 ShortcutsDatabase::Shortcut::MatchCore( |
127 ASCIIToUTF16(info.fill_into_edit), GURL(info.destination_url), | 127 ASCIIToUTF16(info.fill_into_edit), GURL(info.destination_url), |
128 ASCIIToUTF16(info.contents), | 128 ASCIIToUTF16(info.contents), info.contents_class, |
129 AutocompleteMatch::ClassificationsFromString(info.contents_class), | 129 ASCIIToUTF16(info.description), info.description_class, |
130 ASCIIToUTF16(info.description), | |
131 AutocompleteMatch::ClassificationsFromString(info.description_class), | |
132 info.transition, info.type, ASCIIToUTF16(info.keyword)), | 130 info.transition, info.type, ASCIIToUTF16(info.keyword)), |
133 base::Time::Now() - base::TimeDelta::FromDays(info.days_from_now), | 131 base::Time::Now() - base::TimeDelta::FromDays(info.days_from_now), |
134 info.number_of_hits); | 132 info.number_of_hits); |
135 } | 133 } |
136 | 134 |
137 void ShortcutsDatabaseTest::AddAll() { | 135 void ShortcutsDatabaseTest::AddAll() { |
138 ClearDB(); | 136 ClearDB(); |
139 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i) | 137 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i) |
140 db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[i])); | 138 db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[i])); |
141 EXPECT_EQ(arraysize(shortcut_test_db), CountRecords()); | 139 EXPECT_EQ(arraysize(shortcut_test_db), CountRecords()); |
142 } | 140 } |
143 | 141 |
144 | 142 |
145 // Actual tests --------------------------------------------------------------- | 143 // Actual tests --------------------------------------------------------------- |
146 | 144 |
147 TEST_F(ShortcutsDatabaseTest, AddShortcut) { | 145 TEST_F(ShortcutsDatabaseTest, AddShortcut) { |
148 ClearDB(); | 146 ClearDB(); |
149 EXPECT_EQ(0U, CountRecords()); | 147 EXPECT_EQ(0U, CountRecords()); |
150 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[0]))); | 148 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[0]))); |
151 EXPECT_EQ(1U, CountRecords()); | 149 EXPECT_EQ(1U, CountRecords()); |
152 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[1]))); | 150 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[1]))); |
153 EXPECT_EQ(2U, CountRecords()); | 151 EXPECT_EQ(2U, CountRecords()); |
154 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[2]))); | 152 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[2]))); |
155 EXPECT_EQ(3U, CountRecords()); | 153 EXPECT_EQ(3U, CountRecords()); |
156 } | 154 } |
157 | 155 |
158 TEST_F(ShortcutsDatabaseTest, UpdateShortcut) { | 156 TEST_F(ShortcutsDatabaseTest, UpdateShortcut) { |
159 AddAll(); | 157 AddAll(); |
160 ShortcutsBackend::Shortcut shortcut( | 158 ShortcutsDatabase::Shortcut shortcut( |
161 ShortcutFromTestInfo(shortcut_test_db[1])); | 159 ShortcutFromTestInfo(shortcut_test_db[1])); |
162 shortcut.match_core.contents = ASCIIToUTF16("gro.todhsals"); | 160 shortcut.match_core.contents = ASCIIToUTF16("gro.todhsals"); |
163 EXPECT_TRUE(db_->UpdateShortcut(shortcut)); | 161 EXPECT_TRUE(db_->UpdateShortcut(shortcut)); |
164 ShortcutsDatabase::GuidToShortcutMap shortcuts; | 162 ShortcutsDatabase::GuidToShortcutMap shortcuts; |
165 db_->LoadShortcuts(&shortcuts); | 163 db_->LoadShortcuts(&shortcuts); |
166 ShortcutsDatabase::GuidToShortcutMap::const_iterator it( | 164 ShortcutsDatabase::GuidToShortcutMap::const_iterator it( |
167 shortcuts.find(shortcut.id)); | 165 shortcuts.find(shortcut.id)); |
168 EXPECT_TRUE(it != shortcuts.end()); | 166 EXPECT_TRUE(it != shortcuts.end()); |
169 EXPECT_TRUE(it->second.match_core.contents == shortcut.match_core.contents); | 167 EXPECT_TRUE(it->second.match_core.contents == shortcut.match_core.contents); |
170 } | 168 } |
171 | 169 |
172 TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithIds) { | 170 TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithIds) { |
173 AddAll(); | 171 AddAll(); |
174 std::vector<std::string> shortcut_ids; | 172 std::vector<std::string> shortcut_ids; |
175 shortcut_ids.push_back(shortcut_test_db[0].guid); | 173 shortcut_ids.push_back(shortcut_test_db[0].guid); |
176 shortcut_ids.push_back(shortcut_test_db[2].guid); | 174 shortcut_ids.push_back(shortcut_test_db[2].guid); |
177 EXPECT_TRUE(db_->DeleteShortcutsWithIds(shortcut_ids)); | 175 EXPECT_TRUE(db_->DeleteShortcutsWithIDs(shortcut_ids)); |
178 EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords()); | 176 EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords()); |
179 | 177 |
180 ShortcutsDatabase::GuidToShortcutMap shortcuts; | 178 ShortcutsDatabase::GuidToShortcutMap shortcuts; |
181 db_->LoadShortcuts(&shortcuts); | 179 db_->LoadShortcuts(&shortcuts); |
182 | 180 |
183 ShortcutsDatabase::GuidToShortcutMap::iterator it = | 181 ShortcutsDatabase::GuidToShortcutMap::iterator it = |
184 shortcuts.find(shortcut_test_db[0].guid); | 182 shortcuts.find(shortcut_test_db[0].guid); |
185 EXPECT_TRUE(it == shortcuts.end()); | 183 EXPECT_TRUE(it == shortcuts.end()); |
186 | 184 |
187 it = shortcuts.find(shortcut_test_db[1].guid); | 185 it = shortcuts.find(shortcut_test_db[1].guid); |
188 EXPECT_TRUE(it != shortcuts.end()); | 186 EXPECT_TRUE(it != shortcuts.end()); |
189 | 187 |
190 it = shortcuts.find(shortcut_test_db[2].guid); | 188 it = shortcuts.find(shortcut_test_db[2].guid); |
191 EXPECT_TRUE(it == shortcuts.end()); | 189 EXPECT_TRUE(it == shortcuts.end()); |
192 } | 190 } |
193 | 191 |
194 TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithUrl) { | 192 TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithURL) { |
195 AddAll(); | 193 AddAll(); |
196 | 194 |
197 EXPECT_TRUE(db_->DeleteShortcutsWithUrl("http://slashdot.org/")); | 195 EXPECT_TRUE(db_->DeleteShortcutsWithURL("http://slashdot.org/")); |
198 EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords()); | 196 EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords()); |
199 | 197 |
200 ShortcutsDatabase::GuidToShortcutMap shortcuts; | 198 ShortcutsDatabase::GuidToShortcutMap shortcuts; |
201 db_->LoadShortcuts(&shortcuts); | 199 db_->LoadShortcuts(&shortcuts); |
202 | 200 |
203 ShortcutsDatabase::GuidToShortcutMap::iterator it = | 201 ShortcutsDatabase::GuidToShortcutMap::iterator it = |
204 shortcuts.find(shortcut_test_db[0].guid); | 202 shortcuts.find(shortcut_test_db[0].guid); |
205 EXPECT_TRUE(it != shortcuts.end()); | 203 EXPECT_TRUE(it != shortcuts.end()); |
206 | 204 |
207 it = shortcuts.find(shortcut_test_db[1].guid); | 205 it = shortcuts.find(shortcut_test_db[1].guid); |
208 EXPECT_TRUE(it == shortcuts.end()); | 206 EXPECT_TRUE(it == shortcuts.end()); |
209 | 207 |
210 it = shortcuts.find(shortcut_test_db[2].guid); | 208 it = shortcuts.find(shortcut_test_db[2].guid); |
211 EXPECT_TRUE(it == shortcuts.end()); | 209 EXPECT_TRUE(it == shortcuts.end()); |
212 } | 210 } |
213 | 211 |
214 TEST_F(ShortcutsDatabaseTest, LoadShortcuts) { | |
215 AddAll(); | |
216 ShortcutsDatabase::GuidToShortcutMap shortcuts; | |
217 db_->LoadShortcuts(&shortcuts); | |
218 | |
219 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i) { | |
220 SCOPED_TRACE(base::StringPrintf("Comparing shortcut #%" PRIuS, i)); | |
221 ShortcutsDatabase::GuidToShortcutMap::const_iterator it( | |
222 shortcuts.find(shortcut_test_db[i].guid)); | |
223 ASSERT_TRUE(it != shortcuts.end()); | |
224 const ShortcutsBackend::Shortcut& shortcut = it->second; | |
225 EXPECT_EQ(ASCIIToUTF16(shortcut_test_db[i].text), shortcut.text); | |
226 EXPECT_EQ(ASCIIToUTF16(shortcut_test_db[i].fill_into_edit), | |
227 shortcut.match_core.fill_into_edit); | |
228 EXPECT_EQ(shortcut_test_db[i].destination_url, | |
229 shortcut.match_core.destination_url.spec()); | |
230 EXPECT_EQ(ASCIIToUTF16(shortcut_test_db[i].contents), | |
231 shortcut.match_core.contents); | |
232 EXPECT_EQ(shortcut_test_db[i].stripped_contents_class, | |
233 AutocompleteMatch::ClassificationsToString( | |
234 shortcut.match_core.contents_class)); | |
235 EXPECT_EQ(ASCIIToUTF16(shortcut_test_db[i].description), | |
236 shortcut.match_core.description); | |
237 EXPECT_EQ(shortcut_test_db[i].stripped_description_class, | |
238 AutocompleteMatch::ClassificationsToString( | |
239 shortcut.match_core.description_class)); | |
240 EXPECT_EQ(shortcut_test_db[i].transition, shortcut.match_core.transition); | |
241 EXPECT_EQ(shortcut_test_db[i].type, shortcut.match_core.type); | |
242 EXPECT_EQ(ASCIIToUTF16(shortcut_test_db[i].keyword), | |
243 shortcut.match_core.keyword); | |
244 EXPECT_EQ(shortcut_test_db[i].number_of_hits, shortcut.number_of_hits); | |
245 // We don't bother trying to check the |days_from_now| field, since dealing | |
246 // with times in unittests is annoying. | |
247 } | |
248 } | |
249 | 212 |
250 TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) { | 213 TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) { |
251 AddAll(); | 214 AddAll(); |
252 ShortcutsDatabase::GuidToShortcutMap shortcuts; | 215 ShortcutsDatabase::GuidToShortcutMap shortcuts; |
253 db_->LoadShortcuts(&shortcuts); | 216 db_->LoadShortcuts(&shortcuts); |
254 EXPECT_EQ(arraysize(shortcut_test_db), shortcuts.size()); | 217 EXPECT_EQ(arraysize(shortcut_test_db), shortcuts.size()); |
255 EXPECT_TRUE(db_->DeleteAllShortcuts()); | 218 EXPECT_TRUE(db_->DeleteAllShortcuts()); |
256 db_->LoadShortcuts(&shortcuts); | 219 db_->LoadShortcuts(&shortcuts); |
257 EXPECT_EQ(0U, shortcuts.size()); | 220 EXPECT_EQ(0U, shortcuts.size()); |
258 } | 221 } |
(...skipping 27 matching lines...) Expand all Loading... |
286 "FROM omni_box_shortcuts")); | 249 "FROM omni_box_shortcuts")); |
287 ASSERT_TRUE(statement.is_valid()); | 250 ASSERT_TRUE(statement.is_valid()); |
288 while (statement.Step()) { | 251 while (statement.Step()) { |
289 // |fill_into_edit| should have been copied from the |url|. | 252 // |fill_into_edit| should have been copied from the |url|. |
290 EXPECT_EQ(statement.ColumnString(1), statement.ColumnString(0)); | 253 EXPECT_EQ(statement.ColumnString(1), statement.ColumnString(0)); |
291 | 254 |
292 // The other three columns have default values. | 255 // The other three columns have default values. |
293 EXPECT_EQ(content::PAGE_TRANSITION_TYPED, | 256 EXPECT_EQ(content::PAGE_TRANSITION_TYPED, |
294 static_cast<content::PageTransition>(statement.ColumnInt(2))); | 257 static_cast<content::PageTransition>(statement.ColumnInt(2))); |
295 EXPECT_EQ(AutocompleteMatchType::HISTORY_TITLE, | 258 EXPECT_EQ(AutocompleteMatchType::HISTORY_TITLE, |
296 static_cast<AutocompleteMatch::Type>(statement.ColumnInt(3))); | 259 static_cast<AutocompleteMatchType::Type>(statement.ColumnInt(3))); |
297 EXPECT_TRUE(statement.ColumnString(4).empty()); | 260 EXPECT_TRUE(statement.ColumnString(4).empty()); |
298 } | 261 } |
299 EXPECT_TRUE(statement.Succeeded()); | 262 EXPECT_TRUE(statement.Succeeded()); |
300 } | 263 } |
301 | 264 |
302 } // namespace history | 265 } // namespace history |
OLD | NEW |