1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 14:21:16 +00:00

Fixed various test cases. Also removed slow/unreliable tests.

This commit is contained in:
Felix Ableitner 2014-12-01 07:36:58 +02:00
parent ab184aee48
commit 65a45d7408
6 changed files with 6 additions and 168 deletions

View file

@ -6,6 +6,7 @@ import android.content.Context;
import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.RestApi;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MockRestApi extends RestApi { public class MockRestApi extends RestApi {
@ -42,7 +43,7 @@ public class MockRestApi extends RestApi {
@Override @Override
public List<Device> getDevices() { public List<Device> getDevices() {
throw new UnsupportedOperationException(); return new ArrayList<>();
} }
@Override @Override
@ -52,7 +53,7 @@ public class MockRestApi extends RestApi {
@Override @Override
public List<Folder> getFolders() { public List<Folder> getFolders() {
throw new UnsupportedOperationException(); return new ArrayList<>();
} }
@Override @Override

View file

@ -1,57 +0,0 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import com.nutomic.syncthingandroid.syncthing.GetTask;
import com.nutomic.syncthingandroid.syncthing.PostTask;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
import java.io.IOException;
public class PostTaskTest extends AndroidTestCase {
private MockWebServer mServer;
private static final String RESPONSE = "the response";
private static final String API_KEY = "the key";
@Override
protected void setUp() throws Exception {
super.setUp();
mServer = new MockWebServer();
mServer.enqueue(new MockResponse().setBody(RESPONSE));
mServer.play();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
// TODO: causes problems, see https://github.com/square/okhttp/issues/1033
//mServer.shutdown();
}
@MediumTest
public void testGetNoContent() throws IOException, InterruptedException {
new GetTask() {
@Override
protected void onPostExecute(String s) {
assertEquals(RESPONSE, s);
}
}.execute(mServer.getUrl("").toString(), PostTask.URI_CONFIG, API_KEY);
RecordedRequest request = mServer.takeRequest();
assertEquals(API_KEY, request.getHeader(RestApi.HEADER_API_KEY));
Uri uri = Uri.parse(request.getPath());
assertEquals(PostTask.URI_CONFIG, uri.getPath());
}
// TODO: add a test with post content (but request.getUtf8Body() is always empty)
}

View file

@ -20,7 +20,7 @@ public class SyncthingRunnableTest extends AndroidTestCase {
File testFile = new File(context.getFilesDir(), "was_running"); File testFile = new File(context.getFilesDir(), "was_running");
assertFalse(testFile.exists()); assertFalse(testFile.exists());
// Inject a differenct command instead of the syncthing binary for testing. // Inject a differenct command instead of the syncthing binary for testing.
new SyncthingRunnable(context, "touch " + testFile.getAbsolutePath() + "\n").run(); new SyncthingRunnable(context, "touch " + testFile.getAbsolutePath() + "; exit\n").run();
assertTrue(testFile.exists()); assertTrue(testFile.exists());
testFile.delete(); testFile.delete();
} }

View file

@ -24,11 +24,6 @@ import java.util.concurrent.TimeUnit;
/** /**
* These tests assume that syncthing keys have already been generated. If not, tests may fail * These tests assume that syncthing keys have already been generated. If not, tests may fail
* because startup takes too long. * because startup takes too long.
*
* FIXME: These tests are rather fragile and may fail even if they shouldn't. Repeating them
* should fix this.
* NOTE: If a test fails with "expected:<ACTIVE> but was:<INIT>", you may have to increase
* {@link #STARTUP_TIME_SECONDS}.
*/ */
public class SyncthingServiceTest extends ServiceTestCase<SyncthingService> { public class SyncthingServiceTest extends ServiceTestCase<SyncthingService> {
@ -130,102 +125,4 @@ public class SyncthingServiceTest extends ServiceTestCase<SyncthingService> {
} }
private Listener mListener = new Listener();
@MediumTest
public void testStatesAllRequired() throws InterruptedException {
setupStatesTest(true, true, true);
assertState(true, true, SyncthingService.State.ACTIVE);
assertState(true, false, SyncthingService.State.DISABLED);
assertState(false, true, SyncthingService.State.DISABLED);
assertState(false, false, SyncthingService.State.DISABLED);
}
@MediumTest
public void testStatesWifiRequired() throws InterruptedException {
setupStatesTest(true, true, false);
assertState(true, true, SyncthingService.State.ACTIVE);
assertState(false, true, SyncthingService.State.ACTIVE);
assertState(true, false, SyncthingService.State.DISABLED);
assertState(false, false, SyncthingService.State.DISABLED);
}
@MediumTest
public void testStatesChargingRequired() throws InterruptedException {
setupStatesTest(true, false, true);
assertState(true, true, SyncthingService.State.ACTIVE);
assertState(true, false, SyncthingService.State.ACTIVE);
assertState(false, true, SyncthingService.State.DISABLED);
assertState(false, false, SyncthingService.State.DISABLED);
}
@MediumTest
public void testStatesNoneRequired() throws InterruptedException {
setupStatesTest(true, false, false);
assertState(true, true, SyncthingService.State.ACTIVE);
assertState(true, false, SyncthingService.State.ACTIVE);
assertState(false, true, SyncthingService.State.ACTIVE);
assertState(false, false, SyncthingService.State.ACTIVE);
}
public void assertState(boolean charging, boolean wifi, SyncthingService.State expected)
throws InterruptedException {
Intent i = new Intent(getContext(), SyncthingService.class);
i.putExtra(DeviceStateHolder.EXTRA_IS_CHARGING, charging);
i.putExtra(DeviceStateHolder.EXTRA_HAS_WIFI, wifi);
mLatch = new CountDownLatch(1);
startService(i);
// Wait for service to react to preference change.
mLatch.await(1, TimeUnit.SECONDS);
assertEquals(expected, mListener.getLastState());
}
public void setupStatesTest(boolean alwaysRunInBackground,
boolean syncOnlyWifi, boolean syncOnlyCharging) throws InterruptedException {
PreferenceManager.getDefaultSharedPreferences(getContext())
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, alwaysRunInBackground)
.putBoolean(SyncthingService.PREF_SYNC_ONLY_WIFI, syncOnlyWifi)
.putBoolean(SyncthingService.PREF_SYNC_ONLY_CHARGING, syncOnlyCharging)
.commit();
startService(new Intent(getContext(), SyncthingService.class));
// 3 calls plus 1 call immediately when registering.
mLatch = new CountDownLatch(4);
getService().registerOnApiChangeListener(mListener);
if (mListener.getLastState() != SyncthingService.State.ACTIVE) {
// Wait for service to start.
mLatch.await(STARTUP_TIME_SECONDS, TimeUnit.SECONDS);
assertEquals(SyncthingService.State.ACTIVE, mListener.getLastState());
}
}
/**
* For all possible settings and charging/wifi states, service should be active.
*/
@LargeTest
public void testOnlyForeground() throws InterruptedException {
ArrayList<Pair<Boolean, Boolean>> values = new ArrayList<>();
values.add(new Pair(true, true));
values.add(new Pair(true, false));
values.add(new Pair(false, true));
values.add(new Pair(false, false));
for (Pair<Boolean, Boolean> v : values) {
setupStatesTest(false, v.first, v.second);
assertState(true, true, SyncthingService.State.ACTIVE);
assertState(true, false, SyncthingService.State.ACTIVE);
assertState(false, true, SyncthingService.State.ACTIVE);
assertState(false, false, SyncthingService.State.ACTIVE);
}
}
} }

View file

@ -36,7 +36,7 @@ public class ConfigXmlTest extends AndroidTestCase {
@SmallTest @SmallTest
public void testGetWebGuiUrl() { public void testGetWebGuiUrl() {
assertEquals("http://127.0.0.1:8080", mConfig.getWebGuiUrl()); assertTrue(mConfig.getWebGuiUrl().startsWith("http://127.0.0.1:"));
} }
/** /**

View file

@ -202,10 +202,7 @@ public class SyncthingService extends Service {
Log.i(TAG, "Stopping syncthing according to current state and preferences"); Log.i(TAG, "Stopping syncthing according to current state and preferences");
mCurrentState = State.DISABLED; mCurrentState = State.DISABLED;
// Syncthing is currently started, perform the stop later. if (mApi != null) {
if (mCurrentState == State.STARTING) {
mStopScheduled = true;
} else if (mApi != null) {
mApi.shutdown(); mApi.shutdown();
for (FolderObserver ro : mObservers) { for (FolderObserver ro : mObservers) {
ro.stopWatching(); ro.stopWatching();