1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/shortcuts_backend.h"
#include <stddef.h>
#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/test/history_service_test_util.h"
#include "components/omnibox/browser/shortcuts_constants.h"
#include "components/omnibox/browser/shortcuts_database.h"
#include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url_service.h"
#include "testing/gtest/include/gtest/gtest.h"
// ShortcutsBackendTest -------------------------------------------------------
class ShortcutsBackendTest : public testing::Test,
public ShortcutsBackend::ShortcutsBackendObserver {
public:
ShortcutsBackendTest();
ShortcutsDatabase::Shortcut::MatchCore MatchCoreForTesting(
const std::string& url,
const std::string& contents_class = std::string(),
const std::string& description_class = std::string(),
AutocompleteMatch::Type type = AutocompleteMatchType::URL_WHAT_YOU_TYPED);
void SetSearchProvider();
void SetUp() override;
void TearDown() override;
void OnShortcutsLoaded() override;
void OnShortcutsChanged() override;
const ShortcutsBackend::ShortcutMap& shortcuts_map() const {
return backend_->shortcuts_map();
}
bool changed_notified() const { return changed_notified_; }
void set_changed_notified(bool changed_notified) {
changed_notified_ = changed_notified;
}
void InitBackend();
bool AddShortcut(const ShortcutsDatabase::Shortcut& shortcut);
bool UpdateShortcut(const ShortcutsDatabase::Shortcut& shortcut);
bool DeleteShortcutsWithURL(const GURL& url);
bool DeleteShortcutsWithIDs(
const ShortcutsDatabase::ShortcutIDs& deleted_ids);
TemplateURLService* GetTemplateURLService();
private:
base::MessageLoop message_loop_;
base::Thread db_thread_;
base::ScopedTempDir profile_dir_;
std::unique_ptr<TemplateURLService> template_url_service_;
std::unique_ptr<history::HistoryService> history_service_;
scoped_refptr<ShortcutsBackend> backend_;
bool load_notified_;
bool changed_notified_;
DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest);
};
ShortcutsBackendTest::ShortcutsBackendTest()
: db_thread_("Test DB thread"),
load_notified_(false),
changed_notified_(false) {}
ShortcutsDatabase::Shortcut::MatchCore
ShortcutsBackendTest::MatchCoreForTesting(const std::string& url,
const std::string& contents_class,
const std::string& description_class,
AutocompleteMatch::Type type) {
AutocompleteMatch match(NULL, 0, 0, type);
match.destination_url = GURL(url);
match.contents = base::ASCIIToUTF16("test");
match.contents_class =
AutocompleteMatch::ClassificationsFromString(contents_class);
match.description_class =
AutocompleteMatch::ClassificationsFromString(description_class);
match.search_terms_args.reset(
new TemplateURLRef::SearchTermsArgs(match.contents));
SearchTermsData search_terms_data;
return ShortcutsBackend::MatchToMatchCore(match, template_url_service_.get(),
&search_terms_data);
}
void ShortcutsBackendTest::SetSearchProvider() {
TemplateURLData data;
data.SetURL("http://foo.com/search?bar={searchTerms}");
data.SetShortName(base::UTF8ToUTF16("foo"));
data.SetKeyword(base::UTF8ToUTF16("foo"));
TemplateURL* template_url =
template_url_service_->Add(base::MakeUnique<TemplateURL>(data));
template_url_service_->SetUserSelectedDefaultSearchProvider(template_url);
}
void ShortcutsBackendTest::SetUp() {
template_url_service_.reset(new TemplateURLService(nullptr, 0));
if (profile_dir_.CreateUniqueTempDir())
history_service_ =
history::CreateHistoryService(profile_dir_.GetPath(), true);
ASSERT_TRUE(history_service_);
db_thread_.Start();
base::FilePath shortcuts_database_path =
profile_dir_.GetPath().Append(kShortcutsDatabaseName);
backend_ = new ShortcutsBackend(
template_url_service_.get(), base::MakeUnique<SearchTermsData>(),
history_service_.get(), db_thread_.task_runner(), shortcuts_database_path,
false);
ASSERT_TRUE(backend_.get());
backend_->AddObserver(this);
}
void ShortcutsBackendTest::TearDown() {
backend_->RemoveObserver(this);
db_thread_.Stop();
}
void ShortcutsBackendTest::OnShortcutsLoaded() {
load_notified_ = true;
base::MessageLoop::current()->QuitWhenIdle();
}
void ShortcutsBackendTest::OnShortcutsChanged() {
changed_notified_ = true;
}
void ShortcutsBackendTest::InitBackend() {
ASSERT_TRUE(backend_);
ASSERT_FALSE(load_notified_);
ASSERT_FALSE(backend_->initialized());
backend_->Init();
base::RunLoop().Run();
EXPECT_TRUE(load_notified_);
EXPECT_TRUE(backend_->initialized());
}
bool ShortcutsBackendTest::AddShortcut(
const ShortcutsDatabase::Shortcut& shortcut) {
return backend_->AddShortcut(shortcut);
}
bool ShortcutsBackendTest::UpdateShortcut(
const ShortcutsDatabase::Shortcut& shortcut) {
return backend_->UpdateShortcut(shortcut);
}
bool ShortcutsBackendTest::DeleteShortcutsWithURL(const GURL& url) {
return backend_->DeleteShortcutsWithURL(url);
}
bool ShortcutsBackendTest::DeleteShortcutsWithIDs(
const ShortcutsDatabase::ShortcutIDs& deleted_ids) {
return backend_->DeleteShortcutsWithIDs(deleted_ids);
}
TemplateURLService* ShortcutsBackendTest::GetTemplateURLService() {
return template_url_service_.get();
}
// Actual tests ---------------------------------------------------------------
// Verifies that creating MatchCores strips classifications and sanitizes match
// types.
TEST_F(ShortcutsBackendTest, SanitizeMatchCore) {
struct {
std::string input_contents_class;
std::string input_description_class;
AutocompleteMatch::Type input_type;
std::string output_contents_class;
std::string output_description_class;
AutocompleteMatch::Type output_type;
} cases[] = {
{ "0,1,4,0", "0,3,4,1", AutocompleteMatchType::URL_WHAT_YOU_TYPED,
"0,1,4,0", "0,1", AutocompleteMatchType::HISTORY_URL },
{ "0,3,5,1", "0,2,5,0", AutocompleteMatchType::NAVSUGGEST,
"0,1", "0,0", AutocompleteMatchType::HISTORY_URL },
{ "0,1", "0,0,11,2,15,0",
AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
"0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY },
{ "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST,
"0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY },
{ "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_ENTITY,
"", "", AutocompleteMatchType::SEARCH_HISTORY },
{ "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_TAIL,
"", "", AutocompleteMatchType::SEARCH_HISTORY },
{ "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED,
"", "", AutocompleteMatchType::SEARCH_HISTORY },
{ "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_PROFILE,
"", "", AutocompleteMatchType::SEARCH_HISTORY },
};
for (size_t i = 0; i < arraysize(cases); ++i) {
ShortcutsDatabase::Shortcut::MatchCore match_core(MatchCoreForTesting(
std::string(), cases[i].input_contents_class,
cases[i].input_description_class, cases[i].input_type));
EXPECT_EQ(cases[i].output_contents_class, match_core.contents_class)
<< ":i:" << i << ":type:" << cases[i].input_type;
EXPECT_EQ(cases[i].output_description_class, match_core.description_class)
<< ":i:" << i << ":type:" << cases[i].input_type;
EXPECT_EQ(cases[i].output_type, match_core.type)
<< ":i:" << i << ":type:" << cases[i].input_type;
}
}
TEST_F(ShortcutsBackendTest, EntitySuggestionTest) {
SetSearchProvider();
AutocompleteMatch match;
match.fill_into_edit = base::UTF8ToUTF16("franklin d roosevelt");
match.type = AutocompleteMatchType::SEARCH_SUGGEST_ENTITY;
match.contents = base::UTF8ToUTF16("roosevelt");
match.contents_class =
AutocompleteMatch::ClassificationsFromString("0,0,5,2");
match.description = base::UTF8ToUTF16("Franklin D. Roosevelt");
match.description_class = AutocompleteMatch::ClassificationsFromString("0,4");
match.destination_url =
GURL("http://www.foo.com/search?bar=franklin+d+roosevelt&gs_ssp=1234");
match.keyword = base::UTF8ToUTF16("foo");
match.search_terms_args.reset(
new TemplateURLRef::SearchTermsArgs(match.fill_into_edit));
SearchTermsData search_terms_data;
ShortcutsDatabase::Shortcut::MatchCore match_core =
ShortcutsBackend::MatchToMatchCore(match, GetTemplateURLService(),
&search_terms_data);
EXPECT_EQ("http://foo.com/search?bar=franklin+d+roosevelt",
match_core.destination_url.spec());
EXPECT_EQ(match.fill_into_edit, match_core.contents);
EXPECT_EQ("0,0", match_core.contents_class);
EXPECT_EQ(base::string16(), match_core.description);
EXPECT_TRUE(match_core.description_class.empty());
}
TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) {
InitBackend();
EXPECT_FALSE(changed_notified());
ShortcutsDatabase::Shortcut shortcut(
"BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"),
MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
EXPECT_TRUE(AddShortcut(shortcut));
EXPECT_TRUE(changed_notified());
ShortcutsBackend::ShortcutMap::const_iterator shortcut_iter(
shortcuts_map().find(shortcut.text));
ASSERT_TRUE(shortcut_iter != shortcuts_map().end());
EXPECT_EQ(shortcut.id, shortcut_iter->second.id);
EXPECT_EQ(shortcut.match_core.contents,
shortcut_iter->second.match_core.contents);
set_changed_notified(false);
shortcut.match_core.contents = base::ASCIIToUTF16("Google Web Search");
EXPECT_TRUE(UpdateShortcut(shortcut));
EXPECT_TRUE(changed_notified());
shortcut_iter = shortcuts_map().find(shortcut.text);
ASSERT_TRUE(shortcut_iter != shortcuts_map().end());
EXPECT_EQ(shortcut.id, shortcut_iter->second.id);
EXPECT_EQ(shortcut.match_core.contents,
shortcut_iter->second.match_core.contents);
}
TEST_F(ShortcutsBackendTest, DeleteShortcuts) {
InitBackend();
ShortcutsDatabase::Shortcut shortcut1(
"BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"),
MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
EXPECT_TRUE(AddShortcut(shortcut1));
ShortcutsDatabase::Shortcut shortcut2(
"BD85DBA2-8C29-49F9-84AE-48E1E90880E0", base::ASCIIToUTF16("gle"),
MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
EXPECT_TRUE(AddShortcut(shortcut2));
ShortcutsDatabase::Shortcut shortcut3(
"BD85DBA2-8C29-49F9-84AE-48E1E90880E1", base::ASCIIToUTF16("sp"),
MatchCoreForTesting("http://www.sport.com"), base::Time::Now(), 10);
EXPECT_TRUE(AddShortcut(shortcut3));
ShortcutsDatabase::Shortcut shortcut4(
"BD85DBA2-8C29-49F9-84AE-48E1E90880E2", base::ASCIIToUTF16("mov"),
MatchCoreForTesting("http://www.film.com"), base::Time::Now(), 10);
EXPECT_TRUE(AddShortcut(shortcut4));
ASSERT_EQ(4U, shortcuts_map().size());
EXPECT_EQ(shortcut1.id, shortcuts_map().find(shortcut1.text)->second.id);
EXPECT_EQ(shortcut2.id, shortcuts_map().find(shortcut2.text)->second.id);
EXPECT_EQ(shortcut3.id, shortcuts_map().find(shortcut3.text)->second.id);
EXPECT_EQ(shortcut4.id, shortcuts_map().find(shortcut4.text)->second.id);
EXPECT_TRUE(DeleteShortcutsWithURL(shortcut1.match_core.destination_url));
ASSERT_EQ(2U, shortcuts_map().size());
EXPECT_EQ(0U, shortcuts_map().count(shortcut1.text));
EXPECT_EQ(0U, shortcuts_map().count(shortcut2.text));
const ShortcutsBackend::ShortcutMap::const_iterator shortcut3_iter(
shortcuts_map().find(shortcut3.text));
ASSERT_TRUE(shortcut3_iter != shortcuts_map().end());
EXPECT_EQ(shortcut3.id, shortcut3_iter->second.id);
const ShortcutsBackend::ShortcutMap::const_iterator shortcut4_iter(
shortcuts_map().find(shortcut4.text));
ASSERT_TRUE(shortcut4_iter != shortcuts_map().end());
EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id);
ShortcutsDatabase::ShortcutIDs deleted_ids;
deleted_ids.push_back(shortcut3.id);
deleted_ids.push_back(shortcut4.id);
EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids));
ASSERT_EQ(0U, shortcuts_map().size());
}
|