|
8 | 8 | */
|
9 | 9 | package com.parse;
|
10 | 10 |
|
| 11 | +import android.content.pm.ApplicationInfo; |
| 12 | +import android.content.pm.PackageManager; |
| 13 | +import android.os.Bundle; |
| 14 | + |
11 | 15 | import com.parse.http.ParseNetworkInterceptor;
|
12 | 16 |
|
13 | 17 | import org.junit.Test;
|
| 18 | +import org.junit.runner.RunWith; |
| 19 | +import org.robolectric.RobolectricTestRunner; |
| 20 | +import org.robolectric.RuntimeEnvironment; |
| 21 | +import org.robolectric.annotation.Config; |
| 22 | +import org.robolectric.shadows.ShadowPackageManager; |
14 | 23 |
|
| 24 | +import java.net.URL; |
15 | 25 | import java.util.ArrayList;
|
16 | 26 | import java.util.Collection;
|
17 | 27 | import java.util.Iterator;
|
|
22 | 32 | import static org.junit.Assert.assertTrue;
|
23 | 33 | import static org.junit.Assert.fail;
|
24 | 34 | import static org.mockito.Mockito.mock;
|
| 35 | +import static org.mockito.Mockito.verify; |
| 36 | +import static org.mockito.Mockito.when; |
| 37 | +import static org.robolectric.Shadows.shadowOf; |
25 | 38 |
|
| 39 | +@RunWith(RobolectricTestRunner.class) |
| 40 | +@Config(constants = BuildConfig.class) |
26 | 41 | public class ParseClientConfigurationTest {
|
27 | 42 |
|
| 43 | + private final String serverUrl = "http://example.com/parse"; |
| 44 | + private final String appId = "MyAppId"; |
| 45 | + private final String clientKey = "MyClientKey"; |
| 46 | + private final String PARSE_SERVER_URL = "com.parse.SERVER_URL"; |
| 47 | + private final String PARSE_APPLICATION_ID = "com.parse.APPLICATION_ID"; |
| 48 | + private final String PARSE_CLIENT_KEY = "com.parse.CLIENT_KEY"; |
| 49 | + |
28 | 50 | @Test
|
29 | 51 | public void testBuilder() {
|
30 | 52 | Parse.Configuration.Builder builder = new Parse.Configuration.Builder(null);
|
@@ -105,6 +127,75 @@ public void testSetNetworkInterceptors() {
|
105 | 127 | assertTrue(collectionsEqual(configurationB.interceptors, collectionB));
|
106 | 128 | }
|
107 | 129 |
|
| 130 | + @Test |
| 131 | + public void testConfigureFromManifest() throws Exception { |
| 132 | + Bundle metaData = setupMockMetaData(); |
| 133 | + when(metaData.getString(PARSE_SERVER_URL)).thenReturn(serverUrl); |
| 134 | + when(metaData.getString(PARSE_APPLICATION_ID)).thenReturn(appId); |
| 135 | + when(metaData.getString(PARSE_CLIENT_KEY)).thenReturn(clientKey); |
| 136 | + |
| 137 | + Parse.Configuration.Builder builder = new Parse.Configuration.Builder(RuntimeEnvironment.application); |
| 138 | + Parse.Configuration config = builder.build(); |
| 139 | + assertEquals(serverUrl + "/", config.server); |
| 140 | + assertEquals(appId, config.applicationId); |
| 141 | + assertEquals(clientKey, config.clientKey); |
| 142 | + |
| 143 | + verifyMockMetaData(metaData); |
| 144 | + } |
| 145 | + |
| 146 | + @Test(expected = RuntimeException.class) |
| 147 | + public void testConfigureFromManifestWithoutServer() throws Exception { |
| 148 | + Bundle metaData = setupMockMetaData(); |
| 149 | + when(metaData.getString(PARSE_SERVER_URL)).thenReturn(null); |
| 150 | + when(metaData.getString(PARSE_APPLICATION_ID)).thenReturn(appId); |
| 151 | + when(metaData.getString(PARSE_CLIENT_KEY)).thenReturn(clientKey); |
| 152 | + |
| 153 | + // RuntimeException due to serverUrl = null |
| 154 | + Parse.initialize(RuntimeEnvironment.application); |
| 155 | + } |
| 156 | + |
| 157 | + @Test(expected = RuntimeException.class) |
| 158 | + public void testConfigureFromManifestWithoutAppId() throws Exception { |
| 159 | + Bundle metaData = setupMockMetaData(); |
| 160 | + when(metaData.getString(PARSE_SERVER_URL)).thenReturn(serverUrl); |
| 161 | + when(metaData.getString(PARSE_APPLICATION_ID)).thenReturn(null); |
| 162 | + when(metaData.getString(PARSE_CLIENT_KEY)).thenReturn(clientKey); |
| 163 | + |
| 164 | + // RuntimeException due to applicationId = null |
| 165 | + Parse.initialize(RuntimeEnvironment.application); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testConfigureFromManifestWithoutClientKey() throws Exception { |
| 170 | + Bundle metaData = setupMockMetaData(); |
| 171 | + when(metaData.getString(PARSE_SERVER_URL)).thenReturn(serverUrl); |
| 172 | + when(metaData.getString(PARSE_APPLICATION_ID)).thenReturn(appId); |
| 173 | + when(metaData.getString(PARSE_CLIENT_KEY)).thenReturn(null); |
| 174 | + |
| 175 | + Parse.initialize(RuntimeEnvironment.application); |
| 176 | + assertEquals(new URL(serverUrl + "/"), ParseRESTCommand.server); |
| 177 | + assertEquals(appId, ParsePlugins.get().applicationId()); |
| 178 | + assertNull(ParsePlugins.get().clientKey()); |
| 179 | + |
| 180 | + verifyMockMetaData(metaData); |
| 181 | + } |
| 182 | + |
| 183 | + private void verifyMockMetaData(Bundle metaData) throws Exception { |
| 184 | + verify(metaData).getString(PARSE_SERVER_URL); |
| 185 | + verify(metaData).getString(PARSE_APPLICATION_ID); |
| 186 | + verify(metaData).getString(PARSE_CLIENT_KEY); |
| 187 | + } |
| 188 | + |
| 189 | + private Bundle setupMockMetaData() throws Exception { |
| 190 | + Bundle metaData = mock(Bundle.class); |
| 191 | + ShadowPackageManager packageManager = shadowOf(RuntimeEnvironment.application.getPackageManager()); |
| 192 | + ApplicationInfo info = packageManager.getApplicationInfo( |
| 193 | + RuntimeEnvironment.application.getPackageName(), |
| 194 | + PackageManager.GET_META_DATA); |
| 195 | + info.metaData = metaData; |
| 196 | + return metaData; |
| 197 | + } |
| 198 | + |
108 | 199 | private static <T> boolean collectionsEqual(Collection<T> a, Collection<T> b) {
|
109 | 200 | if (a.size() != b.size()) {
|
110 | 201 | return false;
|
|
0 commit comments