Skip to content

Commit

Permalink
Android Lint Cleanup (#726)
Browse files Browse the repository at this point in the history
* Add ci script to replace Rakefile.

* Update Robolectric version.

* Fix unit tests broken by robolectric upgrade.

* Remove assertJ from project and update jsonAssert / mockk dependencies.

* Cleanup test imports.

* Remove unecessary VisibleForTesting annotation in DeviceInspector and fixing unecessary optional unwrap in DeviceInspector.

* Cleanup additional test lint errors.

* Remove unecessary import from AuthenticationInsight.java.

* Remove unecessary import and add TODO.

* Clean up Demo app lint errors.

* Remove Android Pay logo.

* Cleanup Android lint errors.

* Cleanup additional imports.

* Cleanup unused method.

* Add generic type declaration to ManifestValidator methods.

* More test import cleanup.
  • Loading branch information
sshropshire authored May 1, 2023
1 parent d5015fe commit 023cc56
Show file tree
Hide file tree
Showing 57 changed files with 236 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import androidx.work.testing.WorkManagerTestInitHelper;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package com.braintreepayments.api;

import androidx.appcompat.app.AppCompatActivity;
import static com.braintreepayments.api.Assertions.assertIsANonce;
import static junit.framework.Assert.fail;

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

import static com.braintreepayments.api.Assertions.assertIsANonce;
import static junit.framework.Assert.fail;

import android.content.Context;

@RunWith(AndroidJUnit4ClassRunner.class)
public class ApiClientTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.braintreepayments.api;

import static junit.framework.Assert.assertNotNull;

import android.content.Context;

import androidx.annotation.Nullable;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

import static junit.framework.Assert.assertNotNull;

import android.content.Context;

@RunWith(AndroidJUnit4ClassRunner.class)
public class BraintreeClientTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ internal class BraintreeGraphQLResponseParser @VisibleForTesting constructor(
val errors = JSONObject(response).optJSONArray(GraphQLConstants.Keys.ERRORS)
if (errors == null) return response

for (i in 0 until errors!!.length()) {
val error = errors!!.getJSONObject(i)
for (i in 0 until errors.length()) {
val error = errors.getJSONObject(i)
val extensions = error.optJSONObject(GraphQLConstants.Keys.EXTENSIONS)
val message = Json.optString(
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class DeviceInspector @VisibleForTesting internal constructor(
File("/system/app/Superuser.apk")
)

@VisibleForTesting
internal fun getDeviceMetadata(
context: Context?,
sessionId: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ class BraintreeClientUnitTest {
manifestValidator.isUrlSchemeDeclaredInAndroidManifest(
applicationContext,
"a-url-scheme",
any()
FragmentActivity::class.java
)
} returns true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@ import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import java.net.HttpURLConnection

class BraintreeGraphQLResponseParserUnitTest {

private lateinit var urlConnection: HttpURLConnection
private lateinit var baseParser: BaseHttpResponseParser

@Rule
@JvmField
var exceptionRule: ExpectedException = ExpectedException.none()

@Before
fun beforeEach() {
baseParser = mockk()
Expand All @@ -38,14 +32,16 @@ class BraintreeGraphQLResponseParserUnitTest {
@Test
@Throws(Exception::class)
fun parse_propagatesExceptionsByDefault() {
exceptionRule.expect(Exception::class.java)
exceptionRule.expectMessage("error")

val exception = Exception("error")
every { baseParser.parse(123, urlConnection) } throws exception

val sut = BraintreeGraphQLResponseParser(baseParser)
sut.parse(123, urlConnection)
try {
sut.parse(123, urlConnection)
fail("should not get here")
} catch (actualException: Exception) {
assertSame(exception, actualException)
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ package com.braintreepayments.api

import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import java.net.HttpURLConnection

class BraintreeHttpResponseParserUnitTest {

private lateinit var urlConnection: HttpURLConnection
private lateinit var baseParser: BaseHttpResponseParser

@Rule
@JvmField
var exceptionRule: ExpectedException = ExpectedException.none()

@Before
fun beforeEach() {
baseParser = mockk()
Expand All @@ -36,41 +30,48 @@ class BraintreeHttpResponseParserUnitTest {
@Test
@Throws(Exception::class)
fun parse_propagatesExceptionsByDefault() {
exceptionRule.expect(Exception::class.java)
exceptionRule.expectMessage("error")

val exception = Exception("error")
every { baseParser.parse(123, urlConnection) } throws exception

val sut = BraintreeHttpResponseParser(baseParser)
sut.parse(123, urlConnection)
try {
sut.parse(123, urlConnection)
fail("should not get here")
} catch (actualException: Exception) {
assertSame(exception, actualException)
}
}

@Test
@Throws(Exception::class)
fun parse_whenBaseParserThrowsAuthorizationException_throwsNewAuthorizationExceptionWithMessage() {
exceptionRule.expect(AuthorizationException::class.java)
exceptionRule.expectMessage("There was an error")

val authorizationException = AuthorizationException(Fixtures.ERROR_RESPONSE)
every { baseParser.parse(123, urlConnection) } throws authorizationException

val sut = BraintreeHttpResponseParser(baseParser)
sut.parse(123, urlConnection)
try {
sut.parse(123, urlConnection)
fail("should not get here")
} catch (actualException: AuthorizationException) {
assertEquals("There was an error", actualException.message)
}
}

@Test
@Throws(Exception::class)
fun parse_whenBaseParserThrowsUnprocessibleEntityException_throwsErrorWithResponseException() {
exceptionRule.expect(ErrorWithResponse::class.java)
exceptionRule.expectMessage("There was an error")
val unprocessableEntityException = UnprocessableEntityException(Fixtures.ERROR_RESPONSE)

every {
baseParser.parse(123, urlConnection)
} throws unprocessableEntityException

val sut = BraintreeHttpResponseParser(baseParser)
sut.parse(123, urlConnection)
try {
sut.parse(123, urlConnection)
fail("should not get here")
} catch (actualException: ErrorWithResponse) {
assertEquals("There was an error", actualException.message)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.braintreepayments.api
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertNotNull
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull

import org.junit.Test
import java.util.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class VisaCheckoutConfigurationUnitTest {
assertFalse(sut.isEnabled)
assertEquals("", sut.apiKey)
assertEquals("", sut.externalClientId)
assertTrue(sut.acceptedCardBrands!!.isEmpty())
assertTrue(sut.acceptedCardBrands.isEmpty())
}

@Test
Expand All @@ -46,6 +46,6 @@ class VisaCheckoutConfigurationUnitTest {
assertFalse(sut.isEnabled)
assertEquals("", sut.apiKey)
assertEquals("", sut.externalClientId)
assertTrue(sut.acceptedCardBrands!!.isEmpty())
assertTrue(sut.acceptedCardBrands.isEmpty())
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.braintreepayments.api;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.fail;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;

Expand All @@ -14,18 +19,11 @@
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.fail;

@RunWith(AndroidJUnit4ClassRunner.class)
public class DataCollectorTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.braintreepayments.api;

import static com.braintreepayments.api.CardNumber.VISA;
import static com.braintreepayments.api.Fixtures.TOKENIZATION_KEY;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;

import org.json.JSONException;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -15,13 +21,6 @@
import java.util.Collection;
import java.util.concurrent.CountDownLatch;

import static com.braintreepayments.api.CardNumber.VISA;
import static com.braintreepayments.api.Fixtures.TOKENIZATION_KEY;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

@RunWith(Parameterized.class)
public class CardClientTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.json.JSONObject;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.braintreepayments.demo;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.braintreepayments.api.BraintreeClient;

// NEXT MAJOR VERSION: remove
public interface BraintreeClientCallback {
void onResult(@NonNull BraintreeClient braintreeClient);
}
10 changes: 4 additions & 6 deletions Demo/src/main/java/com/braintreepayments/demo/CardFragment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.braintreepayments.demo;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
Expand All @@ -14,18 +17,16 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.fragment.NavHostFragment;

import com.braintreepayments.api.AmericanExpressClient;
import com.braintreepayments.api.AmericanExpressRewardsBalance;
import com.braintreepayments.api.BraintreeClient;
import com.braintreepayments.api.PaymentMethodNonce;
import com.braintreepayments.api.BrowserSwitchResult;
import com.braintreepayments.api.Card;
import com.braintreepayments.api.CardClient;
import com.braintreepayments.api.CardNonce;
import com.braintreepayments.api.DataCollector;
import com.braintreepayments.api.PaymentMethodNonce;
import com.braintreepayments.api.ThreeDSecureAdditionalInformation;
import com.braintreepayments.api.ThreeDSecureClient;
import com.braintreepayments.api.ThreeDSecureListener;
Expand All @@ -48,9 +49,6 @@
import com.braintreepayments.cardform.view.CardForm;
import com.google.android.material.textfield.TextInputLayout;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

public class CardFragment extends BaseFragment implements OnCardFormSubmitListener, OnCardFormFieldFocusedListener, ThreeDSecureListener {

private static final String EXTRA_THREE_D_SECURE_REQUESTED = "com.braintreepayments.demo.EXTRA_THREE_D_SECURE_REQUESTED";
Expand Down
Loading

0 comments on commit 023cc56

Please sign in to comment.