1
+ package com.sample.easyprefs
2
+
3
+ import android.content.Context
4
+ import androidx.test.ext.junit.runners.AndroidJUnit4
5
+ import androidx.test.platform.app.InstrumentationRegistry
6
+ import io.easyprefs.Prefs
7
+ import org.junit.Assert.assertEquals
8
+ import org.junit.Assert.assertTrue
9
+ import org.junit.Before
10
+ import org.junit.Test
11
+ import org.junit.runner.RunWith
12
+
13
+ /* *
14
+ * Instrumented test, which will execute on an Android device.
15
+ *
16
+ * See [testing documentation](http://d.android.com/tools/testing).
17
+ */
18
+ @RunWith(AndroidJUnit4 ::class )
19
+ class PrefsContextFileTest {
20
+
21
+ private lateinit var context: Context
22
+
23
+ @Before
24
+ fun initApp () {
25
+ // Prefs.initializeApp()
26
+ context = InstrumentationRegistry .getInstrumentation().targetContext
27
+ }
28
+
29
+ @Test
30
+ fun testString () {
31
+ val value = " Hello..."
32
+ assertTrue(
33
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 ).string(Const .SAMPLE_STRING_KEY , value)
34
+ )
35
+
36
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 ).string(Const .SAMPLE_STRING_KEY , " " )
37
+ assertEquals(value, data)
38
+ }
39
+
40
+ @Test
41
+ fun testStringAsync () {
42
+ val value = " Async..."
43
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 )
44
+ .stringAsync(Const .SAMPLE_STRING_KEY_ASYNC , value)
45
+
46
+ val data =
47
+ Prefs .read(context, Const .PREF_SAMPLE_FILE_2 ).string(Const .SAMPLE_STRING_KEY_ASYNC , " " )
48
+ assertEquals(value, data)
49
+ }
50
+
51
+ @Test
52
+ fun testInt () {
53
+ val value = Int .MAX_VALUE
54
+ assertTrue(Prefs .write(context, Const .PREF_SAMPLE_FILE_2 ).int(Const .SAMPLE_INT_KEY , value))
55
+
56
+ val data =
57
+ Prefs .read(context, Const .PREF_SAMPLE_FILE_2 ).int(Const .SAMPLE_INT_KEY , Int .MIN_VALUE )
58
+ assertEquals(value, data)
59
+ }
60
+
61
+ @Test
62
+ fun testIntAsync () {
63
+ val value = Int .MAX_VALUE
64
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 ).intAsync(Const .SAMPLE_INT_KEY_ASYNC , value)
65
+
66
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 )
67
+ .int(Const .SAMPLE_INT_KEY_ASYNC , Int .MIN_VALUE )
68
+ assertEquals(value, data)
69
+ }
70
+
71
+ @Test
72
+ fun testFloat () {
73
+ val value = Float .MAX_VALUE
74
+ assertTrue(
75
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 ).float(Const .SAMPLE_FLOAT_KEY , value)
76
+ )
77
+
78
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 )
79
+ .float(Const .SAMPLE_FLOAT_KEY , Float .MIN_VALUE )
80
+ assertEquals(value, data)
81
+ }
82
+
83
+ @Test
84
+ fun testFloatAsync () {
85
+ val value = Float .MAX_VALUE
86
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 )
87
+ .floatAsync(Const .SAMPLE_FLOAT_KEY_ASYNC , value)
88
+
89
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 )
90
+ .float(Const .SAMPLE_FLOAT_KEY_ASYNC , Float .MIN_VALUE )
91
+ assertEquals(value, data)
92
+ }
93
+
94
+ @Test
95
+ fun testLong () {
96
+ val value = Long .MAX_VALUE
97
+ assertTrue(
98
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 ).long(Const .SAMPLE_LONG_KEY , value)
99
+ )
100
+
101
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 )
102
+ .long(Const .SAMPLE_LONG_KEY , Long .MIN_VALUE )
103
+ assertEquals(value, data)
104
+ }
105
+
106
+ @Test
107
+ fun testLongAsync () {
108
+ val value = Long .MAX_VALUE
109
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 ).longAsync(Const .SAMPLE_LONG_KEY_ASYNC , value)
110
+
111
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 )
112
+ .long(Const .SAMPLE_LONG_KEY_ASYNC , Long .MIN_VALUE )
113
+ assertEquals(value, data)
114
+ }
115
+
116
+ @Test
117
+ fun testDouble () {
118
+ val value = Double .MAX_VALUE
119
+ assertTrue(
120
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 ).double(Const .SAMPLE_DOUBLE_KEY , value)
121
+ )
122
+
123
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 )
124
+ .double(Const .SAMPLE_DOUBLE_KEY , Double .MIN_VALUE )
125
+ assertEquals(value.toString(), data.toString())
126
+ }
127
+
128
+ @Test
129
+ fun testDoubleAsync () {
130
+ val value = Double .MAX_VALUE
131
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 )
132
+ .doubleAsync(Const .SAMPLE_DOUBLE_KEY_ASYNC , value)
133
+
134
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 )
135
+ .double(Const .SAMPLE_DOUBLE_KEY_ASYNC , Double .MIN_VALUE )
136
+ assertEquals(value.toString(), data.toString())
137
+ }
138
+
139
+ @Test
140
+ fun testBoolean () {
141
+ val value = false
142
+ assertTrue(
143
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 ).boolean(Const .SAMPLE_BOOLEAN_KEY , value)
144
+ )
145
+
146
+ val data =
147
+ Prefs .read(context, Const .PREF_SAMPLE_FILE_2 ).boolean(Const .SAMPLE_BOOLEAN_KEY , true )
148
+ assertEquals(value, data)
149
+ }
150
+
151
+ @Test
152
+ fun testBooleanAsync () {
153
+ val value = false
154
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 )
155
+ .booleanAsync(Const .SAMPLE_BOOLEAN_KEY_ASYNC , value)
156
+
157
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 )
158
+ .boolean(Const .SAMPLE_BOOLEAN_KEY_ASYNC , true )
159
+ assertEquals(value, data)
160
+ }
161
+
162
+ @Test
163
+ fun testStringSet () {
164
+ val value = setOf (" A" , " B" , " C" , " D" )
165
+ assertTrue(
166
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 )
167
+ .stringSet(Const .SAMPLE_STRING_SET_KEY , value)
168
+ )
169
+
170
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 )
171
+ .stringSet(Const .SAMPLE_STRING_SET_KEY , setOf ())
172
+ assertEquals(value, data)
173
+ }
174
+
175
+ @Test
176
+ fun testStringSetAsync () {
177
+ val value = setOf (" A" , " B" , " C" , " D" )
178
+ Prefs .write(context, Const .PREF_SAMPLE_FILE_2 )
179
+ .stringSetAsync(Const .SAMPLE_STRING_SET_KEY_ASYNC , value)
180
+
181
+ val data = Prefs .read(context, Const .PREF_SAMPLE_FILE_2 )
182
+ .stringSet(Const .SAMPLE_STRING_SET_KEY_ASYNC , setOf ())
183
+ assertEquals(value, data)
184
+ }
185
+ }
0 commit comments