aboutsummaryrefslogtreecommitdiff
path: root/core/src/androidTest/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2020-02-23 13:02:25 -0800
committerAllan Wang <me@allanwang.ca>2020-02-23 13:02:25 -0800
commitd67d4a1204796377040a1769175d5798ce09408b (patch)
tree253eda7836828255e65bf1b22436346338c73ac0 /core/src/androidTest/kotlin
parentce0ae639188dad9ca212667c59131c04de1ed575 (diff)
downloadkau-d67d4a1204796377040a1769175d5798ce09408b.tar.gz
kau-d67d4a1204796377040a1769175d5798ce09408b.tar.bz2
kau-d67d4a1204796377040a1769175d5798ce09408b.zip
Fix kpref tests
Diffstat (limited to 'core/src/androidTest/kotlin')
-rw-r--r--core/src/androidTest/kotlin/ca/allanwang/kau/Utils.kt7
-rw-r--r--core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt38
2 files changed, 28 insertions, 17 deletions
diff --git a/core/src/androidTest/kotlin/ca/allanwang/kau/Utils.kt b/core/src/androidTest/kotlin/ca/allanwang/kau/Utils.kt
new file mode 100644
index 0000000..33252d6
--- /dev/null
+++ b/core/src/androidTest/kotlin/ca/allanwang/kau/Utils.kt
@@ -0,0 +1,7 @@
+package ca.allanwang.kau
+
+import android.content.Context
+import androidx.test.platform.app.InstrumentationRegistry
+
+val context: Context
+ get() = InstrumentationRegistry.getInstrumentation().context \ No newline at end of file
diff --git a/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt b/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt
index 04c6444..222f0f5 100644
--- a/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt
+++ b/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt
@@ -16,14 +16,14 @@
package ca.allanwang.kau.kpref
import android.annotation.SuppressLint
-import android.content.Context
-import androidx.test.core.app.ApplicationProvider
+import android.content.SharedPreferences
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
-import kotlin.test.assertEquals
+import ca.allanwang.kau.context
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
+import kotlin.test.assertEquals
/**
* Created by Allan Wang on 2017-08-01.
@@ -33,13 +33,11 @@ import org.junit.runner.RunWith
class KPrefTest {
lateinit var androidPref: TestPref
+ lateinit var androidSp: SharedPreferences
lateinit var memPref: TestPref
- class TestPref(builder: KPrefBuilder) : KPref(builder) {
-
- init {
- initialize(ApplicationProvider.getApplicationContext<Context>(), "kpref_test_${System.currentTimeMillis()}")
- }
+ class TestPref(factory: KPrefFactory) :
+ KPref("kpref_test_${System.currentTimeMillis()}", factory) {
var postSetterCount: Int = 0
@@ -60,9 +58,10 @@ class KPrefTest {
@Before
fun init() {
- androidPref = TestPref(KPrefBuilderAndroid)
- androidPref.sp.edit().clear().commit()
- memPref = TestPref(KPrefBuilderInMemory)
+ androidPref = TestPref(KPrefFactoryAndroid(context))
+ androidSp = (androidPref.builder as KPrefBuilderAndroid).sp
+ androidSp.edit().clear().commit()
+ memPref = TestPref(KPrefFactoryInMemory)
}
private fun pref(action: TestPref.() -> Unit) {
@@ -70,7 +69,11 @@ class KPrefTest {
memPref.action()
}
- private fun <T> assertPrefEquals(expected: T, actual: TestPref.() -> T, message: String? = null) {
+ private fun <T> assertPrefEquals(
+ expected: T,
+ actual: TestPref.() -> T,
+ message: String? = null
+ ) {
assertEquals(expected, androidPref.actual(), "Android KPrefs: $message")
assertEquals(expected, memPref.actual(), "In Mem KPrefs: $message")
}
@@ -83,7 +86,7 @@ class KPrefTest {
assertPrefEquals("hello", { hello })
assertPrefEquals(3, { set.size })
assertPrefEquals(setOf("po", "ta", "to"), { set })
- assertEquals(0, androidPref.sp.all.size, "Defaults should not be set automatically")
+ assertEquals(0, androidSp.all.size, "Defaults should not be set automatically")
}
@Test
@@ -93,8 +96,8 @@ class KPrefTest {
assertPrefEquals(2, { one })
pref { hello = "goodbye" }
assertPrefEquals("goodbye", { hello })
- assertEquals(androidPref.hello, androidPref.sp.getString("hello", "badfallback"))
- assertEquals(2, androidPref.sp.all.size)
+ assertEquals(androidPref.hello, androidSp.getString("hello", "badfallback"))
+ assertEquals(2, androidSp.all.size)
}
@SuppressLint("CommitPrefEdits")
@@ -104,9 +107,10 @@ class KPrefTest {
assertPrefEquals(2, { one })
assertPrefEquals(6, { prefMap.size }, "Prefmap does not have all elements")
pref { reset() } // only invalidates our lazy delegate; doesn't change the actual pref
- assertPrefEquals(2, { one }, "Kpref did not properly fetch from shared prefs")
+ assertEquals(1, memPref.one, "Memory Kpref did not invalidate value")
+ assertEquals(2, androidPref.one, "Android Kpref did not properly fetch from shared prefs")
// Android pref only
- androidPref.sp.edit().putInt("one", -1).commit()
+ androidSp.edit().putInt("one", -1).commit()
assertEquals(2, androidPref.one, "Lazy kpref should still retain old value")
androidPref.reset()
assertEquals(-1, androidPref.one, "Kpref did not refetch from shared prefs upon reset")