18
18
19
19
import com .google .firebase .FirebaseApp ;
20
20
import com .google .firebase .perf .FirebasePerformanceTestBase ;
21
+ import com .google .testing .timing .FakeScheduledExecutorService ;
21
22
import org .junit .Before ;
22
23
import org .junit .Test ;
23
24
import org .junit .runner .RunWith ;
28
29
public final class DeviceCacheManagerTest extends FirebasePerformanceTestBase {
29
30
30
31
private DeviceCacheManager deviceCacheManager ;
32
+ private FakeScheduledExecutorService fakeScheduledExecutorService ;
31
33
32
34
@ Before
33
35
public void setUp () {
34
- deviceCacheManager = DeviceCacheManager .getInstance ();
36
+ fakeScheduledExecutorService = new FakeScheduledExecutorService ();
37
+ deviceCacheManager = new DeviceCacheManager (fakeScheduledExecutorService );
35
38
}
36
39
37
40
@ Test
38
41
public void getBoolean_valueIsNotSet_returnsEmpty () {
39
42
deviceCacheManager .setContext (context );
43
+ fakeScheduledExecutorService .runAll ();
40
44
41
45
assertThat (deviceCacheManager .getBoolean ("some_key" ).isAvailable ()).isFalse ();
42
46
}
43
47
44
48
@ Test
45
49
public void getBoolean_contextAndValueNotSet_returnsEmpty () {
50
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
46
51
assertThat (deviceCacheManager .getBoolean ("some_key" ).isAvailable ()).isFalse ();
47
52
}
48
53
49
54
@ Test
50
55
public void getBoolean_valueIsSet_returnsSetValue () {
51
56
deviceCacheManager .setContext (context );
57
+ fakeScheduledExecutorService .runAll ();
52
58
deviceCacheManager .setValue ("some_key" , true );
53
59
54
60
assertThat (deviceCacheManager .getBoolean ("some_key" ).get ()).isTrue ();
@@ -57,7 +63,7 @@ public void getBoolean_valueIsSet_returnsSetValue() {
57
63
@ Test
58
64
public void clear_setBooleanThenCleared_returnsEmpty () {
59
65
deviceCacheManager .setContext (context );
60
-
66
+ fakeScheduledExecutorService . runAll ();
61
67
deviceCacheManager .setValue ("some_key" , true );
62
68
63
69
assertThat (deviceCacheManager .getBoolean ("some_key" ).get ()).isTrue ();
@@ -70,14 +76,17 @@ public void clear_setBooleanThenCleared_returnsEmpty() {
70
76
public void getBoolean_firebaseAppNotExist_returnsEmpty () {
71
77
DeviceCacheManager .clearInstance ();
72
78
FirebaseApp .clearInstancesForTest ();
73
- deviceCacheManager = DeviceCacheManager . getInstance ( );
79
+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
74
80
deviceCacheManager .setValue ("some_key" , true );
75
81
82
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
76
83
assertThat (deviceCacheManager .getBoolean ("some_key" ).isAvailable ()).isFalse ();
77
84
}
78
85
79
86
@ Test
80
87
public void setValueBoolean_setTwice_canGetLatestValue () {
88
+ deviceCacheManager .setContext (context );
89
+ fakeScheduledExecutorService .runAll ();
81
90
deviceCacheManager .setValue ("some_key" , true );
82
91
assertThat (deviceCacheManager .getBoolean ("some_key" ).get ()).isTrue ();
83
92
@@ -86,10 +95,10 @@ public void setValueBoolean_setTwice_canGetLatestValue() {
86
95
}
87
96
88
97
@ Test
89
- public void setValueBoolean_contextNotSet_canGetValue () {
98
+ public void setValueBoolean_contextNotSet_returnsEmpty () {
90
99
deviceCacheManager .setValue ("some_key" , true );
91
100
92
- assertThat (deviceCacheManager .getBoolean ("some_key" ).get ()).isTrue ();
101
+ assertThat (deviceCacheManager .getBoolean ("some_key" ).isAvailable ()).isFalse ();
93
102
}
94
103
95
104
@ Test
@@ -100,6 +109,7 @@ public void setValueBoolean_keyIsNull_returnsFalse() {
100
109
@ Test
101
110
public void getString_valueIsNotSet_returnsEmpty () {
102
111
deviceCacheManager .setContext (context );
112
+ fakeScheduledExecutorService .runAll ();
103
113
104
114
assertThat (deviceCacheManager .getString ("some_key" ).isAvailable ()).isFalse ();
105
115
}
@@ -112,23 +122,26 @@ public void getString_contextAndValueNotSet_returnsEmpty() {
112
122
@ Test
113
123
public void getString_valueIsSet_returnsSetValue () {
114
124
deviceCacheManager .setContext (context );
115
- deviceCacheManager .setValue ("some_key" , "speicalValue" );
125
+ fakeScheduledExecutorService .runAll ();
126
+ deviceCacheManager .setValue ("some_key" , "specialValue" );
116
127
117
- assertThat (deviceCacheManager .getString ("some_key" ).get ()).isEqualTo ("speicalValue " );
128
+ assertThat (deviceCacheManager .getString ("some_key" ).get ()).isEqualTo ("specialValue " );
118
129
}
119
130
120
131
@ Test
121
132
public void getString_firebaseAppNotExist_returnsEmpty () {
122
133
DeviceCacheManager .clearInstance ();
123
134
FirebaseApp .clearInstancesForTest ();
124
- deviceCacheManager = DeviceCacheManager . getInstance ( );
125
- deviceCacheManager .setValue ("some_key" , "speicalValue " );
135
+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
136
+ deviceCacheManager .setValue ("some_key" , "specialValue " );
126
137
127
138
assertThat (deviceCacheManager .getString ("some_key" ).isAvailable ()).isFalse ();
128
139
}
129
140
130
141
@ Test
131
142
public void setValueString_setTwice_canGetLatestValue () {
143
+ deviceCacheManager .setContext (context );
144
+ fakeScheduledExecutorService .runAll ();
132
145
deviceCacheManager .setValue ("some_key" , "EarliestValue" );
133
146
assertThat (deviceCacheManager .getString ("some_key" ).get ()).isEqualTo ("EarliestValue" );
134
147
@@ -137,40 +150,47 @@ public void setValueString_setTwice_canGetLatestValue() {
137
150
}
138
151
139
152
@ Test
140
- public void setValueString_contextNotSet_canGetValue () {
153
+ public void setValueString_contextNotSet_returnsEmpty () {
141
154
deviceCacheManager .setValue ("some_key" , "newValue" );
142
- assertThat (deviceCacheManager .getString ("some_key" ).get ()).isEqualTo ( "newValue" );
155
+ assertThat (deviceCacheManager .getString ("some_key" ).isAvailable ()).isFalse ( );
143
156
}
144
157
145
158
@ Test
146
159
public void setValueString_setNullString_returnsEmpty () {
160
+ deviceCacheManager .setContext (context );
161
+ fakeScheduledExecutorService .runAll ();
147
162
deviceCacheManager .setValue ("some_key" , null );
148
163
assertThat (deviceCacheManager .getString ("some_key" ).isAvailable ()).isFalse ();
149
164
}
150
165
151
166
@ Test
152
167
public void setValueString_keyIsNull_returnsFalse () {
168
+ deviceCacheManager .setContext (context );
169
+ fakeScheduledExecutorService .runAll ();
153
170
assertThat (deviceCacheManager .setValue (null , "value" )).isFalse ();
154
171
}
155
172
156
173
@ Test
157
174
public void getFloat_valueIsNotSet_returnsEmpty () {
158
175
deviceCacheManager .setContext (context );
176
+ fakeScheduledExecutorService .runAll ();
159
177
160
178
assertThat (deviceCacheManager .getFloat ("some_key" ).isAvailable ()).isFalse ();
161
179
}
162
180
163
181
@ Test
164
182
public void getFloat_contextAndValueNotSet_returnsEmpty () {
165
183
DeviceCacheManager .clearInstance ();
166
- deviceCacheManager = DeviceCacheManager . getInstance ( );
184
+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
167
185
186
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
168
187
assertThat (deviceCacheManager .getFloat ("some_key" ).isAvailable ()).isFalse ();
169
188
}
170
189
171
190
@ Test
172
191
public void getFloat_valueIsSet_returnsSetValue () {
173
192
deviceCacheManager .setContext (context );
193
+ fakeScheduledExecutorService .runAll ();
174
194
deviceCacheManager .setValue ("some_key" , 1.2f );
175
195
176
196
assertThat (deviceCacheManager .getFloat ("some_key" ).get ()).isEqualTo (1.2f );
@@ -180,14 +200,17 @@ public void getFloat_valueIsSet_returnsSetValue() {
180
200
public void getFloat_firebaseAppNotExist_returnsEmpty () {
181
201
DeviceCacheManager .clearInstance ();
182
202
FirebaseApp .clearInstancesForTest ();
183
- deviceCacheManager = DeviceCacheManager . getInstance ( );
203
+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
184
204
deviceCacheManager .setValue ("some_key" , 1.2f );
185
205
206
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
186
207
assertThat (deviceCacheManager .getFloat ("some_key" ).isAvailable ()).isFalse ();
187
208
}
188
209
189
210
@ Test
190
211
public void setValueFloat_setTwice_canGetLatestValue () {
212
+ deviceCacheManager .setContext (context );
213
+ fakeScheduledExecutorService .runAll ();
191
214
deviceCacheManager .setValue ("some_key" , 1.01f );
192
215
assertThat (deviceCacheManager .getFloat ("some_key" ).get ()).isEqualTo (1.01f );
193
216
@@ -196,36 +219,42 @@ public void setValueFloat_setTwice_canGetLatestValue() {
196
219
}
197
220
198
221
@ Test
199
- public void setValueFloat_contextNotSet_canGetValue () {
222
+ public void setValueFloat_contextNotSet_returnsEmpty () {
200
223
deviceCacheManager .setValue ("some_key" , 100.0f );
201
- assertThat (deviceCacheManager .getFloat ("some_key" ).get ()).isEqualTo ( 100.0f );
224
+ assertThat (deviceCacheManager .getFloat ("some_key" ).isAvailable ()).isFalse ( );
202
225
}
203
226
204
227
@ Test
205
228
public void setValueFloat_keyIsNull_returnsFalse () {
229
+ deviceCacheManager .setContext (context );
230
+ fakeScheduledExecutorService .runAll ();
206
231
assertThat (deviceCacheManager .setValue (null , 10.0f )).isFalse ();
207
232
}
208
233
209
234
@ Test
210
235
public void getLong_valueIsNotSet_returnsEmpty () {
211
236
DeviceCacheManager .clearInstance ();
212
- deviceCacheManager = DeviceCacheManager . getInstance ( );
237
+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
213
238
deviceCacheManager .setContext (context );
239
+ fakeScheduledExecutorService .runAll ();
214
240
241
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
215
242
assertThat (deviceCacheManager .getLong ("some_key" ).isAvailable ()).isFalse ();
216
243
}
217
244
218
245
@ Test
219
246
public void getLong_contextAndValueNotSet_returnsEmpty () {
220
247
DeviceCacheManager .clearInstance ();
221
- deviceCacheManager = DeviceCacheManager . getInstance ( );
248
+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
222
249
250
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
223
251
assertThat (deviceCacheManager .getLong ("some_key" ).isAvailable ()).isFalse ();
224
252
}
225
253
226
254
@ Test
227
255
public void getLong_valueIsSet_returnsSetValue () {
228
256
deviceCacheManager .setContext (context );
257
+ fakeScheduledExecutorService .runAll ();
229
258
deviceCacheManager .setValue ("some_key" , 1L );
230
259
231
260
assertThat (deviceCacheManager .getLong ("some_key" ).get ()).isEqualTo (1L );
@@ -235,14 +264,16 @@ public void getLong_valueIsSet_returnsSetValue() {
235
264
public void getLong_firebaseAppNotExist_returnsEmpty () {
236
265
DeviceCacheManager .clearInstance ();
237
266
FirebaseApp .clearInstancesForTest ();
238
- deviceCacheManager = DeviceCacheManager . getInstance ( );
267
+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
239
268
deviceCacheManager .setValue ("some_key" , 1L );
240
269
241
270
assertThat (deviceCacheManager .getLong ("some_key" ).isAvailable ()).isFalse ();
242
271
}
243
272
244
273
@ Test
245
274
public void setValueLong_setTwice_canGetLatestValue () {
275
+ deviceCacheManager .setContext (context );
276
+ fakeScheduledExecutorService .runAll ();
246
277
deviceCacheManager .setValue ("some_key" , 2L );
247
278
assertThat (deviceCacheManager .getLong ("some_key" ).get ()).isEqualTo (2L );
248
279
@@ -251,13 +282,16 @@ public void setValueLong_setTwice_canGetLatestValue() {
251
282
}
252
283
253
284
@ Test
254
- public void setValueLong_contextNotSet_canGetValue () {
285
+ public void setValueLong_contextNotSet_returnsEmpty () {
255
286
deviceCacheManager .setValue ("some_key" , 100L );
256
- assertThat (deviceCacheManager .getLong ("some_key" ).get ()).isEqualTo (100L );
287
+ // The key is not set if the shared preference is not fetched and available.
288
+ assertThat (deviceCacheManager .getLong ("some_key" ).isAvailable ()).isFalse ();
257
289
}
258
290
259
291
@ Test
260
292
public void setValueLong_keyIsNull_returnsFalse () {
293
+ deviceCacheManager .setContext (context );
294
+ fakeScheduledExecutorService .runAll ();
261
295
assertThat (deviceCacheManager .setValue (null , 10.0f )).isFalse ();
262
296
}
263
297
}
0 commit comments