OLD | NEW |
(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.chrome.browser.payments; |
| 6 |
| 7 import android.content.Intent; |
| 8 import android.content.pm.ActivityInfo; |
| 9 import android.content.pm.PackageInfo; |
| 10 import android.content.pm.ResolveInfo; |
| 11 import android.content.pm.ServiceInfo; |
| 12 import android.content.pm.Signature; |
| 13 import android.net.Uri; |
| 14 |
| 15 import org.junit.Test; |
| 16 import org.junit.runner.RunWith; |
| 17 import org.mockito.ArgumentMatcher; |
| 18 import org.mockito.ArgumentMatchers; |
| 19 import org.mockito.Mockito; |
| 20 import org.robolectric.RobolectricTestRunner; |
| 21 import org.robolectric.annotation.Config; |
| 22 |
| 23 import org.chromium.chrome.browser.payments.PaymentAppFactory.PaymentAppCreatedC
allback; |
| 24 import org.chromium.components.payments.PaymentManifestDownloader; |
| 25 import org.chromium.components.payments.PaymentManifestParser; |
| 26 import org.chromium.content_public.browser.WebContents; |
| 27 import org.chromium.payments.mojom.PaymentManifestSection; |
| 28 |
| 29 import java.net.URI; |
| 30 import java.util.ArrayList; |
| 31 import java.util.HashSet; |
| 32 import java.util.List; |
| 33 import java.util.Set; |
| 34 |
| 35 /** Tests for the native Android payment app finder. */ |
| 36 @RunWith(RobolectricTestRunner.class) |
| 37 @Config(sdk = 21, manifest = Config.NONE) |
| 38 public class AndroidPaymentAppFinderTest { |
| 39 public AndroidPaymentAppFinderTest() {} |
| 40 |
| 41 @Test |
| 42 public void testNoValidPaymentMethodNames() { |
| 43 Set<String> methodNames = new HashSet<>(); |
| 44 methodNames.add("unknown-payment-method-name"); |
| 45 methodNames.add("http://not.secure.payment.method.name.com"); |
| 46 methodNames.add("https://"); // Invalid URI. |
| 47 PaymentAppCreatedCallback callback = Mockito.mock(PaymentAppCreatedCallb
ack.class); |
| 48 |
| 49 AndroidPaymentAppFinder.find(Mockito.mock(WebContents.class), methodName
s, false, |
| 50 Mockito.mock(PaymentManifestDownloader.class), |
| 51 Mockito.mock(PaymentManifestParser.class), |
| 52 Mockito.mock(PackageManagerDelegate.class), callback); |
| 53 |
| 54 Mockito.verify(callback, Mockito.never()) |
| 55 .onPaymentAppCreated(Mockito.any(PaymentApp.class)); |
| 56 Mockito.verify(callback).onAllPaymentAppsCreated(); |
| 57 } |
| 58 |
| 59 @Test |
| 60 public void testQueryBasicCardsWithoutApps() { |
| 61 PackageManagerDelegate packageManagerDelegate = Mockito.mock(PackageMana
gerDelegate.class); |
| 62 Mockito.when(packageManagerDelegate.getActivitiesThatCanRespondToIntent( |
| 63 new Intent(AndroidPaymentAppFinder.ACTION_PAY_BASIC
_CARD))) |
| 64 .thenReturn(new ArrayList<ResolveInfo>()); |
| 65 Set<String> methodNames = new HashSet<>(); |
| 66 methodNames.add(AndroidPaymentAppFinder.BASIC_CARD_PAYMENT_METHOD); |
| 67 PaymentAppCreatedCallback callback = Mockito.mock(PaymentAppCreatedCallb
ack.class); |
| 68 |
| 69 AndroidPaymentAppFinder.find(Mockito.mock(WebContents.class), methodName
s, false, |
| 70 Mockito.mock(PaymentManifestDownloader.class), |
| 71 Mockito.mock(PaymentManifestParser.class), packageManagerDelegat
e, callback); |
| 72 |
| 73 Mockito.verify(callback, Mockito.never()) |
| 74 .onPaymentAppCreated(Mockito.any(PaymentApp.class)); |
| 75 Mockito.verify(callback).onAllPaymentAppsCreated(); |
| 76 } |
| 77 |
| 78 @Test |
| 79 public void testQueryBasicCardsWithTwoApps() { |
| 80 List<ResolveInfo> activities = new ArrayList<>(); |
| 81 ResolveInfo alicePay = new ResolveInfo(); |
| 82 alicePay.activityInfo = new ActivityInfo(); |
| 83 alicePay.activityInfo.packageName = "com.alicepay.app"; |
| 84 alicePay.activityInfo.name = "com.alicepay.app.WebPaymentActivity"; |
| 85 activities.add(alicePay); |
| 86 ResolveInfo bobPay = new ResolveInfo(); |
| 87 bobPay.activityInfo = new ActivityInfo(); |
| 88 bobPay.activityInfo.packageName = "com.bobpay.app"; |
| 89 bobPay.activityInfo.name = "com.bobpay.app.WebPaymentActivity"; |
| 90 activities.add(bobPay); |
| 91 PackageManagerDelegate packageManagerDelegate = Mockito.mock(PackageMana
gerDelegate.class); |
| 92 Mockito.when(packageManagerDelegate.getAppLabel(Mockito.any(ResolveInfo.
class))) |
| 93 .thenReturn("A non-empty label"); |
| 94 Mockito.when(packageManagerDelegate.getActivitiesThatCanRespondToIntent( |
| 95 new Intent(AndroidPaymentAppFinder.ACTION_PAY_BASIC
_CARD))) |
| 96 .thenReturn(activities); |
| 97 Mockito.when(packageManagerDelegate.getServicesThatCanRespondToIntent( |
| 98 new Intent(AndroidPaymentAppFinder.ACTION_IS_READY_
TO_PAY))) |
| 99 .thenReturn(new ArrayList<ResolveInfo>()); |
| 100 Set<String> methodNames = new HashSet<>(); |
| 101 methodNames.add(AndroidPaymentAppFinder.BASIC_CARD_PAYMENT_METHOD); |
| 102 PaymentAppCreatedCallback callback = Mockito.mock(PaymentAppCreatedCallb
ack.class); |
| 103 |
| 104 AndroidPaymentAppFinder.find(Mockito.mock(WebContents.class), methodName
s, false, |
| 105 Mockito.mock(PaymentManifestDownloader.class), |
| 106 Mockito.mock(PaymentManifestParser.class), packageManagerDelegat
e, callback); |
| 107 |
| 108 Mockito.verify(callback).onPaymentAppCreated( |
| 109 ArgumentMatchers.argThat(Matches.paymentAppIdentifier("com.alice
pay.app"))); |
| 110 Mockito.verify(callback).onPaymentAppCreated( |
| 111 ArgumentMatchers.argThat(Matches.paymentAppIdentifier("com.bobpa
y.app"))); |
| 112 Mockito.verify(callback).onAllPaymentAppsCreated(); |
| 113 } |
| 114 |
| 115 @Test |
| 116 public void testQueryBobPayWithoutApps() { |
| 117 Intent bobPayIntent = new Intent("org.chromium.intent.action.PAY"); |
| 118 bobPayIntent.setData(Uri.parse("https://bobpay.com")); |
| 119 PackageManagerDelegate packageManagerDelegate = Mockito.mock(PackageMana
gerDelegate.class); |
| 120 Mockito.when(packageManagerDelegate.getActivitiesThatCanRespondToIntent(
bobPayIntent)) |
| 121 .thenReturn(new ArrayList<ResolveInfo>()); |
| 122 Set<String> methodNames = new HashSet<>(); |
| 123 methodNames.add("https://bobpay.com"); |
| 124 PaymentAppCreatedCallback callback = Mockito.mock(PaymentAppCreatedCallb
ack.class); |
| 125 |
| 126 AndroidPaymentAppFinder.find(Mockito.mock(WebContents.class), methodName
s, false, |
| 127 Mockito.mock(PaymentManifestDownloader.class), |
| 128 Mockito.mock(PaymentManifestParser.class), packageManagerDelegat
e, callback); |
| 129 |
| 130 Mockito.verify(callback, Mockito.never()) |
| 131 .onPaymentAppCreated(Mockito.any(PaymentApp.class)); |
| 132 Mockito.verify(callback).onAllPaymentAppsCreated(); |
| 133 } |
| 134 |
| 135 @Test |
| 136 public void testQueryBobPayWithOneAppThatHasIsReadyToPayService() { |
| 137 List<ResolveInfo> activities = new ArrayList<>(); |
| 138 ResolveInfo bobPay = new ResolveInfo(); |
| 139 bobPay.activityInfo = new ActivityInfo(); |
| 140 bobPay.activityInfo.packageName = "com.bobpay.app"; |
| 141 bobPay.activityInfo.name = "com.bobpay.app.WebPaymentActivity"; |
| 142 activities.add(bobPay); |
| 143 Intent bobPayIntent = new Intent("org.chromium.intent.action.PAY"); |
| 144 bobPayIntent.setData(Uri.parse("https://bobpay.com")); |
| 145 PackageManagerDelegate packageManagerDelegate = Mockito.mock(PackageMana
gerDelegate.class); |
| 146 Mockito.when(packageManagerDelegate.getAppLabel(Mockito.any(ResolveInfo.
class))) |
| 147 .thenReturn("A non-empty label"); |
| 148 Mockito.when(packageManagerDelegate.getActivitiesThatCanRespondToIntent(
bobPayIntent)) |
| 149 .thenReturn(activities); |
| 150 |
| 151 List<ResolveInfo> services = new ArrayList<>(); |
| 152 ResolveInfo isBobPayReadyToPay = new ResolveInfo(); |
| 153 isBobPayReadyToPay.serviceInfo = new ServiceInfo(); |
| 154 isBobPayReadyToPay.serviceInfo.packageName = "com.bobpay.app"; |
| 155 isBobPayReadyToPay.serviceInfo.name = "com.bobpay.app.IsReadyToWebPay"; |
| 156 services.add(isBobPayReadyToPay); |
| 157 Intent isReadyToPayIntent = new Intent(AndroidPaymentAppFinder.ACTION_IS
_READY_TO_PAY); |
| 158 Mockito.when(packageManagerDelegate.getServicesThatCanRespondToIntent(is
ReadyToPayIntent)) |
| 159 .thenReturn(services); |
| 160 |
| 161 PackageInfo bobPayPackageInfo = new PackageInfo(); |
| 162 bobPayPackageInfo.versionCode = 10; |
| 163 bobPayPackageInfo.signatures = new Signature[1]; |
| 164 bobPayPackageInfo.signatures[0] = new Signature("01020304050607080900"); |
| 165 Mockito.when(packageManagerDelegate.getPackageInfoWithSignatures("com.bo
bpay.app")) |
| 166 .thenReturn(bobPayPackageInfo); |
| 167 |
| 168 PaymentManifestDownloader downloader = new PaymentManifestDownloader(nul
l) { |
| 169 @Override |
| 170 public void download(URI uri, ManifestDownloadCallback callback) { |
| 171 callback.onManifestDownloadSuccess("some content here"); |
| 172 } |
| 173 }; |
| 174 |
| 175 PaymentManifestParser parser = new PaymentManifestParser() { |
| 176 @Override |
| 177 public void parse(String content, ManifestParseCallback callback) { |
| 178 PaymentManifestSection[] manifest = new PaymentManifestSection[1
]; |
| 179 manifest[0] = new PaymentManifestSection(); |
| 180 manifest[0].packageName = "com.bobpay.app"; |
| 181 manifest[0].version = 10; |
| 182 // SHA256("01020304050607080900"): |
| 183 manifest[0].sha256CertFingerprints = new byte[][] {{(byte) 0x9A,
(byte) 0x89, |
| 184 (byte) 0xC6, (byte) 0x8C, (byte) 0x4C, (byte) 0x5E, (byt
e) 0x28, |
| 185 (byte) 0xB8, (byte) 0xC4, (byte) 0xA5, (byte) 0x56, (byt
e) 0x76, |
| 186 (byte) 0x73, (byte) 0xD4, (byte) 0x62, (byte) 0xFF, (byt
e) 0xF5, |
| 187 (byte) 0x15, (byte) 0xDB, (byte) 0x46, (byte) 0x11, (byt
e) 0x6F, |
| 188 (byte) 0x99, (byte) 0x00, (byte) 0x62, (byte) 0x4D, (byt
e) 0x09, |
| 189 (byte) 0xC4, (byte) 0x74, (byte) 0xF5, (byte) 0x93, (byt
e) 0xFB}}; |
| 190 callback.onManifestParseSuccess(manifest); |
| 191 } |
| 192 |
| 193 @Override |
| 194 public void startUtilityProcess() {} |
| 195 |
| 196 @Override |
| 197 public void stopUtilityProcess() {} |
| 198 }; |
| 199 |
| 200 Set<String> methodNames = new HashSet<>(); |
| 201 methodNames.add("https://bobpay.com"); |
| 202 PaymentAppCreatedCallback callback = Mockito.mock(PaymentAppCreatedCallb
ack.class); |
| 203 |
| 204 AndroidPaymentAppFinder.find(Mockito.mock(WebContents.class), methodName
s, false, |
| 205 downloader, parser, packageManagerDelegate, callback); |
| 206 |
| 207 Mockito.verify(callback).onPaymentAppCreated( |
| 208 ArgumentMatchers.argThat(Matches.paymentAppIdentifier("com.bobpa
y.app"))); |
| 209 Mockito.verify(callback).onAllPaymentAppsCreated(); |
| 210 } |
| 211 |
| 212 private static final class Matches implements ArgumentMatcher<PaymentApp> { |
| 213 private final String mExpectedAppIdentifier; |
| 214 |
| 215 private Matches(String expectedAppIdentifier) { |
| 216 mExpectedAppIdentifier = expectedAppIdentifier; |
| 217 } |
| 218 |
| 219 /** |
| 220 * Builds a matcher based on payment app identifier. |
| 221 * |
| 222 * @param expectedAppIdentifier The expected app identifier to match. |
| 223 * @return A matcher to use in a mock expectation. |
| 224 */ |
| 225 public static ArgumentMatcher<PaymentApp> paymentAppIdentifier( |
| 226 String expectedAppIdentifier) { |
| 227 return new Matches(expectedAppIdentifier); |
| 228 } |
| 229 |
| 230 @Override |
| 231 public boolean matches(PaymentApp app) { |
| 232 return app.getAppIdentifier().equals(mExpectedAppIdentifier); |
| 233 } |
| 234 } |
| 235 } |
OLD | NEW |