mirror of
https://github.com/hmalik144/EasyCC_Master.git
synced 2026-01-31 02:41:47 +00:00
@@ -5,16 +5,12 @@ import com.appttude.h_mal.easycc.models.CurrencyModelInterface
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class CurrencyResponse(
|
||||
|
||||
@field:SerializedName("date")
|
||||
val date: String? = null,
|
||||
|
||||
@field:SerializedName("amount")
|
||||
val amount: Double? = null,
|
||||
|
||||
@field:SerializedName("rates")
|
||||
var rates: Map<String, Double>? = null,
|
||||
|
||||
@field:SerializedName("base")
|
||||
val base: String? = null
|
||||
) : CurrencyModelInterface {
|
||||
|
||||
@@ -19,7 +19,6 @@ class PreferenceProvider @Inject constructor(@ApplicationContext context: Contex
|
||||
|
||||
private val appContext = context.applicationContext
|
||||
|
||||
// Instance of Shared preferences
|
||||
private val preference: SharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(appContext)
|
||||
|
||||
@@ -28,7 +27,6 @@ class PreferenceProvider @Inject constructor(@ApplicationContext context: Contex
|
||||
context.resources.getStringArray(R.array.currency_arrays)[0]
|
||||
}
|
||||
|
||||
// Save currency pairs into prefs
|
||||
fun saveConversionPair(s1: String, s2: String) {
|
||||
preference.edit()
|
||||
.putString(CURRENCY_ONE, s1)
|
||||
@@ -36,8 +34,6 @@ class PreferenceProvider @Inject constructor(@ApplicationContext context: Contex
|
||||
.apply()
|
||||
}
|
||||
|
||||
// Retrieve Currency pairs from prefs
|
||||
// Returns Pairs
|
||||
fun getConversionPair(): Pair<String?, String?> {
|
||||
val fromString = getConversionString(CURRENCY_ONE)
|
||||
val toString = getConversionString(CURRENCY_TWO)
|
||||
@@ -45,18 +41,15 @@ class PreferenceProvider @Inject constructor(@ApplicationContext context: Contex
|
||||
return Pair(fromString, toString)
|
||||
}
|
||||
|
||||
|
||||
private fun getConversionString(conversionName: String): String? {
|
||||
return preference
|
||||
.getString(conversionName, defaultRate)
|
||||
}
|
||||
|
||||
// Save currency pairs for widget
|
||||
fun saveWidgetConversionPair(
|
||||
fromString: String,
|
||||
toString: String, appWidgetId: Int
|
||||
) {
|
||||
|
||||
preference.edit()
|
||||
.putString("${appWidgetId}_$CURRENCY_ONE", fromString)
|
||||
.putString("${appWidgetId}_$CURRENCY_TWO", toString)
|
||||
|
||||
@@ -10,9 +10,9 @@ class WidgetHelper @Inject constructor(
|
||||
val repository: Repository
|
||||
) {
|
||||
|
||||
suspend fun getWidgetData(): CurrencyModel? {
|
||||
suspend fun getWidgetData(appWidgetId: Int): CurrencyModel? {
|
||||
try {
|
||||
val pair = repository.getConversionPair()
|
||||
val pair = repository.getWidgetConversionPairs(appWidgetId)
|
||||
val s1 = pair.first?.trimToThree() ?: return null
|
||||
val s2 = pair.second?.trimToThree() ?: return null
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ import javax.inject.Inject
|
||||
@AndroidEntryPoint
|
||||
class WidgetServiceIntent : JobIntentService() {
|
||||
|
||||
//DI with kodein to use in CurrencyAppWidgetKotlin
|
||||
@Inject
|
||||
lateinit var helper: WidgetHelper
|
||||
|
||||
@@ -45,7 +44,7 @@ class WidgetServiceIntent : JobIntentService() {
|
||||
val views = RemoteViews(context.packageName, R.layout.currency_app_widget)
|
||||
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
val exchangeResponse = helper.getWidgetData()
|
||||
val exchangeResponse = helper.getWidgetData(appWidgetId)
|
||||
|
||||
exchangeResponse?.let {
|
||||
val titleString = "${it.from}${it.to}"
|
||||
@@ -63,7 +62,7 @@ class WidgetServiceIntent : JobIntentService() {
|
||||
val configPendingIntent =
|
||||
PendingIntent.getActivity(
|
||||
context, appWidgetId, clickIntentTemplate,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
|
||||
FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
|
||||
)
|
||||
views.setOnClickPendingIntent(R.id.widget_view, configPendingIntent)
|
||||
}
|
||||
|
||||
@@ -79,8 +79,7 @@ class RepositoryNetworkTest {
|
||||
val ioExceptionReturned = assertFailsWith<IOException> {
|
||||
repository.getDataFromApi(s1, s2)
|
||||
}
|
||||
assertNotNull(ioExceptionReturned)
|
||||
assertNotNull(ioExceptionReturned.message)
|
||||
assertEquals(ioExceptionReturned.message, "Error Code: 0")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import com.appttude.h_mal.easycc.data.repository.Repository
|
||||
import com.appttude.h_mal.easycc.helper.CurrencyDataHelper
|
||||
import com.appttude.h_mal.easycc.utils.MainCoroutineRule
|
||||
import com.appttude.h_mal.easycc.utils.observeOnce
|
||||
import com.nhaarman.mockitokotlin2.doAnswer
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.*
|
||||
@@ -18,7 +19,9 @@ import org.mockito.Mock
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.mock
|
||||
import org.mockito.MockitoAnnotations
|
||||
import java.io.IOException
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class MainViewModelTest {
|
||||
|
||||
// Run tasks synchronously
|
||||
@@ -70,24 +73,25 @@ class MainViewModelTest {
|
||||
@Test
|
||||
fun initiate_invalidBundleValues_successfulResponse() = runBlocking {
|
||||
//GIVEN
|
||||
val currencyOne = "AUD - Australian Dollar"
|
||||
val currencyTwo = "GBP - British Pound"
|
||||
val pair = Pair(currencyOne, currencyTwo)
|
||||
val responseObject = mock(ResponseObject::class.java)
|
||||
val currencyOne = "corrupted data"
|
||||
val currencyTwo = "corrupted data again"
|
||||
val bundle = mock(Bundle()::class.java)
|
||||
val error = "Corrupted data found"
|
||||
|
||||
//WHEN
|
||||
Mockito.`when`(repository.getConversionPair()).thenReturn(pair)
|
||||
Mockito.`when`(repository.getDataFromApi(currencyOne, currencyTwo))
|
||||
.thenReturn(responseObject)
|
||||
Mockito.`when`(bundle.getString("parse_1")).thenReturn(currencyOne)
|
||||
Mockito.`when`(bundle.getString("parse_2")).thenReturn(currencyTwo)
|
||||
Mockito.`when`(helper.getDataFromApi(currencyOne, currencyTwo))
|
||||
.doAnswer { throw IOException(error) }
|
||||
|
||||
//THEN
|
||||
viewModel.initiate(null)
|
||||
viewModel.initiate(bundle)
|
||||
viewModel.operationStartedListener.observeOnce {
|
||||
assertEquals(true, it)
|
||||
}
|
||||
viewModel.operationFinishedListener.observeOnce {
|
||||
assertEquals(true, it.first)
|
||||
assertNull(it.second)
|
||||
assertEquals(false, it.first)
|
||||
assertEquals(it.second, error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user