diff --git a/Braintree/src/androidTest/java/com/braintreepayments/api/VenmoTest.java b/Braintree/src/androidTest/java/com/braintreepayments/api/VenmoTest.java deleted file mode 100644 index 2164a28135..0000000000 --- a/Braintree/src/androidTest/java/com/braintreepayments/api/VenmoTest.java +++ /dev/null @@ -1,379 +0,0 @@ -package com.braintreepayments.api; - -import android.app.Activity; -import android.content.ComponentName; -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.support.test.runner.AndroidJUnit4; -import android.test.mock.MockContext; -import android.test.suitebuilder.annotation.SmallTest; - -import com.braintreepayments.api.exceptions.AppSwitchNotAvailableException; -import com.braintreepayments.api.exceptions.InvalidArgumentException; -import com.braintreepayments.api.interfaces.BraintreeErrorListener; -import com.braintreepayments.api.interfaces.PaymentMethodNonceCreatedListener; -import com.braintreepayments.api.internal.BraintreeHttpClient; -import com.braintreepayments.api.models.AnalyticsConfiguration; -import com.braintreepayments.api.models.Configuration; -import com.braintreepayments.api.models.PaymentMethodNonce; -import com.braintreepayments.api.models.VenmoAccountNonce; -import com.braintreepayments.api.models.VenmoConfiguration; -import com.braintreepayments.api.test.TestActivity; -import com.braintreepayments.testutils.BraintreeActivityTestRule; -import com.braintreepayments.testutils.MockContextForVenmo; - -import org.json.JSONException; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.InOrder; - -import java.util.concurrent.CountDownLatch; - -import static com.braintreepayments.api.BraintreeFragmentTestUtils.getMockFragment; -import static com.braintreepayments.api.internal.SignatureVerificationTestUtils.disableSignatureVerification; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.inOrder; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class VenmoTest { - - @Rule - public final BraintreeActivityTestRule mActivityTestRule = - new BraintreeActivityTestRule<>(TestActivity.class); - - private Activity mActivity; - - @Before - public void setUp() { - mActivity = mActivityTestRule.getActivity(); - } - - @Test(timeout = 1000) - public void packageIsCorrect() { - assertEquals("com.venmo", Venmo.PACKAGE_NAME); - } - - @Test(timeout = 1000) - public void appSwitchActivityIsCorrect() { - assertEquals("controller.SetupMerchantActivity", Venmo.APP_SWITCH_ACTIVITY); - } - - @Test(timeout = 1000) - public void certificateSubjectIsCorrect() { - assertEquals("CN=Andrew Kortina,OU=Engineering,O=Venmo,L=Philadelphia,ST=PA,C=US", - Venmo.CERTIFICATE_SUBJECT); - } - - @Test(timeout = 1000) - public void certificateIssuerIsCorrect() { - assertEquals("CN=Andrew Kortina,OU=Engineering,O=Venmo,L=Philadelphia,ST=PA,C=US", - Venmo.CERTIFICATE_ISSUER); - } - - @Test(timeout = 1000) - public void publicKeyHashCodeIsCorrect() { - assertEquals(-129711843, Venmo.PUBLIC_KEY_HASH_CODE); - } - - @Test(timeout = 1000) - public void containsCorrectVenmoExtras() throws JSONException { - Configuration configuration = mock(Configuration.class); - VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); - when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); - when(configuration.getMerchantId()).thenReturn("merchant_id"); - when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); - when(configuration.getEnvironment()).thenReturn("environment"); - - Intent intent = Venmo.getLaunchIntent(configuration); - - assertEquals(new ComponentName("com.venmo", "com.venmo.controller.SetupMerchantActivity"), - intent.getComponent()); - assertEquals("merchant_id", intent.getStringExtra(Venmo.EXTRA_MERCHANT_ID)); - assertEquals("access-token", intent.getStringExtra(Venmo.EXTRA_ACCESS_TOKEN)); - assertEquals(BuildConfig.VERSION_NAME, intent.getStringExtra(Venmo.EXTRA_SDK_VERSION)); - assertEquals("environment", intent.getStringExtra(Venmo.EXTRA_ENVIRONMENT)); - } - - @Test(timeout = 1000) - public void authorizeAccount_failsAndSendsExceptionWhenControlPanelNotEnabled() - throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - Configuration configuration = getConfiguration(); - VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); - BraintreeFragment fragment = getMockFragment(mActivity, configuration); - fragment.addListener(new BraintreeErrorListener() { - @Override - public void onError(Exception error) { - assertEquals("Venmo is not enabled in the control panel.", error.getMessage()); - latch.countDown(); - } - }); - Context mockContextForVenmo = new MockContextForVenmo() - .venmoInstalled() - .whitelistValue("true") - .build(); - when(fragment.getApplicationContext()).thenReturn(mockContextForVenmo); - when(venmoConfiguration.isVenmoWhitelisted(any(ContentResolver.class))).thenReturn(true); - when(configuration.getMerchantId()).thenReturn("merchant_id"); - when(configuration.getEnvironment()).thenReturn("environment"); - when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); - doNothing().when(fragment).sendAnalyticsEvent(anyString()); - doNothing().when(fragment).startActivityForResult(any(Intent.class), anyInt()); - disableSignatureVerification(); - - Venmo.authorizeAccount(fragment, configuration); - - latch.await(); - } - - @Test(timeout = 1000) - public void authorizeAccount_failsAndSendsExceptionWhenNotInstalled() - throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - Configuration configuration = getConfiguration(); - VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); - BraintreeFragment fragment = getMockFragment(mActivity, configuration); - fragment.addListener(new BraintreeErrorListener() { - @Override - public void onError(Exception error) { - assertEquals("Venmo is not installed.", error.getMessage()); - latch.countDown(); - } - }); - Context mockContextForVenmo = new MockContextForVenmo() - .whitelistValue("true") - .build(); - when(fragment.getApplicationContext()).thenReturn(mockContextForVenmo); - when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); - when(venmoConfiguration.isAccessTokenValid()).thenReturn(true); - when(venmoConfiguration.isVenmoWhitelisted(any(ContentResolver.class))).thenReturn(true); - when(configuration.getMerchantId()).thenReturn("merchant_id"); - when(configuration.getEnvironment()).thenReturn("environment"); - when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); - doNothing().when(fragment).sendAnalyticsEvent(anyString()); - doNothing().when(fragment).startActivityForResult(any(Intent.class), anyInt()); - disableSignatureVerification(); - - Venmo.authorizeAccount(fragment, configuration); - - latch.await(); - } - - @Test(timeout = 1000) - public void authorizeAccount_failsAndSendsExceptionWhenNotWhitelisted() - throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - Configuration configuration = getConfiguration(); - VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); - BraintreeFragment fragment = getMockFragment(mActivity, configuration); - fragment.addListener(new BraintreeErrorListener() { - @Override - public void onError(Exception error) { - assertEquals("Venmo is not whitelisted.", error.getMessage()); - latch.countDown(); - } - }); - Context mockContextForVenmo = new MockContextForVenmo() - .venmoInstalled() - .build(); - when(fragment.getApplicationContext()).thenReturn(mockContextForVenmo); - when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); - when(venmoConfiguration.isAccessTokenValid()).thenReturn(true); - when(configuration.getMerchantId()).thenReturn("merchant_id"); - when(configuration.getEnvironment()).thenReturn("environment"); - when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); - doNothing().when(fragment).sendAnalyticsEvent(anyString()); - doNothing().when(fragment).startActivityForResult(any(Intent.class), anyInt()); - disableSignatureVerification(); - - Venmo.authorizeAccount(fragment, configuration); - - latch.await(); - } - - @Test(timeout = 1000) - public void performAppSwitch_appSwitchesWithVenmoLaunchIntent() { - ArgumentCaptor launchIntentCaptor = ArgumentCaptor.forClass(Intent.class); - Configuration configuration = getConfiguration(); - VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); - BraintreeFragment fragment = getMockFragment(mActivity, configuration); - Context mockContextForVenmo = new MockContextForVenmo() - .venmoInstalled() - .whitelistValue("true") - .build(); - when(fragment.getApplicationContext()).thenReturn(mockContextForVenmo); - when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); - when(venmoConfiguration.isAccessTokenValid()).thenReturn(true); - when(venmoConfiguration.isVenmoWhitelisted(any(ContentResolver.class))).thenReturn(true); - when(configuration.getMerchantId()).thenReturn("merchant_id"); - when(configuration.getEnvironment()).thenReturn("environment"); - when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); - doNothing().when(fragment).sendAnalyticsEvent(anyString()); - doNothing().when(fragment).startActivityForResult(any(Intent.class), anyInt()); - disableSignatureVerification(); - - Venmo.authorizeAccount(fragment, configuration); - - verify(fragment).startActivityForResult(launchIntentCaptor.capture(), - eq(Venmo.VENMO_REQUEST_CODE)); - Intent launchIntent = launchIntentCaptor.getValue(); - assertEquals("com.venmo/com.venmo.controller.SetupMerchantActivity", - launchIntent.getComponent().flattenToString()); - Bundle extras = launchIntent.getExtras(); - assertEquals("merchant_id", extras.getString(Venmo.EXTRA_MERCHANT_ID)); - assertEquals("access-token", extras.getString(Venmo.EXTRA_ACCESS_TOKEN)); - assertEquals(BuildConfig.VERSION_NAME, extras.getString(Venmo.EXTRA_SDK_VERSION)); - assertEquals("environment", extras.getString(Venmo.EXTRA_ENVIRONMENT)); - } - - @Test(timeout = 1000) - public void performAppSwitch_sendsAnalyticsEvent() { - Configuration configuration = getConfiguration(); - VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); - BraintreeFragment fragment = getMockFragment(mActivity, configuration); - - when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); - when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); - when(fragment.getHttpClient()).thenReturn(mock(BraintreeHttpClient.class)); - doNothing().when(fragment).startActivityForResult(any(Intent.class), anyInt()); - - Venmo.authorizeAccount(fragment); - - verify(fragment).sendAnalyticsEvent("pay-with-venmo.selected"); - } - - @Test(timeout = 1000) - public void performAppSwitch_sendsAnalyticsEventWhenStarted() { - Configuration configuration = getConfiguration(); - VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); - MockContext mockContext = new MockContextForVenmo() - .whitelistValue("true") - .venmoInstalled() - .build(); - BraintreeFragment fragment = getMockFragment(mActivity, configuration); - - when(venmoConfiguration.isAccessTokenValid()).thenReturn(true); - when(venmoConfiguration.isVenmoWhitelisted(any(ContentResolver.class))).thenReturn(true); - when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); - doNothing().when(fragment).sendAnalyticsEvent(anyString()); - doNothing().when(fragment).startActivityForResult(any(Intent.class), anyInt()); - when(fragment.getApplicationContext()).thenReturn(mockContext); - disableSignatureVerification(); - - Venmo.authorizeAccount(fragment, configuration); - - verify(fragment).sendAnalyticsEvent("pay-with-venmo.selected"); - verify(fragment).sendAnalyticsEvent("pay-with-venmo.app-switch.started"); - } - - @Test(timeout = 1000) - public void performAppSwitch_sendsAnalyticsEventWhenUnavailableAndPostException() { - ArgumentCaptor argumentCaptor = - ArgumentCaptor.forClass(AppSwitchNotAvailableException.class); - Configuration configuration = getConfiguration(); - VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); - BraintreeFragment fragment = getMockFragment(mActivity, configuration); - - when(venmoConfiguration.isAccessTokenValid()).thenReturn(true); - when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); - - Venmo.authorizeAccount(fragment, configuration); - - InOrder order = inOrder(fragment); - order.verify(fragment).sendAnalyticsEvent("pay-with-venmo.selected"); - order.verify(fragment).sendAnalyticsEvent("pay-with-venmo.app-switch.failed"); - verify(fragment).postCallback(argumentCaptor.capture()); - assertEquals("Venmo is not installed.", - argumentCaptor.getValue().getMessage()); - } - - @Test(timeout = 1000) - public void onActivityResult_postsPaymentMethodNonceAndUsernameOnSuccess() - throws InterruptedException, InvalidArgumentException { - BraintreeFragment fragment = getMockFragment(mActivity, getConfiguration()); - when(fragment.getHttpClient()).thenReturn(mock(BraintreeHttpClient.class)); - final CountDownLatch latch = new CountDownLatch(1); - fragment.addListener(new PaymentMethodNonceCreatedListener() { - @Override - public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) { - assertTrue(paymentMethodNonce instanceof VenmoAccountNonce); - VenmoAccountNonce venmoAccountNonce = (VenmoAccountNonce) paymentMethodNonce; - assertEquals("123456-12345-12345-a-adfa", venmoAccountNonce.getNonce()); - assertEquals("username", venmoAccountNonce.getDescription()); - assertEquals("username", venmoAccountNonce.getUsername()); - latch.countDown(); - } - }); - Intent intent = new Intent() - .putExtra(Venmo.EXTRA_PAYMENT_METHOD_NONCE, "123456-12345-12345-a-adfa") - .putExtra(Venmo.EXTRA_USERNAME, "username"); - - Venmo.onActivityResult(fragment, Activity.RESULT_OK, intent); - - latch.await(); - } - - @Test(timeout = 1000) - public void onActivityResult_sendsAnalyticsEventOnSuccess() throws InvalidArgumentException { - BraintreeFragment fragment = getMockFragment(mActivity, getConfiguration()); - when(fragment.getHttpClient()).thenReturn(mock(BraintreeHttpClient.class)); - Intent intent = new Intent().putExtra(Venmo.EXTRA_PAYMENT_METHOD_NONCE, - "123456-12345-12345-a-adfa").putExtra(Venmo.EXTRA_USERNAME, "username"); - - Venmo.onActivityResult(fragment, Activity.RESULT_OK, intent); - - verify(fragment).sendAnalyticsEvent("pay-with-venmo.app-switch.success"); - } - - @Test(timeout = 1000) - public void onActivityResult_sendsAnalyticsEventOnCancel() { - BraintreeFragment fragment = getMockFragment(mActivity, getConfiguration()); - when(fragment.getHttpClient()).thenReturn(mock(BraintreeHttpClient.class)); - - Venmo.onActivityResult(fragment, Activity.RESULT_CANCELED, new Intent()); - - verify(fragment).sendAnalyticsEvent("pay-with-venmo.app-switch.canceled"); - } - - @Test(timeout = 2000) - public void isVenmoInstalled_returnsTrueWhenInstalled() { - MockContext mockContext = new MockContextForVenmo() - .whitelistValue("true") - .venmoInstalled() - .build(); - VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); - when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); - Configuration configuration = getConfiguration(); - when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); - BraintreeFragment braintreeFragment = getMockFragment(mActivity, configuration); - when(braintreeFragment.getApplicationContext()).thenReturn(mockContext); - - disableSignatureVerification(); - - assertTrue(Venmo.isVenmoInstalled(mockContext)); - } - - private Configuration getConfiguration() { - AnalyticsConfiguration analyticsConfiguration = mock(AnalyticsConfiguration.class); - when(analyticsConfiguration.isEnabled()).thenReturn(true); - Configuration configuration = mock(Configuration.class); - when(configuration.getAnalytics()).thenReturn(analyticsConfiguration); - - return configuration; - } -} diff --git a/Braintree/src/androidTest/java/com/braintreepayments/api/models/VenmoConfigurationTest.java b/Braintree/src/androidTest/java/com/braintreepayments/api/models/VenmoConfigurationTest.java deleted file mode 100644 index ebf69009d1..0000000000 --- a/Braintree/src/androidTest/java/com/braintreepayments/api/models/VenmoConfigurationTest.java +++ /dev/null @@ -1,107 +0,0 @@ -package com.braintreepayments.api.models; - -import android.support.test.runner.AndroidJUnit4; -import android.test.mock.MockContext; -import android.test.suitebuilder.annotation.SmallTest; -import android.text.TextUtils; - -import com.braintreepayments.api.internal.SignatureVerificationTestUtils; -import com.braintreepayments.testutils.MockContextForVenmo; - -import org.json.JSONException; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -import static com.braintreepayments.testutils.FixturesHelper.stringFromFixture; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class VenmoConfigurationTest { - - private Configuration configurationWithVenmo; - - @Before - public void setup() throws JSONException { - configurationWithVenmo = Configuration.fromJson( - stringFromFixture("configuration_with_pay_with_venmo.json")); - } - - @Test(timeout = 1000) - public void fromJson_parsesPayWithVenmoConfiguration() throws JSONException { - assertEquals("access-token", configurationWithVenmo.getPayWithVenmo().getAccessToken()); - } - - @Test(timeout = 1000) - public void fromJson_parsesEmptyVenmoConfigurationWhenConfigurationDoesntHavePayWithVenmo() - throws JSONException { - Configuration configuration = Configuration.fromJson( - stringFromFixture("configuration.json")); - - VenmoConfiguration venmoConfiguration = configuration.getPayWithVenmo(); - assertEquals("", venmoConfiguration.getAccessToken()); - assertTrue(TextUtils.isEmpty(venmoConfiguration.getAccessToken())); - } - - @Test(timeout = 1000) - public void isEnabled_returnsTrueWhenEnabled() throws JSONException { - VenmoConfiguration venmoConfiguration = configurationWithVenmo.getPayWithVenmo(); - assertFalse(TextUtils.isEmpty(venmoConfiguration.getAccessToken())); - } - - @Test(timeout = 1000) - public void isVenmoWhitelisted_returnsTrueForWhitelist() throws JSONException { - MockContext mockContext = new MockContextForVenmo() - .whitelistValue("true") - .build(); - - assertTrue(configurationWithVenmo.getPayWithVenmo() - .isVenmoWhitelisted(mockContext.getContentResolver())); - } - - @Test(timeout = 1000) - public void isVenmoWhitelisted_returnsFalseForInvalidWhitelist() { - MockContext mockContext = new MockContextForVenmo() - .whitelistValue("false") - .build(); - - assertFalse(configurationWithVenmo.getPayWithVenmo() - .isVenmoWhitelisted(mockContext.getContentResolver())); - } - - @Test(timeout = 1000) - public void isVenmoWhitelisted_returnsFalseForJunkContentProviderAndExceptionIsPosted() { - MockContext mockContext = new MockContextForVenmo() - .whitelistValue("neither") - .build(); - - assertFalse(configurationWithVenmo.getPayWithVenmo() - .isVenmoWhitelisted(mockContext.getContentResolver())); - } - - @Test(timeout = 1000) - public void isEnabled_returnsTrueWhenAppIsInstalled() throws JSONException { - MockContext mockContext = new MockContextForVenmo() - .whitelistValue("true") - .venmoInstalled() - .build(); - - SignatureVerificationTestUtils.disableSignatureVerification(); - - assertTrue(configurationWithVenmo.getPayWithVenmo().isEnabled(mockContext)); - } - - @Test(timeout = 1000) - public void isEnabled_returnsFalseForNotInstalled() { - MockContext mockContext = new MockContextForVenmo() - .whitelistValue("true") - .build(); - - SignatureVerificationTestUtils.disableSignatureVerification(); - - assertFalse(configurationWithVenmo.getPayWithVenmo().isEnabled(mockContext)); - } -} diff --git a/Braintree/src/main/java/com/braintreepayments/api/Venmo.java b/Braintree/src/main/java/com/braintreepayments/api/Venmo.java index 0e5e159cb4..5f9ef1936c 100644 --- a/Braintree/src/main/java/com/braintreepayments/api/Venmo.java +++ b/Braintree/src/main/java/com/braintreepayments/api/Venmo.java @@ -79,11 +79,11 @@ static void authorizeAccount(BraintreeFragment fragment, Configuration configura String exceptionMessage = ""; VenmoConfiguration venmoConfiguration = configuration.getPayWithVenmo(); if(!venmoConfiguration.isAccessTokenValid()) { - exceptionMessage = "Venmo is not enabled in the control panel."; + exceptionMessage = "Venmo is not enabled"; } else if (!Venmo.isVenmoInstalled(fragment.getApplicationContext())) { - exceptionMessage = "Venmo is not installed."; + exceptionMessage = "Venmo is not installed"; } else if (!venmoConfiguration.isVenmoWhitelisted(fragment.getApplicationContext().getContentResolver())) { - exceptionMessage = "Venmo is not whitelisted."; + exceptionMessage = "Venmo is not whitelisted"; } if(!TextUtils.isEmpty(exceptionMessage)) { diff --git a/Braintree/src/main/java/com/braintreepayments/api/models/VenmoConfiguration.java b/Braintree/src/main/java/com/braintreepayments/api/models/VenmoConfiguration.java index 69f52ad67d..7170d527bd 100644 --- a/Braintree/src/main/java/com/braintreepayments/api/models/VenmoConfiguration.java +++ b/Braintree/src/main/java/com/braintreepayments/api/models/VenmoConfiguration.java @@ -10,7 +10,6 @@ import com.braintreepayments.api.Venmo; import org.json.JSONObject; -import org.w3c.dom.Text; /** * Contains the remote Pay with Venmo configuration for the Braintree SDK. @@ -66,11 +65,9 @@ public boolean isAccessTokenValid() { } public boolean isVenmoWhitelisted(ContentResolver contentResolver) { - Cursor cursor = contentResolver - .query(VENMO_AUTHORITY_URI, null, null, null, null); + Cursor cursor = contentResolver.query(VENMO_AUTHORITY_URI, null, null, null, null); - boolean isVenmoWhiteListed = - cursor != null && cursor.moveToFirst() && "true".equals(cursor.getString(0)); + boolean isVenmoWhiteListed = cursor != null && cursor.moveToFirst() && "true".equals(cursor.getString(0)); if (cursor != null) { cursor.close(); @@ -78,5 +75,4 @@ public boolean isVenmoWhitelisted(ContentResolver contentResolver) { return isVenmoWhiteListed; } - } diff --git a/Braintree/src/test/java/com/braintreepayments/api/MockFragmentBuilder.java b/Braintree/src/test/java/com/braintreepayments/api/MockFragmentBuilder.java index 0e0434445d..5797730346 100644 --- a/Braintree/src/test/java/com/braintreepayments/api/MockFragmentBuilder.java +++ b/Braintree/src/test/java/com/braintreepayments/api/MockFragmentBuilder.java @@ -1,5 +1,7 @@ package com.braintreepayments.api; +import android.content.Context; + import com.braintreepayments.api.interfaces.ConfigurationListener; import com.braintreepayments.api.interfaces.HttpResponseCallback; import com.braintreepayments.api.internal.BraintreeHttpClient; @@ -18,11 +20,21 @@ public class MockFragmentBuilder { + private Context mContext; private Authorization mAuthorization; private Configuration mConfiguration; private String mSuccessResponse; private Exception mErrorResponse; + public MockFragmentBuilder() { + mContext = RuntimeEnvironment.application; + } + + public MockFragmentBuilder context(Context context) { + mContext = context; + return this; + } + public MockFragmentBuilder authorization(Authorization authorization) { mAuthorization = authorization; return this; @@ -45,7 +57,7 @@ public MockFragmentBuilder errorResponse(Exception exception) { public BraintreeFragment build() { BraintreeFragment fragment = mock(BraintreeFragment.class); - when(fragment.getApplicationContext()).thenReturn(RuntimeEnvironment.application); + when(fragment.getApplicationContext()).thenReturn(mContext); when(fragment.getAuthorization()).thenReturn(mAuthorization); doAnswer(new Answer() { diff --git a/Braintree/src/test/java/com/braintreepayments/api/VenmoUnitTest.java b/Braintree/src/test/java/com/braintreepayments/api/VenmoUnitTest.java new file mode 100644 index 0000000000..70d414a132 --- /dev/null +++ b/Braintree/src/test/java/com/braintreepayments/api/VenmoUnitTest.java @@ -0,0 +1,289 @@ +package com.braintreepayments.api; + +import android.app.Activity; +import android.content.ComponentName; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; + +import com.braintreepayments.api.exceptions.AppSwitchNotAvailableException; +import com.braintreepayments.api.models.Configuration; +import com.braintreepayments.api.models.VenmoAccountNonce; +import com.braintreepayments.api.models.VenmoConfiguration; +import com.braintreepayments.api.test.VenmoMockContext; + +import org.json.JSONException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InOrder; +import org.robolectric.RobolectricGradleTestRunner; + +import static com.braintreepayments.api.internal.SignatureVerificationUnitTestUtils.disableSignatureVerification; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(RobolectricGradleTestRunner.class) +public class VenmoUnitTest { + + @Test + public void packageIsCorrect() { + assertEquals("com.venmo", Venmo.PACKAGE_NAME); + } + + @Test + public void appSwitchActivityIsCorrect() { + assertEquals("controller.SetupMerchantActivity", Venmo.APP_SWITCH_ACTIVITY); + } + + @Test + public void certificateSubjectIsCorrect() { + assertEquals("CN=Andrew Kortina,OU=Engineering,O=Venmo,L=Philadelphia,ST=PA,C=US", Venmo.CERTIFICATE_SUBJECT); + } + + @Test + public void certificateIssuerIsCorrect() { + assertEquals("CN=Andrew Kortina,OU=Engineering,O=Venmo,L=Philadelphia,ST=PA,C=US", Venmo.CERTIFICATE_ISSUER); + } + + @Test + public void publicKeyHashCodeIsCorrect() { + assertEquals(-129711843, Venmo.PUBLIC_KEY_HASH_CODE); + } + + @Test + public void isVenmoInstalled_returnsTrueWhenInstalled() { + Context context = new VenmoMockContext() + .whitelistValue("true") + .venmoInstalled() + .build(); + disableSignatureVerification(); + + assertTrue(Venmo.isVenmoInstalled(context)); + } + + @Test + public void containsCorrectVenmoExtras() throws JSONException { + Configuration configuration = mock(Configuration.class); + VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); + when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); + when(configuration.getMerchantId()).thenReturn("merchant_id"); + when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); + when(configuration.getEnvironment()).thenReturn("environment"); + + Intent intent = Venmo.getLaunchIntent(configuration); + + assertEquals(new ComponentName("com.venmo", "com.venmo.controller.SetupMerchantActivity"), + intent.getComponent()); + assertEquals("merchant_id", intent.getStringExtra(Venmo.EXTRA_MERCHANT_ID)); + assertEquals("access-token", intent.getStringExtra(Venmo.EXTRA_ACCESS_TOKEN)); + assertEquals(BuildConfig.VERSION_NAME, intent.getStringExtra(Venmo.EXTRA_SDK_VERSION)); + assertEquals("environment", intent.getStringExtra(Venmo.EXTRA_ENVIRONMENT)); + } + + @Test + public void authorizeAccount_postsExceptionWhenNotEnabled() { + Configuration configuration = mock(Configuration.class); + VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); + BraintreeFragment fragment = new MockFragmentBuilder() + .configuration(configuration) + .build(); + when(configuration.getMerchantId()).thenReturn("merchant_id"); + when(configuration.getEnvironment()).thenReturn("environment"); + when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); + + Venmo.authorizeAccount(fragment, configuration); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(AppSwitchNotAvailableException.class); + verify(fragment).postCallback(captor.capture()); + assertEquals("Venmo is not enabled", captor.getValue().getMessage()); + } + + @Test + public void authorizeAccount_postsExceptionWhenNotInstalled() { + VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); + when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); + when(venmoConfiguration.isAccessTokenValid()).thenReturn(true); + when(venmoConfiguration.isVenmoWhitelisted(any(ContentResolver.class))).thenReturn(true); + Configuration configuration = mock(Configuration.class); + when(configuration.getMerchantId()).thenReturn("merchant_id"); + when(configuration.getEnvironment()).thenReturn("environment"); + when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); + BraintreeFragment fragment = new MockFragmentBuilder() + .configuration(configuration) + .build(); + + Venmo.authorizeAccount(fragment, configuration); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(AppSwitchNotAvailableException.class); + verify(fragment).postCallback(captor.capture()); + assertEquals("Venmo is not installed", captor.getValue().getMessage()); + } + + @Test + public void authorizeAccount_postsExceptionWhenNotWhitelisted() { + Configuration configuration = mock(Configuration.class); + VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); + when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); + when(venmoConfiguration.isAccessTokenValid()).thenReturn(true); + when(configuration.getMerchantId()).thenReturn("merchant_id"); + when(configuration.getEnvironment()).thenReturn("environment"); + when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); + Context context = new VenmoMockContext() + .venmoInstalled() + .whitelistValue("true") + .build(); + disableSignatureVerification(); + BraintreeFragment fragment = new MockFragmentBuilder() + .context(context) + .configuration(configuration) + .build(); + + Venmo.authorizeAccount(fragment, configuration); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(AppSwitchNotAvailableException.class); + verify(fragment).postCallback(captor.capture()); + assertEquals("Venmo is not whitelisted", captor.getValue().getMessage()); + } + + @Test(timeout = 1000) + public void performAppSwitch_appSwitchesWithVenmoLaunchIntent() { + VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); + when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); + when(venmoConfiguration.isAccessTokenValid()).thenReturn(true); + when(venmoConfiguration.isVenmoWhitelisted(any(ContentResolver.class))).thenReturn(true); + Configuration configuration = mock(Configuration.class); + when(configuration.getMerchantId()).thenReturn("merchant_id"); + when(configuration.getEnvironment()).thenReturn("environment"); + when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); + Context context = new VenmoMockContext() + .venmoInstalled() + .whitelistValue("true") + .build(); + disableSignatureVerification(); + BraintreeFragment fragment = new MockFragmentBuilder() + .context(context) + .configuration(configuration) + .build(); + + Venmo.authorizeAccount(fragment, configuration); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Intent.class); + verify(fragment).startActivityForResult(captor.capture(), eq(Venmo.VENMO_REQUEST_CODE)); + assertEquals("com.venmo/com.venmo.controller.SetupMerchantActivity", + captor.getValue().getComponent().flattenToString()); + Bundle extras = captor.getValue().getExtras(); + assertEquals("merchant_id", extras.getString(Venmo.EXTRA_MERCHANT_ID)); + assertEquals("access-token", extras.getString(Venmo.EXTRA_ACCESS_TOKEN)); + assertEquals(BuildConfig.VERSION_NAME, extras.getString(Venmo.EXTRA_SDK_VERSION)); + assertEquals("environment", extras.getString(Venmo.EXTRA_ENVIRONMENT)); + } + + @Test + public void performAppSwitch_sendsAnalyticsEvent() { + VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); + when(venmoConfiguration.getAccessToken()).thenReturn("access-token"); + Configuration configuration = mock(Configuration.class); + when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); + BraintreeFragment fragment = new MockFragmentBuilder() + .configuration(configuration) + .build(); + + Venmo.authorizeAccount(fragment); + + verify(fragment).sendAnalyticsEvent("pay-with-venmo.selected"); + } + + @Test + public void performAppSwitch_sendsAnalyticsEventWhenStarted() { + VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); + when(venmoConfiguration.isAccessTokenValid()).thenReturn(true); + when(venmoConfiguration.isVenmoWhitelisted(any(ContentResolver.class))).thenReturn(true); + Configuration configuration = mock(Configuration.class); + when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); + Context context = new VenmoMockContext() + .whitelistValue("true") + .venmoInstalled() + .build(); + BraintreeFragment fragment = new MockFragmentBuilder() + .context(context) + .configuration(configuration) + .build(); + + Venmo.authorizeAccount(fragment, configuration); + + verify(fragment).sendAnalyticsEvent("pay-with-venmo.selected"); + verify(fragment).sendAnalyticsEvent("pay-with-venmo.app-switch.started"); + } + + @Test + public void performAppSwitch_sendsAnalyticsEventWhenUnavailableAndPostException() { + VenmoConfiguration venmoConfiguration = mock(VenmoConfiguration.class); + when(venmoConfiguration.isAccessTokenValid()).thenReturn(true); + Configuration configuration = mock(Configuration.class); + when(configuration.getPayWithVenmo()).thenReturn(venmoConfiguration); + BraintreeFragment fragment = new MockFragmentBuilder() + .configuration(configuration) + .build(); + + Venmo.authorizeAccount(fragment, configuration); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(AppSwitchNotAvailableException.class); + InOrder order = inOrder(fragment); + order.verify(fragment).sendAnalyticsEvent("pay-with-venmo.selected"); + order.verify(fragment).sendAnalyticsEvent("pay-with-venmo.app-switch.failed"); + verify(fragment).postCallback(captor.capture()); + assertEquals("Venmo is not installed", captor.getValue().getMessage()); + } + + @Test + public void onActivityResult_postsPaymentMethodNonceOnSuccess() { + BraintreeFragment fragment = new MockFragmentBuilder() + .build(); + Intent intent = new Intent() + .putExtra(Venmo.EXTRA_PAYMENT_METHOD_NONCE, "123456-12345-12345-a-adfa") + .putExtra(Venmo.EXTRA_USERNAME, "username"); + + Venmo.onActivityResult(fragment, Activity.RESULT_OK, intent); + + ArgumentCaptor captor = ArgumentCaptor.forClass(VenmoAccountNonce.class); + verify(fragment).postCallback(captor.capture()); + assertEquals("123456-12345-12345-a-adfa", captor.getValue().getNonce()); + assertEquals("username", captor.getValue().getDescription()); + assertEquals("username", captor.getValue().getUsername()); + } + + @Test + public void onActivityResult_sendsAnalyticsEventOnSuccess() { + BraintreeFragment fragment = new MockFragmentBuilder() + .build(); + Intent intent = new Intent() + .putExtra(Venmo.EXTRA_PAYMENT_METHOD_NONCE, "123456-12345-12345-a-adfa") + .putExtra(Venmo.EXTRA_USERNAME, "username"); + + Venmo.onActivityResult(fragment, Activity.RESULT_OK, intent); + + verify(fragment).sendAnalyticsEvent("pay-with-venmo.app-switch.success"); + } + + @Test + public void onActivityResult_sendsAnalyticsEventOnCancel() { + BraintreeFragment fragment = new MockFragmentBuilder() + .build(); + + Venmo.onActivityResult(fragment, Activity.RESULT_CANCELED, new Intent()); + + verify(fragment).sendAnalyticsEvent("pay-with-venmo.app-switch.canceled"); + } +} diff --git a/Braintree/src/androidTest/java/com/braintreepayments/api/internal/SignatureVerificationTestUtils.java b/Braintree/src/test/java/com/braintreepayments/api/internal/SignatureVerificationUnitTestUtils.java similarity index 77% rename from Braintree/src/androidTest/java/com/braintreepayments/api/internal/SignatureVerificationTestUtils.java rename to Braintree/src/test/java/com/braintreepayments/api/internal/SignatureVerificationUnitTestUtils.java index 7cd7cd8ea4..8f91e9433a 100644 --- a/Braintree/src/androidTest/java/com/braintreepayments/api/internal/SignatureVerificationTestUtils.java +++ b/Braintree/src/test/java/com/braintreepayments/api/internal/SignatureVerificationUnitTestUtils.java @@ -1,6 +1,6 @@ package com.braintreepayments.api.internal; -public class SignatureVerificationTestUtils { +public class SignatureVerificationUnitTestUtils { public static void disableSignatureVerification() { SignatureVerification.sEnableSignatureVerification = false; diff --git a/Braintree/src/test/java/com/braintreepayments/api/models/VenmoConfigurationTest.java b/Braintree/src/test/java/com/braintreepayments/api/models/VenmoConfigurationTest.java new file mode 100644 index 0000000000..0086b25fdf --- /dev/null +++ b/Braintree/src/test/java/com/braintreepayments/api/models/VenmoConfigurationTest.java @@ -0,0 +1,95 @@ +package com.braintreepayments.api.models; + +import android.content.Context; +import android.text.TextUtils; + +import com.braintreepayments.api.test.VenmoMockContext; + +import org.json.JSONException; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; + +import static com.braintreepayments.api.internal.SignatureVerificationUnitTestUtils.disableSignatureVerification; +import static com.braintreepayments.testutils.FixturesHelper.stringFromFixture; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; + +@RunWith(RobolectricGradleTestRunner.class) +public class VenmoConfigurationTest { + + private Configuration mConfiguration; + + @Before + public void setup() throws JSONException { + mConfiguration = Configuration.fromJson(stringFromFixture("configuration_with_pay_with_venmo.json")); + } + + @Test + public void fromJson_parsesPayWithVenmoConfiguration() throws JSONException { + assertEquals("access-token", mConfiguration.getPayWithVenmo().getAccessToken()); + } + + @Test + public void fromJson_parsesEmptyVenmoConfigurationWhenConfigurationDoesntHavePayWithVenmo() throws JSONException { + Configuration configuration = Configuration.fromJson(stringFromFixture("configuration.json")); + + assertEquals("", configuration.getPayWithVenmo().getAccessToken()); + assertTrue(TextUtils.isEmpty(configuration.getPayWithVenmo().getAccessToken())); + } + + @Test + public void isEnabled_returnsTrueWhenEnabled() throws JSONException { + assertFalse(TextUtils.isEmpty(mConfiguration.getPayWithVenmo().getAccessToken())); + } + + @Test + public void isVenmoWhitelisted_returnsTrueForWhitelist() throws JSONException { + Context context = new VenmoMockContext() + .whitelistValue("true") + .build(); + + assertTrue(mConfiguration.getPayWithVenmo().isVenmoWhitelisted(context.getContentResolver())); + } + + @Test + public void isVenmoWhitelisted_returnsFalseForInvalidWhitelist() { + Context context = new VenmoMockContext() + .whitelistValue("false") + .build(); + + assertFalse(mConfiguration.getPayWithVenmo().isVenmoWhitelisted(context.getContentResolver())); + } + + @Test + public void isVenmoWhitelisted_returnsFalseForJunkContentProviderAndExceptionIsPosted() { + Context context = new VenmoMockContext() + .whitelistValue("neither") + .build(); + + assertFalse(mConfiguration.getPayWithVenmo().isVenmoWhitelisted(context.getContentResolver())); + } + + @Test + public void isEnabled_returnsTrueWhenAppIsInstalled() throws JSONException { + Context context = new VenmoMockContext() + .whitelistValue("true") + .venmoInstalled() + .build(); + + disableSignatureVerification(); + + assertTrue(mConfiguration.getPayWithVenmo().isEnabled(context)); + } + + @Test + public void isEnabled_returnsFalseForNotInstalled() { + Context context = new VenmoMockContext() + .whitelistValue("true") + .build(); + + assertFalse(mConfiguration.getPayWithVenmo().isEnabled(context)); + } +} diff --git a/Braintree/src/test/java/com/braintreepayments/api/test/VenmoMockContext.java b/Braintree/src/test/java/com/braintreepayments/api/test/VenmoMockContext.java new file mode 100644 index 0000000000..d572b3b483 --- /dev/null +++ b/Braintree/src/test/java/com/braintreepayments/api/test/VenmoMockContext.java @@ -0,0 +1,80 @@ +package com.braintreepayments.api.test; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.database.Cursor; +import android.net.Uri; +import android.test.mock.MockContentProvider; +import android.test.mock.MockCursor; + +import org.robolectric.RuntimeEnvironment; +import org.robolectric.shadows.ShadowContentResolver; + +import java.util.Collections; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class VenmoMockContext { + + private PackageManager mPackageManager; + private MockCursor mMockCursor; + + public VenmoMockContext() { + mPackageManager = mock(PackageManager.class); + } + + public VenmoMockContext whitelistValue(String whitelistValue) { + mMockCursor = mockCursor(whitelistValue); + return this; + } + + public VenmoMockContext venmoInstalled() { + mPackageManager = mockPackageManager(); + return this; + } + + public Context build() { + Context context = mock(Context.class); + when(context.getContentResolver()).thenReturn(RuntimeEnvironment.application.getContentResolver()); + when(context.getPackageManager()).thenReturn(mPackageManager); + + if (mMockCursor != null) { + MockContentProvider mockContentProvider = new MockContentProvider(context) { + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, + String sortOrder) { + return mMockCursor; + } + }; + ShadowContentResolver.registerProvider("com.venmo.whitelistprovider", mockContentProvider); + } + + return context; + } + + private MockCursor mockCursor(String whitelistValue) { + final MockCursor mockCursor = mock(MockCursor.class); + when(mockCursor.getCount()).thenReturn(1); + when(mockCursor.getString(anyInt())).thenReturn(whitelistValue); + when(mockCursor.moveToFirst()).thenReturn(true); + return mockCursor; + } + + private PackageManager mockPackageManager() { + ActivityInfo activityInfo = new ActivityInfo(); + activityInfo.packageName = "com.venmo"; + ResolveInfo resolveInfo = new ResolveInfo(); + resolveInfo.activityInfo = activityInfo; + final PackageManager packageManager = mock(PackageManager.class); + when(packageManager.queryIntentActivities(any(Intent.class), anyInt())) + .thenReturn(Collections.singletonList(resolveInfo)); + return packageManager; + } +} + diff --git a/Drop-In/src/androidTest/java/com/braintreepayments/api/test/BraintreePaymentActivityTestRunner.java b/Drop-In/src/androidTest/java/com/braintreepayments/api/test/BraintreePaymentActivityTestRunner.java index 0be6d1f29c..699d0a3901 100644 --- a/Drop-In/src/androidTest/java/com/braintreepayments/api/test/BraintreePaymentActivityTestRunner.java +++ b/Drop-In/src/androidTest/java/com/braintreepayments/api/test/BraintreePaymentActivityTestRunner.java @@ -6,7 +6,6 @@ import com.braintreepayments.api.BraintreePaymentActivity; import com.braintreepayments.api.BraintreePaymentTestActivity; import com.braintreepayments.api.PaymentRequest; -import com.braintreepayments.api.internal.SignatureVerificationTestUtils; import com.braintreepayments.testutils.BraintreeActivityTestRule; import org.junit.Before; diff --git a/TestUtils/src/main/java/com/braintreepayments/testutils/MockContextForVenmo.java b/TestUtils/src/main/java/com/braintreepayments/testutils/MockContextForVenmo.java deleted file mode 100644 index dc25eba474..0000000000 --- a/TestUtils/src/main/java/com/braintreepayments/testutils/MockContextForVenmo.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.braintreepayments.testutils; - -import android.content.ContentResolver; -import android.content.Intent; -import android.content.pm.ActivityInfo; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.database.Cursor; -import android.net.Uri; -import android.test.mock.MockContentProvider; -import android.test.mock.MockContentResolver; -import android.test.mock.MockContext; -import android.test.mock.MockCursor; - -import java.util.Collections; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class MockContextForVenmo { - - private PackageManager mPackageManager = null; - private MockCursor mMockCursor = null; - private MockContentResolver mContentResolver = null; - - public MockContextForVenmo whitelistValue(String whitelistValue) { - mMockCursor = mockCursor(whitelistValue); - mContentResolver = new MockContentResolver(); - return this; - } - - public MockContextForVenmo venmoInstalled() { - mPackageManager = mockPackageManager(); - return this; - } - - public MockContext build() { - MockContext mockContext = new MockContext() { - @Override - public ContentResolver getContentResolver() { - if (mContentResolver == null) { - return mock(ContentResolver.class); - } else { - return mContentResolver; - } - } - - @Override - public PackageManager getPackageManager() { - if (mPackageManager == null) { - return mock(PackageManager.class); - } else { - return mPackageManager; - } - } - }; - - if (mMockCursor != null) { - MockContentProvider mockContentProvider = mockContentProvider(mockContext, mMockCursor); - mContentResolver.addProvider("com.venmo.whitelistprovider", mockContentProvider); - } - - return mockContext; - } - - private MockCursor mockCursor(String whitelistValue) { - final MockCursor mockCursor = mock(MockCursor.class); - when(mockCursor.getCount()).thenReturn(1); - when(mockCursor.getString(anyInt())).thenReturn(whitelistValue); - when(mockCursor.moveToFirst()).thenReturn(true); - return mockCursor; - } - - private MockContentProvider mockContentProvider(MockContext mockContext, - final MockCursor mockCursor) { - MockContentProvider mockContentProvider = new MockContentProvider(mockContext) { - @Override - public Cursor query(Uri uri, String[] projection, String selection, - String[] selectionArgs, - String sortOrder) { - return mockCursor; - } - }; - mContentResolver.addProvider("com.venmo.whitelistprovider", mockContentProvider); - return mockContentProvider; - } - - private PackageManager mockPackageManager() { - ActivityInfo activityInfo = new ActivityInfo(); - activityInfo.packageName = "com.venmo"; - ResolveInfo resolveInfo = new ResolveInfo(); - resolveInfo.activityInfo = activityInfo; - final PackageManager packageManager = mock(PackageManager.class); - when(packageManager.queryIntentActivities(any(Intent.class), anyInt())) - .thenReturn(Collections.singletonList(resolveInfo)); - return packageManager; - } -} -