Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ipc 186 unit tests payment component #395

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.gini.android.health.sdk.paymentcomponent

import android.content.Context
import androidx.annotation.VisibleForTesting
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -22,7 +23,8 @@ class PaymentComponent(private val context: Context, private val giniHealth: Gin
MutableStateFlow<SelectedPaymentProviderAppState>(SelectedPaymentProviderAppState.NothingSelected)
val selectedPaymentProviderAppFlow: StateFlow<SelectedPaymentProviderAppState> = _selectedPaymentProviderAppFlow.asStateFlow()

private val paymentComponentPreferences = PaymentComponentPreferences(context)
@VisibleForTesting
internal val paymentComponentPreferences = PaymentComponentPreferences(context)

var listener: Listener? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.util.TypedValue
import androidx.annotation.ColorInt
import net.gini.android.health.api.models.PaymentProvider
import net.gini.android.health.sdk.util.extensions.generateBitmapDrawableIcon
import org.slf4j.LoggerFactory

internal const val Scheme = "ginipay" // It has to match the scheme in query tag in manifest
Expand Down Expand Up @@ -122,22 +119,7 @@ data class PaymentProviderApp(
}
return PaymentProviderApp(
name = paymentProvider.name,
icon = BitmapFactory.decodeByteArray(paymentProvider.icon, 0, paymentProvider.icon.size)
?.let { bitmap ->
val iconSizePx = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
ICON_SIZE,
context.resources.displayMetrics
).toInt()
val scaledBitamp = Bitmap.createScaledBitmap(
bitmap,
iconSizePx,
iconSizePx,
true
)
bitmap.recycle()
BitmapDrawable(context.resources, scaledBitamp)
},
icon = context.generateBitmapDrawableIcon(paymentProvider.icon, paymentProvider.icon.size),
colors = PaymentProviderAppColors(
backgroundColor = Color.parseColor("#${paymentProvider.colors.backgroundColorRGBHex}"),
textColor = Color.parseColor("#${paymentProvider.colors.textColoRGBHex}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.gini.android.health.sdk.util.extensions

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.util.TypedValue
import androidx.annotation.VisibleForTesting
import net.gini.android.health.sdk.paymentprovider.PaymentProviderApp

// In a future refactoring we can split extensions into files according to what component they extend
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Comment] Yes, thank you for adding this comment! I wholeheartedly agree! 👍 👍 👍

@VisibleForTesting
internal fun Context.generateBitmapDrawableIcon(icon: ByteArray, iconSize: Int): BitmapDrawable? {
return BitmapFactory.decodeByteArray(icon, 0, iconSize)
?.let { bitmap ->
val iconSizePx = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
PaymentProviderApp.ICON_SIZE,
this.resources.displayMetrics
).toInt()
val scaledBitmap = Bitmap.createScaledBitmap(
bitmap,
iconSizePx,
iconSizePx,
true
)
bitmap.recycle()
BitmapDrawable(this.resources, scaledBitmap)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.gini.android.health.sdk.paymentComponent

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import net.gini.android.health.sdk.paymentcomponent.PaymentComponentPreferences
import net.gini.android.health.sdk.test.ViewModelTestCoroutineRule
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class PaymentComponentPreferencesTest {

@get:Rule
val testCoroutineRule = ViewModelTestCoroutineRule()
private lateinit var context: Context

@Before
fun setup() {
context = ApplicationProvider.getApplicationContext()
}

@Test
fun `sets selected payment provider app`() = runTest {
// Given
val paymentComponentPreferences = PaymentComponentPreferences(context)

assertThat(paymentComponentPreferences.getSelectedPaymentProviderId()).isNull()

// When
paymentComponentPreferences.saveSelectedPaymentProviderId("123")

// Then
assertThat(paymentComponentPreferences.getSelectedPaymentProviderId()).isEqualTo("123")
}

@Test
fun `deletes selected payment provider app`() = runTest {
// Given
val paymentComponentPreferences = PaymentComponentPreferences(context)

assertThat(paymentComponentPreferences.getSelectedPaymentProviderId()).isNull()
paymentComponentPreferences.saveSelectedPaymentProviderId("123")
assertThat(paymentComponentPreferences.getSelectedPaymentProviderId()).isEqualTo("123")

// When
paymentComponentPreferences.deleteSelectedPaymentProviderId()

// Then
assertThat(paymentComponentPreferences.getSelectedPaymentProviderId()).isNull()
}
}
Loading
Loading