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

Side by Side Diff: components/omnibox/browser/shortcuts_backend_unittest.cc

Issue 1654833005: Componentizes shortcuts unittests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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
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 "components/omnibox/browser/shortcuts_backend.h" 5 #include "components/omnibox/browser/shortcuts_backend.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h" 14 #include "base/threading/thread.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h" 15 #include "components/history/core/browser/history_service.h"
16 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" 16 #include "components/history/core/test/history_service_test_util.h"
17 #include "chrome/test/base/search_test_utils.h" 17 #include "components/omnibox/browser/shortcuts_constants.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/omnibox/browser/shortcuts_database.h" 18 #include "components/omnibox/browser/shortcuts_database.h"
19 #include "components/search_engines/search_terms_data.h"
20 #include "components/search_engines/template_url_service.h" 20 #include "components/search_engines/template_url_service.h"
21 #include "content/public/test/test_browser_thread.h"
22
23 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
24 22
25
26 // ShortcutsBackendTest ------------------------------------------------------- 23 // ShortcutsBackendTest -------------------------------------------------------
27 24
28 class ShortcutsBackendTest : public testing::Test, 25 class ShortcutsBackendTest : public testing::Test,
29 public ShortcutsBackend::ShortcutsBackendObserver { 26 public ShortcutsBackend::ShortcutsBackendObserver {
30 public: 27 public:
31 ShortcutsBackendTest(); 28 ShortcutsBackendTest();
32 29
33 ShortcutsDatabase::Shortcut::MatchCore MatchCoreForTesting( 30 ShortcutsDatabase::Shortcut::MatchCore MatchCoreForTesting(
34 const std::string& url, 31 const std::string& url,
35 const std::string& contents_class = std::string(), 32 const std::string& contents_class = std::string(),
36 const std::string& description_class = std::string(), 33 const std::string& description_class = std::string(),
37 AutocompleteMatch::Type type = AutocompleteMatchType::URL_WHAT_YOU_TYPED); 34 AutocompleteMatch::Type type = AutocompleteMatchType::URL_WHAT_YOU_TYPED);
38 void SetSearchProvider(); 35 void SetSearchProvider();
39 36
40 void SetUp() override; 37 void SetUp() override;
41 void TearDown() override; 38 void TearDown() override;
42 39
43 void OnShortcutsLoaded() override; 40 void OnShortcutsLoaded() override;
44 void OnShortcutsChanged() override; 41 void OnShortcutsChanged() override;
45 42
46 const ShortcutsBackend::ShortcutMap& shortcuts_map() const { 43 const ShortcutsBackend::ShortcutMap& shortcuts_map() const {
47 return backend_->shortcuts_map(); 44 return backend_->shortcuts_map();
48 } 45 }
49 bool changed_notified() const { return changed_notified_; } 46 bool changed_notified() const { return changed_notified_; }
50 void set_changed_notified(bool changed_notified) { 47 void set_changed_notified(bool changed_notified) {
51 changed_notified_ = changed_notified; 48 changed_notified_ = changed_notified;
52 } 49 }
53 50
54 void InitBackend(); 51 void InitBackend();
55 bool AddShortcut(const ShortcutsDatabase::Shortcut& shortcut); 52 bool AddShortcut(const ShortcutsDatabase::Shortcut& shortcut);
56 bool UpdateShortcut(const ShortcutsDatabase::Shortcut& shortcut); 53 bool UpdateShortcut(const ShortcutsDatabase::Shortcut& shortcut);
57 bool DeleteShortcutsWithURL(const GURL& url); 54 bool DeleteShortcutsWithURL(const GURL& url);
58 bool DeleteShortcutsWithIDs( 55 bool DeleteShortcutsWithIDs(
59 const ShortcutsDatabase::ShortcutIDs& deleted_ids); 56 const ShortcutsDatabase::ShortcutIDs& deleted_ids);
60 57
61 private: 58 TemplateURLService* GetTemplateURLService();
62 base::MessageLoopForUI ui_message_loop_;
63 content::TestBrowserThread ui_thread_;
64 content::TestBrowserThread db_thread_;
65
66 protected:
67 TestingProfile profile_;
68 UIThreadSearchTermsData search_terms_data_;
69 59
70 private: 60 private:
61 base::MessageLoop message_loop_;
62 base::Thread db_thread_;
63 base::ScopedTempDir profile_dir_;
64 scoped_ptr<TemplateURLService> template_url_service_;
65 scoped_ptr<history::HistoryService> history_service_;
66
71 scoped_refptr<ShortcutsBackend> backend_; 67 scoped_refptr<ShortcutsBackend> backend_;
72 68
73 bool load_notified_; 69 bool load_notified_;
74 bool changed_notified_; 70 bool changed_notified_;
75 71
76 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest); 72 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest);
77 }; 73 };
78 74
79 ShortcutsBackendTest::ShortcutsBackendTest() 75 ShortcutsBackendTest::ShortcutsBackendTest()
80 : ui_thread_(content::BrowserThread::UI, &ui_message_loop_), 76 : db_thread_("Test DB thread"),
81 db_thread_(content::BrowserThread::DB),
82 search_terms_data_(&profile_),
83 load_notified_(false), 77 load_notified_(false),
84 changed_notified_(false) { 78 changed_notified_(false) {}
85 }
86 79
87 ShortcutsDatabase::Shortcut::MatchCore 80 ShortcutsDatabase::Shortcut::MatchCore
88 ShortcutsBackendTest::MatchCoreForTesting(const std::string& url, 81 ShortcutsBackendTest::MatchCoreForTesting(const std::string& url,
89 const std::string& contents_class, 82 const std::string& contents_class,
90 const std::string& description_class, 83 const std::string& description_class,
91 AutocompleteMatch::Type type) { 84 AutocompleteMatch::Type type) {
92 AutocompleteMatch match(NULL, 0, 0, type); 85 AutocompleteMatch match(NULL, 0, 0, type);
93 match.destination_url = GURL(url); 86 match.destination_url = GURL(url);
94 match.contents = base::ASCIIToUTF16("test"); 87 match.contents = base::ASCIIToUTF16("test");
95 match.contents_class = 88 match.contents_class =
96 AutocompleteMatch::ClassificationsFromString(contents_class); 89 AutocompleteMatch::ClassificationsFromString(contents_class);
97 match.description_class = 90 match.description_class =
98 AutocompleteMatch::ClassificationsFromString(description_class); 91 AutocompleteMatch::ClassificationsFromString(description_class);
99 match.search_terms_args.reset( 92 match.search_terms_args.reset(
100 new TemplateURLRef::SearchTermsArgs(match.contents)); 93 new TemplateURLRef::SearchTermsArgs(match.contents));
101 return ShortcutsBackend::MatchToMatchCore( 94 SearchTermsData search_terms_data;
102 match, TemplateURLServiceFactory::GetForProfile(&profile_), 95 return ShortcutsBackend::MatchToMatchCore(match, template_url_service_.get(),
103 &search_terms_data_); 96 &search_terms_data);
104 } 97 }
105 98
106 void ShortcutsBackendTest::SetSearchProvider() { 99 void ShortcutsBackendTest::SetSearchProvider() {
107 TemplateURLService* template_url_service =
108 TemplateURLServiceFactory::GetForProfile(&profile_);
109 TemplateURLData data; 100 TemplateURLData data;
110 data.SetURL("http://foo.com/search?bar={searchTerms}"); 101 data.SetURL("http://foo.com/search?bar={searchTerms}");
111 data.SetShortName(base::UTF8ToUTF16("foo")); 102 data.SetShortName(base::UTF8ToUTF16("foo"));
112 data.SetKeyword(base::UTF8ToUTF16("foo")); 103 data.SetKeyword(base::UTF8ToUTF16("foo"));
113 104
114 TemplateURL* template_url = new TemplateURL(data); 105 TemplateURL* template_url = new TemplateURL(data);
115 // Takes ownership of |template_url|. 106 // Takes ownership of |template_url|.
116 template_url_service->Add(template_url); 107 template_url_service_->Add(template_url);
117 template_url_service->SetUserSelectedDefaultSearchProvider(template_url); 108 template_url_service_->SetUserSelectedDefaultSearchProvider(template_url);
118 } 109 }
119 110
120 void ShortcutsBackendTest::SetUp() { 111 void ShortcutsBackendTest::SetUp() {
112 template_url_service_.reset(new TemplateURLService(nullptr, 0));
113 if (profile_dir_.CreateUniqueTempDir()) {
114 history_service_ =
115 history::CreateHistoryService(profile_dir_.path(), std::string(), true);
116 }
117 ASSERT_TRUE(history_service_);
118
121 db_thread_.Start(); 119 db_thread_.Start();
122 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( 120 base::FilePath shortcuts_database_path =
123 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); 121 profile_dir_.path().Append(kShortcutsDatabaseName);
124 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); 122 backend_ = new ShortcutsBackend(
123 template_url_service_.get(), make_scoped_ptr(new SearchTermsData()),
124 history_service_.get(), db_thread_.task_runner(), shortcuts_database_path,
125 false);
125 ASSERT_TRUE(backend_.get()); 126 ASSERT_TRUE(backend_.get());
126 backend_->AddObserver(this); 127 backend_->AddObserver(this);
127
128 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
129 &profile_, &TemplateURLServiceFactory::BuildInstanceFor);
130 TemplateURLService* template_url_service =
131 TemplateURLServiceFactory::GetForProfile(&profile_);
132 search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
133 } 128 }
134 129
135 void ShortcutsBackendTest::TearDown() { 130 void ShortcutsBackendTest::TearDown() {
136 backend_->RemoveObserver(this); 131 backend_->RemoveObserver(this);
137 db_thread_.Stop(); 132 db_thread_.Stop();
138 } 133 }
139 134
140 void ShortcutsBackendTest::OnShortcutsLoaded() { 135 void ShortcutsBackendTest::OnShortcutsLoaded() {
141 load_notified_ = true; 136 load_notified_ = true;
142 base::MessageLoop::current()->QuitWhenIdle(); 137 base::MessageLoop::current()->QuitWhenIdle();
143 } 138 }
144 139
145 void ShortcutsBackendTest::OnShortcutsChanged() { 140 void ShortcutsBackendTest::OnShortcutsChanged() {
146 changed_notified_ = true; 141 changed_notified_ = true;
147 } 142 }
148 143
149 void ShortcutsBackendTest::InitBackend() { 144 void ShortcutsBackendTest::InitBackend() {
150 ShortcutsBackend* backend = 145 ASSERT_TRUE(backend_);
151 ShortcutsBackendFactory::GetForProfile(&profile_).get();
152 ASSERT_TRUE(backend);
153 ASSERT_FALSE(load_notified_); 146 ASSERT_FALSE(load_notified_);
154 ASSERT_FALSE(backend_->initialized()); 147 ASSERT_FALSE(backend_->initialized());
148 backend_->Init();
155 base::MessageLoop::current()->Run(); 149 base::MessageLoop::current()->Run();
156 EXPECT_TRUE(load_notified_); 150 EXPECT_TRUE(load_notified_);
157 EXPECT_TRUE(backend_->initialized()); 151 EXPECT_TRUE(backend_->initialized());
158 } 152 }
159 153
160 bool ShortcutsBackendTest::AddShortcut( 154 bool ShortcutsBackendTest::AddShortcut(
161 const ShortcutsDatabase::Shortcut& shortcut) { 155 const ShortcutsDatabase::Shortcut& shortcut) {
162 return backend_->AddShortcut(shortcut); 156 return backend_->AddShortcut(shortcut);
163 } 157 }
164 158
165 bool ShortcutsBackendTest::UpdateShortcut( 159 bool ShortcutsBackendTest::UpdateShortcut(
166 const ShortcutsDatabase::Shortcut& shortcut) { 160 const ShortcutsDatabase::Shortcut& shortcut) {
167 return backend_->UpdateShortcut(shortcut); 161 return backend_->UpdateShortcut(shortcut);
168 } 162 }
169 163
170 bool ShortcutsBackendTest::DeleteShortcutsWithURL(const GURL& url) { 164 bool ShortcutsBackendTest::DeleteShortcutsWithURL(const GURL& url) {
171 return backend_->DeleteShortcutsWithURL(url); 165 return backend_->DeleteShortcutsWithURL(url);
172 } 166 }
173 167
174 bool ShortcutsBackendTest::DeleteShortcutsWithIDs( 168 bool ShortcutsBackendTest::DeleteShortcutsWithIDs(
175 const ShortcutsDatabase::ShortcutIDs& deleted_ids) { 169 const ShortcutsDatabase::ShortcutIDs& deleted_ids) {
176 return backend_->DeleteShortcutsWithIDs(deleted_ids); 170 return backend_->DeleteShortcutsWithIDs(deleted_ids);
177 } 171 }
178 172
173 TemplateURLService* ShortcutsBackendTest::GetTemplateURLService() {
174 return template_url_service_.get();
175 }
179 176
180 // Actual tests --------------------------------------------------------------- 177 // Actual tests ---------------------------------------------------------------
181 178
182 // Verifies that creating MatchCores strips classifications and sanitizes match 179 // Verifies that creating MatchCores strips classifications and sanitizes match
183 // types. 180 // types.
184 TEST_F(ShortcutsBackendTest, SanitizeMatchCore) { 181 TEST_F(ShortcutsBackendTest, SanitizeMatchCore) {
185 struct { 182 struct {
186 std::string input_contents_class; 183 std::string input_contents_class;
187 std::string input_description_class; 184 std::string input_description_class;
188 AutocompleteMatch::Type input_type; 185 AutocompleteMatch::Type input_type;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 TEST_F(ShortcutsBackendTest, EntitySuggestionTest) { 222 TEST_F(ShortcutsBackendTest, EntitySuggestionTest) {
226 SetSearchProvider(); 223 SetSearchProvider();
227 AutocompleteMatch match; 224 AutocompleteMatch match;
228 match.fill_into_edit = base::UTF8ToUTF16("franklin d roosevelt"); 225 match.fill_into_edit = base::UTF8ToUTF16("franklin d roosevelt");
229 match.type = AutocompleteMatchType::SEARCH_SUGGEST_ENTITY; 226 match.type = AutocompleteMatchType::SEARCH_SUGGEST_ENTITY;
230 match.contents = base::UTF8ToUTF16("roosevelt"); 227 match.contents = base::UTF8ToUTF16("roosevelt");
231 match.contents_class = 228 match.contents_class =
232 AutocompleteMatch::ClassificationsFromString("0,0,5,2"); 229 AutocompleteMatch::ClassificationsFromString("0,0,5,2");
233 match.description = base::UTF8ToUTF16("Franklin D. Roosevelt"); 230 match.description = base::UTF8ToUTF16("Franklin D. Roosevelt");
234 match.description_class = AutocompleteMatch::ClassificationsFromString("0,4"); 231 match.description_class = AutocompleteMatch::ClassificationsFromString("0,4");
235 match.destination_url = GURL( 232 match.destination_url =
236 "http://www.foo.com/search?bar=franklin+d+roosevelt&gs_ssp=1234"); 233 GURL("http://www.foo.com/search?bar=franklin+d+roosevelt&gs_ssp=1234");
237 match.keyword = base::UTF8ToUTF16("foo"); 234 match.keyword = base::UTF8ToUTF16("foo");
238 match.search_terms_args.reset( 235 match.search_terms_args.reset(
239 new TemplateURLRef::SearchTermsArgs(match.fill_into_edit)); 236 new TemplateURLRef::SearchTermsArgs(match.fill_into_edit));
240 237
238 SearchTermsData search_terms_data;
241 ShortcutsDatabase::Shortcut::MatchCore match_core = 239 ShortcutsDatabase::Shortcut::MatchCore match_core =
242 ShortcutsBackend::MatchToMatchCore( 240 ShortcutsBackend::MatchToMatchCore(match, GetTemplateURLService(),
243 match, TemplateURLServiceFactory::GetForProfile(&profile_), 241 &search_terms_data);
244 &search_terms_data_);
245 EXPECT_EQ("http://foo.com/search?bar=franklin+d+roosevelt", 242 EXPECT_EQ("http://foo.com/search?bar=franklin+d+roosevelt",
246 match_core.destination_url.spec()); 243 match_core.destination_url.spec());
247 EXPECT_EQ(match.fill_into_edit, match_core.contents); 244 EXPECT_EQ(match.fill_into_edit, match_core.contents);
248 EXPECT_EQ("0,0", match_core.contents_class); 245 EXPECT_EQ("0,0", match_core.contents_class);
249 EXPECT_EQ(base::string16(), match_core.description); 246 EXPECT_EQ(base::string16(), match_core.description);
250 EXPECT_TRUE(match_core.description_class.empty()); 247 EXPECT_TRUE(match_core.description_class.empty());
251 } 248 }
252 249
253 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { 250 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) {
254 InitBackend(); 251 InitBackend();
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 ASSERT_TRUE(shortcut4_iter != shortcuts_map().end()); 316 ASSERT_TRUE(shortcut4_iter != shortcuts_map().end());
320 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id); 317 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id);
321 318
322 ShortcutsDatabase::ShortcutIDs deleted_ids; 319 ShortcutsDatabase::ShortcutIDs deleted_ids;
323 deleted_ids.push_back(shortcut3.id); 320 deleted_ids.push_back(shortcut3.id);
324 deleted_ids.push_back(shortcut4.id); 321 deleted_ids.push_back(shortcut4.id);
325 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids)); 322 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids));
326 323
327 ASSERT_EQ(0U, shortcuts_map().size()); 324 ASSERT_EQ(0U, shortcuts_map().size());
328 } 325 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/shortcuts_backend.h ('k') | components/omnibox/browser/shortcuts_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698