Commit 03291f77 authored by rahadi's avatar rahadi

Starts Over

parent 1f6fe52e
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
buildscript { buildscript {
repositories { repositories {
jcenter() jcenter()
google()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.0.1' classpath 'com.android.tools.build:gradle:3.0.1'
...@@ -22,6 +23,7 @@ allprojects { ...@@ -22,6 +23,7 @@ allprojects {
jcenter() jcenter()
maven { url = 'https://oss.sonatype.org/content/groups/public' } maven { url = 'https://oss.sonatype.org/content/groups/public' }
maven { url "https://maven.google.com" } maven { url "https://maven.google.com" }
google()
} }
} }
......
package id.ac.stis.capi;
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.TestUtils;
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;
public abstract class MockedServerTest {
private Map<String, ?> prefsBackup;
protected MockWebServer server;
@Before
public void http_setUp() throws Exception {
prefsBackup = TestUtils.backupPreferences();
server = mockWebServer();
}
@After
public void http_tearDown() throws Exception {
if (server != null) {
server.shutdown();
}
if (prefsBackup != null) {
TestUtils.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 id.ac.stis.capi;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;
import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import id.ac.stis.capi.application.Collect;
import id.ac.stis.capi.provider.InstanceProviderAPI;
public final class TestUtils {
private TestUtils() {}
public static Map<String, ?> backupPreferences() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Collect.getInstance().getBaseContext());
return Collections.unmodifiableMap(prefs.getAll());
}
public static void restorePreferences(Map<String, ?> backup) {
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(Collect.getInstance().getBaseContext()).edit();
editor.clear();
for (Map.Entry<String, ?> e : backup.entrySet()) {
Object v = e.getValue();
if (v instanceof Boolean) {
editor.putBoolean(e.getKey(), (Boolean) v);
} else if (v instanceof Float) {
editor.putFloat(e.getKey(), (Float) v);
} else if (v instanceof Integer) {
editor.putInt(e.getKey(), (Integer) v);
} else if (v instanceof Long) {
editor.putLong(e.getKey(), (Long) v);
} else if (v instanceof String) {
editor.putString(e.getKey(), (String) v);
} else if (v instanceof Set) {
editor.putStringSet(e.getKey(), (Set<String>) v);
} else {
throw new RuntimeException("Unhandled preference value type: " + v);
}
}
editor.commit();
}
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);
}
}
...@@ -91,7 +91,7 @@ public class CapiLoginActivity extends AppCompatActivity ...@@ -91,7 +91,7 @@ public class CapiLoginActivity extends AppCompatActivity
//disable login button //disable login button
loginBtn.setClickable(false); loginBtn.setClickable(false);
loginBtn.setText("Proses login"); loginBtn.setText("Proses login");
loginBtn.setBackgroundColor(Color.GRAY); // loginBtn.setBackgroundColor(Color.GRAY);
//create dialog //create dialog
dialog = new ProgressDialog(this); dialog = new ProgressDialog(this);
...@@ -109,40 +109,42 @@ public class CapiLoginActivity extends AppCompatActivity ...@@ -109,40 +109,42 @@ public class CapiLoginActivity extends AppCompatActivity
public void loginDone(String result) { public void loginDone(String result) {
if (result.equals(GetUuidTask.SUCCESS)) { if (result.equals(GetUuidTask.SUCCESS)) {
// RAHADI: Register ke Pemutakhiran, etc. // RAHADI: Register ke Pemutakhiran, etc.
final String username = (String) GeneralSharedPreferences.getInstance().get(PreferenceKeys.KEY_USERNAME); // final String username = (String) GeneralSharedPreferences.getInstance().get(PreferenceKeys.KEY_USERNAME);
final String uuid = (String) GeneralSharedPreferences.getInstance().get(PreferenceKeys.KEY_UNIQUES); // final String uuid = (String) GeneralSharedPreferences.getInstance().get(PreferenceKeys.KEY_UNIQUES);
new UserRegisterTask(username, uuid).execute(new OnSynchronizationCompletedListener<JSONObject>() { // new UserRegisterTask(username, uuid).execute(new OnSynchronizationCompletedListener<JSONObject>() {
@Override // @Override
public void onSynchronizationCompleted(JSONObject result) { // public void onSynchronizationCompleted(JSONObject result) {
UserModel r = new GsonBuilder().create().fromJson(result.toString(), UserModel.class); // UserModel r = new GsonBuilder().create().fromJson(result.toString(), UserModel.class);
Timber.d(r.toString()); // Timber.d(r.toString());
//
Vsen.getInstance().setUserSession(r); // Vsen.getInstance().setUserSession(r);
//
dialog.setMessage("Sinkronisasi Data.."); // dialog.setMessage("Sinkronisasi Data..");
//
new BSSynchronizationTask(null, null).execute(username, uuid, // new BSSynchronizationTask(null, null).execute(username, uuid,
new OnSynchronizationCompletedListener<JSONArray>() { // new OnSynchronizationCompletedListener<JSONArray>() {
@Override // @Override
public void onSynchronizationCompleted(JSONArray result) { // public void onSynchronizationCompleted(JSONArray result) {
onLoginSucceed(); // onLoginSucceed();
} // }
//
@Override // @Override
public void onSynchronizationFailed(Exception e) { // public void onSynchronizationFailed(Exception e) {
Timber.e(e); // Timber.e(e);
onLoginFailed(e.toString()); // onLoginFailed(e.toString());
} // }
}); // });
} // }
//
@Override // @Override
public void onSynchronizationFailed(Exception e) { // public void onSynchronizationFailed(Exception e) {
Timber.e(e); // Timber.e(e);
onLoginFailed(e.toString()); // onLoginFailed(e.toString());
} // }
}); // });
onLoginSucceed();
} else { } else {
onLoginFailed(result); onLoginFailed(result);
} }
...@@ -152,7 +154,7 @@ public class CapiLoginActivity extends AppCompatActivity ...@@ -152,7 +154,7 @@ public class CapiLoginActivity extends AppCompatActivity
dialog.dismiss(); dialog.dismiss();
loginBtn.setClickable(true); loginBtn.setClickable(true);
loginBtn.setText("LOGIN"); loginBtn.setText("LOGIN");
loginBtn.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); // loginBtn.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
GeneralSharedPreferences.getInstance().save(PreferenceKeys.KEY_LOGIN_STATUS, true); GeneralSharedPreferences.getInstance().save(PreferenceKeys.KEY_LOGIN_STATUS, true);
// Intent toMain = new Intent(this, BSActivity.class); // Intent toMain = new Intent(this, BSActivity.class);
...@@ -165,7 +167,7 @@ public class CapiLoginActivity extends AppCompatActivity ...@@ -165,7 +167,7 @@ public class CapiLoginActivity extends AppCompatActivity
dialog.dismiss(); dialog.dismiss();
loginBtn.setClickable(true); loginBtn.setClickable(true);
loginBtn.setText("LOGIN"); loginBtn.setText("LOGIN");
loginBtn.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); // loginBtn.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this); AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder alertBuilder
......
...@@ -219,10 +219,10 @@ public class CapiMainActivity extends AppCompatActivity implements DiskSyncListe ...@@ -219,10 +219,10 @@ public class CapiMainActivity extends AppCompatActivity implements DiskSyncListe
private void initializeDrawer(final Context context, Bundle savedInstanceState) { private void initializeDrawer(final Context context, Bundle savedInstanceState) {
//get user data //get user data
// String username = (String) prefs.get(PreferenceKeys.KEY_USERNAME); String username = (String) prefs.get(PreferenceKeys.KEY_USERNAME);
// String userGroup = (String) prefs.get(PreferenceKeys.KEY_USERGROUP); String userGroup = (String) prefs.get(PreferenceKeys.KEY_USERGROUP);
String username = (String) Vsen.getInstance().getUserSession().getNamaUser(); // String username = (String) Vsen.getInstance().getUserSession().getNamaUser();
String userGroup = (String) Vsen.getInstance().getUserSession().getIdUser(); // String userGroup = (String) Vsen.getInstance().getUserSession().getIdUser();
//build drawer header //build drawer header
AccountHeader accountHeader = new AccountHeaderBuilder() AccountHeader accountHeader = new AccountHeaderBuilder()
...@@ -249,10 +249,10 @@ public class CapiMainActivity extends AppCompatActivity implements DiskSyncListe ...@@ -249,10 +249,10 @@ public class CapiMainActivity extends AppCompatActivity implements DiskSyncListe
.withName("Menu") .withName("Menu")
.withEnabled(true) .withEnabled(true)
.withDivider(false), .withDivider(false),
new PrimaryDrawerItem() // new PrimaryDrawerItem()
.withName("Pemutakhiran") // .withName("Pemutakhiran")
.withIcon(R.drawable.wb_ic_edit_bs) // .withIcon(R.drawable.wb_ic_edit_bs)
.withIdentifier(MENU_PEMUTAKHIRAN), // .withIdentifier(MENU_PEMUTAKHIRAN),
new PrimaryDrawerItem() new PrimaryDrawerItem()
.withName("Daftar Kuesioner") .withName("Daftar Kuesioner")
.withIcon(R.drawable.ic_survey) .withIcon(R.drawable.ic_survey)
......