OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/files/scoped_temp_dir.h" | |
6 #include "base/message_loop/message_loop.h" | |
7 #include "base/path_service.h" | |
8 #include "base/strings/stringprintf.h" | |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "chrome/browser/history/shortcuts_backend.h" | |
11 #include "chrome/browser/history/shortcuts_backend_factory.h" | |
12 #include "chrome/browser/history/shortcuts_database.h" | |
13 #include "chrome/test/base/testing_profile.h" | |
14 #include "content/public/test/test_browser_thread.h" | |
15 #include "sql/statement.h" | |
16 | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 | |
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 ------------------------------------------------------- | |
45 | |
46 class ShortcutsBackendTest : public testing::Test, | |
47 public ShortcutsBackend::ShortcutsBackendObserver { | |
48 public: | |
49 ShortcutsBackendTest() | |
50 : ui_thread_(content::BrowserThread::UI, &ui_message_loop_), | |
51 db_thread_(content::BrowserThread::DB), | |
52 load_notified_(false), | |
53 changed_notified_(false) {} | |
54 | |
55 virtual void SetUp(); | |
56 virtual void TearDown(); | |
57 | |
58 virtual void OnShortcutsLoaded() OVERRIDE; | |
59 virtual void OnShortcutsChanged() OVERRIDE; | |
60 | |
61 void InitBackend(); | |
62 | |
63 TestingProfile profile_; | |
64 scoped_refptr<ShortcutsBackend> backend_; | |
65 base::MessageLoopForUI ui_message_loop_; | |
66 content::TestBrowserThread ui_thread_; | |
67 content::TestBrowserThread db_thread_; | |
68 | |
69 bool load_notified_; | |
70 bool changed_notified_; | |
71 }; | |
72 | |
73 void ShortcutsBackendTest::SetUp() { | |
74 db_thread_.Start(); | |
75 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( | |
76 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); | |
77 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); | |
78 ASSERT_TRUE(backend_.get()); | |
79 backend_->AddObserver(this); | |
80 } | |
81 | |
82 void ShortcutsBackendTest::TearDown() { | |
83 backend_->RemoveObserver(this); | |
84 db_thread_.Stop(); | |
85 } | |
86 | |
87 void ShortcutsBackendTest::OnShortcutsLoaded() { | |
88 load_notified_ = true; | |
89 base::MessageLoop::current()->Quit(); | |
90 } | |
91 | |
92 void ShortcutsBackendTest::OnShortcutsChanged() { | |
93 changed_notified_ = true; | |
94 } | |
95 | |
96 void ShortcutsBackendTest::InitBackend() { | |
97 ShortcutsBackend* backend = | |
98 ShortcutsBackendFactory::GetForProfile(&profile_).get(); | |
99 ASSERT_TRUE(backend); | |
100 ASSERT_FALSE(load_notified_); | |
101 ASSERT_FALSE(backend_->initialized()); | |
102 base::MessageLoop::current()->Run(); | |
103 EXPECT_TRUE(load_notified_); | |
104 EXPECT_TRUE(backend_->initialized()); | |
105 } | |
106 | |
107 | |
108 // Actual tests --------------------------------------------------------------- | |
109 | |
110 // Verifies that particular original match types are automatically modified when | |
111 // creating shortcuts. | |
112 TEST_F(ShortcutsBackendTest, ChangeMatchTypeOnShortcutCreation) { | |
113 struct { | |
114 AutocompleteMatch::Type input_type; | |
115 AutocompleteMatch::Type output_type; | |
116 } type_cases[] = { | |
117 { AutocompleteMatchType::URL_WHAT_YOU_TYPED, | |
118 AutocompleteMatchType::HISTORY_URL }, | |
119 { AutocompleteMatchType::NAVSUGGEST, | |
120 AutocompleteMatchType::HISTORY_URL }, | |
121 { AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | |
122 AutocompleteMatchType::SEARCH_HISTORY }, | |
123 { AutocompleteMatchType::SEARCH_SUGGEST, | |
124 AutocompleteMatchType::SEARCH_HISTORY }, | |
125 }; | |
126 | |
127 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(type_cases); ++i) { | |
128 EXPECT_EQ(type_cases[i].output_type, | |
129 ConvertedMatchType(type_cases[i].input_type)); | |
130 } | |
131 } | |
132 | |
133 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { | |
134 InitBackend(); | |
135 EXPECT_FALSE(changed_notified_); | |
136 ShortcutsBackend::Shortcut shortcut( | |
137 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"), | |
138 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); | |
139 EXPECT_TRUE(backend_->AddShortcut(shortcut)); | |
140 EXPECT_TRUE(changed_notified_); | |
141 changed_notified_ = false; | |
142 | |
143 const ShortcutsBackend::ShortcutMap& shortcuts = backend_->shortcuts_map(); | |
144 ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut.text)); | |
145 EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id); | |
146 EXPECT_EQ(shortcut.match_core.contents, | |
147 shortcuts.find(shortcut.text)->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 } | |
155 | |
156 TEST_F(ShortcutsBackendTest, DeleteShortcuts) { | |
157 InitBackend(); | |
158 ShortcutsBackend::Shortcut shortcut1( | |
159 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"), | |
160 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); | |
161 EXPECT_TRUE(backend_->AddShortcut(shortcut1)); | |
162 | |
163 ShortcutsBackend::Shortcut shortcut2( | |
164 "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", base::ASCIIToUTF16("gle"), | |
165 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); | |
166 EXPECT_TRUE(backend_->AddShortcut(shortcut2)); | |
167 | |
168 ShortcutsBackend::Shortcut shortcut3( | |
169 "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", base::ASCIIToUTF16("sp"), | |
170 MatchCoreForTesting("http://www.sport.com"), base::Time::Now(), 10); | |
171 EXPECT_TRUE(backend_->AddShortcut(shortcut3)); | |
172 | |
173 ShortcutsBackend::Shortcut shortcut4( | |
174 "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", base::ASCIIToUTF16("mov"), | |
175 MatchCoreForTesting("http://www.film.com"), base::Time::Now(), 10); | |
176 EXPECT_TRUE(backend_->AddShortcut(shortcut4)); | |
177 | |
178 const ShortcutsBackend::ShortcutMap& shortcuts = backend_->shortcuts_map(); | |
179 | |
180 ASSERT_EQ(4U, shortcuts.size()); | |
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 | |
186 EXPECT_TRUE(backend_->DeleteShortcutsWithUrl( | |
187 shortcut1.match_core.destination_url)); | |
188 | |
189 ASSERT_EQ(2U, shortcuts.size()); | |
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); | |
199 deleted_ids.push_back(shortcut4.id); | |
200 | |
201 EXPECT_TRUE(backend_->DeleteShortcutsWithIds(deleted_ids)); | |
202 | |
203 ASSERT_EQ(0U, shortcuts.size()); | |
204 } | |
205 | |
206 } // namespace history | |
OLD | NEW |