diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/MockRestApi.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/MockRestApi.java index 1e3caaa6..18cdf669 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/MockRestApi.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/MockRestApi.java @@ -6,6 +6,7 @@ import android.content.Context; import com.nutomic.syncthingandroid.syncthing.RestApi; +import java.util.ArrayList; import java.util.List; public class MockRestApi extends RestApi { @@ -42,7 +43,7 @@ public class MockRestApi extends RestApi { @Override public List getDevices() { - throw new UnsupportedOperationException(); + return new ArrayList<>(); } @Override @@ -52,7 +53,7 @@ public class MockRestApi extends RestApi { @Override public List getFolders() { - throw new UnsupportedOperationException(); + return new ArrayList<>(); } @Override diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PostTaskTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PostTaskTest.java deleted file mode 100644 index 0c6b7af0..00000000 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PostTaskTest.java +++ /dev/null @@ -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) - -} diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingRunnableTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingRunnableTest.java index c6e78677..1f41cb68 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingRunnableTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingRunnableTest.java @@ -20,7 +20,7 @@ public class SyncthingRunnableTest extends AndroidTestCase { File testFile = new File(context.getFilesDir(), "was_running"); assertFalse(testFile.exists()); // 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()); testFile.delete(); } diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingServiceTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingServiceTest.java index fd64ec6f..35340ae6 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingServiceTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingServiceTest.java @@ -24,11 +24,6 @@ import java.util.concurrent.TimeUnit; /** * These tests assume that syncthing keys have already been generated. If not, tests may fail * 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: but was:", you may have to increase - * {@link #STARTUP_TIME_SECONDS}. */ public class SyncthingServiceTest extends ServiceTestCase { @@ -130,102 +125,4 @@ public class SyncthingServiceTest extends ServiceTestCase { } - 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> 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 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); - } - } - } diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java index 997f5b92..94662cb7 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java @@ -36,7 +36,7 @@ public class ConfigXmlTest extends AndroidTestCase { @SmallTest public void testGetWebGuiUrl() { - assertEquals("http://127.0.0.1:8080", mConfig.getWebGuiUrl()); + assertTrue(mConfig.getWebGuiUrl().startsWith("http://127.0.0.1:")); } /** diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index 2285ae25..a9f23545 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -202,10 +202,7 @@ public class SyncthingService extends Service { Log.i(TAG, "Stopping syncthing according to current state and preferences"); mCurrentState = State.DISABLED; - // Syncthing is currently started, perform the stop later. - if (mCurrentState == State.STARTING) { - mStopScheduled = true; - } else if (mApi != null) { + if (mApi != null) { mApi.shutdown(); for (FolderObserver ro : mObservers) { ro.stopWatching();