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

Side by Side Diff: chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkValidatorTest.java

Issue 1971773002: Upstream: Add WebAPK's client library. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add WebApkValidatorTest. Created 4 years, 7 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 package org.chromium.webapk.lib.client;
6
7 import static org.junit.Assert.assertFalse;
8 import static org.junit.Assert.assertTrue;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.when;
11
12 import android.content.Context;
13 import android.content.Intent;
14 import android.content.pm.ActivityInfo;
15 import android.content.pm.PackageInfo;
16 import android.content.pm.PackageManager;
17 import android.content.pm.PackageManager.NameNotFoundException;
18 import android.content.pm.ResolveInfo;
19 import android.content.pm.Signature;
20
21 import junit.framework.Assert;
22
23 import org.chromium.testing.local.LocalRobolectricTestRunner;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.robolectric.Robolectric;
28 import org.robolectric.annotation.Config;
29 import org.robolectric.res.builder.RobolectricPackageManager;
30
31 import java.net.URISyntaxException;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 /**
36 * Unit tests for {@link org.chromium.webapk.lib.client.WebApkValidator}.
37 */
38 @RunWith(LocalRobolectricTestRunner.class)
39 @Config(manifest = Config.NONE)
40 public class WebApkValidatorTest {
41 private static final String WEBAPK_PACKAGE_NAME = "org.chromium.webapk.foo";
42 private static final String INVALID_WEBAPK_PACKAGE_NAME = "invalid.org.chrom ium.webapk.foo";
43 private static final String URL_OF_WEBAPK = "https://www.foo.com";
44 private static final String URL_WITHOUT_WEBAPK = "https://www.other.com";
45 private static final byte[] EXPECTED_SIGNATURE = new byte[] {
46 48, -126, 3, -121, 48, -126, 2, 111, -96, 3, 2, 1, 2, 2, 4, 20, -104, -6 6, -126, 48, 13,
47 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 11, 5, 0, 48, 116, 49, 11, 48, 9 , 6, 3, 85, 4,
48 6, 19, 2, 67, 65, 49, 16, 48, 14, 6, 3, 85, 4, 8, 19, 7, 79, 110, 116, 9 7, 114, 105,
49 111, 49, 17, 48, 15, 6, 3, 85, 4, 7, 19, 8, 87, 97, 116, 101, 114, 108, 111, 111, 49,
50 17, 48, 15, 6, 3, 85, 4, 10, 19, 8, 67, 104, 114, 111, 109, 105, 117, 10 9, 49, 17, 48};
51
52 private Context mContext;
53 private PackageManager mPackageManager;
54 private PackageInfo mWebApkPackageInfo;
55 private PackageInfo mInvalidWebApkPackageInfo;
56
57 @Before
58 public void setUp() {
59 mContext = mock(Context.class);
60 mPackageManager = mock(PackageManager.class);
61 when(mContext.getPackageManager()).thenReturn(mPackageManager);
62 WebApkValidator.initWithBrowserHostSignature(EXPECTED_SIGNATURE);
63 }
64
65 @Test
Yaron 2016/05/12 22:11:39 nit: move @Test annotation to after comments
Xi Han 2016/05/13 15:42:55 Done.
66 /**
67 * Tests {@link WebApkValidator.queryWebApkPackage()} returns a WebAPK's pac kage name if it can
68 * handle the given URL.
69 */
70 public void testQueryWebAPKPackageReturnWebApkPackageNameWhichCanHandleTheUR L() {
Yaron 2016/05/12 22:11:39 please add test for non-browsable (should not retu
Yaron 2016/05/12 22:11:40 WebApk (throughout)
Xi Han 2016/05/13 15:42:55 Done.
71 Intent intent;
Yaron 2016/05/12 22:11:40 nit: move to L73 (no need to be separate declarati
Xi Han 2016/05/13 15:42:55 Done.
72 try {
73 intent = Intent.parseUri(URL_OF_WEBAPK, Intent.URI_INTENT_SCHEME);
74 intent.addCategory(Intent.CATEGORY_BROWSABLE);
75
76 ResolveInfo info = newResolveInfo(WEBAPK_PACKAGE_NAME);
77 RobolectricPackageManager packageManager =
78 (RobolectricPackageManager) Robolectric.application.getPacka geManager();
79 packageManager.addResolveInfoForIntent(intent, info);
80
81 assertTrue(WebApkValidator.queryWebAPKPackage(
Yaron 2016/05/12 22:11:40 assertEquals (throughout)
Xi Han 2016/05/13 15:42:55 Done.
82 Robolectric.application, URL_OF_WEBAPK).equals(WEBAPK_PACKAG E_NAME));
83 } catch (URISyntaxException e) {
84 Assert.fail("URI is invalid.");
85 }
86 }
87
88 @Test
89 /**
90 * Tests {@link WebApkValidator.queryWebApkPackage()} returns null if no Web APK can handle
91 * the given URL.
92 */
93 public void testQueryWebApkPackageReturnNullWhenNoWebApkCanHandleTheURL() {
94 Intent intent;
95 try {
96 intent = Intent.parseUri(URL_OF_WEBAPK, Intent.URI_INTENT_SCHEME);
97 intent.addCategory(Intent.CATEGORY_BROWSABLE);
98
99 ResolveInfo info = newResolveInfo(WEBAPK_PACKAGE_NAME);
100 RobolectricPackageManager packageManager =
101 (RobolectricPackageManager) Robolectric.application.getPacka geManager();
102 packageManager.addResolveInfoForIntent(intent, info);
103
104 assertTrue(WebApkValidator.queryWebAPKPackage(
Yaron 2016/05/12 22:11:40 assertNull (throughout)
Xi Han 2016/05/13 15:42:54 Done.
105 Robolectric.application, URL_WITHOUT_WEBAPK) == null);
106 } catch (URISyntaxException e) {
107 Assert.fail("URI is invalid.");
108 }
109 }
110
111 @Test
112 /**
113 * Tests {@link WebApkValidator.findWebAPKPackage()} returns a WebAPK's pack age name when there
114 * are ResolveInfos corresponds to a WebAPK.
115 */
116 public void testFindWebAPKPackageReturnValidWebApkPackageName() {
117 List<ResolveInfo> infos = new ArrayList<ResolveInfo>();
118 infos.add(newResolveInfo(WEBAPK_PACKAGE_NAME));
119 assertTrue(WebApkValidator.findWebAPKPackage(infos).equals(WEBAPK_PACKAG E_NAME));
120 }
121
122 @Test
123 /**
124 * Tests {@link WebApkValidator.findWebAPKPackage()} returns null when there isn't any
125 * ResolveInfos corresponds to a WebAPK.
126 */
127 public void testFindWebAPKPackageReturnNullWhenNoResolvesInfosCorrespondsToW ebApk() {
Yaron 2016/05/12 22:11:40 s/ResolvesInfosCorresponds/ResolveInfosCorrespondi
Xi Han 2016/05/13 15:42:55 Done.
128 List<ResolveInfo> infos = new ArrayList<ResolveInfo>();
129 infos.add(newResolveInfo("com.google.android"));
130 assertTrue(WebApkValidator.findWebAPKPackage(infos) == null);
131 }
132
133 @Test
134 /**
135 * Tests {@link WebApkValidator.IsValidWebApk} returns true if the WebAPK ha s the expected
136 * signature.
137 */
138 public void testIsValidWebApkReturnsTrueForValidWebApk() throws NameNotFound Exception {
Yaron 2016/05/12 22:11:39 please add test for multiple signatures with one m
Xi Han 2016/05/13 15:42:55 It is possible to update these two tests to cover
139 mWebApkPackageInfo = mock(PackageInfo.class);
140 when(mPackageManager.getPackageInfo(WEBAPK_PACKAGE_NAME, PackageManager. GET_SIGNATURES))
141 .thenReturn(mWebApkPackageInfo);
142 mWebApkPackageInfo.signatures = new Signature[] {new Signature(EXPECTED_ SIGNATURE)};
143
144 assertTrue(WebApkValidator.isValidWebApk(mContext, WEBAPK_PACKAGE_NAME)) ;
145 }
146
147 @Test
148 /**
149 * Tests {@link WebApkValidator.IsValidWebApk} returns true if the WebAPK do esn't have the
150 * expected signature.
151 */
152 public void testIsValidWebApkReturnsFalseForInValidWebApk() throws NameNotFo undException {
Yaron 2016/05/12 22:11:39 s/InValid/Invalid/
Xi Han 2016/05/13 15:42:55 Done.
153 mInvalidWebApkPackageInfo = mock(PackageInfo.class);
154 when(mPackageManager.getPackageInfo(INVALID_WEBAPK_PACKAGE_NAME,
155 PackageManager.GET_SIGNATURES)).thenReturn(mInvalidWebApkPackage Info);
156 mInvalidWebApkPackageInfo.signatures = new Signature[] {new Signature(ne w byte[] {})};
157
158 assertFalse(WebApkValidator.isValidWebApk(mContext, INVALID_WEBAPK_PACKA GE_NAME));
159 }
160
161 private static ResolveInfo newResolveInfo(String packageName) {
162 ActivityInfo activityInfo = new ActivityInfo();
163 activityInfo.packageName = packageName;
164 ResolveInfo resolveInfo = new ResolveInfo();
165 resolveInfo.activityInfo = activityInfo;
166 return resolveInfo;
167 }
168 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698