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

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

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

Powered by Google App Engine
This is Rietveld 408576698