This repository was archived by the owner on Sep 7, 2022. It is now read-only.
forked from parse-community/Parse-SDK-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseInstallationTest.java
More file actions
305 lines (249 loc) · 11 KB
/
Copy pathParseInstallationTest.java
File metadata and controls
305 lines (249 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.parse;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.res.builder.RobolectricPackageManager;
import java.util.Arrays;
import java.util.Locale;
import java.util.TimeZone;
import bolts.Task;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class ParseInstallationTest {
private static final String KEY_INSTALLATION_ID = "installationId";
private static final String KEY_DEVICE_TYPE = "deviceType";
private static final String KEY_APP_NAME = "appName";
private static final String KEY_APP_IDENTIFIER = "appIdentifier";
private static final String KEY_TIME_ZONE = "timeZone";
private static final String KEY_LOCALE_IDENTIFIER = "localeIdentifier";
private static final String KEY_APP_VERSION = "appVersion";
private Locale defaultLocale;
@Before
public void setUp() {
ParseObject.registerSubclass(ParseInstallation.class);
defaultLocale = Locale.getDefault();
}
@After
public void tearDown() {
ParseObject.unregisterSubclass(ParseInstallation.class);
ParseCorePlugins.getInstance().reset();
ParsePlugins.reset();
Locale.setDefault(defaultLocale);
}
@Test
public void testImmutableKeys() {
String[] immutableKeys = {
"installationId",
"deviceType",
"appName",
"appIdentifier",
"parseVersion",
"deviceToken",
"deviceTokenLastModified",
"pushType",
"timeZone",
"localeIdentifier",
"appVersion"
};
ParseInstallation installation = new ParseInstallation();
installation.put("foo", "bar");
for (String immutableKey : immutableKeys) {
try {
installation.put(immutableKey, "blah");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Cannot modify"));
}
try {
installation.remove(immutableKey);
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Cannot modify"));
}
try {
installation.removeAll(immutableKey, Arrays.asList());
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Cannot modify"));
}
}
}
@Test
public void testHandleSaveResultAsync() throws Exception {
// Mock currentInstallationController to make setAsync work
ParseCurrentInstallationController controller =
mock(ParseCurrentInstallationController.class);
when(controller.setAsync(any(ParseInstallation.class))).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerCurrentInstallationController(controller);
// Mock return state
ParseInstallation.State state = new ParseInstallation.State.Builder("_Installation")
.put("key", "value")
.build();
ParseInstallation installation = new ParseInstallation();
installation.put("keyAgain", "valueAgain");
ParseOperationSet operationSet = installation.startSave();
ParseTaskUtils.wait(installation.handleSaveResultAsync(state, operationSet));
// Make sure the installation data is correct
assertEquals("value", installation.get("key"));
assertEquals("valueAgain", installation.get("keyAgain"));
// Make sure we set the currentInstallation
verify(controller, times(1)).setAsync(installation);
}
@Test
public void testHandleFetchResultAsync() throws Exception {
// Mock currentInstallationController to make setAsync work
ParseCurrentInstallationController controller =
mock(ParseCurrentInstallationController.class);
when(controller.setAsync(any(ParseInstallation.class))).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerCurrentInstallationController(controller);
// Mock return state
ParseInstallation.State state = new ParseInstallation.State.Builder("_Installation")
.put("key", "value")
.isComplete(true)
.build();
ParseInstallation installation = new ParseInstallation();
ParseTaskUtils.wait(installation.handleFetchResultAsync(state));
// Make sure the installation data is correct
assertEquals("value", installation.get("key"));
// Make sure we set the currentInstallation
verify(controller, times(1)).setAsync(installation);
}
@Test
public void testUpdateBeforeSave() throws Exception {
mocksForUpdateBeforeSave();
Locale.setDefault(new Locale("en", "US"));
ParseInstallation installation = new ParseInstallation();
installation.updateBeforeSave();
// Make sure we update timezone
String zone = TimeZone.getDefault().getID();
assertEquals(zone, installation.getString(KEY_TIME_ZONE));
// Make sure we update version info
Context context = Parse.getApplicationContext();
String packageName = context.getPackageName();
PackageManager pm = context.getPackageManager();
PackageInfo pkgInfo = pm.getPackageInfo(packageName, 0);
String appVersion = pkgInfo.versionName;
String appName = pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0)).toString();
assertEquals(packageName, installation.getString(KEY_APP_IDENTIFIER));
assertEquals(appName, installation.getString(KEY_APP_NAME));
assertEquals(appVersion, installation.getString(KEY_APP_VERSION));
// Make sure we update device info
assertEquals("android", installation.getString(KEY_DEVICE_TYPE));
assertEquals("installationId", installation.getString(KEY_INSTALLATION_ID));
// Make sure we update the locale identifier
assertEquals("en-US", installation.getString(KEY_LOCALE_IDENTIFIER));
}
// TODO(mengyan): Add other testUpdateBeforeSave cases to cover all branches
@Test
public void testPushType() throws Exception {
ParseInstallation installation = new ParseInstallation();
installation.setPushType(PushType.GCM);
assertEquals(PushType.GCM, installation.getPushType());
installation.removePushType();
assertNull(installation.getPushType());
// Make sure we add the pushType to operationSetQueue instead of serverData
assertEquals(1, installation.operationSetQueue.getLast().size());
}
@Test
public void testPushTypeWithNullPushType() throws Exception {
ParseInstallation installation = new ParseInstallation();
installation.setPushType(PushType.GCM);
assertEquals(PushType.GCM, installation.getPushType());
installation.setPushType(null);
assertEquals(PushType.GCM, installation.getPushType());
}
@Test
public void testDeviceToken() throws Exception {
ParseInstallation installation = new ParseInstallation();
installation.setDeviceToken("deviceToken");
assertEquals("deviceToken", installation.getDeviceToken());
installation.removeDeviceToken();
assertNull(installation.getDeviceToken());
// Make sure we add the pushType to operationSetQueue instead of serverData
assertEquals(1, installation.operationSetQueue.getLast().size());
}
@Test
public void testDeviceTokenWithNullDeviceToken() throws Exception {
ParseInstallation installation = new ParseInstallation();
installation.setDeviceToken("deviceToken");
assertEquals("deviceToken", installation.getDeviceToken());
installation.setDeviceToken(null);
assertEquals("deviceToken", installation.getDeviceToken());
}
@Test
public void testGetCurrentInstallation() throws Exception {
// Mock currentInstallationController to make setAsync work
ParseCurrentInstallationController controller =
mock(ParseCurrentInstallationController.class);
ParseInstallation currentInstallation = new ParseInstallation();
when(controller.getAsync()).thenReturn(Task.forResult(currentInstallation));
ParseCorePlugins.getInstance().registerCurrentInstallationController(controller);
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
assertEquals(currentInstallation, installation);
verify(controller, times(1)).getAsync();
}
@Test
public void testLocaleIdentifierSpecialCases() throws Exception {
mocksForUpdateBeforeSave();
ParseInstallation installation = new ParseInstallation();
// Deprecated two-letter codes (Java issue).
Locale.setDefault(new Locale("iw", "US"));
installation.updateBeforeSave();
assertEquals("he-US", installation.getString(KEY_LOCALE_IDENTIFIER));
Locale.setDefault(new Locale("in", "US"));
installation.updateBeforeSave();
assertEquals("id-US", installation.getString(KEY_LOCALE_IDENTIFIER));
Locale.setDefault(new Locale("ji", "US"));
installation.updateBeforeSave();
assertEquals("yi-US", installation.getString(KEY_LOCALE_IDENTIFIER));
// No country code.
Locale.setDefault(new Locale("en"));
installation.updateBeforeSave();
assertEquals("en", installation.getString(KEY_LOCALE_IDENTIFIER));
}
// TODO(mengyan): Add testFetchAsync, right now we can not test super methods inside
// testFetchAsync
private static void mocksForUpdateBeforeSave() {
// Mock currentInstallationController to make setAsync work
ParseCurrentInstallationController controller =
mock(ParseCurrentInstallationController.class);
when(controller.isCurrent(any(ParseInstallation.class))).thenReturn(true);
ParseCorePlugins.getInstance().registerCurrentInstallationController(controller);
// Mock package manager
RobolectricPackageManager packageManager =
spy(RuntimeEnvironment.getRobolectricPackageManager());
doReturn("parseTest").when(packageManager).getApplicationLabel(any(ApplicationInfo.class));
RuntimeEnvironment.setRobolectricPackageManager(packageManager);
ParsePlugins.Android plugins = mock(ParsePlugins.Android.class);
// Mock installationId
InstallationId installationId = mock(InstallationId.class);
when(installationId.get()).thenReturn("installationId");
when(plugins.installationId()).thenReturn(installationId);
// Mock application context
when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application);
ParsePlugins.set(plugins);
}
}