Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(702)

Unified Diff: chrome/browser/autocomplete/shortcuts_backend_unittest.cc

Issue 200493006: Move the ShortcutsBackend from history to autocomplete so that it can fully (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/autocomplete/shortcuts_backend_unittest.cc
===================================================================
--- chrome/browser/autocomplete/shortcuts_backend_unittest.cc (revision 256894)
+++ chrome/browser/autocomplete/shortcuts_backend_unittest.cc (working copy)
@@ -2,13 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "chrome/browser/autocomplete/shortcuts_backend.h"
+
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
-#include "chrome/browser/history/shortcuts_backend.h"
-#include "chrome/browser/history/shortcuts_backend_factory.h"
+#include "chrome/browser/autocomplete/shortcuts_backend_factory.h"
#include "chrome/browser/history/shortcuts_database.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread.h"
@@ -17,41 +18,19 @@
#include "testing/gtest/include/gtest/gtest.h"
-// Helpers --------------------------------------------------------------------
-
-namespace history {
-
-namespace {
-
-AutocompleteMatch::Type ConvertedMatchType(AutocompleteMatch::Type type) {
- return ShortcutsBackend::Shortcut(
- std::string(), base::string16(), ShortcutsBackend::Shortcut::MatchCore(
- AutocompleteMatch(NULL, 0, 0, type)),
- base::Time::Now(), 0).match_core.type;
-}
-
-ShortcutsBackend::Shortcut::MatchCore MatchCoreForTesting(
- const std::string& url) {
- AutocompleteMatch match;
- match.destination_url = GURL(url);
- match.contents = base::ASCIIToUTF16("test");
- return ShortcutsBackend::Shortcut::MatchCore(match);
-}
-
-} // namespace
-
-
// ShortcutsBackendTest -------------------------------------------------------
class ShortcutsBackendTest : public testing::Test,
public ShortcutsBackend::ShortcutsBackendObserver {
public:
- ShortcutsBackendTest()
- : ui_thread_(content::BrowserThread::UI, &ui_message_loop_),
- db_thread_(content::BrowserThread::DB),
- load_notified_(false),
- changed_notified_(false) {}
+ ShortcutsBackendTest();
+ history::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);
+
virtual void SetUp();
virtual void TearDown();
@@ -58,8 +37,23 @@
virtual void OnShortcutsLoaded() OVERRIDE;
virtual 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 history::ShortcutsDatabase::Shortcut& shortcut);
+ bool UpdateShortcut(const history::ShortcutsDatabase::Shortcut& shortcut);
+ bool DeleteShortcutsWithURL(const GURL& url);
+ bool DeleteShortcutsWithIDs(
+ const history::ShortcutsDatabase::ShortcutIDs& deleted_ids);
+
+ private:
TestingProfile profile_;
scoped_refptr<ShortcutsBackend> backend_;
base::MessageLoopForUI ui_message_loop_;
@@ -68,8 +62,33 @@
bool load_notified_;
bool changed_notified_;
+
+ DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest);
};
+ShortcutsBackendTest::ShortcutsBackendTest()
+ : ui_thread_(content::BrowserThread::UI, &ui_message_loop_),
+ db_thread_(content::BrowserThread::DB),
+ load_notified_(false),
+ changed_notified_(false) {
+}
+
+history::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);
+ return ShortcutsBackend::MatchToMatchCore(match);
+}
+
void ShortcutsBackendTest::SetUp() {
db_thread_.Start();
ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse(
@@ -104,103 +123,133 @@
EXPECT_TRUE(backend_->initialized());
}
+bool ShortcutsBackendTest::AddShortcut(
+ const history::ShortcutsDatabase::Shortcut& shortcut) {
+ return backend_->AddShortcut(shortcut);
+}
+bool ShortcutsBackendTest::UpdateShortcut(
+ const history::ShortcutsDatabase::Shortcut& shortcut) {
+ return backend_->UpdateShortcut(shortcut);
+}
+
+bool ShortcutsBackendTest::DeleteShortcutsWithURL(const GURL& url) {
+ return backend_->DeleteShortcutsWithURL(url);
+}
+
+bool ShortcutsBackendTest::DeleteShortcutsWithIDs(
+ const history::ShortcutsDatabase::ShortcutIDs& deleted_ids) {
+ return backend_->DeleteShortcutsWithIDs(deleted_ids);
+}
+
+
// Actual tests ---------------------------------------------------------------
-// Verifies that particular original match types are automatically modified when
-// creating shortcuts.
-TEST_F(ShortcutsBackendTest, ChangeMatchTypeOnShortcutCreation) {
+// 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;
- } type_cases[] = {
- { AutocompleteMatchType::URL_WHAT_YOU_TYPED,
- AutocompleteMatchType::HISTORY_URL },
- { AutocompleteMatchType::NAVSUGGEST,
- AutocompleteMatchType::HISTORY_URL },
- { AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
- AutocompleteMatchType::SEARCH_HISTORY },
- { AutocompleteMatchType::SEARCH_SUGGEST,
- AutocompleteMatchType::SEARCH_HISTORY },
+ } 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 },
};
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(type_cases); ++i) {
- EXPECT_EQ(type_cases[i].output_type,
- ConvertedMatchType(type_cases[i].input_type));
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ history::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);
+ EXPECT_EQ(cases[i].output_description_class, match_core.description_class);
+ EXPECT_EQ(cases[i].output_type, match_core.type);
}
}
TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) {
InitBackend();
- EXPECT_FALSE(changed_notified_);
- ShortcutsBackend::Shortcut shortcut(
+ EXPECT_FALSE(changed_notified());
+
+ history::ShortcutsDatabase::Shortcut shortcut(
"BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"),
MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
- EXPECT_TRUE(backend_->AddShortcut(shortcut));
- EXPECT_TRUE(changed_notified_);
- changed_notified_ = false;
+ 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);
- const ShortcutsBackend::ShortcutMap& shortcuts = backend_->shortcuts_map();
- ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut.text));
- EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id);
- EXPECT_EQ(shortcut.match_core.contents,
- shortcuts.find(shortcut.text)->second.match_core.contents);
+ set_changed_notified(false);
shortcut.match_core.contents = base::ASCIIToUTF16("Google Web Search");
- EXPECT_TRUE(backend_->UpdateShortcut(shortcut));
- EXPECT_TRUE(changed_notified_);
- EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id);
+ 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,
- shortcuts.find(shortcut.text)->second.match_core.contents);
+ shortcut_iter->second.match_core.contents);
}
TEST_F(ShortcutsBackendTest, DeleteShortcuts) {
InitBackend();
- ShortcutsBackend::Shortcut shortcut1(
+ history::ShortcutsDatabase::Shortcut shortcut1(
"BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"),
MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
- EXPECT_TRUE(backend_->AddShortcut(shortcut1));
+ EXPECT_TRUE(AddShortcut(shortcut1));
- ShortcutsBackend::Shortcut shortcut2(
+ history::ShortcutsDatabase::Shortcut shortcut2(
"BD85DBA2-8C29-49F9-84AE-48E1E90880E0", base::ASCIIToUTF16("gle"),
MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
- EXPECT_TRUE(backend_->AddShortcut(shortcut2));
+ EXPECT_TRUE(AddShortcut(shortcut2));
- ShortcutsBackend::Shortcut shortcut3(
+ history::ShortcutsDatabase::Shortcut shortcut3(
"BD85DBA2-8C29-49F9-84AE-48E1E90880E1", base::ASCIIToUTF16("sp"),
MatchCoreForTesting("http://www.sport.com"), base::Time::Now(), 10);
- EXPECT_TRUE(backend_->AddShortcut(shortcut3));
+ EXPECT_TRUE(AddShortcut(shortcut3));
- ShortcutsBackend::Shortcut shortcut4(
+ history::ShortcutsDatabase::Shortcut shortcut4(
"BD85DBA2-8C29-49F9-84AE-48E1E90880E2", base::ASCIIToUTF16("mov"),
MatchCoreForTesting("http://www.film.com"), base::Time::Now(), 10);
- EXPECT_TRUE(backend_->AddShortcut(shortcut4));
+ EXPECT_TRUE(AddShortcut(shortcut4));
- const ShortcutsBackend::ShortcutMap& shortcuts = backend_->shortcuts_map();
+ 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);
- ASSERT_EQ(4U, shortcuts.size());
- EXPECT_EQ(shortcut1.id, shortcuts.find(shortcut1.text)->second.id);
- EXPECT_EQ(shortcut2.id, shortcuts.find(shortcut2.text)->second.id);
- EXPECT_EQ(shortcut3.id, shortcuts.find(shortcut3.text)->second.id);
- EXPECT_EQ(shortcut4.id, shortcuts.find(shortcut4.text)->second.id);
+ EXPECT_TRUE(DeleteShortcutsWithURL(shortcut1.match_core.destination_url));
- EXPECT_TRUE(backend_->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);
- ASSERT_EQ(2U, shortcuts.size());
- EXPECT_TRUE(shortcuts.end() == shortcuts.find(shortcut1.text));
- EXPECT_TRUE(shortcuts.end() == shortcuts.find(shortcut2.text));
- ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut3.text));
- ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut4.text));
- EXPECT_EQ(shortcut3.id, shortcuts.find(shortcut3.text)->second.id);
- EXPECT_EQ(shortcut4.id, shortcuts.find(shortcut4.text)->second.id);
-
- std::vector<std::string> deleted_ids;
+ history::ShortcutsDatabase::ShortcutIDs deleted_ids;
deleted_ids.push_back(shortcut3.id);
deleted_ids.push_back(shortcut4.id);
+ EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids));
- EXPECT_TRUE(backend_->DeleteShortcutsWithIds(deleted_ids));
-
- ASSERT_EQ(0U, shortcuts.size());
+ ASSERT_EQ(0U, shortcuts_map().size());
}
-
-} // namespace history
« no previous file with comments | « chrome/browser/autocomplete/shortcuts_backend_factory.cc ('k') | chrome/browser/autocomplete/shortcuts_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698