1 // Copyright (c) 2011 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/file_util.h" 6 #include "base/path_service.h" 7 #include "base/string_number_conversions.h" 8 #include "base/time.h" 9 #include "base/utf_string_conversions.h" 10 #include "chrome/common/chrome_paths.h" 11 #include "chrome/browser/search_engines/template_url.h" 12 #include "chrome/browser/webdata/web_database.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 using base::Time; 16 17 class KeywordTableTest : public testing::Test { 18 public: 19 KeywordTableTest() {} 20 virtual ~KeywordTableTest() {} 21 22 protected: 23 virtual void SetUp() { 24 PathService::Get(chrome::DIR_TEST_DATA, &file_); 25 const std::string test_db = "TestWebDatabase" + 26 base::Int64ToString(Time::Now().ToTimeT()) + 27 ".db"; 28 file_ = file_.AppendASCII(test_db); 29 file_util::Delete(file_, false); 30 } 31 32 virtual void TearDown() { 33 file_util::Delete(file_, false); 34 } 35 36 static int64 GetID(const TemplateURL* url) { 37 return url->id(); 38 } 39 40 static void SetID(int64 new_id, TemplateURL* url) { 41 url->set_id(new_id); 42 } 43 44 static void set_prepopulate_id(TemplateURL* url, int id) { 45 url->set_prepopulate_id(id); 46 } 47 48 static void set_logo_id(TemplateURL* url, int id) { 49 url->set_logo_id(id); 50 } 51 52 FilePath file_; 53 54 private: 55 DISALLOW_COPY_AND_ASSIGN(KeywordTableTest); 56 }; 57 58 59 TEST_F(KeywordTableTest, Keywords) { 60 WebDatabase db; 61 62 ASSERT_EQ(sql::INIT_OK, db.Init(file_)); 63 64 TemplateURL template_url; 65 template_url.set_short_name(ASCIIToUTF16("short_name")); 66 template_url.set_keyword(ASCIIToUTF16("keyword")); 67 GURL favicon_url("http://favicon.url/"); 68 GURL originating_url("http://google.com/"); 69 template_url.SetFaviconURL(favicon_url); 70 template_url.SetURL("http://url/", 0, 0); 71 template_url.set_safe_for_autoreplace(true); 72 Time created_time = Time::Now(); 73 template_url.set_date_created(created_time); 74 template_url.set_show_in_default_list(true); 75 template_url.set_originating_url(originating_url); 76 template_url.set_usage_count(32); 77 template_url.add_input_encoding("UTF-8"); 78 template_url.add_input_encoding("UTF-16"); 79 set_prepopulate_id(&template_url, 10); 80 set_logo_id(&template_url, 1000); 81 template_url.set_created_by_policy(true); 82 template_url.SetInstantURL("http://instant/", 0, 0); 83 SetID(1, &template_url); 84 85 EXPECT_TRUE(db.GetKeywordTable()->AddKeyword(template_url)); 86 87 std::vector<TemplateURL*> template_urls; 88 EXPECT_TRUE(db.GetKeywordTable()->GetKeywords(&template_urls)); 89 90 EXPECT_EQ(1U, template_urls.size()); 91 const TemplateURL* restored_url = template_urls.front(); 92 93 EXPECT_EQ(template_url.short_name(), restored_url->short_name()); 94 95 EXPECT_EQ(template_url.keyword(), restored_url->keyword()); 96 97 EXPECT_FALSE(restored_url->autogenerate_keyword()); 98 99 EXPECT_TRUE(favicon_url == restored_url->GetFaviconURL()); 100 101 EXPECT_TRUE(restored_url->safe_for_autoreplace()); 102 103 // The database stores time only at the resolution of a second. 104 EXPECT_EQ(created_time.ToTimeT(), restored_url->date_created().ToTimeT()); 105 106 EXPECT_TRUE(restored_url->show_in_default_list()); 107 108 EXPECT_EQ(GetID(&template_url), GetID(restored_url)); 109 110 EXPECT_TRUE(originating_url == restored_url->originating_url()); 111 112 EXPECT_EQ(32, restored_url->usage_count()); 113 114 ASSERT_EQ(2U, restored_url->input_encodings().size()); 115 EXPECT_EQ("UTF-8", restored_url->input_encodings()[0]); 116 EXPECT_EQ("UTF-16", restored_url->input_encodings()[1]); 117 118 EXPECT_EQ(10, restored_url->prepopulate_id()); 119 120 EXPECT_EQ(1000, restored_url->logo_id()); 121 122 EXPECT_TRUE(restored_url->created_by_policy()); 123 124 ASSERT_TRUE(restored_url->instant_url()); 125 EXPECT_EQ("http://instant/", restored_url->instant_url()->url()); 126 127 EXPECT_TRUE(db.GetKeywordTable()->RemoveKeyword(restored_url->id())); 128 129 template_urls.clear(); 130 EXPECT_TRUE(db.GetKeywordTable()->GetKeywords(&template_urls)); 131 132 EXPECT_EQ(0U, template_urls.size()); 133 134 delete restored_url; 135 } 136 137 TEST_F(KeywordTableTest, KeywordMisc) { 138 WebDatabase db; 139 140 ASSERT_EQ(sql::INIT_OK, db.Init(file_)); 141 142 ASSERT_EQ(0, db.GetKeywordTable()->GetDefaulSearchProviderID()); 143 ASSERT_EQ(0, db.GetKeywordTable()->GetBuitinKeywordVersion()); 144 145 db.GetKeywordTable()->SetDefaultSearchProviderID(10); 146 db.GetKeywordTable()->SetBuitinKeywordVersion(11); 147 148 ASSERT_EQ(10, db.GetKeywordTable()->GetDefaulSearchProviderID()); 149 ASSERT_EQ(11, db.GetKeywordTable()->GetBuitinKeywordVersion()); 150 } 151 152 TEST_F(KeywordTableTest, UpdateKeyword) { 153 WebDatabase db; 154 155 ASSERT_EQ(sql::INIT_OK, db.Init(file_)); 156 157 TemplateURL template_url; 158 template_url.set_short_name(ASCIIToUTF16("short_name")); 159 template_url.set_keyword(ASCIIToUTF16("keyword")); 160 GURL favicon_url("http://favicon.url/"); 161 GURL originating_url("http://originating.url/"); 162 template_url.SetFaviconURL(favicon_url); 163 template_url.SetURL("http://url/", 0, 0); 164 template_url.set_safe_for_autoreplace(true); 165 template_url.set_show_in_default_list(true); 166 template_url.SetSuggestionsURL("url2", 0, 0); 167 SetID(1, &template_url); 168 169 EXPECT_TRUE(db.GetKeywordTable()->AddKeyword(template_url)); 170 171 GURL originating_url2("http://originating.url/"); 172 template_url.set_originating_url(originating_url2); 173 template_url.set_autogenerate_keyword(true); 174 EXPECT_EQ(ASCIIToUTF16("url"), template_url.keyword()); 175 template_url.add_input_encoding("Shift_JIS"); 176 set_prepopulate_id(&template_url, 5); 177 set_logo_id(&template_url, 2000); 178 template_url.SetInstantURL("http://instant2/", 0, 0); 179 EXPECT_TRUE(db.GetKeywordTable()->UpdateKeyword(template_url)); 180 181 std::vector<TemplateURL*> template_urls; 182 EXPECT_TRUE(db.GetKeywordTable()->GetKeywords(&template_urls)); 183 184 EXPECT_EQ(1U, template_urls.size()); 185 const TemplateURL* restored_url = template_urls.front(); 186 187 EXPECT_EQ(template_url.short_name(), restored_url->short_name()); 188 189 EXPECT_EQ(template_url.keyword(), restored_url->keyword()); 190 191 EXPECT_TRUE(restored_url->autogenerate_keyword()); 192 193 EXPECT_TRUE(favicon_url == restored_url->GetFaviconURL()); 194 195 EXPECT_TRUE(restored_url->safe_for_autoreplace()); 196 197 EXPECT_TRUE(restored_url->show_in_default_list()); 198 199 EXPECT_EQ(GetID(&template_url), GetID(restored_url)); 200 201 EXPECT_TRUE(originating_url2 == restored_url->originating_url()); 202 203 ASSERT_EQ(1U, restored_url->input_encodings().size()); 204 ASSERT_EQ("Shift_JIS", restored_url->input_encodings()[0]); 205 206 EXPECT_EQ(template_url.suggestions_url()->url(), 207 restored_url->suggestions_url()->url()); 208 209 EXPECT_EQ(template_url.id(), restored_url->id()); 210 211 EXPECT_EQ(template_url.prepopulate_id(), restored_url->prepopulate_id()); 212 213 EXPECT_EQ(template_url.logo_id(), restored_url->logo_id()); 214 215 EXPECT_TRUE(restored_url->instant_url()); 216 EXPECT_EQ(template_url.instant_url()->url(), 217 restored_url->instant_url()->url()); 218 219 delete restored_url; 220 } 221 222 TEST_F(KeywordTableTest, KeywordWithNoFavicon) { 223 WebDatabase db; 224 225 ASSERT_EQ(sql::INIT_OK, db.Init(file_)); 226 227 TemplateURL template_url; 228 template_url.set_short_name(ASCIIToUTF16("short_name")); 229 template_url.set_keyword(ASCIIToUTF16("keyword")); 230 template_url.SetURL("http://url/", 0, 0); 231 template_url.set_safe_for_autoreplace(true); 232 SetID(-100, &template_url); 233 234 EXPECT_TRUE(db.GetKeywordTable()->AddKeyword(template_url)); 235 236 std::vector<TemplateURL*> template_urls; 237 EXPECT_TRUE(db.GetKeywordTable()->GetKeywords(&template_urls)); 238 EXPECT_EQ(1U, template_urls.size()); 239 const TemplateURL* restored_url = template_urls.front(); 240 241 EXPECT_EQ(template_url.short_name(), restored_url->short_name()); 242 EXPECT_EQ(template_url.keyword(), restored_url->keyword()); 243 EXPECT_TRUE(!restored_url->GetFaviconURL().is_valid()); 244 EXPECT_TRUE(restored_url->safe_for_autoreplace()); 245 EXPECT_EQ(GetID(&template_url), GetID(restored_url)); 246 delete restored_url; 247 } 248