aboutsummaryrefslogtreecommitdiff
path: root/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt
blob: 2a9263a9c18ac48cef568fa29895f9c75e737a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package ca.allanwang.kau.kpref

import android.annotation.SuppressLint
import android.support.test.InstrumentationRegistry
import android.support.test.filters.MediumTest
import android.support.test.runner.AndroidJUnit4
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
 * Created by Allan Wang on 2017-08-01.
 */
@RunWith(AndroidJUnit4::class)
@MediumTest
class KPrefTest {

    lateinit var pref: TestPref

    class TestPref : KPref() {

        init {
            initialize(InstrumentationRegistry.getTargetContext(), "kpref_test_${System.currentTimeMillis()}")
        }

        var one by kpref("one", 1)

        var two by kpref("two", 2f)

        var `true` by kpref("true", true)

        var hello by kpref("hello", "hello")

        var set by kpref("set", setOf("po", "ta", "to"))

        val oneShot by kprefSingle("asdf")
    }

    @Before
    fun init() {
        pref = TestPref()
        pref.sp.edit().clear().commit()
    }

    @Test
    fun getDefaults() {
        assertEquals(1, pref.one)
        assertEquals(2f, pref.two)
        assertEquals(true, pref.`true`)
        assertEquals("hello", pref.hello)
        assertEquals(3, pref.set.size)
        assertTrue(pref.set.contains("po"))
        assertTrue(pref.set.contains("ta"))
        assertTrue(pref.set.contains("to"))
        assertEquals(0, pref.sp.all.size, "Defaults should not be set automatically")
    }

    @Test
    fun setter() {
        assertEquals(1, pref.one)
        pref.one = 2
        assertEquals(2, pref.one)
        pref.hello = "goodbye"
        assertEquals("goodbye", pref.hello)
        assertEquals(pref.hello, pref.sp.getString("hello", "hello"))
        assertEquals(2, pref.sp.all.size)
    }

    @SuppressLint("CommitPrefEdits")
    @Test
    fun reset() {
        pref.one = 2
        assertEquals(2, pref.one)
        assertEquals(6, pref.prefMap.size, "Prefmap does not have all elements")
        pref.reset() //only invalidates our lazy delegate; doesn't change the actual pref
        assertEquals(2, pref.one, "Kpref did not properly fetch from shared prefs")
        pref.sp.edit().putInt("one", -1).commit()
        assertEquals(2, pref.one, "Lazy kpref should still retain old value")
        pref.reset()
        assertEquals(-1, pref.one, "Kpref did not refetch from shared prefs upon reset")
    }


    @Test
    fun single() {
        assertTrue(pref.oneShot)
        assertFalse(pref.oneShot)
    }

}