1818
1919import com .google .firebase .FirebaseApp ;
2020import com .google .firebase .perf .FirebasePerformanceTestBase ;
21+ import com .google .testing .timing .FakeScheduledExecutorService ;
2122import org .junit .Before ;
2223import org .junit .Test ;
2324import org .junit .runner .RunWith ;
2829public final class DeviceCacheManagerTest extends FirebasePerformanceTestBase {
2930
3031 private DeviceCacheManager deviceCacheManager ;
32+ private FakeScheduledExecutorService fakeScheduledExecutorService ;
3133
3234 @ Before
3335 public void setUp () {
34- deviceCacheManager = DeviceCacheManager .getInstance ();
36+ fakeScheduledExecutorService = new FakeScheduledExecutorService ();
37+ deviceCacheManager = new DeviceCacheManager (fakeScheduledExecutorService );
3538 }
3639
3740 @ Test
3841 public void getBoolean_valueIsNotSet_returnsEmpty () {
3942 deviceCacheManager .setContext (context );
43+ fakeScheduledExecutorService .runAll ();
4044
4145 assertThat (deviceCacheManager .getBoolean ("some_key" ).isAvailable ()).isFalse ();
4246 }
4347
4448 @ Test
4549 public void getBoolean_contextAndValueNotSet_returnsEmpty () {
50+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
4651 assertThat (deviceCacheManager .getBoolean ("some_key" ).isAvailable ()).isFalse ();
4752 }
4853
4954 @ Test
5055 public void getBoolean_valueIsSet_returnsSetValue () {
5156 deviceCacheManager .setContext (context );
57+ fakeScheduledExecutorService .runAll ();
5258 deviceCacheManager .setValue ("some_key" , true );
5359
5460 assertThat (deviceCacheManager .getBoolean ("some_key" ).get ()).isTrue ();
@@ -57,7 +63,7 @@ public void getBoolean_valueIsSet_returnsSetValue() {
5763 @ Test
5864 public void clear_setBooleanThenCleared_returnsEmpty () {
5965 deviceCacheManager .setContext (context );
60-
66+ fakeScheduledExecutorService . runAll ();
6167 deviceCacheManager .setValue ("some_key" , true );
6268
6369 assertThat (deviceCacheManager .getBoolean ("some_key" ).get ()).isTrue ();
@@ -70,14 +76,17 @@ public void clear_setBooleanThenCleared_returnsEmpty() {
7076 public void getBoolean_firebaseAppNotExist_returnsEmpty () {
7177 DeviceCacheManager .clearInstance ();
7278 FirebaseApp .clearInstancesForTest ();
73- deviceCacheManager = DeviceCacheManager . getInstance ( );
79+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
7480 deviceCacheManager .setValue ("some_key" , true );
7581
82+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
7683 assertThat (deviceCacheManager .getBoolean ("some_key" ).isAvailable ()).isFalse ();
7784 }
7885
7986 @ Test
8087 public void setValueBoolean_setTwice_canGetLatestValue () {
88+ deviceCacheManager .setContext (context );
89+ fakeScheduledExecutorService .runAll ();
8190 deviceCacheManager .setValue ("some_key" , true );
8291 assertThat (deviceCacheManager .getBoolean ("some_key" ).get ()).isTrue ();
8392
@@ -86,10 +95,10 @@ public void setValueBoolean_setTwice_canGetLatestValue() {
8695 }
8796
8897 @ Test
89- public void setValueBoolean_contextNotSet_canGetValue () {
98+ public void setValueBoolean_contextNotSet_returnsEmpty () {
9099 deviceCacheManager .setValue ("some_key" , true );
91100
92- assertThat (deviceCacheManager .getBoolean ("some_key" ).get ()).isTrue ();
101+ assertThat (deviceCacheManager .getBoolean ("some_key" ).isAvailable ()).isFalse ();
93102 }
94103
95104 @ Test
@@ -100,6 +109,7 @@ public void setValueBoolean_keyIsNull_returnsFalse() {
100109 @ Test
101110 public void getString_valueIsNotSet_returnsEmpty () {
102111 deviceCacheManager .setContext (context );
112+ fakeScheduledExecutorService .runAll ();
103113
104114 assertThat (deviceCacheManager .getString ("some_key" ).isAvailable ()).isFalse ();
105115 }
@@ -112,23 +122,26 @@ public void getString_contextAndValueNotSet_returnsEmpty() {
112122 @ Test
113123 public void getString_valueIsSet_returnsSetValue () {
114124 deviceCacheManager .setContext (context );
115- deviceCacheManager .setValue ("some_key" , "speicalValue" );
125+ fakeScheduledExecutorService .runAll ();
126+ deviceCacheManager .setValue ("some_key" , "specialValue" );
116127
117- assertThat (deviceCacheManager .getString ("some_key" ).get ()).isEqualTo ("speicalValue " );
128+ assertThat (deviceCacheManager .getString ("some_key" ).get ()).isEqualTo ("specialValue " );
118129 }
119130
120131 @ Test
121132 public void getString_firebaseAppNotExist_returnsEmpty () {
122133 DeviceCacheManager .clearInstance ();
123134 FirebaseApp .clearInstancesForTest ();
124- deviceCacheManager = DeviceCacheManager . getInstance ( );
125- deviceCacheManager .setValue ("some_key" , "speicalValue " );
135+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
136+ deviceCacheManager .setValue ("some_key" , "specialValue " );
126137
127138 assertThat (deviceCacheManager .getString ("some_key" ).isAvailable ()).isFalse ();
128139 }
129140
130141 @ Test
131142 public void setValueString_setTwice_canGetLatestValue () {
143+ deviceCacheManager .setContext (context );
144+ fakeScheduledExecutorService .runAll ();
132145 deviceCacheManager .setValue ("some_key" , "EarliestValue" );
133146 assertThat (deviceCacheManager .getString ("some_key" ).get ()).isEqualTo ("EarliestValue" );
134147
@@ -137,40 +150,47 @@ public void setValueString_setTwice_canGetLatestValue() {
137150 }
138151
139152 @ Test
140- public void setValueString_contextNotSet_canGetValue () {
153+ public void setValueString_contextNotSet_returnsEmpty () {
141154 deviceCacheManager .setValue ("some_key" , "newValue" );
142- assertThat (deviceCacheManager .getString ("some_key" ).get ()).isEqualTo ( "newValue" );
155+ assertThat (deviceCacheManager .getString ("some_key" ).isAvailable ()).isFalse ( );
143156 }
144157
145158 @ Test
146159 public void setValueString_setNullString_returnsEmpty () {
160+ deviceCacheManager .setContext (context );
161+ fakeScheduledExecutorService .runAll ();
147162 deviceCacheManager .setValue ("some_key" , null );
148163 assertThat (deviceCacheManager .getString ("some_key" ).isAvailable ()).isFalse ();
149164 }
150165
151166 @ Test
152167 public void setValueString_keyIsNull_returnsFalse () {
168+ deviceCacheManager .setContext (context );
169+ fakeScheduledExecutorService .runAll ();
153170 assertThat (deviceCacheManager .setValue (null , "value" )).isFalse ();
154171 }
155172
156173 @ Test
157174 public void getFloat_valueIsNotSet_returnsEmpty () {
158175 deviceCacheManager .setContext (context );
176+ fakeScheduledExecutorService .runAll ();
159177
160178 assertThat (deviceCacheManager .getFloat ("some_key" ).isAvailable ()).isFalse ();
161179 }
162180
163181 @ Test
164182 public void getFloat_contextAndValueNotSet_returnsEmpty () {
165183 DeviceCacheManager .clearInstance ();
166- deviceCacheManager = DeviceCacheManager . getInstance ( );
184+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
167185
186+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
168187 assertThat (deviceCacheManager .getFloat ("some_key" ).isAvailable ()).isFalse ();
169188 }
170189
171190 @ Test
172191 public void getFloat_valueIsSet_returnsSetValue () {
173192 deviceCacheManager .setContext (context );
193+ fakeScheduledExecutorService .runAll ();
174194 deviceCacheManager .setValue ("some_key" , 1.2f );
175195
176196 assertThat (deviceCacheManager .getFloat ("some_key" ).get ()).isEqualTo (1.2f );
@@ -180,14 +200,17 @@ public void getFloat_valueIsSet_returnsSetValue() {
180200 public void getFloat_firebaseAppNotExist_returnsEmpty () {
181201 DeviceCacheManager .clearInstance ();
182202 FirebaseApp .clearInstancesForTest ();
183- deviceCacheManager = DeviceCacheManager . getInstance ( );
203+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
184204 deviceCacheManager .setValue ("some_key" , 1.2f );
185205
206+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
186207 assertThat (deviceCacheManager .getFloat ("some_key" ).isAvailable ()).isFalse ();
187208 }
188209
189210 @ Test
190211 public void setValueFloat_setTwice_canGetLatestValue () {
212+ deviceCacheManager .setContext (context );
213+ fakeScheduledExecutorService .runAll ();
191214 deviceCacheManager .setValue ("some_key" , 1.01f );
192215 assertThat (deviceCacheManager .getFloat ("some_key" ).get ()).isEqualTo (1.01f );
193216
@@ -196,36 +219,42 @@ public void setValueFloat_setTwice_canGetLatestValue() {
196219 }
197220
198221 @ Test
199- public void setValueFloat_contextNotSet_canGetValue () {
222+ public void setValueFloat_contextNotSet_returnsEmpty () {
200223 deviceCacheManager .setValue ("some_key" , 100.0f );
201- assertThat (deviceCacheManager .getFloat ("some_key" ).get ()).isEqualTo ( 100.0f );
224+ assertThat (deviceCacheManager .getFloat ("some_key" ).isAvailable ()).isFalse ( );
202225 }
203226
204227 @ Test
205228 public void setValueFloat_keyIsNull_returnsFalse () {
229+ deviceCacheManager .setContext (context );
230+ fakeScheduledExecutorService .runAll ();
206231 assertThat (deviceCacheManager .setValue (null , 10.0f )).isFalse ();
207232 }
208233
209234 @ Test
210235 public void getLong_valueIsNotSet_returnsEmpty () {
211236 DeviceCacheManager .clearInstance ();
212- deviceCacheManager = DeviceCacheManager . getInstance ( );
237+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
213238 deviceCacheManager .setContext (context );
239+ fakeScheduledExecutorService .runAll ();
214240
241+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
215242 assertThat (deviceCacheManager .getLong ("some_key" ).isAvailable ()).isFalse ();
216243 }
217244
218245 @ Test
219246 public void getLong_contextAndValueNotSet_returnsEmpty () {
220247 DeviceCacheManager .clearInstance ();
221- deviceCacheManager = DeviceCacheManager . getInstance ( );
248+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
222249
250+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
223251 assertThat (deviceCacheManager .getLong ("some_key" ).isAvailable ()).isFalse ();
224252 }
225253
226254 @ Test
227255 public void getLong_valueIsSet_returnsSetValue () {
228256 deviceCacheManager .setContext (context );
257+ fakeScheduledExecutorService .runAll ();
229258 deviceCacheManager .setValue ("some_key" , 1L );
230259
231260 assertThat (deviceCacheManager .getLong ("some_key" ).get ()).isEqualTo (1L );
@@ -235,14 +264,16 @@ public void getLong_valueIsSet_returnsSetValue() {
235264 public void getLong_firebaseAppNotExist_returnsEmpty () {
236265 DeviceCacheManager .clearInstance ();
237266 FirebaseApp .clearInstancesForTest ();
238- deviceCacheManager = DeviceCacheManager . getInstance ( );
267+ deviceCacheManager = new DeviceCacheManager ( fakeScheduledExecutorService );
239268 deviceCacheManager .setValue ("some_key" , 1L );
240269
241270 assertThat (deviceCacheManager .getLong ("some_key" ).isAvailable ()).isFalse ();
242271 }
243272
244273 @ Test
245274 public void setValueLong_setTwice_canGetLatestValue () {
275+ deviceCacheManager .setContext (context );
276+ fakeScheduledExecutorService .runAll ();
246277 deviceCacheManager .setValue ("some_key" , 2L );
247278 assertThat (deviceCacheManager .getLong ("some_key" ).get ()).isEqualTo (2L );
248279
@@ -251,13 +282,16 @@ public void setValueLong_setTwice_canGetLatestValue() {
251282 }
252283
253284 @ Test
254- public void setValueLong_contextNotSet_canGetValue () {
285+ public void setValueLong_contextNotSet_returnsEmpty () {
255286 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 ();
257289 }
258290
259291 @ Test
260292 public void setValueLong_keyIsNull_returnsFalse () {
293+ deviceCacheManager .setContext (context );
294+ fakeScheduledExecutorService .runAll ();
261295 assertThat (deviceCacheManager .setValue (null , 10.0f )).isFalse ();
262296 }
263297}
0 commit comments