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. 4 5 #include "base/files/scoped_temp_dir.h" 6 #include "base/path_service.h" 7 #include "base/strings/stringprintf.h" 8 #include "base/strings/utf_string_conversions.h" 9 #include "base/time/time.h" 10 #include "chrome/browser/history/shortcuts_database.h" 11 #include "chrome/test/base/testing_profile.h" 12 #include "sql/statement.h" 13 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace history { 17 18 struct ShortcutsDatabaseTestInfo { 19 std::string guid; 20 std::string url; 21 std::string title; // The text that orginally was searched for. 22 std::string contents; 23 std::string contents_class; 24 std::string description; 25 std::string description_class; 26 int typed_count; 27 int days_from_now; 28 } shortcut_test_db[] = { 29 { "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", 30 "http://www.google.com/", "goog", 31 "Google", "0,1,4,0", "Google", "0,3,4,1", 100, 1 }, 32 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", 33 "http://slashdot.org/", "slash", 34 "slashdot.org", "0,3,5,1", 35 "Slashdot - News for nerds, stuff that matters", "0,2,5,0", 100, 0}, 36 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", 37 "http://slashdot.org/", "news", 38 "slashdot.org", "0,1", 39 "Slashdot - News for nerds, stuff that matters", "0,0,11,2,15,0", 5, 0}, 40 }; 41 42 class ShortcutsDatabaseTest : public testing::Test { 43 public: 44 virtual void SetUp(); 45 virtual void TearDown(); 46 47 void ClearDB(); 48 size_t CountRecords() const; 49 50 ShortcutsBackend::Shortcut ShortcutFromTestInfo( 51 const ShortcutsDatabaseTestInfo& info); 52 53 void AddAll(); 54 55 scoped_ptr<TestingProfile> profile_; 56 scoped_refptr<ShortcutsDatabase> db_; 57 }; 58 59 void ShortcutsDatabaseTest::SetUp() { 60 profile_.reset(new TestingProfile()); 61 db_ = new ShortcutsDatabase(profile_.get()); 62 ASSERT_TRUE(db_->Init()); 63 ClearDB(); 64 } 65 66 void ShortcutsDatabaseTest::TearDown() { 67 db_ = NULL; 68 } 69 70 void ShortcutsDatabaseTest::ClearDB() { 71 sql::Statement 72 s(db_->db_.GetUniqueStatement("DELETE FROM omni_box_shortcuts")); 73 EXPECT_TRUE(s.Run()); 74 } 75 76 size_t ShortcutsDatabaseTest::CountRecords() const { 77 sql::Statement s(db_->db_.GetUniqueStatement( 78 "SELECT count(*) FROM omni_box_shortcuts")); 79 EXPECT_TRUE(s.Step()); 80 return static_cast<size_t>(s.ColumnInt(0)); 81 } 82 83 ShortcutsBackend::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo( 84 const ShortcutsDatabaseTestInfo& info) { 85 return ShortcutsBackend::Shortcut(info.guid, ASCIIToUTF16(info.title), 86 GURL(info.url), ASCIIToUTF16(info.contents), 87 AutocompleteMatch::ClassificationsFromString(info.contents_class), 88 ASCIIToUTF16(info.description), 89 AutocompleteMatch::ClassificationsFromString(info.description_class), 90 base::Time::Now() - base::TimeDelta::FromDays(info.days_from_now), 91 info.typed_count); 92 } 93 94 void ShortcutsDatabaseTest::AddAll() { 95 ClearDB(); 96 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i) { 97 db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[i])); 98 } 99 EXPECT_EQ(arraysize(shortcut_test_db), CountRecords()); 100 } 101 102 TEST_F(ShortcutsDatabaseTest, AddShortcut) { 103 ClearDB(); 104 EXPECT_EQ(0U, CountRecords()); 105 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[0]))); 106 EXPECT_EQ(1U, CountRecords()); 107 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[1]))); 108 EXPECT_EQ(2U, CountRecords()); 109 EXPECT_TRUE(db_->AddShortcut(ShortcutFromTestInfo(shortcut_test_db[2]))); 110 EXPECT_EQ(3U, CountRecords()); 111 } 112 113 TEST_F(ShortcutsDatabaseTest, UpdateShortcut) { 114 AddAll(); 115 ShortcutsBackend::Shortcut shortcut( 116 ShortcutFromTestInfo(shortcut_test_db[1])); 117 shortcut.contents = ASCIIToUTF16("gro.todhsals"); 118 EXPECT_TRUE(db_->UpdateShortcut(shortcut)); 119 ShortcutsDatabase::GuidToShortcutMap shortcuts; 120 EXPECT_TRUE(db_->LoadShortcuts(&shortcuts)); 121 ShortcutsDatabase::GuidToShortcutMap::iterator it = 122 shortcuts.find(shortcut.id); 123 EXPECT_TRUE(it != shortcuts.end()); 124 EXPECT_TRUE(it->second.contents == shortcut.contents); 125 } 126 127 TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithIds) { 128 AddAll(); 129 std::vector<std::string> shortcut_ids; 130 shortcut_ids.push_back(shortcut_test_db[0].guid); 131 shortcut_ids.push_back(shortcut_test_db[2].guid); 132 EXPECT_TRUE(db_->DeleteShortcutsWithIds(shortcut_ids)); 133 EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords()); 134 135 ShortcutsDatabase::GuidToShortcutMap shortcuts; 136 EXPECT_TRUE(db_->LoadShortcuts(&shortcuts)); 137 138 ShortcutsDatabase::GuidToShortcutMap::iterator it = 139 shortcuts.find(shortcut_test_db[0].guid); 140 EXPECT_TRUE(it == shortcuts.end()); 141 142 it = shortcuts.find(shortcut_test_db[1].guid); 143 EXPECT_TRUE(it != shortcuts.end()); 144 145 it = shortcuts.find(shortcut_test_db[2].guid); 146 EXPECT_TRUE(it == shortcuts.end()); 147 } 148 149 TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithUrl) { 150 AddAll(); 151 152 EXPECT_TRUE(db_->DeleteShortcutsWithUrl("http://slashdot.org/")); 153 EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords()); 154 155 ShortcutsDatabase::GuidToShortcutMap shortcuts; 156 EXPECT_TRUE(db_->LoadShortcuts(&shortcuts)); 157 158 ShortcutsDatabase::GuidToShortcutMap::iterator it = 159 shortcuts.find(shortcut_test_db[0].guid); 160 EXPECT_TRUE(it != shortcuts.end()); 161 162 it = shortcuts.find(shortcut_test_db[1].guid); 163 EXPECT_TRUE(it == shortcuts.end()); 164 165 it = shortcuts.find(shortcut_test_db[2].guid); 166 EXPECT_TRUE(it == shortcuts.end()); 167 } 168 169 TEST_F(ShortcutsDatabaseTest, LoadShortcuts) { 170 AddAll(); 171 ShortcutsDatabase::GuidToShortcutMap shortcuts; 172 EXPECT_TRUE(db_->LoadShortcuts(&shortcuts)); 173 174 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i) { 175 SCOPED_TRACE(base::StringPrintf("Looking for shortcut #%d", 176 static_cast<int>(i))); 177 EXPECT_TRUE(shortcuts.find(shortcut_test_db[i].guid) != shortcuts.end()); 178 } 179 } 180 181 TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) { 182 AddAll(); 183 ShortcutsDatabase::GuidToShortcutMap shortcuts; 184 EXPECT_TRUE(db_->LoadShortcuts(&shortcuts)); 185 EXPECT_EQ(arraysize(shortcut_test_db), shortcuts.size()); 186 EXPECT_TRUE(db_->DeleteAllShortcuts()); 187 EXPECT_TRUE(db_->LoadShortcuts(&shortcuts)); 188 EXPECT_EQ(0U, shortcuts.size()); 189 } 190 191 } // namespace history 192