Skip to content

Commit

Permalink
Restore url when BraintreeFragment is restored from savedInstanceState
Browse files Browse the repository at this point in the history
  • Loading branch information
lkorth committed Aug 23, 2016
1 parent 639c946 commit 1907afe
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void onCreate(Bundle savedInstanceState) {
mHasFetchedPaymentMethodNonces = savedInstanceState.getBoolean(EXTRA_FETCHED_PAYMENT_METHOD_NONCES);
mIsBrowserSwitching = savedInstanceState.getBoolean(EXTRA_BROWSER_SWITCHING);
try {
mConfiguration = Configuration.fromJson(savedInstanceState.getString(EXTRA_CONFIGURATION));
setConfiguration(Configuration.fromJson(savedInstanceState.getString(EXTRA_CONFIGURATION)));
} catch (JSONException ignored) {}
} else {
if (mAuthorization instanceof TokenizationKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.braintreepayments.api.internal.AnalyticsDatabase;
import com.braintreepayments.api.internal.AnalyticsDatabaseTestUtils;
import com.braintreepayments.api.internal.AnalyticsIntentService;
import com.braintreepayments.api.internal.HttpClient;
import com.braintreepayments.api.models.CardNonce;
import com.braintreepayments.api.models.Configuration;
import com.braintreepayments.api.models.PayPalAccountNonce;
Expand Down Expand Up @@ -47,6 +48,7 @@

import static com.braintreepayments.api.internal.AnalyticsDatabaseTestUtils.verifyAnalyticsEvent;
import static com.braintreepayments.testutils.FixturesHelper.stringFromFixture;
import static com.braintreepayments.testutils.ReflectionHelper.getField;
import static com.braintreepayments.testutils.TestConfigurationBuilder.basicConfig;
import static com.braintreepayments.testutils.TestTokenizationKey.TOKENIZATION_KEY;
import static junit.framework.Assert.assertEquals;
Expand Down Expand Up @@ -165,6 +167,26 @@ public void onCreate_doesNotCallFetchConfigurationWhenConfigurationIsNotNull() t
any(BraintreeResponseListener.class));
}

@Test
public void onCreate_restoresConfigurationAndHttpClient() throws InvalidArgumentException, NoSuchFieldException,
IllegalAccessException {
BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY);
fragment.mConfiguration = new TestConfigurationBuilder()
.clientApiUrl("client_api_url")
.buildConfiguration();
Bundle bundle = new Bundle();
fragment.onSaveInstanceState(bundle);
fragment.mConfiguration = null;
fragment.mHttpClient = null;

fragment.onCreate(bundle);

assertNotNull(fragment.mConfiguration);
assertNotNull(fragment.mHttpClient);
assertEquals("client_api_url", getField(HttpClient.class, "mBaseUrl", fragment.mHttpClient));
}


@Test
public void newInstance_setsIntegrationTypeToCustomForAllActivities() throws InvalidArgumentException {
BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.concurrent.CountDownLatch;

import static com.braintreepayments.testutils.FixturesHelper.stringFromFixture;
import static com.braintreepayments.testutils.ReflectionHelper.setField;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
Expand Down Expand Up @@ -142,8 +141,8 @@ public void authorizeAccount_isSuccessful() throws Exception {
final AuthorizationRequest request = new AuthorizationRequest(RuntimeEnvironment.application);
request.environment("test");
request.successUrl("com.braintreepayments.api.test.braintree", "success");
setField("mMsgGuid", request, "c862cf00-f878-4e38-bb83-65bcc4b51831");
setField("mEncryptionKey", request, EncryptionUtils.hexStringToByteArray("0481806100DE4EBB5581163579990EE825737255A81A883B791A1BB6F5A7E81C"));
setField(AuthorizationRequest.class, "mMsgGuid", request, "c862cf00-f878-4e38-bb83-65bcc4b51831");
setField(AuthorizationRequest.class, "mEncryptionKey", request, EncryptionUtils.hexStringToByteArray("0481806100DE4EBB5581163579990EE825737255A81A883B791A1BB6F5A7E81C"));

doAnswer(new Answer<AuthorizationRequest>() {
@Override
Expand Down Expand Up @@ -184,8 +183,8 @@ public void authorizeAccount_doesNotCallCancelListenerWhenSuccessful() throws Ex
final AuthorizationRequest request = new AuthorizationRequest(RuntimeEnvironment.application);
request.environment("test");
request.successUrl("com.braintreepayments.api.test.braintree", "success");
setField("mMsgGuid", request, "c862cf00-f878-4e38-bb83-65bcc4b51831");
setField("mEncryptionKey", request, EncryptionUtils.hexStringToByteArray("0481806100DE4EBB5581163579990EE825737255A81A883B791A1BB6F5A7E81C"));
setField(AuthorizationRequest.class, "mMsgGuid", request, "c862cf00-f878-4e38-bb83-65bcc4b51831");
setField(AuthorizationRequest.class, "mEncryptionKey", request, EncryptionUtils.hexStringToByteArray("0481806100DE4EBB5581163579990EE825737255A81A883B791A1BB6F5A7E81C"));

doAnswer(new Answer<AuthorizationRequest>() {
@Override
Expand Down Expand Up @@ -701,16 +700,4 @@ public void onActivityResult_postsCancelWhenIntentIsNull() {

verify(fragment).postCancelCallback(PayPal.PAYPAL_REQUEST_CODE);
}

private void setField(String fieldName, Object src, Object value)
throws NoSuchFieldException, IllegalAccessException {
Field field = AuthorizationRequest.class.getDeclaredField(fieldName);
field.setAccessible(true);

Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

field.set(src, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.braintreepayments.testutils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class ReflectionHelper {

public static Object getField(Class clazz, String fieldName, Object src)
throws NoSuchFieldException, IllegalAccessException {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(src);
}

public static void setField(Class clazz, String fieldName, Object src, Object value)
throws NoSuchFieldException, IllegalAccessException {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);

Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

field.set(src, value);
}
}

0 comments on commit 1907afe

Please sign in to comment.