Revert of Refactor the avatar button/icon class (patchset #14 id:320001 of https...
[chromium-blink-merge.git] / components / search_engines / keyword_table_unittest.cc
blob15fa614f418d368c8b67a9e4c9cdaec8f29eb7c8
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.
5 #include <string>
6 #include <vector>
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/stl_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "components/search_engines/keyword_table.h"
14 #include "components/search_engines/template_url_data.h"
15 #include "components/webdata/common/web_database.h"
16 #include "sql/statement.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using base::ASCIIToUTF16;
20 using base::Time;
21 using base::TimeDelta;
23 class KeywordTableTest : public testing::Test {
24 public:
25 KeywordTableTest() {}
26 ~KeywordTableTest() override {}
28 protected:
29 void SetUp() override {
30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
31 file_ = temp_dir_.path().AppendASCII("TestWebDatabase");
33 table_.reset(new KeywordTable);
34 db_.reset(new WebDatabase);
35 db_->AddTable(table_.get());
36 ASSERT_EQ(sql::INIT_OK, db_->Init(file_));
39 void AddKeyword(const TemplateURLData& keyword) const {
40 EXPECT_TRUE(table_->AddKeyword(keyword));
43 TemplateURLData CreateAndAddKeyword() const {
44 TemplateURLData keyword;
45 keyword.short_name = ASCIIToUTF16("short_name");
46 keyword.SetKeyword(ASCIIToUTF16("keyword"));
47 keyword.SetURL("http://url/");
48 keyword.suggestions_url = "url2";
49 keyword.instant_url = "http://instant/";
50 keyword.image_url = "http://image-search-url/";
51 keyword.new_tab_url = "http://new-tab-url/";
52 keyword.search_url_post_params = "ie=utf-8,oe=utf-8";
53 keyword.image_url_post_params = "name=1,value=2";
54 keyword.favicon_url = GURL("http://favicon.url/");
55 keyword.originating_url = GURL("http://google.com/");
56 keyword.show_in_default_list = true;
57 keyword.safe_for_autoreplace = true;
58 keyword.input_encodings.push_back("UTF-8");
59 keyword.input_encodings.push_back("UTF-16");
60 keyword.id = 1;
61 keyword.date_created = base::Time::UnixEpoch();
62 keyword.last_modified = base::Time::UnixEpoch();
63 keyword.created_by_policy = true;
64 keyword.usage_count = 32;
65 keyword.prepopulate_id = 10;
66 keyword.sync_guid = "1234-5678-90AB-CDEF";
67 keyword.alternate_urls.push_back("a_url1");
68 keyword.alternate_urls.push_back("a_url2");
69 keyword.search_terms_replacement_key = "espv";
70 AddKeyword(keyword);
71 return keyword;
74 void RemoveKeyword(TemplateURLID id) const {
75 EXPECT_TRUE(table_->RemoveKeyword(id));
78 void UpdateKeyword(const TemplateURLData& keyword) const {
79 EXPECT_TRUE(table_->UpdateKeyword(keyword));
82 KeywordTable::Keywords GetKeywords() const {
83 KeywordTable::Keywords keywords;
84 EXPECT_TRUE(table_->GetKeywords(&keywords));
85 return keywords;
88 void KeywordMiscTest() const {
89 EXPECT_EQ(kInvalidTemplateURLID, table_->GetDefaultSearchProviderID());
90 EXPECT_EQ(0, table_->GetBuiltinKeywordVersion());
92 EXPECT_TRUE(table_->SetDefaultSearchProviderID(10));
93 EXPECT_TRUE(table_->SetBuiltinKeywordVersion(11));
95 EXPECT_EQ(10, table_->GetDefaultSearchProviderID());
96 EXPECT_EQ(11, table_->GetBuiltinKeywordVersion());
99 void GetStatement(const char* sql, sql::Statement* statement) const {
100 statement->Assign(table_->db_->GetUniqueStatement(sql));
103 private:
104 base::FilePath file_;
105 base::ScopedTempDir temp_dir_;
106 scoped_ptr<KeywordTable> table_;
107 scoped_ptr<WebDatabase> db_;
109 DISALLOW_COPY_AND_ASSIGN(KeywordTableTest);
113 TEST_F(KeywordTableTest, Keywords) {
114 TemplateURLData keyword(CreateAndAddKeyword());
116 KeywordTable::Keywords keywords(GetKeywords());
117 EXPECT_EQ(1U, keywords.size());
118 const TemplateURLData& restored_keyword = keywords.front();
120 EXPECT_EQ(keyword.short_name, restored_keyword.short_name);
121 EXPECT_EQ(keyword.keyword(), restored_keyword.keyword());
122 EXPECT_EQ(keyword.url(), restored_keyword.url());
123 EXPECT_EQ(keyword.suggestions_url, restored_keyword.suggestions_url);
124 EXPECT_EQ(keyword.instant_url, restored_keyword.instant_url);
125 EXPECT_EQ(keyword.favicon_url, restored_keyword.favicon_url);
126 EXPECT_EQ(keyword.originating_url, restored_keyword.originating_url);
127 EXPECT_EQ(keyword.show_in_default_list,
128 restored_keyword.show_in_default_list);
129 EXPECT_EQ(keyword.safe_for_autoreplace,
130 restored_keyword.safe_for_autoreplace);
131 EXPECT_EQ(keyword.input_encodings, restored_keyword.input_encodings);
132 EXPECT_EQ(keyword.id, restored_keyword.id);
133 // The database stores time only at the resolution of a second.
134 EXPECT_EQ(keyword.date_created.ToTimeT(),
135 restored_keyword.date_created.ToTimeT());
136 EXPECT_EQ(keyword.last_modified.ToTimeT(),
137 restored_keyword.last_modified.ToTimeT());
138 EXPECT_EQ(keyword.created_by_policy, restored_keyword.created_by_policy);
139 EXPECT_EQ(keyword.usage_count, restored_keyword.usage_count);
140 EXPECT_EQ(keyword.prepopulate_id, restored_keyword.prepopulate_id);
142 RemoveKeyword(restored_keyword.id);
144 EXPECT_EQ(0U, GetKeywords().size());
147 TEST_F(KeywordTableTest, KeywordMisc) {
148 KeywordMiscTest();
151 TEST_F(KeywordTableTest, UpdateKeyword) {
152 TemplateURLData keyword(CreateAndAddKeyword());
154 keyword.SetKeyword(ASCIIToUTF16("url"));
155 keyword.instant_url = "http://instant2/";
156 keyword.originating_url = GURL("http://originating.url/");
157 keyword.input_encodings.push_back("Shift_JIS");
158 keyword.prepopulate_id = 5;
159 UpdateKeyword(keyword);
161 KeywordTable::Keywords keywords(GetKeywords());
162 EXPECT_EQ(1U, keywords.size());
163 const TemplateURLData& restored_keyword = keywords.front();
165 EXPECT_EQ(keyword.short_name, restored_keyword.short_name);
166 EXPECT_EQ(keyword.keyword(), restored_keyword.keyword());
167 EXPECT_EQ(keyword.suggestions_url, restored_keyword.suggestions_url);
168 EXPECT_EQ(keyword.instant_url, restored_keyword.instant_url);
169 EXPECT_EQ(keyword.favicon_url, restored_keyword.favicon_url);
170 EXPECT_EQ(keyword.originating_url, restored_keyword.originating_url);
171 EXPECT_EQ(keyword.show_in_default_list,
172 restored_keyword.show_in_default_list);
173 EXPECT_EQ(keyword.safe_for_autoreplace,
174 restored_keyword.safe_for_autoreplace);
175 EXPECT_EQ(keyword.input_encodings, restored_keyword.input_encodings);
176 EXPECT_EQ(keyword.id, restored_keyword.id);
177 EXPECT_EQ(keyword.prepopulate_id, restored_keyword.prepopulate_id);
180 TEST_F(KeywordTableTest, KeywordWithNoFavicon) {
181 TemplateURLData keyword;
182 keyword.short_name = ASCIIToUTF16("short_name");
183 keyword.SetKeyword(ASCIIToUTF16("keyword"));
184 keyword.SetURL("http://url/");
185 keyword.safe_for_autoreplace = true;
186 keyword.id = -100;
187 AddKeyword(keyword);
189 KeywordTable::Keywords keywords(GetKeywords());
190 EXPECT_EQ(1U, keywords.size());
191 const TemplateURLData& restored_keyword = keywords.front();
193 EXPECT_EQ(keyword.short_name, restored_keyword.short_name);
194 EXPECT_EQ(keyword.keyword(), restored_keyword.keyword());
195 EXPECT_EQ(keyword.favicon_url, restored_keyword.favicon_url);
196 EXPECT_EQ(keyword.safe_for_autoreplace,
197 restored_keyword.safe_for_autoreplace);
198 EXPECT_EQ(keyword.id, restored_keyword.id);
201 TEST_F(KeywordTableTest, SanitizeURLs) {
202 TemplateURLData keyword;
203 keyword.short_name = ASCIIToUTF16("legit");
204 keyword.SetKeyword(ASCIIToUTF16("legit"));
205 keyword.SetURL("http://url/");
206 keyword.id = 1000;
207 AddKeyword(keyword);
209 keyword.short_name = ASCIIToUTF16("bogus");
210 keyword.SetKeyword(ASCIIToUTF16("bogus"));
211 keyword.id = 2000;
212 AddKeyword(keyword);
214 EXPECT_EQ(2U, GetKeywords().size());
216 // Erase the URL field for the second keyword to simulate having bogus data
217 // previously saved into the database.
218 sql::Statement s;
219 GetStatement("UPDATE keywords SET url=? WHERE id=?", &s);
220 s.BindString16(0, base::string16());
221 s.BindInt64(1, 2000);
222 EXPECT_TRUE(s.Run());
224 // GetKeywords() should erase the entry with the empty URL field.
225 EXPECT_EQ(1U, GetKeywords().size());