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,6 +47,7 @@ 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
51
53
@ Before
@@ -57,6 +59,28 @@ public void setUp() {
57
59
public void tearDown () {
58
60
ParseObject .unregisterSubclass (ParseInstallation .class );
59
61
ParseCorePlugins .getInstance ().reset ();
62
+ ParsePlugins .reset ();
63
+ }
64
+
65
+ private void mocksForUpdateBeforeSave () {
66
+ // Mock currentInstallationController to make setAsync work
67
+ ParseCurrentInstallationController controller =
68
+ mock (ParseCurrentInstallationController .class );
69
+ when (controller .isCurrent (any (ParseInstallation .class ))).thenReturn (true );
70
+ ParseCorePlugins .getInstance ().registerCurrentInstallationController (controller );
71
+ // Mock package manager
72
+ RobolectricPackageManager packageManager =
73
+ spy (RuntimeEnvironment .getRobolectricPackageManager ());
74
+ doReturn ("parseTest" ).when (packageManager ).getApplicationLabel (any (ApplicationInfo .class ));
75
+ RuntimeEnvironment .setRobolectricPackageManager (packageManager );
76
+ ParsePlugins .Android plugins = mock (ParsePlugins .Android .class );
77
+ // Mock installationId
78
+ InstallationId installationId = mock (InstallationId .class );
79
+ when (installationId .get ()).thenReturn ("installationId" );
80
+ when (plugins .installationId ()).thenReturn (installationId );
81
+ // Mock application context
82
+ when (plugins .applicationContext ()).thenReturn (RuntimeEnvironment .application );
83
+ ParsePlugins .set (plugins );
60
84
}
61
85
62
86
@ Test
@@ -71,6 +95,7 @@ public void testImmutableKeys() {
71
95
"deviceTokenLastModified" ,
72
96
"pushType" ,
73
97
"timeZone" ,
98
+ "localeIdentifier" ,
74
99
"appVersion"
75
100
};
76
101
@@ -146,24 +171,9 @@ public void testHandleFetchResultAsync() throws Exception {
146
171
147
172
@ Test
148
173
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 );
174
+ mocksForUpdateBeforeSave ();
175
+
176
+ Locale .setDefault (new Locale ("en" , "US" ));
167
177
168
178
ParseInstallation installation = new ParseInstallation ();
169
179
installation .updateBeforeSave ();
@@ -183,7 +193,9 @@ public void testUpdateBeforeSave() throws Exception {
183
193
assertEquals (appVersion , installation .getString (KEY_APP_VERSION ));
184
194
// Make sure we update device info
185
195
assertEquals ("android" , installation .getString (KEY_DEVICE_TYPE ));
186
- assertEquals (installationId .get (), installation .getString (KEY_INSTALLATION_ID ));
196
+ assertEquals ("installationId" , installation .getString (KEY_INSTALLATION_ID ));
197
+ // Make sure we update the locale identifier
198
+ assertEquals ("en-US" , installation .getString (KEY_LOCALE_IDENTIFIER ));
187
199
}
188
200
189
201
// TODO(mengyan): Add other testUpdateBeforeSave cases to cover all branches
@@ -255,6 +267,33 @@ public void testGetCurrentInstallation() throws Exception {
255
267
verify (controller , times (1 )).getAsync ();
256
268
}
257
269
270
+ @ Test
271
+ public void testLocaleIdentifierSpecialCases () throws Exception {
272
+ mocksForUpdateBeforeSave ();
273
+
274
+ ParseInstallation installation = new ParseInstallation ();
275
+
276
+ // Deprecated two-letter codes (Java issue).
277
+ Locale .setDefault (new Locale ("iw" , "US" ));
278
+ installation .updateBeforeSave ();
279
+ assertEquals ("he-US" , installation .getString (KEY_LOCALE_IDENTIFIER ));
280
+
281
+ Locale .setDefault (new Locale ("in" , "US" ));
282
+ installation .updateBeforeSave ();
283
+ assertEquals ("id-US" , installation .getString (KEY_LOCALE_IDENTIFIER ));
284
+
285
+ Locale .setDefault (new Locale ("ji" , "US" ));
286
+ installation .updateBeforeSave ();
287
+ assertEquals ("yi-US" , installation .getString (KEY_LOCALE_IDENTIFIER ));
288
+
289
+ // No country code.
290
+ Locale .setDefault (new Locale ("en" ));
291
+ installation .updateBeforeSave ();
292
+ assertEquals ("en" , installation .getString (KEY_LOCALE_IDENTIFIER ));
293
+ }
294
+
295
+
296
+
258
297
// TODO(mengyan): Add testFetchAsync, right now we can not test super methods inside
259
298
// testFetchAsync
260
299
}
0 commit comments