Commit 1f6fe52e authored by rahadi's avatar rahadi

Added Pemutakhiran Module

Migrate to id.ac.stis.capi
parent fb614149
{
"project_info": {
"project_number": "1043539700437",
"firebase_url": "https://capi-stis-95e29.firebaseio.com",
"project_id": "capi-stis-95e29",
"storage_bucket": "capi-stis-95e29.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1043539700437:android:5e1d7c3c296329e8",
"android_client_info": {
"package_name": "org.odk.collect.android"
}
},
"oauth_client": [
{
"client_id": "1043539700437-q0vjmegkorlp8enhq26eahj2p0f5dil8.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyC8ocyCpQ_khywwxxF_SHd7aBqW1ME5pbY"
}
],
"services": {
"analytics_service": {
"status": 1
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"ads_service": {
"status": 2
}
}
}
],
"configuration_version": "1"
}
\ No newline at end of file
package id.ac.stis.capi.tasks;
import org.junit.Test;
import id.ac.stis.capi.MockedServerTest;
import id.ac.stis.capi.logic.FormDetails;
import org.ac.stis.capi.test.MockedServerTest;
import java.util.Map;
import okhttp3.mockwebserver.RecordedRequest;
import static id.ac.stis.capi.TestUtils.assertMatches;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.ac.stis.capi.test.TestUtils.assertMatches;
public class DownloadFormListTaskTest extends MockedServerTest {
......@@ -26,7 +27,7 @@ public class DownloadFormListTaskTest extends MockedServerTest {
RecordedRequest r = nextRequest();
assertEquals("GET", r.getMethod());
assertEquals("/formList", r.getPath());
assertMatches("Dalvik/.* org.odk.collect.android/.*", r.getHeader("User-Agent"));
assertMatches("Dalvik/.* id.ac.stis.capi/.*", r.getHeader("User-Agent"));
assertEquals("1.0", r.getHeader("X-OpenRosa-Version"));
assertEquals("gzip", r.getHeader("Accept-Encoding"));
......
......@@ -7,20 +7,21 @@ import java.io.File;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import id.ac.stis.capi.MockedServerTest;
import id.ac.stis.capi.dao.InstancesDao;
import id.ac.stis.capi.dto.Instance;
import id.ac.stis.capi.provider.InstanceProviderAPI;
import id.ac.stis.capi.tasks.InstanceUploaderTask.Outcome;
import org.ac.stis.capi.test.MockedServerTest;
import okhttp3.mockwebserver.RecordedRequest;
import static id.ac.stis.capi.TestUtils.assertMatches;
import static id.ac.stis.capi.TestUtils.cleanUpTempFiles;
import static id.ac.stis.capi.TestUtils.createTempFile;
import static id.ac.stis.capi.TestUtils.resetInstancesContentProvider;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.ac.stis.capi.test.TestUtils.assertMatches;
import static org.ac.stis.capi.test.TestUtils.cleanUpTempFiles;
import static org.ac.stis.capi.test.TestUtils.createTempFile;
import static org.ac.stis.capi.test.TestUtils.resetInstancesContentProvider;
public class InstanceUploaderTest extends MockedServerTest {
private InstancesDao dao;
......@@ -56,7 +57,7 @@ public class InstanceUploaderTest extends MockedServerTest {
RecordedRequest r = nextRequest();
assertEquals("HEAD", r.getMethod());
assertMatches("/submission\\?deviceID=\\w+%3A\\w+", r.getPath());
assertMatches("Dalvik/.* org.odk.collect.android/.*", r.getHeader("User-Agent"));
assertMatches("Dalvik/.* id.ac.stis.capi/.*", r.getHeader("User-Agent"));
assertEquals("1.0", r.getHeader("X-OpenRosa-Version"));
assertEquals("gzip,deflate", r.getHeader("Accept-Encoding"));
}
......@@ -66,7 +67,7 @@ public class InstanceUploaderTest extends MockedServerTest {
RecordedRequest r = nextRequest();
assertEquals("POST", r.getMethod());
assertEquals("/submission", r.getPath());
assertMatches("Dalvik/.* org.odk.collect.android/.*", r.getHeader("User-Agent"));
assertMatches("Dalvik/.* id.ac.stis.capi/.*", r.getHeader("User-Agent"));
assertEquals("1.0", r.getHeader("X-OpenRosa-Version"));
assertEquals("gzip,deflate", r.getHeader("Accept-Encoding"));
assertMatches("multipart/form-data; boundary=.*", r.getHeader("Content-Type"));
......
......@@ -2,8 +2,9 @@ package id.ac.stis.capi.utilities;
import org.junit.Before;
import org.junit.Test;
import id.ac.stis.capi.MockedServerTest;
import id.ac.stis.capi.application.Collect;
import org.ac.stis.capi.test.MockedServerTest;
import org.opendatakit.httpclientandroidlib.client.HttpClient;
import org.opendatakit.httpclientandroidlib.client.methods.HttpGet;
import org.opendatakit.httpclientandroidlib.protocol.HttpContext;
......@@ -13,10 +14,10 @@ import java.net.URI;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;
import static id.ac.stis.capi.TestUtils.assertMatches;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.ac.stis.capi.test.TestUtils.assertMatches;
public class WebUtilsTest extends MockedServerTest {
@Before
......@@ -33,7 +34,7 @@ public class WebUtilsTest extends MockedServerTest {
// then
RecordedRequest r = nextRequest();
assertEquals("GET /some-path HTTP/1.1", r.getRequestLine());
assertTrue(r.getHeader("User-Agent").matches("Dalvik/.* org.odk.collect.android/.*"));
assertTrue(r.getHeader("User-Agent").matches("Dalvik/.* id.ac.stis.capi/.*"));
}
@Test
......@@ -42,7 +43,7 @@ public class WebUtilsTest extends MockedServerTest {
WebUtils.getXmlDocument(url("/list-forms"), httpContext(), httpClient());
// then
assertMatches("Dalvik/.* org.odk.collect.android/.*",
assertMatches("Dalvik/.* id.ac.stis.capi/.*",
nextRequest().getHeader("User-Agent"));
}
......
package org.ac.stis.capi;
import android.support.test.filters.Suppress;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opendatakit.httpclientandroidlib.HttpResponse;
import org.opendatakit.httpclientandroidlib.HttpStatus;
import org.opendatakit.httpclientandroidlib.client.HttpClient;
import org.opendatakit.httpclientandroidlib.client.methods.HttpGet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import javax.net.ssl.HttpsURLConnection;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import static id.ac.stis.capi.utilities.WebUtils.CONNECTION_TIMEOUT;
import static id.ac.stis.capi.utilities.WebUtils.createHttpClient;
import static id.ac.stis.capi.utilities.WebUtils.createOpenRosaHttpGet;
/**
* An on-device test for TLS server name indication support.
*
* @see <a href="https://github.com/opendatakit/collect/issues/199">COLLECT-199</a>
*/
@Suppress
@RunWith(AndroidJUnit4.class)
public class SNITest {
public static final URI SNI_URI = URI.create("https://sni.velox.ch/");
public static final String SUCCESS_SENTINEL = "sent the following TLS server name indication extension";
@Test
public void apacheHttpClientSupportsSNI() throws IOException {
HttpClient client = createHttpClient(CONNECTION_TIMEOUT);
HttpGet req = createOpenRosaHttpGet(SNI_URI);
HttpResponse rsp = client.execute(req);
assertHttpSuccess(rsp.getStatusLine().getStatusCode());
assertPageContent(rsp.getEntity().getContent());
}
@Test
public void urlConnectionSupportsSNI() throws IOException {
HttpsURLConnection conn = (HttpsURLConnection) SNI_URI.toURL().openConnection();
assertHttpSuccess(conn.getResponseCode());
assertPageContent(conn.getInputStream());
}
/*
* Confirms the request returned HTTP success (200) or fails test.
*/
private void assertHttpSuccess(int statusCode) {
assertEquals(HttpStatus.SC_OK, statusCode);
}
/*
* Confirms that the response content contains confirmation that SNI is working.
*/
private void assertPageContent(InputStream contentStream) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(contentStream));
String line;
while ((line = in.readLine()) != null) {
if (line.contains(SUCCESS_SENTINEL)) {
return;
}
}
fail(String.format("did not find sentinel '%s' in response", SUCCESS_SENTINEL));
}
}
package org.ac.stis.capi.externalintents;
import junit.framework.Assert;
import java.io.File;
import timber.log.Timber;
import static id.ac.stis.capi.application.Collect.CACHE_PATH;
import static id.ac.stis.capi.application.Collect.FORMS_PATH;
import static id.ac.stis.capi.application.Collect.INSTANCES_PATH;
import static id.ac.stis.capi.application.Collect.METADATA_PATH;
import static id.ac.stis.capi.application.Collect.ODK_ROOT;
import static id.ac.stis.capi.application.Collect.OFFLINE_LAYERS;
class ExportedActivitiesUtils {
private static final String[] DIRS = new String[]{
ODK_ROOT, FORMS_PATH, INSTANCES_PATH, CACHE_PATH, METADATA_PATH, OFFLINE_LAYERS
};
static void clearDirectories() {
for (String dirName : DIRS) {
File dir = new File(dirName);
if (dir.exists()) {
if (dir.delete()) {
Timber.i("Directory was not deleted");
}
}
}
}
static void testDirectories() {
for (String dirName : DIRS) {
File dir = new File(dirName);
Assert.assertTrue("File " + dirName + "does not exist", dir.exists());
Assert.assertTrue("File" + dirName + "does not exist", dir.isDirectory());
}
}
}
package org.ac.stis.capi.externalintents;
import android.app.Activity;
import android.support.test.rule.ActivityTestRule;
import static org.ac.stis.capi.externalintents.ExportedActivitiesUtils.clearDirectories;
class ExportedActivityTestRule<A extends Activity> extends ActivityTestRule<A> {
ExportedActivityTestRule(Class<A> activityClass) {
super(activityClass);
}
@Override
protected void beforeActivityLaunched() {
super.beforeActivityLaunched();
clearDirectories();
}
}
package org.ac.stis.capi.externalintents;
import android.support.test.filters.Suppress;
import android.support.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import id.ac.stis.capi.activities.FormChooserList;
import java.io.IOException;
import static org.ac.stis.capi.externalintents.ExportedActivitiesUtils.testDirectories;
@Suppress
// Frequent failures: https://github.com/opendatakit/collect/issues/796
public class FormChooserListTest {
@Rule
public ActivityTestRule<FormChooserList> formChooserListRule =
new ExportedActivityTestRule<>(FormChooserList.class);
@Test
public void formChooserListMakesDirsTest() throws IOException {
testDirectories();
}
}
package org.ac.stis.capi.externalintents;
import android.support.test.filters.Suppress;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import id.ac.stis.capi.activities.FormEntryActivity;
import java.io.IOException;
import static org.ac.stis.capi.externalintents.ExportedActivitiesUtils.testDirectories;
@Suppress
// Frequent failures: https://github.com/opendatakit/collect/issues/796
@RunWith(AndroidJUnit4.class)
public class FormEntryActivityTest {
@Rule
public ActivityTestRule<FormEntryActivity> formEntryActivityRule =
new ExportedActivityTestRule<>(FormEntryActivity.class);
@Test
public void formEntryActivityMakesDirsTest() throws IOException {
testDirectories();
}
}
package org.ac.stis.capi.externalintents;
import android.support.test.filters.Suppress;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import id.ac.stis.capi.activities.InstanceChooserList;
import java.io.IOException;
import static org.ac.stis.capi.externalintents.ExportedActivitiesUtils.testDirectories;
@Suppress
// Frequent failures: https://github.com/opendatakit/collect/issues/796
@RunWith(AndroidJUnit4.class)
public class InstanceChooserListTest {
@Rule
public ActivityTestRule<InstanceChooserList> instanceChooserListRule =
new ExportedActivityTestRule<>(InstanceChooserList.class);
@Test
public void instanceChooserListMakesDirsTest() throws IOException {
testDirectories();
}
}
package org.ac.stis.capi.externalintents;
import android.support.test.filters.Suppress;
import android.support.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import id.ac.stis.capi.activities.MainMenuActivity;
import java.io.IOException;
import static org.ac.stis.capi.externalintents.ExportedActivitiesUtils.testDirectories;
@Suppress
// Frequent failures: https://github.com/opendatakit/collect/issues/796
public class MainMenuActivityTest {
@Rule
public ActivityTestRule<MainMenuActivity> mainMenuActivityRule =
new ExportedActivityTestRule<>(MainMenuActivity.class);
@Test
public void mainMenuActivityMakesDirsTest() throws IOException {
testDirectories();
}
}
package org.ac.stis.capi.externalintents;
import android.support.test.filters.Suppress;
import android.support.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import id.ac.stis.capi.activities.SplashScreenActivity;
import java.io.IOException;
import static org.ac.stis.capi.externalintents.ExportedActivitiesUtils.testDirectories;
@Suppress
// Frequent failures: https://github.com/opendatakit/collect/issues/796
public class SplashScreenActivityTest {
@Rule
public ActivityTestRule<SplashScreenActivity> splashScreenActivityRule =
new ExportedActivityTestRule<>(SplashScreenActivity.class);
@Test
public void splashScreenActivityMakesDirsTest() throws IOException {
testDirectories();
}
}
package org.ac.stis.capi.test;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import id.ac.stis.capi.application.Collect;
import id.ac.stis.capi.preferences.PreferenceKeys;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import static org.ac.stis.capi.test.TestUtils.backupPreferences;
import static org.ac.stis.capi.test.TestUtils.restorePreferences;
public abstract class MockedServerTest {
private Map<String, ?> prefsBackup;
protected MockWebServer server;
@Before
public void http_setUp() throws Exception {
prefsBackup = backupPreferences();
server = mockWebServer();
}
@After
public void http_tearDown() throws Exception {
if (server != null) {
server.shutdown();
}
if (prefsBackup != null) {
restorePreferences(prefsBackup);
}
}
protected void willRespondWith(String... rawResponses) {
for (String rawResponse : rawResponses) {
MockResponse response = new MockResponse();
String[] parts = rawResponse.split("\r\n\r\n", 2);
String[] headerLines = parts[0].split("\r\n");
response.setStatus(headerLines[0]);
for (int i = 1; i < headerLines.length; ++i) {
String[] headerParts = headerLines[i].split(": ", 2);
response.addHeader(headerParts[0], headerParts[1]);
}
response.setBody(parts[1]);
server.enqueue(response);
}
}
protected RecordedRequest nextRequest() throws Exception {
return server.takeRequest(1, TimeUnit.MILLISECONDS);
}
protected static String join(String... strings) {
StringBuilder bob = new StringBuilder();
for (String s : strings) {
bob.append(s).append('\n');
}
return bob.toString();
}
private static MockWebServer mockWebServer() throws Exception {
MockWebServer server = new MockWebServer();
server.start();
configAppFor(server);
return server;
}
private static void configAppFor(MockWebServer server) {
Editor prefs = PreferenceManager.getDefaultSharedPreferences(Collect.getInstance().getBaseContext()).edit();
prefs.putString(PreferenceKeys.KEY_SERVER_URL, server.url("/").toString());
if (!prefs.commit()) {
throw new RuntimeException("Failed to set up SharedPreferences for MockWebServer");
}
}
}
package org.ac.stis.capi.test;
import android.os.Environment;
import id.ac.stis.capi.application.Collect;
import id.ac.stis.capi.preferences.GeneralSharedPreferences;
import id.ac.stis.capi.provider.InstanceProviderAPI;
import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
public final class TestUtils {
private TestUtils() {
}
public static Map<String, ?> backupPreferences() {
return Collections.unmodifiableMap(GeneralSharedPreferences.getInstance().getAll());
}
public static void restorePreferences(Map<String, ?> backup) {
GeneralSharedPreferences.getInstance().clear();
for (Map.Entry<String, ?> e : backup.entrySet()) {
Object v = e.getValue();
GeneralSharedPreferences.getInstance().save(e.getKey(), v);
}
}
public static File createTempFile(String content) throws Exception {
File f = createTempFile();
FileWriter fw = null;
try {
fw = new FileWriter(f);
fw.write(content);
} finally {
closeSafely(fw);
}
return f;
}
public static File createTempFile() throws Exception {
// Create our own directory, because 2-arg `createTempFile()` sometimes fails with:
// java.io.IOException: open failed: ENOENT (No such file or directory)
File dir = tempFileDirectory();
dir.mkdirs();
File tempFile = File.createTempFile("tst", null, dir);
dir.deleteOnExit(); // Not fail-safe on android )¬;
return tempFile;
}
public static void cleanUpTempFiles() {
File[] tempFiles = tempFileDirectory().listFiles();
if (tempFiles != null) {
for (File f : tempFiles) {
f.delete();
}
}
}
private static File tempFileDirectory() {
return new File(Environment.getExternalStorageDirectory(), "test-tmp");
}
public static void closeSafely(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException ex) {
// not much you can do at this point
}
}
}
public static void resetInstancesContentProvider() {
Collect.getInstance().getContentResolver().delete(InstanceProviderAPI.InstanceColumns.CONTENT_URI, null, null);
}
public static void assertMatches(String expectedPattern, Object actual) {
if (!testMatches(expectedPattern, actual)) {
throw new AssertionError(String.format("Expected <%s> to match <%s>.", actual, expectedPattern));
}
}
public static void assertMatches(String message, String expectedPattern, Object actual) {
if (!testMatches(expectedPattern, actual)) {
throw new AssertionError(String.format("%s Expected <%s> to match <%s>.", message, actual, expectedPattern));
}
}
private static boolean testMatches(String expectedPattern, Object actual) {
if (expectedPattern == null) {
throw new IllegalArgumentException("No pattern provided.");
}
if (actual == null) {
return false;
}
return actual.toString().matches(expectedPattern);
}
}
......@@ -22,13 +22,14 @@ import android.view.View;
import android.view.ViewGroup;
import org.javarosa.core.services.IPropertyManager;
import id.ac.stis.capi.R;
import id.ac.stis.capi.logic.FormController;
import id.ac.stis.capi.logic.PropertyManager;
import java.util.Collection;
import java.util.List;
import id.ac.stis.capi.R;
import id.ac.stis.capi.logic.FormController;
import id.ac.stis.capi.logic.PropertyManager;
/**
* Handles general preferences.
*/
......@@ -83,7 +84,7 @@ public class PreferencesActivity extends PreferenceActivity {
for (String key : keys) {
boolean value = (boolean) sharedPreferences.get(key);
if (value) {
return true;
return true;
}
}
return false;
......@@ -99,6 +100,8 @@ public class PreferencesActivity extends PreferenceActivity {
super.onCreate(savedInstanceState);
ViewGroup root = getRootView();
root.setPadding(0, 0, 0, 0);