-
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.
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package network.xyo.client.settings | ||
|
||
import android.content.Context | ||
import network.xyo.client.datastore.XyoAccountPrefsRepository | ||
|
||
class XyoSdk(val settings: SettingsInterface) { | ||
companion object { | ||
@Volatile | ||
private var INSTANCE: XyoSdk? = null | ||
|
||
fun getInstance(context: Context, settings: SettingsInterface = DefaultXyoSdkSettings()): XyoSdk { | ||
// Initialize the singleton with the users accountPreferences | ||
XyoAccountPrefsRepository.getInstance(context, settings.accountPreferences) | ||
|
||
val newInstance = INSTANCE ?: synchronized(this) { | ||
INSTANCE ?: XyoSdk(settings).also { INSTANCE = it } | ||
} | ||
return newInstance | ||
} | ||
|
||
fun refresh(context: Context, settings: SettingsInterface = DefaultXyoSdkSettings()): XyoSdk { | ||
synchronized(this) { | ||
// Initialize the singleton with the users accountPreferences | ||
XyoAccountPrefsRepository.getInstance(context, settings.accountPreferences) | ||
INSTANCE = XyoSdk(settings) | ||
} | ||
return INSTANCE!! | ||
} | ||
|
||
fun resetInstance() { | ||
INSTANCE = null | ||
} | ||
} | ||
} |