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

Side by Side Diff: chrome/browser/chromeos/arc/arc_external_protocol_dialog_unittest.cc

Issue 2432013002: Improve intent: URI handling (Closed)
Patch Set: review Created 4 years, 2 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
(Empty)
1 // Copyright 2016 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 "chrome/browser/chromeos/arc/arc_external_protocol_dialog.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "url/gurl.h"
9
10 namespace arc {
11
12 namespace {
13
14 constexpr char kChromePackageName[] = "org.chromium.arc.intent_helper";
15
16 // Creates and returns a new IntentHandlerInfo object.
17 mojom::IntentHandlerInfoPtr Create(const std::string& name,
18 const std::string& package_name,
19 bool is_preferred,
20 const GURL& fallback_url) {
21 mojom::IntentHandlerInfoPtr ptr = mojom::IntentHandlerInfo::New();
22 ptr->name = name;
23 ptr->package_name = package_name;
24 ptr->is_preferred = is_preferred;
25 if (!fallback_url.is_empty())
26 ptr->fallback_url = fallback_url.spec();
27 return ptr;
28 }
29
30 } // namespace
31
32 // Tests that GetAppIndex() works as intended.
33 TEST(ArcExternalProtocolDialogTest, TestGetAppIndex) {
34 {
35 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
36 EXPECT_EQ(handlers.size(), GetAppIndexForTesting(handlers, "package_name"));
37 }
38 {
39 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
40 handlers.push_back(Create("0", "package_name0", false, GURL()));
41 EXPECT_EQ(0U, GetAppIndexForTesting(handlers, "package_name0"));
42 EXPECT_EQ(handlers.size(), GetAppIndexForTesting(handlers, "package_name"));
43 }
44 {
45 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
46 handlers.push_back(Create("0", "package_name0", false, GURL()));
47 handlers.push_back(Create("1", "package_name1", false, GURL()));
48 EXPECT_EQ(0U, GetAppIndexForTesting(handlers, "package_name0"));
49 EXPECT_EQ(1U, GetAppIndexForTesting(handlers, "package_name1"));
50 EXPECT_EQ(handlers.size(), GetAppIndexForTesting(handlers, "package_name"));
51 }
52 }
53
54 // Tests that when no apps are returned from ARC, GetAction returns
55 // SHOW_CHROME_OS_DIALOG.
56 TEST(ArcExternalProtocolDialogTest, TestGetActionWithNoApp) {
57 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
58 std::pair<GURL, std::string> url_and_package;
59 EXPECT_EQ(GetActionResult::SHOW_CHROME_OS_DIALOG,
60 GetActionForTesting(GURL("external-protocol:foo"), handlers,
61 handlers.size(), &url_and_package));
62 }
63
64 // Tests that when one app is passed to GetAction but the user hasn't selected
65 // it, the function returns ASK_USER.
66 TEST(ArcExternalProtocolDialogTest, TestGetActionWithOneApp) {
67 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
68 handlers.push_back(
69 Create("package", "com.google.package.name", false, GURL()));
70
71 const size_t no_selection = handlers.size();
72 std::pair<GURL, std::string> url_and_package;
73 EXPECT_EQ(GetActionResult::ASK_USER,
74 GetActionForTesting(GURL("external-protocol:foo"), handlers,
75 no_selection, &url_and_package));
76 }
77
78 // Tests that when one preferred app is passed to GetAction, the function
79 // returns HANDLE_URL_IN_ARC even if the user hasn't selected the app.
80 TEST(ArcExternalProtocolDialogTest, TestGetActionWithOnePreferredApp) {
81 const GURL external_url("external-protocol:foo");
82 const std::string package_name("com.google.package.name");
83
84 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
85 handlers.push_back(Create("package", package_name, true, GURL()));
86
87 const size_t no_selection = handlers.size();
88 std::pair<GURL, std::string> url_and_package;
89 EXPECT_EQ(GetActionResult::HANDLE_URL_IN_ARC,
90 GetActionForTesting(external_url, handlers, no_selection,
91 &url_and_package));
92 EXPECT_EQ(external_url, url_and_package.first);
93 EXPECT_EQ(package_name, url_and_package.second);
94 }
95
96 // Tests that when one app is passed to GetAction, the user has already selected
97 // it, the function returns HANDLE_URL_IN_ARC.
98 TEST(ArcExternalProtocolDialogTest, TestGetActionWithOneAppSelected) {
99 const GURL external_url("external-protocol:foo");
100 const std::string package_name("com.google.package.name");
101
102 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
103 handlers.push_back(Create("package", package_name, false, GURL()));
104
105 constexpr size_t kSelection = 0;
106 std::pair<GURL, std::string> url_and_package;
107 EXPECT_EQ(GetActionResult::HANDLE_URL_IN_ARC,
108 GetActionForTesting(external_url, handlers, kSelection,
109 &url_and_package));
110 EXPECT_EQ(external_url, url_and_package.first);
111 EXPECT_EQ(package_name, url_and_package.second);
112 }
113
114 // Tests the same as TestGetActionWithOnePreferredApp but with two apps.
115 TEST(ArcExternalProtocolDialogTest,
116 TestGetActionWithOnePreferredAppAndOneOther) {
117 const GURL external_url("external-protocol:foo");
118 const std::string package_name("com.google.package2.name");
119
120 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
121 handlers.push_back(
122 Create("package", "com.google.package.name", false, GURL()));
123 handlers.push_back(Create("package2", package_name, true, GURL()));
124
125 const size_t no_selection = handlers.size();
126 std::pair<GURL, std::string> url_and_package;
127 EXPECT_EQ(GetActionResult::HANDLE_URL_IN_ARC,
128 GetActionForTesting(external_url, handlers, no_selection,
129 &url_and_package));
130 EXPECT_EQ(external_url, url_and_package.first);
131 EXPECT_EQ(package_name, url_and_package.second);
132 }
133
134 // Tests that HANDLE_URL_IN_ARC is returned for geo: URL. The URL is special in
135 // that intent_helper (i.e. the Chrome proxy) can handle it but Chrome cannot.
136 // We have to send such a URL to intent_helper to let the helper rewrite the
137 // URL to https://maps.google.com/?latlon=xxx which Chrome can handle.
138 TEST(ArcExternalProtocolDialogTest, TestGetActionWithGeoUrl) {
139 const GURL geo_url("geo:37.7749,-122.4194");
140
141 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
142 handlers.push_back(Create("Chrome", kChromePackageName, true, GURL()));
143
144 const size_t no_selection = handlers.size();
145 std::pair<GURL, std::string> url_and_package;
146 EXPECT_EQ(
147 GetActionResult::HANDLE_URL_IN_ARC,
148 GetActionForTesting(geo_url, handlers, no_selection, &url_and_package));
149 EXPECT_EQ(geo_url, url_and_package.first);
150 EXPECT_EQ(kChromePackageName, url_and_package.second);
151 }
152
153 // Tests that OPEN_URL_IN_CHROME is returned when a handler with a fallback http
154 // URL and kChromePackageName is passed to GetAction, even if the handler is not
155 // a preferred one.
156 TEST(ArcExternalProtocolDialogTest, TestGetActionWithOneFallbackUrl) {
157 const GURL intent_url_with_fallback(
158 "intent://scan/#Intent;scheme=abc;package=com.google.abc;"
159 "S.browser_fallback_url=http://zxing.org;end");
160 const GURL fallback_url("http://zxing.org");
161
162 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
163 handlers.push_back(Create("Chrome", kChromePackageName, false, fallback_url));
164
165 const size_t no_selection = handlers.size();
166 std::pair<GURL, std::string> url_and_package;
167 EXPECT_EQ(GetActionResult::OPEN_URL_IN_CHROME,
168 GetActionForTesting(intent_url_with_fallback, handlers,
169 no_selection, &url_and_package));
170 EXPECT_EQ(fallback_url, url_and_package.first);
171 EXPECT_EQ(kChromePackageName, url_and_package.second);
172 }
173
174 // Tests the same with https and is_preferred == true.
175 TEST(ArcExternalProtocolDialogTest, TestGetActionWithOnePreferredFallbackUrl) {
176 const GURL intent_url_with_fallback(
177 "intent://scan/#Intent;scheme=abc;package=com.google.abc;"
178 "S.browser_fallback_url=https://zxing.org;end");
179 const GURL fallback_url("https://zxing.org");
180
181 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
182 handlers.push_back(Create("Chrome", kChromePackageName, true, fallback_url));
183
184 const size_t no_selection = handlers.size();
185 std::pair<GURL, std::string> url_and_package;
186 EXPECT_EQ(GetActionResult::OPEN_URL_IN_CHROME,
187 GetActionForTesting(intent_url_with_fallback, handlers,
188 no_selection, &url_and_package));
189 EXPECT_EQ(fallback_url, url_and_package.first);
190 EXPECT_EQ(kChromePackageName, url_and_package.second);
191 }
192
193 // Tests that ASK_USER is returned when two handlers with fallback URLs are
194 // passed to GetAction. This may happen when the user has installed a 3rd party
195 // browser app, and then clicks a intent: URI with a http fallback.
196 TEST(ArcExternalProtocolDialogTest, TestGetActionWithTwoFallbackUrls) {
197 const GURL intent_url_with_fallback(
198 "intent://scan/#Intent;scheme=abc;package=com.google.abc;"
199 "S.browser_fallback_url=http://zxing.org;end");
200 const GURL fallback_url("http://zxing.org");
201
202 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
203 handlers.push_back(
204 Create("Other browser", "com.other.browser", false, fallback_url));
205 handlers.push_back(Create("Chrome", kChromePackageName, false, fallback_url));
206
207 const size_t no_selection = handlers.size();
208 std::pair<GURL, std::string> url_and_package;
209 EXPECT_EQ(GetActionResult::ASK_USER,
210 GetActionForTesting(intent_url_with_fallback, handlers,
211 no_selection, &url_and_package));
212 }
213
214 // Tests the same but set Chrome as a preferred app. In this case, ASK_USER
215 // shouldn't be returned.
216 TEST(ArcExternalProtocolDialogTest,
217 TestGetActionWithTwoFallbackUrlsChromePreferred) {
218 const GURL intent_url_with_fallback(
219 "intent://scan/#Intent;scheme=abc;package=com.google.abc;"
220 "S.browser_fallback_url=http://zxing.org;end");
221 const GURL fallback_url("http://zxing.org");
222
223 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
224 handlers.push_back(
225 Create("Other browser", "com.other.browser", false, fallback_url));
226 handlers.push_back(Create("Chrome", kChromePackageName, true, fallback_url));
227
228 const size_t no_selection = handlers.size();
229 std::pair<GURL, std::string> url_and_package;
230 EXPECT_EQ(GetActionResult::OPEN_URL_IN_CHROME,
231 GetActionForTesting(intent_url_with_fallback, handlers,
232 no_selection, &url_and_package));
233 EXPECT_EQ(fallback_url, url_and_package.first);
234 EXPECT_EQ(kChromePackageName, url_and_package.second);
235 }
236
237 // Tests the same but set "other browser" as a preferred app. In this case,
238 // ASK_USER shouldn't be returned either.
239 TEST(ArcExternalProtocolDialogTest,
240 TestGetActionWithTwoFallbackUrlsOtherBrowserPreferred) {
241 const GURL intent_url_with_fallback(
242 "intent://scan/#Intent;scheme=abc;package=com.google.abc;"
243 "S.browser_fallback_url=http://zxing.org;end");
244 const GURL fallback_url("http://zxing.org");
245 const std::string package_name = "com.other.browser";
246
247 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
248 handlers.push_back(Create("Other browser", package_name, true, fallback_url));
249 handlers.push_back(Create("Chrome", kChromePackageName, false, fallback_url));
250
251 const size_t no_selection = handlers.size();
252 std::pair<GURL, std::string> url_and_package;
253 EXPECT_EQ(GetActionResult::HANDLE_URL_IN_ARC,
254 GetActionForTesting(intent_url_with_fallback, handlers,
255 no_selection, &url_and_package));
256 EXPECT_EQ(fallback_url, url_and_package.first);
257 EXPECT_EQ(package_name, url_and_package.second);
258 }
259
260 // Tests the same but set Chrome as a user-selected app.
261 TEST(ArcExternalProtocolDialogTest,
262 TestGetActionWithTwoFallbackUrlsChromeSelected) {
263 const GURL intent_url_with_fallback(
264 "intent://scan/#Intent;scheme=abc;package=com.google.abc;"
265 "S.browser_fallback_url=http://zxing.org;end");
266 const GURL fallback_url("http://zxing.org");
267
268 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
269 handlers.push_back(
270 Create("Other browser", "com.other.browser", false, fallback_url));
271 handlers.push_back(Create("Chrome", kChromePackageName, false, fallback_url));
272
273 constexpr size_t kSelection = 1; // Chrome
274 std::pair<GURL, std::string> url_and_package;
275 EXPECT_EQ(GetActionResult::OPEN_URL_IN_CHROME,
276 GetActionForTesting(intent_url_with_fallback, handlers, kSelection,
277 &url_and_package));
278 EXPECT_EQ(fallback_url, url_and_package.first);
279 EXPECT_EQ(kChromePackageName, url_and_package.second);
280 }
281
282 // Tests the same but set "other browser" as a preferred app.
283 TEST(ArcExternalProtocolDialogTest,
284 TestGetActionWithTwoFallbackUrlsOtherBrowserSelected) {
285 const GURL intent_url_with_fallback(
286 "intent://scan/#Intent;scheme=abc;package=com.google.abc;"
287 "S.browser_fallback_url=http://zxing.org;end");
288 const GURL fallback_url("http://zxing.org");
289 const std::string package_name = "com.other.browser";
djacobo_ 2016/10/24 18:17:36 awesome name :P
290
291 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
292 handlers.push_back(
293 Create("Other browser", package_name, false, fallback_url));
294 handlers.push_back(Create("Chrome", kChromePackageName, false, fallback_url));
295
296 constexpr size_t kSelection = 0; // the other browser
297 std::pair<GURL, std::string> url_and_package;
298 EXPECT_EQ(GetActionResult::HANDLE_URL_IN_ARC,
299 GetActionForTesting(intent_url_with_fallback, handlers, kSelection,
300 &url_and_package));
301 EXPECT_EQ(fallback_url, url_and_package.first);
302 EXPECT_EQ(package_name, url_and_package.second);
303 }
304
305 // Tests that ASK_USER is returned when a handler with a fallback market: URL
306 // is passed to GetAction.
307 TEST(ArcExternalProtocolDialogTest, TestGetActionWithOneMarketFallbackUrl) {
308 const GURL intent_url_with_fallback(
309 "intent://scan/#Intent;scheme=abc;package=com.google.abc;end");
310 const GURL fallback_url("market://details?id=com.google.abc");
311
312 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
313 handlers.push_back(
314 Create("Play Store", "com.google.play.store", false, fallback_url));
315
316 const size_t no_selection = handlers.size();
317 std::pair<GURL, std::string> url_and_package;
318 EXPECT_EQ(GetActionResult::ASK_USER,
319 GetActionForTesting(intent_url_with_fallback, handlers,
320 no_selection, &url_and_package));
321 }
322
323 // Tests the same but with is_preferred == true.
324 TEST(ArcExternalProtocolDialogTest,
325 TestGetActionWithOnePreferredMarketFallbackUrl) {
326 const GURL intent_url_with_fallback(
327 "intent://scan/#Intent;scheme=abc;package=com.google.abc;end");
328 const GURL fallback_url("market://details?id=com.google.abc");
329 const std::string play_store_package_name = "com.google.play.store";
330
331 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
332 handlers.push_back(
333 Create("Play Store", play_store_package_name, true, fallback_url));
334
335 const size_t no_selection = handlers.size();
336 std::pair<GURL, std::string> url_and_package;
337 EXPECT_EQ(GetActionResult::HANDLE_URL_IN_ARC,
338 GetActionForTesting(intent_url_with_fallback, handlers,
339 no_selection, &url_and_package));
340 EXPECT_EQ(fallback_url, url_and_package.first);
341 EXPECT_EQ(play_store_package_name, url_and_package.second);
342 }
343
344 // Tests the same but with an app_seleteced_index.
345 TEST(ArcExternalProtocolDialogTest,
346 TestGetActionWithOneSelectedMarketFallbackUrl) {
347 const GURL intent_url_with_fallback(
348 "intent://scan/#Intent;scheme=abc;package=com.google.abc;end");
349 const GURL fallback_url("market://details?id=com.google.abc");
350 const std::string play_store_package_name = "com.google.play.store";
351
352 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
353 handlers.push_back(
354 Create("Play Store", play_store_package_name, false, fallback_url));
355
356 constexpr size_t kSelection = 0;
357 std::pair<GURL, std::string> url_and_package;
358 EXPECT_EQ(GetActionResult::HANDLE_URL_IN_ARC,
359 GetActionForTesting(intent_url_with_fallback, handlers, kSelection,
360 &url_and_package));
361 EXPECT_EQ(fallback_url, url_and_package.first);
362 EXPECT_EQ(play_store_package_name, url_and_package.second);
363 }
364
365 // Tests that ASK_USER is returned when two handlers with fallback market: URLs
366 // are passed to GetAction. Unlike the two browsers case, this rarely happens on
367 // the user's device, though.
368 TEST(ArcExternalProtocolDialogTest, TestGetActionWithTwoMarketFallbackUrls) {
369 const GURL intent_url_with_fallback(
370 "intent://scan/#Intent;scheme=abc;package=com.google.abc;end");
371 const GURL fallback_url("market://details?id=com.google.abc");
372
373 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
374 handlers.push_back(
375 Create("Play Store", "com.google.play.store", false, fallback_url));
376 handlers.push_back(
377 Create("Other Store app", "com.other.play.store", false, fallback_url));
378
379 const size_t no_selection = handlers.size();
380 std::pair<GURL, std::string> url_and_package;
381 EXPECT_EQ(GetActionResult::ASK_USER,
382 GetActionForTesting(intent_url_with_fallback, handlers,
383 no_selection, &url_and_package));
384 }
385
386 // Tests the same, but make the first handler a preferred one.
djacobo_ 2016/10/24 18:17:36 Isn't that the second? You are talking about the o
Yusuke Sato 2016/10/24 19:30:11 Done.
387 TEST(ArcExternalProtocolDialogTest,
388 TestGetActionWithTwoMarketFallbackUrlsOnePreferred) {
389 const GURL intent_url_with_fallback(
390 "intent://scan/#Intent;scheme=abc;package=com.google.abc;end");
391 const GURL fallback_url("market://details?id=com.google.abc");
392 const std::string play_store_package_name = "com.google.play.store";
393
394 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
395 handlers.push_back(
396 Create("Other Store app", "com.other.play.store", false, fallback_url));
397 handlers.push_back(
398 Create("Play Store", play_store_package_name, true, fallback_url));
399
400 const size_t no_selection = handlers.size();
401 std::pair<GURL, std::string> url_and_package;
402 EXPECT_EQ(GetActionResult::HANDLE_URL_IN_ARC,
403 GetActionForTesting(intent_url_with_fallback, handlers,
404 no_selection, &url_and_package));
405 EXPECT_EQ(fallback_url, url_and_package.first);
406 EXPECT_EQ(play_store_package_name, url_and_package.second);
407 }
408
409 // Tests the same, but make the first handler a selected one.
410 TEST(ArcExternalProtocolDialogTest,
411 TestGetActionWithTwoMarketFallbackUrlsOneSelected) {
412 const GURL intent_url_with_fallback(
413 "intent://scan/#Intent;scheme=abc;package=com.google.abc;end");
414 const GURL fallback_url("market://details?id=com.google.abc");
415 const std::string play_store_package_name = "com.google.play.store";
416
417 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
418 handlers.push_back(
419 Create("Other Store app", "com.other.play.store", false, fallback_url));
420 handlers.push_back(
421 Create("Play Store", play_store_package_name, false, fallback_url));
422
423 const size_t kSelection = 1; // Play Store
424 std::pair<GURL, std::string> url_and_package;
425 EXPECT_EQ(GetActionResult::HANDLE_URL_IN_ARC,
426 GetActionForTesting(intent_url_with_fallback, handlers, kSelection,
427 &url_and_package));
428 EXPECT_EQ(fallback_url, url_and_package.first);
429 EXPECT_EQ(play_store_package_name, url_and_package.second);
430 }
431
432 // Tests the case where geo: URL is returned as a fallback. This should never
433 // happen because intent_helper ignores such a fallback, but just in case.
434 // GetAction shouldn't crash at least.
435 TEST(ArcExternalProtocolDialogTest, TestGetActionWithGeoUrlAsFallback) {
436 // Note: geo: as a browser fallback is banned in the production code.
437 const GURL intent_url_with_fallback(
438 "intent://scan/#Intent;scheme=abc;package=com.google.abc;"
439 "S.browser_fallback_url=geo:37.7749,-122.4194;end");
440 const GURL geo_url("geo:37.7749,-122.4194");
441
442 mojo::Array<mojom::IntentHandlerInfoPtr> handlers;
443 handlers.push_back(Create("Chrome", kChromePackageName, true, geo_url));
444
445 const size_t no_selection = handlers.size();
446 std::pair<GURL, std::string> url_and_package;
447 // GetAction shouldn't return OPEN_URL_IN_CHROME because Chrome doesn't
448 // directly support geo:.
449 EXPECT_EQ(GetActionResult::HANDLE_URL_IN_ARC,
450 GetActionForTesting(intent_url_with_fallback, handlers,
451 no_selection, &url_and_package));
452 EXPECT_EQ(geo_url, url_and_package.first);
453 EXPECT_EQ(kChromePackageName, url_and_package.second);
454 }
455
456 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698