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 <algorithm> | |
6 #include <string> | |
7 #include <vector> | |
8 | |
9 #include "base/file_util.h" | |
10 #include "base/files/scoped_temp_dir.h" | |
11 #include "base/string16.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "chrome/browser/intents/default_web_intent_service.h" | |
14 #include "chrome/browser/webdata/web_database.h" | |
15 #include "chrome/browser/webdata/web_intents_table.h" | |
16 #include "chrome/common/chrome_paths.h" | |
17 #include "googleurl/src/gurl.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 #include "webkit/glue/web_intent_service_data.h" | |
20 | |
21 using webkit_glue::WebIntentServiceData; | |
22 | |
23 namespace { | |
24 | |
25 const string16 test_action = | |
26 ASCIIToUTF16("http://webintents.org/intents/share"); | |
27 const string16 test_action_2 = | |
28 ASCIIToUTF16("http://webintents.org/intents/view"); | |
29 const string16 test_scheme = ASCIIToUTF16("mailto"); | |
30 const string16 test_scheme_2 = ASCIIToUTF16("web+poodles"); | |
31 const GURL test_url("http://google.com/"); | |
32 const GURL test_url_fake("http://fakegoogle.com/"); | |
33 const GURL test_service_url("http://jiggle.com/dojiggle"); | |
34 const GURL test_service_url_2("http://waddle.com/waddler"); | |
35 const string16 test_title = ASCIIToUTF16("Test WebIntent"); | |
36 const string16 test_title_2 = ASCIIToUTF16("Test WebIntent #2"); | |
37 const string16 mime_image = ASCIIToUTF16("image/*"); | |
38 const string16 mime_video = ASCIIToUTF16("video/*"); | |
39 | |
40 WebIntentServiceData MakeActionService(const GURL& url, | |
41 const string16& action, | |
42 const string16& type, | |
43 const string16& title) { | |
44 WebIntentServiceData service; | |
45 service.service_url = url; | |
46 service.action = action; | |
47 service.type = type; | |
48 service.title = title; | |
49 service.disposition = WebIntentServiceData::DISPOSITION_INLINE; | |
50 return service; | |
51 } | |
52 | |
53 WebIntentServiceData MakeSchemeService( | |
54 const string16& scheme, const GURL& url, const string16& title) { | |
55 WebIntentServiceData service; | |
56 service.scheme = scheme; | |
57 service.service_url = url; | |
58 service.title = title; | |
59 service.disposition = WebIntentServiceData::DISPOSITION_INLINE; | |
60 return service; | |
61 } | |
62 | |
63 class WebIntentsTableTest : public testing::Test { | |
64 protected: | |
65 virtual void SetUp() { | |
66 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
67 ASSERT_EQ(sql::INIT_OK, | |
68 db_.Init(temp_dir_.path().AppendASCII("TestWebDatabase.db"), | |
69 std::string())); | |
70 } | |
71 | |
72 WebIntentsTable* IntentsTable() { | |
73 return db_.GetWebIntentsTable(); | |
74 } | |
75 | |
76 WebDatabase db_; | |
77 base::ScopedTempDir temp_dir_; | |
78 }; | |
79 | |
80 // Test we can add, retrieve, and remove intent services from the database. | |
81 TEST_F(WebIntentsTableTest, ActionIntents) { | |
82 std::vector<WebIntentServiceData> services; | |
83 | |
84 // By default, no intent services exist. | |
85 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForAction(test_action, | |
86 &services)); | |
87 EXPECT_EQ(0U, services.size()); | |
88 | |
89 // Now adding one. | |
90 WebIntentServiceData service = | |
91 MakeActionService(test_url, test_action, mime_image, test_title); | |
92 EXPECT_TRUE(IntentsTable()->SetWebIntentService(service)); | |
93 | |
94 // Make sure that service can now be fetched | |
95 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForAction(test_action, | |
96 &services)); | |
97 ASSERT_EQ(1U, services.size()); | |
98 EXPECT_EQ(service, services[0]); | |
99 | |
100 // Remove the service. | |
101 EXPECT_TRUE(IntentsTable()->RemoveWebIntentService(service)); | |
102 | |
103 // Should now be gone. | |
104 services.clear(); | |
105 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForAction(test_action, | |
106 &services)); | |
107 EXPECT_EQ(0U, services.size()); | |
108 } | |
109 | |
110 // Test we can add, retrieve, and remove intent services from the database. | |
111 TEST_F(WebIntentsTableTest, SchemeIntents) { | |
112 std::vector<WebIntentServiceData> services; | |
113 | |
114 // By default, no intent services exist. | |
115 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForScheme( | |
116 test_scheme, &services)); | |
117 EXPECT_EQ(0U, services.size()); | |
118 | |
119 // Add a couple new intent services. | |
120 WebIntentServiceData service = | |
121 MakeSchemeService(test_scheme, test_url, test_title); | |
122 EXPECT_TRUE(IntentsTable()->SetWebIntentService(service)); | |
123 | |
124 WebIntentServiceData service2 = | |
125 MakeSchemeService(test_scheme_2, test_url, test_title_2); | |
126 EXPECT_TRUE(IntentsTable()->SetWebIntentService(service2)); | |
127 | |
128 // Make sure we can load both services... | |
129 services.clear(); | |
130 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForScheme( | |
131 test_scheme, &services)); | |
132 ASSERT_EQ(1U, services.size()); | |
133 EXPECT_EQ(service, services[0]); | |
134 | |
135 services.clear(); | |
136 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForScheme( | |
137 test_scheme_2, &services)); | |
138 ASSERT_EQ(1U, services.size()); | |
139 EXPECT_EQ(service2, services[0]); | |
140 | |
141 // Remove the first service. | |
142 EXPECT_TRUE(IntentsTable()->RemoveWebIntentService(service)); | |
143 | |
144 // Should now be gone. | |
145 services.clear(); | |
146 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForScheme( | |
147 test_scheme, &services)); | |
148 EXPECT_EQ(0U, services.size()); | |
149 | |
150 // Service2 should still be present. | |
151 services.clear(); | |
152 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForScheme( | |
153 test_scheme_2, &services)); | |
154 ASSERT_EQ(1U, services.size()); | |
155 EXPECT_EQ(service2, services[0]); | |
156 } | |
157 | |
158 // Test we support multiple intent services for the same MIME type | |
159 TEST_F(WebIntentsTableTest, SetMultipleIntents) { | |
160 std::vector<WebIntentServiceData> services; | |
161 | |
162 WebIntentServiceData service = | |
163 MakeActionService(test_url, test_action, mime_image, test_title); | |
164 EXPECT_TRUE(IntentsTable()->SetWebIntentService(service)); | |
165 | |
166 service.type = mime_video; | |
167 service.title = test_title_2; | |
168 EXPECT_TRUE(IntentsTable()->SetWebIntentService(service)); | |
169 | |
170 // Recover stored intent services from DB. | |
171 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForAction(test_action, | |
172 &services)); | |
173 ASSERT_EQ(2U, services.size()); | |
174 | |
175 // WebIntentsTable does not guarantee order, so ensure order here. | |
176 if (services[0].type == mime_video) | |
177 std::swap(services[0], services[1]); | |
178 | |
179 EXPECT_EQ(service, services[1]); | |
180 | |
181 service.type = mime_image; | |
182 service.title = test_title; | |
183 EXPECT_EQ(service, services[0]); | |
184 } | |
185 | |
186 // Test we support getting all intent services independent of action. | |
187 TEST_F(WebIntentsTableTest, GetAllIntents) { | |
188 std::vector<WebIntentServiceData> services; | |
189 | |
190 WebIntentServiceData service = | |
191 MakeActionService(test_url, test_action, mime_image, test_title); | |
192 EXPECT_TRUE(IntentsTable()->SetWebIntentService(service)); | |
193 | |
194 service.action = test_action_2; | |
195 service.title = test_title_2; | |
196 EXPECT_TRUE(IntentsTable()->SetWebIntentService(service)); | |
197 | |
198 // Recover stored services from DB. | |
199 EXPECT_TRUE(IntentsTable()->GetAllWebIntentServices(&services)); | |
200 ASSERT_EQ(2U, services.size()); | |
201 | |
202 // WebIntentsTable does not guarantee order, so ensure order here. | |
203 if (services[0].type == test_action_2) | |
204 std::swap(services[0], services[1]); | |
205 | |
206 EXPECT_EQ(service, services[1]); | |
207 | |
208 service.action = test_action; | |
209 service.title = test_title; | |
210 EXPECT_EQ(service, services[0]); | |
211 } | |
212 | |
213 TEST_F(WebIntentsTableTest, DispositionToStringMapping) { | |
214 WebIntentServiceData service = | |
215 MakeActionService(test_url, test_action, mime_image, test_title); | |
216 service.disposition = WebIntentServiceData::DISPOSITION_WINDOW; | |
217 EXPECT_TRUE(IntentsTable()->SetWebIntentService(service)); | |
218 | |
219 service = MakeActionService(test_url, test_action, mime_video, test_title); | |
220 service.disposition = WebIntentServiceData::DISPOSITION_INLINE; | |
221 EXPECT_TRUE(IntentsTable()->SetWebIntentService(service)); | |
222 | |
223 std::vector<WebIntentServiceData> services; | |
224 EXPECT_TRUE(IntentsTable()->GetAllWebIntentServices(&services)); | |
225 ASSERT_EQ(2U, services.size()); | |
226 | |
227 if (services[0].disposition == WebIntentServiceData::DISPOSITION_WINDOW) | |
228 std::swap(services[0], services[1]); | |
229 | |
230 EXPECT_EQ(WebIntentServiceData::DISPOSITION_INLINE, services[0].disposition); | |
231 EXPECT_EQ(WebIntentServiceData::DISPOSITION_WINDOW, services[1].disposition); | |
232 } | |
233 | |
234 TEST_F(WebIntentsTableTest, GetByURL) { | |
235 WebIntentServiceData intent = MakeActionService( | |
236 test_url, test_action, mime_image, test_title); | |
237 ASSERT_TRUE(IntentsTable()->SetWebIntentService(intent)); | |
238 | |
239 std::vector<WebIntentServiceData> intents; | |
240 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForURL( | |
241 UTF8ToUTF16(test_url.spec()), &intents)); | |
242 ASSERT_EQ(1U, intents.size()); | |
243 EXPECT_EQ(intent, intents[0]); | |
244 | |
245 intents.clear(); | |
246 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForURL( | |
247 UTF8ToUTF16(test_url_fake.spec()), &intents)); | |
248 EXPECT_EQ(0U, intents.size()); | |
249 | |
250 intent.action = test_action_2; | |
251 ASSERT_TRUE(IntentsTable()->SetWebIntentService(intent)); | |
252 EXPECT_TRUE(IntentsTable()->GetWebIntentServicesForURL( | |
253 UTF8ToUTF16(test_url.spec()), &intents)); | |
254 ASSERT_EQ(2U, intents.size()); | |
255 } | |
256 | |
257 TEST_F(WebIntentsTableTest, DefaultServices) { | |
258 DefaultWebIntentService default_service; | |
259 default_service.action = test_action; | |
260 default_service.type = mime_image; | |
261 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | |
262 default_service.url_pattern.Parse(test_url.spec())); | |
263 default_service.user_date = 1; | |
264 default_service.suppression = 4; | |
265 default_service.service_url = "service_url"; | |
266 ASSERT_TRUE(IntentsTable()->SetDefaultService(default_service)); | |
267 | |
268 default_service.action = test_action_2; | |
269 ASSERT_TRUE(IntentsTable()->SetDefaultService(default_service)); | |
270 | |
271 std::vector<DefaultWebIntentService> defaults; | |
272 ASSERT_TRUE(IntentsTable()->GetDefaultServices(ASCIIToUTF16("no_action"), | |
273 &defaults)); | |
274 EXPECT_EQ(0U, defaults.size()); | |
275 | |
276 ASSERT_TRUE(IntentsTable()->GetDefaultServices(test_action, &defaults)); | |
277 ASSERT_EQ(1U, defaults.size()); | |
278 | |
279 EXPECT_EQ(test_action, defaults[0].action); | |
280 EXPECT_EQ(mime_image, defaults[0].type); | |
281 URLPattern test_pattern(URLPattern::SCHEME_HTTP, test_url.spec()); | |
282 EXPECT_EQ(test_pattern.GetAsString(), defaults[0].url_pattern.GetAsString()); | |
283 EXPECT_EQ(1, defaults[0].user_date); | |
284 EXPECT_EQ(4, defaults[0].suppression); | |
285 EXPECT_EQ("service_url", defaults[0].service_url); | |
286 | |
287 defaults.clear(); | |
288 ASSERT_TRUE(IntentsTable()->GetAllDefaultServices(&defaults)); | |
289 ASSERT_EQ(2U, defaults.size()); | |
290 | |
291 default_service.action = test_action; | |
292 IntentsTable()->RemoveDefaultService(default_service); | |
293 | |
294 defaults.clear(); | |
295 ASSERT_TRUE(IntentsTable()->GetDefaultServices(test_action, &defaults)); | |
296 ASSERT_EQ(0U, defaults.size()); | |
297 | |
298 defaults.clear(); | |
299 ASSERT_TRUE(IntentsTable()->GetDefaultServices(test_action_2, &defaults)); | |
300 ASSERT_EQ(1U, defaults.size()); | |
301 | |
302 defaults.clear(); | |
303 ASSERT_TRUE(IntentsTable()->GetAllDefaultServices(&defaults)); | |
304 ASSERT_EQ(1U, defaults.size()); | |
305 } | |
306 | |
307 TEST_F(WebIntentsTableTest, RemoveDefaultServicesForServiceURL) { | |
308 DefaultWebIntentService s0; | |
309 s0.action = test_action; | |
310 s0.type = mime_image; | |
311 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | |
312 s0.url_pattern.Parse(test_url.spec())); | |
313 s0.user_date = 1; | |
314 s0.suppression = 4; | |
315 s0.service_url = test_service_url.spec(); | |
316 ASSERT_TRUE(IntentsTable()->SetDefaultService(s0)); | |
317 | |
318 DefaultWebIntentService s1; | |
319 s1.action = test_action_2; | |
320 s1.type = mime_image; | |
321 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | |
322 s1.url_pattern.Parse(test_url.spec())); | |
323 s1.user_date = 1; | |
324 s1.suppression = 4; | |
325 s1.service_url = test_service_url.spec(); | |
326 ASSERT_TRUE(IntentsTable()->SetDefaultService(s1)); | |
327 | |
328 DefaultWebIntentService s2; | |
329 s2.action = test_action_2; | |
330 s2.type = mime_image; | |
331 ASSERT_EQ(URLPattern::PARSE_SUCCESS, | |
332 s2.url_pattern.Parse(test_url.spec())); | |
333 s2.user_date = 1; | |
334 s2.suppression = 4; | |
335 s2.service_url = test_service_url_2.spec(); | |
336 ASSERT_TRUE(IntentsTable()->SetDefaultService(s2)); | |
337 | |
338 std::vector<DefaultWebIntentService> defaults; | |
339 ASSERT_TRUE(IntentsTable()->GetAllDefaultServices(&defaults)); | |
340 ASSERT_EQ(2U, defaults.size()); | |
341 | |
342 ASSERT_TRUE(IntentsTable()->RemoveServiceDefaults(test_service_url)); | |
343 | |
344 defaults.clear(); | |
345 ASSERT_TRUE(IntentsTable()->GetAllDefaultServices(&defaults)); | |
346 ASSERT_EQ(1U, defaults.size()); | |
347 EXPECT_EQ(test_service_url_2.spec(), defaults[0].service_url); | |
348 } | |
349 | |
350 TEST_F(WebIntentsTableTest, OverwriteDefaults) { | |
351 DefaultWebIntentService default_service; | |
352 default_service.action = test_action; | |
353 default_service.type = mime_image; | |
354 default_service.user_date = 1; | |
355 default_service.suppression = 4; | |
356 default_service.service_url = "service_url"; | |
357 ASSERT_TRUE(IntentsTable()->SetDefaultService(default_service)); | |
358 | |
359 default_service.user_date = 2; | |
360 default_service.service_url = "service_url2"; | |
361 ASSERT_TRUE(IntentsTable()->SetDefaultService(default_service)); | |
362 | |
363 default_service.user_date = 3; | |
364 default_service.service_url = "service_url3"; | |
365 ASSERT_TRUE(IntentsTable()->SetDefaultService(default_service)); | |
366 | |
367 std::vector<DefaultWebIntentService> defaults; | |
368 ASSERT_TRUE(IntentsTable()->GetAllDefaultServices(&defaults)); | |
369 ASSERT_EQ(1U, defaults.size()); | |
370 EXPECT_EQ("service_url3", defaults[0].service_url); | |
371 } | |
372 | |
373 } // namespace | |
OLD | NEW |