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

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

Powered by Google App Engine
This is Rietveld 408576698