-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from XYOracleNetwork/feature/sdk-settings-acco…
…unt-storage sdk settings for account storage
- Loading branch information
Showing
10 changed files
with
348 additions
and
94 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
sdk/src/androidTest/java/network/xyo/client/XyoAccountPrefsRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package network.xyo.client | ||
|
||
import android.content.Context | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import kotlinx.coroutines.runBlocking | ||
import network.xyo.client.datastore.XyoAccountPrefsRepository | ||
import network.xyo.client.settings.AccountPreferences | ||
import network.xyo.client.witness.system.info.XyoSystemInfoWitness | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertNotEquals | ||
|
||
class XyoAccountPrefsRepositoryTest { | ||
|
||
private lateinit var appContext: Context | ||
|
||
private val apiDomainBeta = "${TestConstants.nodeUrlBeta}/Archivist" | ||
|
||
@Before | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
this.appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
} | ||
|
||
@Test | ||
fun testAccountPersistence() { | ||
runBlocking { | ||
val prefsRepository = | ||
XyoAccountPrefsRepository.getInstance(appContext) | ||
prefsRepository.clearSavedAccountKey() | ||
|
||
val panel = XyoPanel( | ||
appContext, arrayListOf(Pair(apiDomainBeta, null)), listOf( | ||
XyoSystemInfoWitness() | ||
) | ||
) | ||
panel.resolveNodes() | ||
val generatedAddress = panel.defaultAccount?.address?.hex | ||
assertNotEquals(generatedAddress, null) | ||
|
||
val panel2 = XyoPanel( | ||
appContext, arrayListOf(Pair(apiDomainBeta, null)), listOf( | ||
XyoSystemInfoWitness() | ||
) | ||
) | ||
panel2.resolveNodes() | ||
val secondGeneratedAddress = panel2.defaultAccount?.address?.hex | ||
assertEquals(generatedAddress, secondGeneratedAddress) | ||
} | ||
} | ||
|
||
@Test | ||
fun testClearingExistingAccount() { | ||
runBlocking { | ||
val instance = XyoAccountPrefsRepository.getInstance(appContext) | ||
val originalAddress = instance.getAccount().private.hex | ||
|
||
instance.clearSavedAccountKey() | ||
|
||
val refreshedAddress = instance.getAccount().private.hex | ||
|
||
assert(originalAddress !== refreshedAddress) | ||
} | ||
} | ||
|
||
@Test | ||
fun testUpdatingAccountPreferences() { | ||
runBlocking { | ||
val instance = XyoAccountPrefsRepository.getInstance(appContext) | ||
val originalAddress = instance.getAccount().private.hex | ||
|
||
open class UpdatedAccountPreferences : AccountPreferences { | ||
override val fileName = "network-xyo-sdk-prefs-1" | ||
override val storagePath = "__xyo-client-sdk-1__" | ||
} | ||
|
||
val updatedAccountPrefs = UpdatedAccountPreferences() | ||
|
||
val refreshedInstance = | ||
XyoAccountPrefsRepository.refresh(appContext, updatedAccountPrefs) | ||
|
||
// Test that accountPreferences are updated | ||
assertEquals( | ||
refreshedInstance.accountPreferences.fileName, | ||
updatedAccountPrefs.fileName | ||
) | ||
assertEquals( | ||
refreshedInstance.accountPreferences.storagePath, | ||
updatedAccountPrefs.storagePath | ||
) | ||
|
||
val refreshedAddress = refreshedInstance.getAccount().private.hex | ||
|
||
assert(originalAddress !== refreshedAddress) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package network.xyo.client | ||
|
||
import android.content.Context | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import kotlinx.coroutines.runBlocking | ||
import network.xyo.client.settings.AccountPreferences | ||
import network.xyo.client.settings.DefaultXyoSdkSettings | ||
import network.xyo.client.settings.XyoSdk | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
|
||
class XyoSdkTest { | ||
|
||
private lateinit var appContext: Context | ||
|
||
@Before | ||
fun resetSingleton() { | ||
XyoSdk.resetInstance() | ||
} | ||
|
||
@Before | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
this.appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
} | ||
|
||
@Test | ||
fun testDefaultSettings() { | ||
runBlocking { | ||
val instance = XyoSdk.getInstance(appContext) | ||
assertEquals(instance.settings.accountPreferences.fileName, DefaultXyoSdkSettings().accountPreferences.fileName) | ||
assertEquals(instance.settings.accountPreferences.storagePath, DefaultXyoSdkSettings().accountPreferences.storagePath) | ||
} | ||
} | ||
|
||
@Test | ||
fun testCustomSettings() { | ||
runBlocking { | ||
class UpdatedAccountPreferences : AccountPreferences { | ||
override val fileName = "network-xyo-sdk-prefs-1" | ||
override val storagePath = "__xyo-client-sdk-1__" | ||
} | ||
class UpdatedSettings: DefaultXyoSdkSettings() { | ||
override val accountPreferences = UpdatedAccountPreferences() | ||
} | ||
val updatedSettings = UpdatedSettings() | ||
val instance = XyoSdk.getInstance(appContext, updatedSettings) | ||
assertEquals(instance.settings.accountPreferences.fileName, updatedSettings.accountPreferences.fileName) | ||
assertEquals(instance.settings.accountPreferences.storagePath, updatedSettings.accountPreferences.storagePath) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 0 additions & 72 deletions
72
sdk/src/main/java/network/xyo/client/datastore/PrefsRepository.kt
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
sdk/src/main/java/network/xyo/client/datastore/XyoAccountDataStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package network.xyo.client.datastore | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.core.MultiProcessDataStoreFactory | ||
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import network.xyo.client.settings.DefaultXyoSdkSettings | ||
import network.xyo.data.PrefsDataStoreProtos.PrefsDataStore | ||
import java.io.File | ||
|
||
val defaults = DefaultXyoSdkSettings() | ||
|
||
fun Context.xyoAccountDataStore(name: String?, path: String?): DataStore<PrefsDataStore> { | ||
val resolvedName = name ?: defaults.accountPreferences.fileName | ||
val resolvedPath = path ?: defaults.accountPreferences.storagePath | ||
|
||
val dataStoreFile = File(filesDir, "$resolvedPath/$resolvedName") | ||
|
||
return MultiProcessDataStoreFactory.create( | ||
serializer = PrefsDataStoreSerializer, | ||
produceFile = { dataStoreFile }, | ||
corruptionHandler = ReplaceFileCorruptionHandler( | ||
produceNewData = { PrefsDataStore.getDefaultInstance() } | ||
), | ||
scope = CoroutineScope(Dispatchers.IO) | ||
) | ||
} |
Oops, something went wrong.