23
23
import org .robolectric .res .builder .RobolectricPackageManager ;
24
24
25
25
import java .util .Arrays ;
26
+ import java .util .Locale ;
26
27
import java .util .TimeZone ;
27
28
28
29
import bolts .Task ;
@@ -46,17 +47,25 @@ public class ParseInstallationTest {
46
47
private static final String KEY_APP_NAME = "appName" ;
47
48
private static final String KEY_APP_IDENTIFIER = "appIdentifier" ;
48
49
private static final String KEY_TIME_ZONE = "timeZone" ;
50
+ private static final String KEY_LOCALE_IDENTIFIER = "localeIdentifier" ;
49
51
private static final String KEY_APP_VERSION = "appVersion" ;
50
52
53
+ private Locale defaultLocale ;
54
+
51
55
@ Before
52
56
public void setUp () {
53
57
ParseObject .registerSubclass (ParseInstallation .class );
58
+
59
+ defaultLocale = Locale .getDefault ();
54
60
}
55
61
56
62
@ After
57
63
public void tearDown () {
58
64
ParseObject .unregisterSubclass (ParseInstallation .class );
59
65
ParseCorePlugins .getInstance ().reset ();
66
+ ParsePlugins .reset ();
67
+
68
+ Locale .setDefault (defaultLocale );
60
69
}
61
70
62
71
@ Test
@@ -71,6 +80,7 @@ public void testImmutableKeys() {
71
80
"deviceTokenLastModified" ,
72
81
"pushType" ,
73
82
"timeZone" ,
83
+ "localeIdentifier" ,
74
84
"appVersion"
75
85
};
76
86
@@ -146,24 +156,9 @@ public void testHandleFetchResultAsync() throws Exception {
146
156
147
157
@ Test
148
158
public void testUpdateBeforeSave () throws Exception {
149
- // Mock currentInstallationController to make setAsync work
150
- ParseCurrentInstallationController controller =
151
- mock (ParseCurrentInstallationController .class );
152
- when (controller .isCurrent (any (ParseInstallation .class ))).thenReturn (true );
153
- ParseCorePlugins .getInstance ().registerCurrentInstallationController (controller );
154
- // Mock package manager
155
- RobolectricPackageManager packageManager =
156
- spy (RuntimeEnvironment .getRobolectricPackageManager ());
157
- doReturn ("parseTest" ).when (packageManager ).getApplicationLabel (any (ApplicationInfo .class ));
158
- RuntimeEnvironment .setRobolectricPackageManager (packageManager );
159
- ParsePlugins .Android plugins = mock (ParsePlugins .Android .class );
160
- // Mock installationId
161
- InstallationId installationId = mock (InstallationId .class );
162
- when (installationId .get ()).thenReturn ("installationId" );
163
- when (plugins .installationId ()).thenReturn (installationId );
164
- // Mock application context
165
- when (plugins .applicationContext ()).thenReturn (RuntimeEnvironment .application );
166
- ParsePlugins .set (plugins );
159
+ mocksForUpdateBeforeSave ();
160
+
161
+ Locale .setDefault (new Locale ("en" , "US" ));
167
162
168
163
ParseInstallation installation = new ParseInstallation ();
169
164
installation .updateBeforeSave ();
@@ -183,7 +178,9 @@ public void testUpdateBeforeSave() throws Exception {
183
178
assertEquals (appVersion , installation .getString (KEY_APP_VERSION ));
184
179
// Make sure we update device info
185
180
assertEquals ("android" , installation .getString (KEY_DEVICE_TYPE ));
186
- assertEquals (installationId .get (), installation .getString (KEY_INSTALLATION_ID ));
181
+ assertEquals ("installationId" , installation .getString (KEY_INSTALLATION_ID ));
182
+ // Make sure we update the locale identifier
183
+ assertEquals ("en-US" , installation .getString (KEY_LOCALE_IDENTIFIER ));
187
184
}
188
185
189
186
// TODO(mengyan): Add other testUpdateBeforeSave cases to cover all branches
@@ -255,6 +252,54 @@ public void testGetCurrentInstallation() throws Exception {
255
252
verify (controller , times (1 )).getAsync ();
256
253
}
257
254
255
+ @ Test
256
+ public void testLocaleIdentifierSpecialCases () throws Exception {
257
+ mocksForUpdateBeforeSave ();
258
+
259
+ ParseInstallation installation = new ParseInstallation ();
260
+
261
+ // Deprecated two-letter codes (Java issue).
262
+ Locale .setDefault (new Locale ("iw" , "US" ));
263
+ installation .updateBeforeSave ();
264
+ assertEquals ("he-US" , installation .getString (KEY_LOCALE_IDENTIFIER ));
265
+
266
+ Locale .setDefault (new Locale ("in" , "US" ));
267
+ installation .updateBeforeSave ();
268
+ assertEquals ("id-US" , installation .getString (KEY_LOCALE_IDENTIFIER ));
269
+
270
+ Locale .setDefault (new Locale ("ji" , "US" ));
271
+ installation .updateBeforeSave ();
272
+ assertEquals ("yi-US" , installation .getString (KEY_LOCALE_IDENTIFIER ));
273
+
274
+ // No country code.
275
+ Locale .setDefault (new Locale ("en" ));
276
+ installation .updateBeforeSave ();
277
+ assertEquals ("en" , installation .getString (KEY_LOCALE_IDENTIFIER ));
278
+ }
279
+
280
+
281
+
258
282
// TODO(mengyan): Add testFetchAsync, right now we can not test super methods inside
259
283
// testFetchAsync
284
+
285
+ private static void mocksForUpdateBeforeSave () {
286
+ // Mock currentInstallationController to make setAsync work
287
+ ParseCurrentInstallationController controller =
288
+ mock (ParseCurrentInstallationController .class );
289
+ when (controller .isCurrent (any (ParseInstallation .class ))).thenReturn (true );
290
+ ParseCorePlugins .getInstance ().registerCurrentInstallationController (controller );
291
+ // Mock package manager
292
+ RobolectricPackageManager packageManager =
293
+ spy (RuntimeEnvironment .getRobolectricPackageManager ());
294
+ doReturn ("parseTest" ).when (packageManager ).getApplicationLabel (any (ApplicationInfo .class ));
295
+ RuntimeEnvironment .setRobolectricPackageManager (packageManager );
296
+ ParsePlugins .Android plugins = mock (ParsePlugins .Android .class );
297
+ // Mock installationId
298
+ InstallationId installationId = mock (InstallationId .class );
299
+ when (installationId .get ()).thenReturn ("installationId" );
300
+ when (plugins .installationId ()).thenReturn (installationId );
301
+ // Mock application context
302
+ when (plugins .applicationContext ()).thenReturn (RuntimeEnvironment .application );
303
+ ParsePlugins .set (plugins );
304
+ }
260
305
}
0 commit comments