aboutsummaryrefslogtreecommitdiff
path: root/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt
blob: 70558dff2e250c98736b46f6fd9db1facc851713 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright 2018 Allan Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ca.allanwang.kau.kpref

import android.annotation.SuppressLint
import android.content.SharedPreferences
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import ca.allanwang.kau.context
import kotlin.test.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

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

    lateinit var androidPref: TestPref
    lateinit var androidSp: SharedPreferences
    lateinit var memPref: TestPref

    class TestPref(factory: KPrefFactory) :
        KPref("kpref_test_${System.currentTimeMillis()}", factory) {

        var postSetterCount: Int = 0

        var one by kpref("one", 1)

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

        var `true` by kpref("true", true, postSetter = {
            postSetterCount++
        })

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

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

        val oneShot by kprefSingle("asdf")
    }

    @Before
    fun init() {
        androidPref = TestPref(KPrefFactoryAndroid(context))
        androidSp = (androidPref.builder as KPrefBuilderAndroid).sp
        androidSp.edit().clear().commit()
        memPref = TestPref(KPrefFactoryInMemory)
    }

    private fun pref(action: TestPref.() -> Unit) {
        androidPref.action()
        memPref.action()
    }

    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")
    }

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

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

    @SuppressLint("CommitPrefEdits")
    @Test
    fun reset() {
        pref { one = 2 }
        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
        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
        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")
    }

    @Test
    fun single() {
        assertPrefEquals(true, { oneShot })
        assertPrefEquals(false, { androidPref.oneShot })
    }

    @Test
    fun postSetter() {
        pref { `true` = true }
        assertPrefEquals(1, { postSetterCount }, "Post setter was not called")
    }
}