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

Side by Side Diff: content/public/android/junit/src/org/chromium/content/browser/installedapp/InstalledAppProviderTest.java

Issue 2706403014: Add Android implementation of navigator.getInstalledRelatedApps. (Closed)
Patch Set: Respond to review (importantly, getting the URL on every request, not caching). Created 3 years, 9 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 2017 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 package org.chromium.content.browser.installedapp;
6
7 import android.content.pm.ApplicationInfo;
8 import android.content.pm.PackageManager;
9 import android.content.pm.PackageManager.NameNotFoundException;
10 import android.content.res.AssetManager;
11 import android.content.res.Resources;
12 import android.os.Bundle;
13
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18
19 import org.robolectric.RuntimeEnvironment;
20 import org.robolectric.annotation.Config;
21 import org.robolectric.res.builder.DefaultPackageManager;
22
23 import org.chromium.base.test.util.Feature;
24 import org.chromium.installedapp.mojom.InstalledAppProvider;
25 import org.chromium.installedapp.mojom.RelatedApplication;
26 import org.chromium.testing.local.LocalRobolectricTestRunner;
27
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.util.HashMap;
31
32 /**
33 * Ensure that the InstalledAppProvider returns the correct apps.
34 */
35 @RunWith(LocalRobolectricTestRunner.class)
36 @Config(manifest = Config.NONE)
37 public class InstalledAppProviderTest {
38 private static final String ASSET_STATEMENTS_KEY =
39 InstalledAppProviderImpl.ASSET_STATEMENTS_KEY;
40 private static final String RELATION_HANDLE_ALL_URLS =
41 "delegate_permission/common.handle_all_urls";
42 private static final String NAMESPACE_WEB =
43 InstalledAppProviderImpl.ASSET_STATEMENT_NAMESPACE_WEB;
44 private static final String PLATFORM_ANDROID =
45 InstalledAppProviderImpl.RELATED_APP_PLATFORM_ANDROID;
46 private static final String PLATFORM_OTHER = "itunes";
47 // Note: Android package name and origin deliberately unrelated (there is no requirement that
48 // they be the same).
49 private static final String PACKAGE_NAME_1 = "com.app1.package";
50 private static final String PACKAGE_NAME_2 = "com.app2.package";
51 private static final String PACKAGE_NAME_3 = "com.app3.package";
52 private static final String URL_UNRELATED = "https://appstore.example.com/ap p1";
53 private static final String ORIGIN = "https://example.com:8000";
54 private static final String URL_ON_ORIGIN =
55 "https://example.com:8000/path/to/page.html?key=value#fragment";
56 private static final String ORIGIN_SYNTAX_ERROR = "https:{";
57 private static final String ORIGIN_MISSING_SCHEME = "path/only";
58 private static final String ORIGIN_MISSING_HOST = "file:///path/piece";
59 private static final String ORIGIN_MISSING_PORT = "http://example.com";
60 private static final String ORIGIN_DIFFERENT_SCHEME = "http://example.com:80 00";
61 private static final String ORIGIN_DIFFERENT_HOST = "https://example.org:800 0";
62 private static final String ORIGIN_DIFFERENT_PORT = "https://example.com:800 1";
63
64 private FakePackageManager mPackageManager;
65 private FakePageUrlDelegate mPageUrlDelegate;
66 private InstalledAppProviderImpl mInstalledAppProvider;
67
68 /**
69 * FakePackageManager allows for the "installation" of Android package names and setting up
70 * Resources for installed packages.
71 */
72 private static class FakePackageManager extends DefaultPackageManager {
73 private final HashMap<String, Bundle> mMetaDataMap;
74 private final HashMap<String, Resources> mResourceMap;
75
76 public FakePackageManager() {
77 super();
78 mMetaDataMap = new HashMap<String, Bundle>();
79 mResourceMap = new HashMap<String, Resources>();
80 }
81
82 @Override
83 public ApplicationInfo getApplicationInfo(String packageName, int flags)
84 throws NameNotFoundException {
85 if (packageName == null) throw new NullPointerException();
86
87 Bundle metaData = mMetaDataMap.get(packageName);
88 if (metaData == null) throw new NameNotFoundException(packageName);
89
90 // Create an application with this metadata (but only if |flags| all ows). Doing it this
91 // way (rather than simply storing the ApplicationInfo in a map) ens ures that the
92 // |flags| is set correctly.
93 ApplicationInfo appInfo = new ApplicationInfo();
94 appInfo.packageName = packageName;
95 if ((flags & PackageManager.GET_META_DATA) != 0) {
96 appInfo.metaData = metaData;
97 }
98 return appInfo;
99 }
100
101 @Override
102 public Resources getResourcesForApplication(ApplicationInfo app)
103 throws NameNotFoundException {
104 if (app == null) throw new NullPointerException();
105
106 Resources result = mResourceMap.get(app.packageName);
107 if (result == null) throw new NameNotFoundException(app.packageName) ;
108
109 return result;
110 }
111
112 public void setMetaDataAndResourcesForTest(
113 String packageName, Bundle metaData, Resources resources) {
114 mMetaDataMap.put(packageName, metaData);
115 mResourceMap.put(packageName, resources);
116 }
117 }
118
119 /**
120 * Fakes the Resources object, allowing lookup of a single String value.
121 *
122 * Note: The real Resources object defines a mapping to many values. This fa ke object only
123 * allows a single value in the mapping, and it must be a String (which is a ll that is required
124 * for these tests).
125 */
126 private static class FakeResources extends Resources {
127 private final int mId;
128 private final String mValue;
129
130 // Do not warn about deprecated call to Resources(); the documentation s ays code is not
131 // supposed to create its own Resources object, but we are using it to f ake out the
132 // Resources, and there is no other way to do that.
133 @SuppressWarnings("deprecation")
134 public FakeResources(int identifier, String value) {
135 super(new AssetManager(), null, null);
136 mId = identifier;
137 mValue = value;
138 }
139
140 @Override
141 public int getIdentifier(String name, String defType, String defPackage) {
142 if (name == null) throw new NullPointerException();
143
144 // There is *no guarantee* (in the Digital Asset Links spec) about w hat the string
145 // resource should be called ("asset_statements" is just an example) . Therefore,
146 // getIdentifier cannot be used to get the asset statements string. Always fail the
147 // lookup here, to ensure the implementation isn't relying on any pa rticular hard-coded
148 // string.
149 return 0;
150 }
151
152 @Override
153 public String getString(int id) {
154 if (id != mId) {
155 throw new Resources.NotFoundException("id 0x" + Integer.toHexStr ing(id));
156 }
157
158 return mValue;
159 }
160 }
161
162 private static final class FakePageUrlDelegate
163 implements InstalledAppProviderImpl.PageUrlDelegate {
164 private URI mPageUrl;
165
166 public FakePageUrlDelegate(String pageUrl) {
167 setPageUrl(pageUrl);
168 }
169
170 public void setPageUrl(String pageUrl) {
171 try {
172 mPageUrl = new URI(pageUrl);
173 } catch (URISyntaxException e) {
174 throw new AssertionError(e);
175 }
176 }
177
178 @Override
179 public URI getUrl() {
180 return mPageUrl;
181 }
182 }
183
184 /**
185 * Creates a metaData bundle with a single resource-id key.
186 */
187 private static Bundle createMetaData(String metaDataName, int metaDataResour ceId) {
188 Bundle metaData = new Bundle();
189 metaData.putInt(metaDataName, metaDataResourceId);
190 return metaData;
191 }
192
193 /**
194 * Sets a resource with a single key-value pair in an Android package's mani fest.
195 *
196 * The value is always a string.
197 */
198 private void setStringResource(String packageName, String key, String value) {
199 int identifier = 0x1234;
200 Bundle metaData = createMetaData(key, identifier);
201 FakeResources resources = new FakeResources(identifier, value);
202 mPackageManager.setMetaDataAndResourcesForTest(packageName, metaData, re sources);
203 }
204
205 /**
206 * Creates a valid Android asset statement string.
207 */
208 private String createAssetStatement(String platform, String relation, String url) {
209 return String.format(
210 "{\"relation\": [\"%s\"], \"target\": {\"namespace\": \"%s\", \" site\": \"%s\"}}",
211 relation, platform, url);
212 }
213
214 /**
215 * Sets an asset statement to an Android package's manifest (in the fake pac kage manager).
216 *
217 * Only one asset statement can be set for a given package (if this is calle d twice on the same
218 * package, overwrites the previous asset statement).
219 *
220 * This corresponds to a Statement List in the Digital Asset Links spec v1.
221 */
222 private void setAssetStatement(
223 String packageName, String platform, String relation, String url) {
224 String statements = "[" + createAssetStatement(platform, relation, url) + "]";
225 setStringResource(packageName, ASSET_STATEMENTS_KEY, statements);
226 }
227
228 /**
229 * Creates a RelatedApplication to put in the web app manifest.
230 */
231 private RelatedApplication createRelatedApplication(String platform, String id, String url) {
232 RelatedApplication application = new RelatedApplication();
233 application.platform = platform;
234 application.id = id;
235 application.url = url;
236 return application;
237 }
238
239 /**
240 * Calls filterInstalledApps with the given inputs, and tests that the expec ted result is
241 * returned.
242 */
243 private void verifyInstalledApps(RelatedApplication[] manifestRelatedApps,
244 RelatedApplication[] expectedInstalledRelatedApps) {
245 mInstalledAppProvider.filterInstalledApps(
246 manifestRelatedApps, new InstalledAppProvider.FilterInstalledApp sResponse() {
247 @Override
248 public void call(RelatedApplication[] installedRelatedApps) {
249 Assert.assertEquals(
250 expectedInstalledRelatedApps.length, installedRe latedApps.length);
251
252 for (int i = 0; i < installedRelatedApps.length; i++) {
253 Assert.assertEquals(
254 expectedInstalledRelatedApps[i], installedRe latedApps[i]);
255 }
256 }
257 });
258 }
259
260 @Before
261 public void setUp() {
262 mPackageManager = new FakePackageManager();
263 RuntimeEnvironment.setRobolectricPackageManager(mPackageManager);
264 mPageUrlDelegate = new FakePageUrlDelegate(URL_ON_ORIGIN);
265 mInstalledAppProvider =
266 new InstalledAppProviderImpl(mPageUrlDelegate, RuntimeEnvironmen t.application);
267 }
268
269 /**
270 * Origin of the page using the API is missing certain parts of the URI.
271 */
272 @Test
273 @Feature({"InstalledApp"})
274 public void testOriginMissingParts() {
275 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
276 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
277 setAssetStatement(PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
278 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
279
280 mPageUrlDelegate.setPageUrl(ORIGIN_MISSING_SCHEME);
281 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
282
283 mPageUrlDelegate.setPageUrl(ORIGIN_MISSING_HOST);
284 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
285 }
286
287 /**
288 * No related Android apps.
289 *
290 * An Android app relates to the web app, but not mutual.
291 */
292 @Test
293 @Feature({"InstalledApp"})
294 public void testNoRelatedApps() {
295 // The web manifest has no related apps.
296 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {};
297
298 // One Android app is installed named |PACKAGE_NAME_1|. It has a related web app with origin
299 // |ORIGIN|.
300 setAssetStatement(PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
301
302 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
303 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
304 }
305
306 /**
307 * One related Android app with no id (package name).
308 *
309 * An Android app relates to the web app, but not mutual.
310 */
311 @Test
312 @Feature({"InstalledApp"})
313 public void testOneRelatedAppNoId() {
314 RelatedApplication manifestRelatedApps[] =
315 new RelatedApplication[] {createRelatedApplication(PLATFORM_ANDR OID, null, null)};
316
317 setAssetStatement(PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
318
319 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
320 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
321 }
322
323 /**
324 * One related app (from a non-Android platform).
325 *
326 * An Android app with the same id relates to the web app. This should be ig nored since the
327 * manifest doesn't mention the Android app.
328 */
329 @Test
330 @Feature({"InstalledApp"})
331 public void testOneRelatedNonAndroidApp() {
332 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
333 createRelatedApplication(PLATFORM_OTHER, PACKAGE_NAME_1, null)};
334
335 setAssetStatement(PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
336
337 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
338 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
339 }
340
341 /**
342 * One related Android app; Android app is not installed.
343 *
344 * Another Android app relates to the web app, but not mutual.
345 */
346 @Test
347 @Feature({"InstalledApp"})
348 public void testOneRelatedAppNotInstalled() {
349 // The web manifest has a related Android app named |PACKAGE_NAME_1|.
350 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
351 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
352
353 // One Android app is installed named |PACKAGE_NAME_2|. It has a related web app with origin
354 // |ORIGIN|.
355 setAssetStatement(PACKAGE_NAME_2, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
356
357 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
358 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
359 }
360
361 /**
362 * Android app manifest has an asset_statements key, but the resource it lin ks to is missing.
363 */
364 @Test
365 @Feature({"InstalledApp"})
366 public void testOneRelatedAppBrokenAssetStatementsResource() {
367 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
368 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
369
370 Bundle metaData = createMetaData(ASSET_STATEMENTS_KEY, 0x1234);
371 String statements =
372 "[" + createAssetStatement(NAMESPACE_WEB, RELATION_HANDLE_ALL_UR LS, ORIGIN) + "]";
373 FakeResources resources = new FakeResources(0x4321, statements);
374 mPackageManager.setMetaDataAndResourcesForTest(PACKAGE_NAME_1, metaData, resources);
375 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
376 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
377 }
378
379 /**
380 * One related Android app; Android app is not mutually related (has no asse t_statements).
381 */
382 @Test
383 @Feature({"InstalledApp"})
384 public void testOneRelatedAppNoAssetStatements() {
385 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
386 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
387
388 setStringResource(PACKAGE_NAME_1, null, null);
389 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
390 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
391 }
392
393 /**
394 * One related Android app; Android app is related to other origins.
395 *
396 * Tests three cases:
397 * - The Android app is related to a web app with a different scheme.
398 * - The Android app is related to a web app with a different host.
399 * - The Android app is related to a web app with a different port.
400 */
401 @Test
402 @Feature({"InstalledApp"})
403 public void testOneRelatedAppRelatedToDifferentOrigins() {
404 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
405 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
406
407 setAssetStatement(
408 PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URLS, ORIGIN_ DIFFERENT_SCHEME);
409 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
410 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
411
412 setAssetStatement(
413 PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URLS, ORIGIN_ DIFFERENT_HOST);
414 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
415
416 setAssetStatement(
417 PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URLS, ORIGIN_ DIFFERENT_PORT);
418 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
419 }
420
421 /**
422 * One related Android app; Android app is installed and mutually related.
423 */
424 @Test
425 @Feature({"InstalledApp"})
426 public void testOneInstalledRelatedApp() {
427 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
428 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
429
430 setAssetStatement(PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
431
432 RelatedApplication[] expectedInstalledRelatedApps = manifestRelatedApps;
433 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
434 }
435
436 /**
437 * Change the page URL and ensure the app relates to the new URL, not the ol d one.
438 *
439 * This simulates navigating the page while keeping the same Mojo service op en.
440 */
441 @Test
442 @Feature({"InstalledApp"})
443 public void testDynamicallyChangingUrl() {
444 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
445 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
446
447 setAssetStatement(
448 PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URLS, ORIGIN_ DIFFERENT_SCHEME);
449
450 // Should be empty, since Android app does not relate to this page's ori gin.
451 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
452 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
453
454 // Simulate a navigation to a different origin.
455 mPageUrlDelegate.setPageUrl(ORIGIN_DIFFERENT_SCHEME);
456
457 // Now the result should include the Android app that relates to the new origin.
458 expectedInstalledRelatedApps = manifestRelatedApps;
459 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
460 }
461
462 /**
463 * One related Android app (installed and mutually related), with a non-null URL field.
464 */
465 @Test
466 @Feature({"InstalledApp"})
467 public void testInstalledRelatedAppWithUrl() {
468 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
469 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, URL_U NRELATED)};
470
471 setAssetStatement(PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
472
473 RelatedApplication[] expectedInstalledRelatedApps = manifestRelatedApps;
474 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
475 }
476
477 /**
478 * One related Android app; Android app is related to multiple origins.
479 */
480 @Test
481 @Feature({"InstalledApp"})
482 public void testMultipleAssetStatements() {
483 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
484 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
485
486 // Create an asset_statements field with multiple statements. The second one matches the web
487 // app.
488 String statements = "["
489 + createAssetStatement(
490 NAMESPACE_WEB, RELATION_HANDLE_ALL_URLS, ORIGIN_DIFFER ENT_HOST)
491 + ", " + createAssetStatement(NAMESPACE_WEB, RELATION_HANDLE_ALL _URLS, ORIGIN)
492 + "]";
493 setStringResource(PACKAGE_NAME_1, ASSET_STATEMENTS_KEY, statements);
494
495 RelatedApplication[] expectedInstalledRelatedApps = manifestRelatedApps;
496 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
497 }
498
499 /**
500 * A JSON syntax error in the Android app's asset statement.
501 */
502 @Test
503 @Feature({"InstalledApp"})
504 public void testAssetStatementSyntaxError() {
505 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
506 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
507
508 String statements = "[{\"target\" {}}]";
509 setStringResource(PACKAGE_NAME_1, ASSET_STATEMENTS_KEY, statements);
510
511 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
512 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
513 }
514
515 /**
516 * The Android app's asset statement is not an array.
517 */
518 @Test
519 @Feature({"InstalledApp"})
520 public void testAssetStatementNotArray() {
521 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
522 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
523
524 String statement = createAssetStatement(NAMESPACE_WEB, RELATION_HANDLE_A LL_URLS, ORIGIN);
525 setStringResource(PACKAGE_NAME_1, ASSET_STATEMENTS_KEY, statement);
526
527 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
528 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
529 }
530
531 /**
532 * The Android app's asset statement array contains non-objects.
533 */
534 @Test
535 @Feature({"InstalledApp"})
536 public void testAssetStatementArrayNoObjects() {
537 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
538 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
539
540 String statements = "["
541 + createAssetStatement(NAMESPACE_WEB, RELATION_HANDLE_ALL_URLS, ORIGIN) + ", 4]";
542 setStringResource(PACKAGE_NAME_1, ASSET_STATEMENTS_KEY, statements);
543
544 // Expect it to ignore the integer and successfully parse the valid obje ct.
545 RelatedApplication[] expectedInstalledRelatedApps = manifestRelatedApps;
546 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
547 }
548
549 /**
550 * Android app has no "relation" in the asset statement.
551 *
552 * Currently, the relation string (in the Android package's asset statement) is ignored, so the
553 * app is still returned as "installed".
554 */
555 @Test
556 @Feature({"InstalledApp"})
557 public void testAssetStatementNoRelation() {
558 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
559 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
560
561 String statements = String.format(
562 "[{\"target\": {\"namespace\": \"%s\", \"site\": \"%s\"}}]", NAM ESPACE_WEB, ORIGIN);
563 setStringResource(PACKAGE_NAME_1, ASSET_STATEMENTS_KEY, statements);
564
565 // TODO(mgiuca): [Spec issue] Should we require a specific relation stri ng, rather than any
566 // or no relation?
567 RelatedApplication[] expectedInstalledRelatedApps = manifestRelatedApps;
568 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
569 }
570
571 /**
572 * Android app is related with a non-standard relation.
573 *
574 * Currently, the relation string (in the Android package's asset statement) is ignored, so any
575 * will do. Is this desirable, or do we want to require a specific relation string?
576 */
577 @Test
578 @Feature({"InstalledApp"})
579 public void testAssetStatementNonStandardRelation() {
580 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
581 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
582
583 setAssetStatement(PACKAGE_NAME_1, NAMESPACE_WEB, "nonstandard/relation", ORIGIN);
584
585 // TODO(mgiuca): [Spec issue] Should we require a specific relation stri ng, rather than any
586 // or no relation?
587 RelatedApplication[] expectedInstalledRelatedApps = manifestRelatedApps;
588 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
589 }
590
591 /**
592 * Android app has no "target" in the asset statement.
593 */
594 @Test
595 @Feature({"InstalledApp"})
596 public void testAssetStatementNoTarget() {
597 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
598 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
599
600 String statements = String.format("[{\"relation\": [\"%s\"]}]", RELATION _HANDLE_ALL_URLS);
601 setStringResource(PACKAGE_NAME_1, ASSET_STATEMENTS_KEY, statements);
602
603 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
604 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
605 }
606
607 /**
608 * Android app has no "namespace" in the asset statement.
609 */
610 @Test
611 @Feature({"InstalledApp"})
612 public void testAssetStatementNoNamespace() {
613 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
614 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
615
616 String statements =
617 String.format("[{\"relation\": [\"%s\"], \"target\": {\"site\": \"%s\"}}]",
618 RELATION_HANDLE_ALL_URLS, ORIGIN);
619 setStringResource(PACKAGE_NAME_1, ASSET_STATEMENTS_KEY, statements);
620
621 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
622 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
623 }
624
625 /**
626 * Android app is related, but not to the web namespace.
627 */
628 @Test
629 @Feature({"InstalledApp"})
630 public void testNonWebAssetStatement() {
631 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
632 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
633
634 setAssetStatement(PACKAGE_NAME_1, "play", RELATION_HANDLE_ALL_URLS, ORIG IN);
635
636 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
637 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
638 }
639
640 /**
641 * Android app has no "site" in the asset statement.
642 */
643 @Test
644 @Feature({"InstalledApp"})
645 public void testAssetStatementNoSite() {
646 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
647 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
648
649 String statements =
650 String.format("[{\"relation\": [\"%s\"], \"target\": {\"namespac e\": \"%s\"}}]",
651 RELATION_HANDLE_ALL_URLS, NAMESPACE_WEB);
652 setStringResource(PACKAGE_NAME_1, ASSET_STATEMENTS_KEY, statements);
653
654 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
655 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
656 }
657
658 /**
659 * Android app has a syntax error in the "site" field of the asset statement .
660 */
661 @Test
662 @Feature({"InstalledApp"})
663 public void testAssetStatementSiteSyntaxError() {
664 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
665 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
666
667 setAssetStatement(
668 PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URLS, ORIGIN_ SYNTAX_ERROR);
669
670 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
671 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
672 }
673
674 /**
675 * Android app has a "site" field missing certain parts of the URI (scheme, host, port).
676 */
677 @Test
678 @Feature({"InstalledApp"})
679 public void testAssetStatementSiteMissingParts() {
680 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
681 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
682
683 setAssetStatement(
684 PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URLS, ORIGIN_ MISSING_SCHEME);
685 RelatedApplication[] expectedInstalledRelatedApps = new RelatedApplicati on[] {};
686 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
687
688 setAssetStatement(
689 PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URLS, ORIGIN_ MISSING_HOST);
690 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
691
692 setAssetStatement(
693 PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URLS, ORIGIN_ MISSING_PORT);
694 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
695 }
696
697 /**
698 * One related Android app; Android app is related with a path part in the " site" field.
699 *
700 * The path part shouldn't really be there (according to the Digital Asset L inks spec), but if
701 * it is, we are lenient and just ignore it (matching only the origin).
702 */
703 @Test
704 @Feature({"InstalledApp"})
705 public void testAssetStatementSiteHasPath() {
706 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
707 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
708
709 String site = ORIGIN + "/path";
710 setAssetStatement(PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, site);
711
712 RelatedApplication[] expectedInstalledRelatedApps = manifestRelatedApps;
713 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
714 }
715
716 /**
717 * One related Android app; Android app is installed and mutually related.
718 *
719 * Another Android app relates to the web app, but not mutual.
720 */
721 @Test
722 @Feature({"InstalledApp"})
723 public void testExtraInstalledApp() {
724 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
725 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
726
727 setAssetStatement(PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
728 setAssetStatement(PACKAGE_NAME_2, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
729
730 RelatedApplication[] expectedInstalledRelatedApps = manifestRelatedApps;
731 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
732 }
733
734 /**
735 * Two related Android apps; Android apps both installed and mutually relate d.
736 *
737 * Web app also related to an app with the same name on another platform, an d another Android
738 * app which is not installed.
739 */
740 @Test
741 @Feature({"InstalledApp"})
742 public void testMultipleInstalledRelatedApps() {
743 RelatedApplication[] manifestRelatedApps = new RelatedApplication[] {
744 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) ,
745 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_2, null) ,
746 createRelatedApplication(PLATFORM_OTHER, PACKAGE_NAME_2, null),
747 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_3, null) };
748
749 setAssetStatement(PACKAGE_NAME_2, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
750 setAssetStatement(PACKAGE_NAME_3, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
751
752 RelatedApplication[] expectedInstalledRelatedApps =
753 new RelatedApplication[] {manifestRelatedApps[1], manifestRelate dApps[3]};
754 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
755 }
756 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698