2323import org .robolectric .res .builder .RobolectricPackageManager ;
2424
2525import java .util .Arrays ;
26+ import java .util .Locale ;
2627import java .util .TimeZone ;
2728
2829import bolts .Task ;
@@ -46,17 +47,25 @@ public class ParseInstallationTest {
4647 private static final String KEY_APP_NAME = "appName" ;
4748 private static final String KEY_APP_IDENTIFIER = "appIdentifier" ;
4849 private static final String KEY_TIME_ZONE = "timeZone" ;
50+ private static final String KEY_LOCALE_IDENTIFIER = "localeIdentifier" ;
4951 private static final String KEY_APP_VERSION = "appVersion" ;
5052
53+ private Locale defaultLocale ;
54+
5155 @ Before
5256 public void setUp () {
5357 ParseObject .registerSubclass (ParseInstallation .class );
58+
59+ defaultLocale = Locale .getDefault ();
5460 }
5561
5662 @ After
5763 public void tearDown () {
5864 ParseObject .unregisterSubclass (ParseInstallation .class );
5965 ParseCorePlugins .getInstance ().reset ();
66+ ParsePlugins .reset ();
67+
68+ Locale .setDefault (defaultLocale );
6069 }
6170
6271 @ Test
@@ -71,6 +80,7 @@ public void testImmutableKeys() {
7180 "deviceTokenLastModified" ,
7281 "pushType" ,
7382 "timeZone" ,
83+ "localeIdentifier" ,
7484 "appVersion"
7585 };
7686
@@ -146,24 +156,9 @@ public void testHandleFetchResultAsync() throws Exception {
146156
147157 @ Test
148158 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" ));
167162
168163 ParseInstallation installation = new ParseInstallation ();
169164 installation .updateBeforeSave ();
@@ -183,7 +178,9 @@ public void testUpdateBeforeSave() throws Exception {
183178 assertEquals (appVersion , installation .getString (KEY_APP_VERSION ));
184179 // Make sure we update device info
185180 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 ));
187184 }
188185
189186 // TODO(mengyan): Add other testUpdateBeforeSave cases to cover all branches
@@ -255,6 +252,54 @@ public void testGetCurrentInstallation() throws Exception {
255252 verify (controller , times (1 )).getAsync ();
256253 }
257254
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+
258282 // TODO(mengyan): Add testFetchAsync, right now we can not test super methods inside
259283 // 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+ }
260305}
0 commit comments