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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/autocomplete/shortcuts_backend.h"
6
5 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
6 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
7 #include "base/path_service.h" 9 #include "base/path_service.h"
8 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/history/shortcuts_backend.h" 12 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h"
11 #include "chrome/browser/history/shortcuts_backend_factory.h"
12 #include "chrome/browser/history/shortcuts_database.h" 13 #include "chrome/browser/history/shortcuts_database.h"
13 #include "chrome/test/base/testing_profile.h" 14 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/test/test_browser_thread.h" 15 #include "content/public/test/test_browser_thread.h"
15 #include "sql/statement.h" 16 #include "sql/statement.h"
16 17
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 20
20 // Helpers --------------------------------------------------------------------
21
22 namespace history {
23
24 namespace {
25
26 AutocompleteMatch::Type ConvertedMatchType(AutocompleteMatch::Type type) {
27 return ShortcutsBackend::Shortcut(
28 std::string(), base::string16(), ShortcutsBackend::Shortcut::MatchCore(
29 AutocompleteMatch(NULL, 0, 0, type)),
30 base::Time::Now(), 0).match_core.type;
31 }
32
33 ShortcutsBackend::Shortcut::MatchCore MatchCoreForTesting(
34 const std::string& url) {
35 AutocompleteMatch match;
36 match.destination_url = GURL(url);
37 match.contents = base::ASCIIToUTF16("test");
38 return ShortcutsBackend::Shortcut::MatchCore(match);
39 }
40
41 } // namespace
42
43
44 // ShortcutsBackendTest ------------------------------------------------------- 21 // ShortcutsBackendTest -------------------------------------------------------
45 22
46 class ShortcutsBackendTest : public testing::Test, 23 class ShortcutsBackendTest : public testing::Test,
47 public ShortcutsBackend::ShortcutsBackendObserver { 24 public ShortcutsBackend::ShortcutsBackendObserver {
48 public: 25 public:
49 ShortcutsBackendTest() 26 ShortcutsBackendTest();
50 : ui_thread_(content::BrowserThread::UI, &ui_message_loop_), 27
51 db_thread_(content::BrowserThread::DB), 28 history::ShortcutsDatabase::Shortcut::MatchCore MatchCoreForTesting(
52 load_notified_(false), 29 const std::string& url,
53 changed_notified_(false) {} 30 const std::string& contents_class = std::string(),
31 const std::string& description_class = std::string(),
32 AutocompleteMatch::Type type = AutocompleteMatchType::URL_WHAT_YOU_TYPED);
54 33
55 virtual void SetUp(); 34 virtual void SetUp();
56 virtual void TearDown(); 35 virtual void TearDown();
57 36
58 virtual void OnShortcutsLoaded() OVERRIDE; 37 virtual void OnShortcutsLoaded() OVERRIDE;
59 virtual void OnShortcutsChanged() OVERRIDE; 38 virtual void OnShortcutsChanged() OVERRIDE;
60 39
40 const ShortcutsBackend::ShortcutMap& shortcuts_map() const {
41 return backend_->shortcuts_map();
42 }
43 bool changed_notified() const { return changed_notified_; }
44 void set_changed_notified(bool changed_notified) {
45 changed_notified_ = changed_notified;
46 }
47
61 void InitBackend(); 48 void InitBackend();
49 bool AddShortcut(const history::ShortcutsDatabase::Shortcut& shortcut);
50 bool UpdateShortcut(const history::ShortcutsDatabase::Shortcut& shortcut);
51 bool DeleteShortcutsWithURL(const GURL& url);
52 bool DeleteShortcutsWithIDs(
53 const history::ShortcutsDatabase::ShortcutIDs& deleted_ids);
62 54
55
56 private:
63 TestingProfile profile_; 57 TestingProfile profile_;
64 scoped_refptr<ShortcutsBackend> backend_; 58 scoped_refptr<ShortcutsBackend> backend_;
65 base::MessageLoopForUI ui_message_loop_; 59 base::MessageLoopForUI ui_message_loop_;
66 content::TestBrowserThread ui_thread_; 60 content::TestBrowserThread ui_thread_;
67 content::TestBrowserThread db_thread_; 61 content::TestBrowserThread db_thread_;
68 62
69 bool load_notified_; 63 bool load_notified_;
70 bool changed_notified_; 64 bool changed_notified_;
65
66 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest);
71 }; 67 };
72 68
69 ShortcutsBackendTest::ShortcutsBackendTest()
70 : ui_thread_(content::BrowserThread::UI, &ui_message_loop_),
71 db_thread_(content::BrowserThread::DB),
72 load_notified_(false),
73 changed_notified_(false) {
74 }
75
76 history::ShortcutsDatabase::Shortcut::MatchCore
77 ShortcutsBackendTest::MatchCoreForTesting(
78 const std::string& url,
79 const std::string& contents_class,
80 const std::string& description_class,
81 AutocompleteMatch::Type type) {
82 AutocompleteMatch match(NULL, 0, 0, type);
83 match.destination_url = GURL(url);
84 match.contents = base::ASCIIToUTF16("test");
85 match.contents_class =
86 AutocompleteMatch::ClassificationsFromString(contents_class);
87 match.description_class =
88 AutocompleteMatch::ClassificationsFromString(description_class);
89 return ShortcutsBackend::MatchToMatchCore(match);
90 }
91
73 void ShortcutsBackendTest::SetUp() { 92 void ShortcutsBackendTest::SetUp() {
74 db_thread_.Start(); 93 db_thread_.Start();
75 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( 94 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse(
76 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); 95 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting);
77 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); 96 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_);
78 ASSERT_TRUE(backend_.get()); 97 ASSERT_TRUE(backend_.get());
79 backend_->AddObserver(this); 98 backend_->AddObserver(this);
80 } 99 }
81 100
82 void ShortcutsBackendTest::TearDown() { 101 void ShortcutsBackendTest::TearDown() {
(...skipping 14 matching lines...) Expand all
97 ShortcutsBackend* backend = 116 ShortcutsBackend* backend =
98 ShortcutsBackendFactory::GetForProfile(&profile_).get(); 117 ShortcutsBackendFactory::GetForProfile(&profile_).get();
99 ASSERT_TRUE(backend); 118 ASSERT_TRUE(backend);
100 ASSERT_FALSE(load_notified_); 119 ASSERT_FALSE(load_notified_);
101 ASSERT_FALSE(backend_->initialized()); 120 ASSERT_FALSE(backend_->initialized());
102 base::MessageLoop::current()->Run(); 121 base::MessageLoop::current()->Run();
103 EXPECT_TRUE(load_notified_); 122 EXPECT_TRUE(load_notified_);
104 EXPECT_TRUE(backend_->initialized()); 123 EXPECT_TRUE(backend_->initialized());
105 } 124 }
106 125
126 bool ShortcutsBackendTest::AddShortcut(
127 const history::ShortcutsDatabase::Shortcut& shortcut) {
128 return backend_->AddShortcut(shortcut);
129 }
130
131 bool ShortcutsBackendTest::UpdateShortcut(
132 const history::ShortcutsDatabase::Shortcut& shortcut) {
133 return backend_->UpdateShortcut(shortcut);
134 }
135
136 bool ShortcutsBackendTest::DeleteShortcutsWithURL(const GURL& url) {
137 return backend_->DeleteShortcutsWithURL(url);
138 }
139
140 bool ShortcutsBackendTest::DeleteShortcutsWithIDs(
141 const history::ShortcutsDatabase::ShortcutIDs& deleted_ids) {
142 return backend_->DeleteShortcutsWithIDs(deleted_ids);
143 }
144
107 145
108 // Actual tests --------------------------------------------------------------- 146 // Actual tests ---------------------------------------------------------------
109 147
110 // Verifies that particular original match types are automatically modified when 148 // Verifies that creating MatchCores strips classifications and sanitizes match
111 // creating shortcuts. 149 // types.
112 TEST_F(ShortcutsBackendTest, ChangeMatchTypeOnShortcutCreation) { 150 TEST_F(ShortcutsBackendTest, SanitizeMatchCore) {
113 struct { 151 struct {
152 std::string input_contents_class;
153 std::string input_description_class;
114 AutocompleteMatch::Type input_type; 154 AutocompleteMatch::Type input_type;
155 std::string output_contents_class;
156 std::string output_description_class;
115 AutocompleteMatch::Type output_type; 157 AutocompleteMatch::Type output_type;
116 } type_cases[] = { 158 } cases[] = {
117 { AutocompleteMatchType::URL_WHAT_YOU_TYPED, 159 { "0,1,4,0", "0,3,4,1", AutocompleteMatchType::URL_WHAT_YOU_TYPED,
118 AutocompleteMatchType::HISTORY_URL }, 160 "0,1,4,0", "0,1", AutocompleteMatchType::HISTORY_URL },
119 { AutocompleteMatchType::NAVSUGGEST, 161 { "0,3,5,1", "0,2,5,0", AutocompleteMatchType::NAVSUGGEST,
120 AutocompleteMatchType::HISTORY_URL }, 162 "0,1", "0,0", AutocompleteMatchType::HISTORY_URL },
121 { AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, 163 { "0,1", "0,0,11,2,15,0", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
122 AutocompleteMatchType::SEARCH_HISTORY }, 164 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY },
123 { AutocompleteMatchType::SEARCH_SUGGEST, 165 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST,
124 AutocompleteMatchType::SEARCH_HISTORY }, 166 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY },
125 }; 167 };
126 168
127 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(type_cases); ++i) { 169 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
128 EXPECT_EQ(type_cases[i].output_type, 170 history::ShortcutsDatabase::Shortcut::MatchCore match_core(
129 ConvertedMatchType(type_cases[i].input_type)); 171 MatchCoreForTesting(std::string(), cases[i].input_contents_class,
172 cases[i].input_description_class,
173 cases[i].input_type));
174 EXPECT_EQ(cases[i].output_contents_class, match_core.contents_class);
175 EXPECT_EQ(cases[i].output_description_class, match_core.description_class);
176 EXPECT_EQ(cases[i].output_type, match_core.type);
130 } 177 }
131 } 178 }
132 179
133 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { 180 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) {
134 InitBackend(); 181 InitBackend();
135 EXPECT_FALSE(changed_notified_); 182 EXPECT_FALSE(changed_notified());
136 ShortcutsBackend::Shortcut shortcut( 183
184 history::ShortcutsDatabase::Shortcut shortcut(
137 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"), 185 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"),
138 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); 186 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
139 EXPECT_TRUE(backend_->AddShortcut(shortcut)); 187 EXPECT_TRUE(AddShortcut(shortcut));
140 EXPECT_TRUE(changed_notified_); 188 EXPECT_TRUE(changed_notified());
141 changed_notified_ = false; 189 ShortcutsBackend::ShortcutMap::const_iterator shortcut_iter(
190 shortcuts_map().find(shortcut.text));
191 ASSERT_TRUE(shortcut_iter != shortcuts_map().end());
192 EXPECT_EQ(shortcut.id, shortcut_iter->second.id);
193 EXPECT_EQ(shortcut.match_core.contents,
194 shortcut_iter->second.match_core.contents);
142 195
143 const ShortcutsBackend::ShortcutMap& shortcuts = backend_->shortcuts_map(); 196 set_changed_notified(false);
144 ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut.text)); 197 shortcut.match_core.contents = base::ASCIIToUTF16("Google Web Search");
145 EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id); 198 EXPECT_TRUE(UpdateShortcut(shortcut));
199 EXPECT_TRUE(changed_notified());
200 shortcut_iter = shortcuts_map().find(shortcut.text);
201 ASSERT_TRUE(shortcut_iter != shortcuts_map().end());
202 EXPECT_EQ(shortcut.id, shortcut_iter->second.id);
146 EXPECT_EQ(shortcut.match_core.contents, 203 EXPECT_EQ(shortcut.match_core.contents,
147 shortcuts.find(shortcut.text)->second.match_core.contents); 204 shortcut_iter->second.match_core.contents);
148 shortcut.match_core.contents = base::ASCIIToUTF16("Google Web Search");
149 EXPECT_TRUE(backend_->UpdateShortcut(shortcut));
150 EXPECT_TRUE(changed_notified_);
151 EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id);
152 EXPECT_EQ(shortcut.match_core.contents,
153 shortcuts.find(shortcut.text)->second.match_core.contents);
154 } 205 }
155 206
156 TEST_F(ShortcutsBackendTest, DeleteShortcuts) { 207 TEST_F(ShortcutsBackendTest, DeleteShortcuts) {
157 InitBackend(); 208 InitBackend();
158 ShortcutsBackend::Shortcut shortcut1( 209 history::ShortcutsDatabase::Shortcut shortcut1(
159 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"), 210 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"),
160 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); 211 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
161 EXPECT_TRUE(backend_->AddShortcut(shortcut1)); 212 EXPECT_TRUE(AddShortcut(shortcut1));
162 213
163 ShortcutsBackend::Shortcut shortcut2( 214 history::ShortcutsDatabase::Shortcut shortcut2(
164 "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", base::ASCIIToUTF16("gle"), 215 "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", base::ASCIIToUTF16("gle"),
165 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); 216 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
166 EXPECT_TRUE(backend_->AddShortcut(shortcut2)); 217 EXPECT_TRUE(AddShortcut(shortcut2));
167 218
168 ShortcutsBackend::Shortcut shortcut3( 219 history::ShortcutsDatabase::Shortcut shortcut3(
169 "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", base::ASCIIToUTF16("sp"), 220 "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", base::ASCIIToUTF16("sp"),
170 MatchCoreForTesting("http://www.sport.com"), base::Time::Now(), 10); 221 MatchCoreForTesting("http://www.sport.com"), base::Time::Now(), 10);
171 EXPECT_TRUE(backend_->AddShortcut(shortcut3)); 222 EXPECT_TRUE(AddShortcut(shortcut3));
172 223
173 ShortcutsBackend::Shortcut shortcut4( 224 history::ShortcutsDatabase::Shortcut shortcut4(
174 "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", base::ASCIIToUTF16("mov"), 225 "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", base::ASCIIToUTF16("mov"),
175 MatchCoreForTesting("http://www.film.com"), base::Time::Now(), 10); 226 MatchCoreForTesting("http://www.film.com"), base::Time::Now(), 10);
176 EXPECT_TRUE(backend_->AddShortcut(shortcut4)); 227 EXPECT_TRUE(AddShortcut(shortcut4));
177 228
178 const ShortcutsBackend::ShortcutMap& shortcuts = backend_->shortcuts_map(); 229 ASSERT_EQ(4U, shortcuts_map().size());
230 EXPECT_EQ(shortcut1.id, shortcuts_map().find(shortcut1.text)->second.id);
231 EXPECT_EQ(shortcut2.id, shortcuts_map().find(shortcut2.text)->second.id);
232 EXPECT_EQ(shortcut3.id, shortcuts_map().find(shortcut3.text)->second.id);
233 EXPECT_EQ(shortcut4.id, shortcuts_map().find(shortcut4.text)->second.id);
179 234
180 ASSERT_EQ(4U, shortcuts.size()); 235 EXPECT_TRUE(DeleteShortcutsWithURL(shortcut1.match_core.destination_url));
181 EXPECT_EQ(shortcut1.id, shortcuts.find(shortcut1.text)->second.id);
182 EXPECT_EQ(shortcut2.id, shortcuts.find(shortcut2.text)->second.id);
183 EXPECT_EQ(shortcut3.id, shortcuts.find(shortcut3.text)->second.id);
184 EXPECT_EQ(shortcut4.id, shortcuts.find(shortcut4.text)->second.id);
185 236
186 EXPECT_TRUE(backend_->DeleteShortcutsWithUrl( 237 ASSERT_EQ(2U, shortcuts_map().size());
187 shortcut1.match_core.destination_url)); 238 EXPECT_EQ(0U, shortcuts_map().count(shortcut1.text));
239 EXPECT_EQ(0U, shortcuts_map().count(shortcut2.text));
240 const ShortcutsBackend::ShortcutMap::const_iterator shortcut3_iter(
241 shortcuts_map().find(shortcut3.text));
242 ASSERT_TRUE(shortcut3_iter != shortcuts_map().end());
243 EXPECT_EQ(shortcut3.id, shortcut3_iter->second.id);
244 const ShortcutsBackend::ShortcutMap::const_iterator shortcut4_iter(
245 shortcuts_map().find(shortcut4.text));
246 ASSERT_TRUE(shortcut4_iter != shortcuts_map().end());
247 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id);
188 248
189 ASSERT_EQ(2U, shortcuts.size()); 249 history::ShortcutsDatabase::ShortcutIDs deleted_ids;
190 EXPECT_TRUE(shortcuts.end() == shortcuts.find(shortcut1.text));
191 EXPECT_TRUE(shortcuts.end() == shortcuts.find(shortcut2.text));
192 ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut3.text));
193 ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut4.text));
194 EXPECT_EQ(shortcut3.id, shortcuts.find(shortcut3.text)->second.id);
195 EXPECT_EQ(shortcut4.id, shortcuts.find(shortcut4.text)->second.id);
196
197 std::vector<std::string> deleted_ids;
198 deleted_ids.push_back(shortcut3.id); 250 deleted_ids.push_back(shortcut3.id);
199 deleted_ids.push_back(shortcut4.id); 251 deleted_ids.push_back(shortcut4.id);
252 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids));
200 253
201 EXPECT_TRUE(backend_->DeleteShortcutsWithIds(deleted_ids)); 254 ASSERT_EQ(0U, shortcuts_map().size());
202
203 ASSERT_EQ(0U, shortcuts.size());
204 } 255 }
205
206 } // namespace history
OLDNEW
« 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