Fixed unit tests

This commit is contained in:
Felix Ableitner 2016-10-10 16:13:39 +09:00
parent f37ad202f9
commit 923d700d54
15 changed files with 330 additions and 228 deletions

View File

@ -39,7 +39,7 @@ android {
versionCode 105
versionName "0.8.6"
testApplicationId 'com.nutomic.syncthingandroid.test'
testInstrumentationRunner 'android.test.InstrumentationTestRunner'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
testHandleProfiling true
testFunctionalTest true
jackOptions {

View File

@ -2,8 +2,8 @@ package com.nutomic.syncthingandroid.test;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import java.net.URL;

View File

@ -1,6 +1,6 @@
package com.nutomic.syncthingandroid.test.activities;
import android.test.ActivityInstrumentationTestCase2;
import android.support.test.rule.ActivityTestRule;
import com.nutomic.syncthingandroid.activities.MainActivity;
import com.nutomic.syncthingandroid.fragments.DeviceListFragment;
@ -8,19 +8,23 @@ import com.nutomic.syncthingandroid.fragments.FolderListFragment;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
import com.nutomic.syncthingandroid.test.MockSyncthingService;
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
public class MainActivityTest {
@Rule
public final ActivityTestRule<MainActivity> mRule = new ActivityTestRule<>(MainActivity.class);
private final MockSyncthingService mService = new MockSyncthingService();
public MainActivityTest() {
super(MainActivity.class);
}
@Test
public void testOnServiceConnected() {
getActivity().onServiceConnected(null, new SyncthingServiceBinder(mService));
assertTrue(mService.containsListenerInstance(MainActivity.class));
assertTrue(mService.containsListenerInstance(FolderListFragment.class));
assertTrue(mService.containsListenerInstance(DeviceListFragment.class));
mRule.getActivity().onServiceConnected(null, new SyncthingServiceBinder(mService));
Assert.assertTrue(mService.containsListenerInstance(MainActivity.class));
Assert.assertTrue(mService.containsListenerInstance(FolderListFragment.class));
Assert.assertTrue(mService.containsListenerInstance(DeviceListFragment.class));
}
}

View File

@ -2,32 +2,39 @@ package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.syncthing.AppConfigReceiver;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.test.MockContext;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
/**
* Test the correct behaviour of the AppConfigReceiver
*
* Created by sqrt-1674 on 27.03.16.
*/
public class AppConfigReceiverTest extends AndroidTestCase {
public class AppConfigReceiverTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private AppConfigReceiver mReceiver;
private MockContext mContext;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
mReceiver = new AppConfigReceiver();
mContext = new MockContext(getContext());
mContext = new MockContext(InstrumentationRegistry.getTargetContext());
}
@Override
protected void tearDown() throws Exception {
PreferenceManager.getDefaultSharedPreferences(mContext).edit().clear().commit();
super.tearDown();
@After
public void tearDown() throws Exception {
PreferenceManager.getDefaultSharedPreferences(mContext).edit().clear().apply();
}
@ -35,18 +42,19 @@ public class AppConfigReceiverTest extends AndroidTestCase {
* Test starting the Syncthing-Service if "always run in background" is enabled
* In this case starting the service is allowed
*/
@Test
public void testStartSyncthingServiceBackground() {
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, true)
.commit();
.apply();
Intent intent = new Intent(new Intent(mContext, AppConfigReceiver.class));
intent.setAction(AppConfigReceiver.ACTION_START);
mReceiver.onReceive(mContext, intent);
assertEquals("Start SyncthingService Background", 1, mContext.getReceivedIntents().size());
assertEquals("Start SyncthingService Background", SyncthingService.class.getName(),
Assert.assertEquals("Start SyncthingService Background", 1, mContext.getReceivedIntents().size());
Assert.assertEquals("Start SyncthingService Background", SyncthingService.class.getName(),
mContext.getReceivedIntents().get(0).getComponent().getClassName());
}
@ -54,35 +62,37 @@ public class AppConfigReceiverTest extends AndroidTestCase {
* Test stopping the service if "alway run in background" is enabled.
* Stopping the service in this mode is not allowed, so no stopService-intent may be issued.
*/
@Test
public void testStopSyncthingServiceBackground() {
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, true)
.commit();
.apply();
Intent intent = new Intent(new Intent(mContext, AppConfigReceiver.class));
intent.setAction(AppConfigReceiver.ACTION_STOP);
mReceiver.onReceive(mContext, intent);
assertEquals("Stop SyncthingService Background", 0, mContext.getStopServiceIntents().size());
Assert.assertEquals("Stop SyncthingService Background", 0, mContext.getStopServiceIntents().size());
}
/**
* Test starting the Syncthing-Service if "always run in background" is disabled
* In this case starting the service is allowed
*/
@Test
public void testStartSyncthingServiceForeground() {
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, false)
.commit();
.apply();
Intent intent = new Intent(new Intent(mContext, AppConfigReceiver.class));
intent.setAction(AppConfigReceiver.ACTION_START);
mReceiver.onReceive(mContext, intent);
assertEquals("Start SyncthingService Foreround", 1, mContext.getReceivedIntents().size());
assertEquals("Start SyncthingService Foreround", SyncthingService.class.getName(),
Assert.assertEquals("Start SyncthingService Foreround", 1, mContext.getReceivedIntents().size());
Assert.assertEquals("Start SyncthingService Foreround", SyncthingService.class.getName(),
mContext.getReceivedIntents().get(0).getComponent().getClassName());
}
@ -90,19 +100,20 @@ public class AppConfigReceiverTest extends AndroidTestCase {
* Test stopping the Syncthing-Service if "always run in background" is disabled
* In this case stopping the service is allowed
*/
@Test
public void testStopSyncthingServiceForeground() {
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, false)
.commit();
.apply();
Intent intent = new Intent(new Intent(mContext, AppConfigReceiver.class));
intent.setAction(AppConfigReceiver.ACTION_STOP);
mReceiver.onReceive(mContext, intent);
assertEquals("Stop SyncthingService Foreround", 1, mContext.getStopServiceIntents().size());
Assert.assertEquals("Stop SyncthingService Foreround", 1, mContext.getStopServiceIntents().size());
Intent receivedIntent = mContext.getStopServiceIntents().get(0);
assertEquals("Stop SyncthingService Foreround", SyncthingService.class.getName(),
Assert.assertEquals("Stop SyncthingService Foreround", SyncthingService.class.getName(),
receivedIntent.getComponent().getClassName());
}
}

View File

@ -2,71 +2,82 @@ package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.syncthing.BatteryReceiver;
import com.nutomic.syncthingandroid.syncthing.DeviceStateHolder;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.test.MockContext;
public class BatteryReceiverTest extends AndroidTestCase {
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
public class BatteryReceiverTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private BatteryReceiver mReceiver;
private MockContext mContext;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
mReceiver = new BatteryReceiver();
mContext = new MockContext(getContext());
mContext = new MockContext(InstrumentationRegistry.getTargetContext());
}
@Override
protected void tearDown() throws Exception {
PreferenceManager.getDefaultSharedPreferences(mContext).edit().clear().commit();
super.tearDown();
@After
public void tearDown() throws Exception {
PreferenceManager.getDefaultSharedPreferences(mContext).edit().clear().apply();
}
@Test
public void testOnReceiveCharging() {
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, true)
.commit();
.apply();
Intent intent = new Intent(Intent.ACTION_POWER_CONNECTED);
mReceiver.onReceive(mContext, intent);
assertEquals(1, mContext.getReceivedIntents().size());
Assert.assertEquals(1, mContext.getReceivedIntents().size());
Intent receivedIntent = mContext.getReceivedIntents().get(0);
assertTrue(receivedIntent.getBooleanExtra(DeviceStateHolder.EXTRA_IS_CHARGING, false));
Assert.assertTrue(receivedIntent.getBooleanExtra(DeviceStateHolder.EXTRA_IS_CHARGING, false));
mContext.clearReceivedIntents();
}
@Test
public void testOnReceiveNotCharging() {
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, true)
.commit();
.apply();
Intent intent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
mReceiver.onReceive(mContext, intent);
assertEquals(1, mContext.getReceivedIntents().size());
Assert.assertEquals(1, mContext.getReceivedIntents().size());
Intent receivedIntent = mContext.getReceivedIntents().get(0);
assertEquals(SyncthingService.class.getName(), receivedIntent.getComponent().getClassName());
assertFalse(receivedIntent.getBooleanExtra(DeviceStateHolder.EXTRA_IS_CHARGING, true));
Assert.assertEquals(SyncthingService.class.getName(), receivedIntent.getComponent().getClassName());
Assert.assertFalse(receivedIntent.getBooleanExtra(DeviceStateHolder.EXTRA_IS_CHARGING, true));
mContext.clearReceivedIntents();
}
@Test
public void testOnlyRunInForeground() {
PreferenceManager.getDefaultSharedPreferences(getContext())
PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getTargetContext())
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, false)
.commit();
.apply();
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_CONNECTED));
assertEquals(0, mContext.getReceivedIntents().size());
Assert.assertEquals(0, mContext.getReceivedIntents().size());
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_DISCONNECTED));
assertEquals(0, mContext.getReceivedIntents().size());
Assert.assertEquals(0, mContext.getReceivedIntents().size());
}
}

View File

@ -2,54 +2,64 @@ package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.syncthing.BootReceiver;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.test.MockContext;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
/**
* Tests that {@link com.nutomic.syncthingandroid.syncthing.BootReceiver} starts the right service
* ({@link com.nutomic.syncthingandroid.syncthing.SyncthingService}.
*/
public class BootReceiverTest extends AndroidTestCase {
public class BootReceiverTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private BootReceiver mReceiver;
private MockContext mContext;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
mReceiver = new BootReceiver();
mContext = new MockContext(getContext());
mContext = new MockContext(InstrumentationRegistry.getTargetContext());
}
@Override
protected void tearDown() throws Exception {
PreferenceManager.getDefaultSharedPreferences(mContext).edit().clear().commit();
super.tearDown();
@After
public void tearDown() throws Exception {
PreferenceManager.getDefaultSharedPreferences(mContext).edit().clear().apply();
}
@Test
public void testOnReceiveCharging() {
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, true)
.commit();
.apply();
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_BOOT_COMPLETED));
assertEquals(1, mContext.getReceivedIntents().size());
Assert.assertEquals(1, mContext.getReceivedIntents().size());
Intent receivedIntent = mContext.getReceivedIntents().get(0);
assertEquals(SyncthingService.class.getName(), receivedIntent.getComponent().getClassName());
Assert.assertEquals(SyncthingService.class.getName(), receivedIntent.getComponent().getClassName());
mContext.clearReceivedIntents();
}
@Test
public void testOnlyRunInForeground() {
PreferenceManager.getDefaultSharedPreferences(getContext())
PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getTargetContext())
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, false)
.commit();
.apply();
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_BOOT_COMPLETED));
assertEquals(0, mContext.getReceivedIntents().size());
Assert.assertEquals(0, mContext.getReceivedIntents().size());
}
}

View File

@ -2,58 +2,69 @@ package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.os.BatteryManager;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.syncthing.DeviceStateHolder;
import com.nutomic.syncthingandroid.test.MockContext;
public class DeviceStateHolderTest extends AndroidTestCase {
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
public class DeviceStateHolderTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private DeviceStateHolder mReceiver;
private MockContext mContext;
@Override
protected void setUp() throws Exception {
super.setUp();
mReceiver = new DeviceStateHolder(getContext());
@Before
public void setUp() throws Exception {
mReceiver = new DeviceStateHolder(InstrumentationRegistry.getTargetContext());
mContext = new MockContext(null);
}
@Test
public void testIsCharging() {
Intent i = new Intent();
i.putExtra(DeviceStateHolder.EXTRA_IS_CHARGING, false);
mReceiver.update(i);
assertFalse(mReceiver.isCharging());
Assert.assertFalse(mReceiver.isCharging());
i.putExtra(DeviceStateHolder.EXTRA_IS_CHARGING, true);
mReceiver.update(i);
assertTrue(mReceiver.isCharging());
Assert.assertTrue(mReceiver.isCharging());
}
@Test
public void testWifiConnected() {
Intent i = new Intent();
i.putExtra(DeviceStateHolder.EXTRA_HAS_WIFI, false);
mReceiver.update(i);
assertFalse(mReceiver.isWifiConnected());
Assert.assertFalse(mReceiver.isWifiConnected());
i.putExtra(DeviceStateHolder.EXTRA_HAS_WIFI, true);
mReceiver.update(i);
assertTrue(mReceiver.isWifiConnected());
Assert.assertTrue(mReceiver.isWifiConnected());
}
@Test
public void testonReceiveInitialChargingState() {
Intent i = new Intent();
mReceiver.onReceive(mContext, i);
assertFalse(mReceiver.isCharging());
assertEquals(mContext.getLastUnregistered(), mReceiver);
Assert.assertFalse(mReceiver.isCharging());
Assert.assertEquals(mContext.getLastUnregistered(), mReceiver);
i.putExtra(BatteryManager.EXTRA_PLUGGED, 0);
mReceiver.onReceive(mContext, i);
assertFalse(mReceiver.isCharging());
Assert.assertFalse(mReceiver.isCharging());
i.putExtra(BatteryManager.EXTRA_PLUGGED, 1);
mReceiver.onReceive(mContext, i);
assertTrue(mReceiver.isCharging());
Assert.assertTrue(mReceiver.isCharging());
}
}

View File

@ -3,13 +3,20 @@ package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.syncthing.DeviceStateHolder;
import com.nutomic.syncthingandroid.syncthing.NetworkReceiver;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.test.MockContext;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
/**
* Tests for correct extras on the Intent sent by
* {@link com.nutomic.syncthingandroid.syncthing.NetworkReceiver}.
@ -17,46 +24,49 @@ import com.nutomic.syncthingandroid.test.MockContext;
* Does not test for correct result value, as that would require mocking
* {@link android.net.ConnectivityManager} (or replacing it with an interface).
*/
public class NetworkReceiverTest extends AndroidTestCase {
public class NetworkReceiverTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private NetworkReceiver mReceiver;
private MockContext mContext;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
mReceiver = new NetworkReceiver();
mContext = new MockContext(getContext());
mContext = new MockContext(InstrumentationRegistry.getTargetContext());
}
@Override
protected void tearDown() throws Exception {
PreferenceManager.getDefaultSharedPreferences(mContext).edit().clear().commit();
super.tearDown();
@After
public void tearDown() throws Exception {
PreferenceManager.getDefaultSharedPreferences(mContext).edit().clear().apply();
}
@Test
public void testOnReceive() {
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, true)
.commit();
.apply();
mReceiver.onReceive(mContext, new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
assertEquals(1, mContext.getReceivedIntents().size());
Assert.assertEquals(1, mContext.getReceivedIntents().size());
Intent receivedIntent = mContext.getReceivedIntents().get(0);
assertEquals(SyncthingService.class.getName(), receivedIntent.getComponent().getClassName());
assertNull(receivedIntent.getAction());
assertTrue(receivedIntent.hasExtra(DeviceStateHolder.EXTRA_HAS_WIFI));
Assert.assertEquals(SyncthingService.class.getName(), receivedIntent.getComponent().getClassName());
Assert.assertNull(receivedIntent.getAction());
Assert.assertTrue(receivedIntent.hasExtra(DeviceStateHolder.EXTRA_HAS_WIFI));
mContext.clearReceivedIntents();
}
@Test
public void testOnlyRunInForeground() {
PreferenceManager.getDefaultSharedPreferences(getContext())
PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getTargetContext())
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, false)
.commit();
.apply();
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_CONNECTED));
assertEquals(0, mContext.getReceivedIntents().size());
Assert.assertEquals(0, mContext.getReceivedIntents().size());
}
}

View File

@ -1,6 +1,8 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.test.AndroidTestCase;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.http.PollWebGuiAvailableTask;
import com.nutomic.syncthingandroid.syncthing.RestApi;
@ -9,23 +11,31 @@ import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.test.MockContext;
import com.nutomic.syncthingandroid.util.ConfigXml;
import java.util.Map;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class RestApiTest extends AndroidTestCase {
public class RestApiTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private RestApi mApi;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
Context context = InstrumentationRegistry.getTargetContext();
new SyncthingRunnable(new MockContext(getContext()), SyncthingRunnable.Command.main);
new SyncthingRunnable(context, SyncthingRunnable.Command.main);
ConfigXml config = new ConfigXml(new MockContext(getContext()));
ConfigXml config = new ConfigXml(context);
String httpsCertPath = getContext().getFilesDir() + "/" + SyncthingService.HTTPS_CERT_FILE;
String httpsCertPath = context.getFilesDir() + "/" + SyncthingService.HTTPS_CERT_FILE;
final CountDownLatch latch = new CountDownLatch(2);
new PollWebGuiAvailableTask(config.getWebGuiUrl(), httpsCertPath, config.getApiKey()) {
@ -35,96 +45,96 @@ public class RestApiTest extends AndroidTestCase {
latch.countDown();
}
}.execute();
mApi = new RestApi(getContext(), config.getWebGuiUrl(), config.getApiKey(),
latch::countDown, null);
latch.await(1, TimeUnit.SECONDS);
mApi = new RestApi(context, config.getWebGuiUrl(), config.getApiKey(),
new RestApi.OnApiAvailableListener() {
@Override
public void onApiAvailable() {
latch.countDown();
}
}, null);
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
// TODO: Unit tests fail when Syncthing is killed SyncthingRunnable.killSyncthing();
ConfigXml.getConfigFile(new MockContext(getContext())).delete();
@After
public void tearDown() throws Exception {
// TODO: Unit tests fail when Syncthing is killed
// SyncthingRunnable.killSyncthing();
Context context = InstrumentationRegistry.getTargetContext();
ConfigXml.getConfigFile(new MockContext(context)).delete();
}
@Test
public void testGetDevices() {
assertNotNull(mApi.getDevices(false));
Assert.assertNotNull(mApi.getDevices(false));
}
@Test
public void testGetSystemInfo() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mApi.getSystemInfo(new RestApi.OnReceiveSystemInfoListener() {
@Override
public void onReceiveSystemInfo(RestApi.SystemInfo info) {
assertNotNull(info);
latch.countDown();
}
mApi.getSystemInfo((info) -> {
Assert.assertNotNull(info);
latch.countDown();
});
latch.await(1, TimeUnit.SECONDS);
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Test
public void testGetSystemVersion() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mApi.getSystemVersion(new RestApi.OnReceiveSystemVersionListener() {
@Override
public void onReceiveSystemVersion(RestApi.SystemVersion info) {
assertNotNull(info);
latch.countDown();
}
mApi.getSystemVersion(info -> {
Assert.assertNotNull(info);
latch.countDown();
});
latch.await(1, TimeUnit.SECONDS);
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Test
public void testGetFolders() {
assertNotNull(mApi.getFolders());
Assert.assertNotNull(mApi.getFolders());
}
@Test
public void testConvertNotCrashing() {
long[] values = new long[]{-1, 0, 1, 2, 4, 8, 16, 1024, 2^10, 2^15, 2^20, 2^25, 2^30};
for (long l : values) {
assertNotSame("", RestApi.readableFileSize(getContext(), l));
assertNotSame("", RestApi.readableTransferRate(getContext(), l));
Assert.assertNotSame("", RestApi.readableFileSize(InstrumentationRegistry.getTargetContext(), l));
Assert.assertNotSame("", RestApi.readableTransferRate(InstrumentationRegistry.getTargetContext(), l));
}
}
@Test
public void testGetConnections() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mApi.getConnections(new RestApi.OnReceiveConnectionsListener() {
@Override
public void onReceiveConnections(Map<String, RestApi.Connection> connections) {
assertNotNull(connections);
latch.countDown();
}
mApi.getConnections(connections -> {
Assert.assertNotNull(connections);
latch.countDown();
});
latch.await(1, TimeUnit.SECONDS);
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Test
public void testGetModel() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mApi.getModel("camera", new RestApi.OnReceiveModelListener() {
@Override
public void onReceiveModel(String folderId, RestApi.Model model) {
assertNotNull(model);
latch.countDown();
}
mApi.getModel("camera", (folderId, model) -> {
Assert.assertNotNull(model);
latch.countDown();
});
latch.await(1, TimeUnit.SECONDS);
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Test
public void testNormalizeDeviceId() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mApi.normalizeDeviceId("p56ioi7m--zjnu2iq-gdr-eydm-2mgtmgl3bxnpq6w5btbbz4tjxzwicq",
new RestApi.OnDeviceIdNormalizedListener() {
@Override
public void onDeviceIdNormalized(String normalizedId, String error) {
assertEquals("P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2",
normalizedId);
latch.countDown();
}
(normalizedId, error) -> {
Assert.assertEquals("P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2",
normalizedId);
latch.countDown();
});
latch.await(1, TimeUnit.SECONDS);
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Test
public void testGetValueEarly() {
// Should never throw an exception.
mApi.getValue("Options", "ListenAddress");

View File

@ -1,25 +1,34 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.syncthing.SyncthingRunnable;
import com.nutomic.syncthingandroid.test.MockContext;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
/**
* NOTE: This test will cause a "syncthing binary crashed" notification, because
* {@code -home " + mContext.getFilesDir()} is run as a "command" and fails.
*/
public class SyncthingRunnableTest extends AndroidTestCase {
public class SyncthingRunnableTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
@Test
public void testRunning() throws InterruptedException {
MockContext context = new MockContext(getContext());
MockContext context = new MockContext(InstrumentationRegistry.getTargetContext());
File testFile = new File(context.getFilesDir(), SyncthingRunnable.UNIT_TEST_PATH);
assertFalse(testFile.exists());
Assert.assertFalse(testFile.exists());
// Inject a different command instead of the Syncthing binary for testing.
new SyncthingRunnable(context, new String[]{"touch", testFile.getAbsolutePath()}).run();
assertTrue(testFile.exists());
Assert.assertTrue(testFile.exists());
testFile.delete();
}

View File

@ -1,16 +1,24 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.test.AndroidTestCase;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
public class SyncthingServiceBinderTest extends AndroidTestCase {
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
public class SyncthingServiceBinderTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
@Test
public void testBinder() {
SyncthingService service = new SyncthingService();
SyncthingServiceBinder binder = new SyncthingServiceBinder(service);
assertEquals(service, binder.getService());
Assert.assertEquals(service, binder.getService());
}
}

View File

@ -3,6 +3,8 @@ package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
@ -11,6 +13,7 @@ import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
import com.nutomic.syncthingandroid.util.ConfigXml;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
@ -37,8 +40,21 @@ public class SyncthingServiceTest {
public void setup() throws TimeoutException {
Intent intent =
new Intent(InstrumentationRegistry.getTargetContext(), SyncthingService.class);
SyncthingServiceBinder binder = (SyncthingServiceBinder) mServiceRule.bindService(intent);
mService = binder.getService();
// Workaround for
// https://code.google.com/p/android/issues/detail?id=200071
// https://code.google.com/p/android/issues/detail?id=180396
IBinder binder;
int it = 0;
while((binder = mServiceRule.bindService(intent)) == null && it < 100){
it++;
}
mService = ((SyncthingServiceBinder) binder).getService();
}
@After
public void tearDown() {
new File(mService.getFilesDir(), SyncthingService.PUBLIC_KEY_FILE).delete();
}
@Test
@ -52,20 +68,6 @@ public class SyncthingServiceTest {
Assert.assertFalse(mService.isFirstStart());
}
@Test
public void testBindService() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(2);
mService.registerOnWebGuiAvailableListener(latch::countDown);
mService.registerOnApiChangeListener(new SyncthingService.OnApiChangeListener() {
@Override
public void onApiChange(SyncthingService.State currentState) {
latch.countDown();
}
});
latch.await(1, TimeUnit.SECONDS);
Assert.assertNotNull(mService);
}
@Test
public void testImportExportConfig() {
File config = new File(mService.getFilesDir(), ConfigXml.CONFIG_FILE);
@ -94,13 +96,11 @@ public class SyncthingServiceTest {
@Test
public void testPassword() throws InterruptedException {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mService);
Assert.assertNotNull(sp.getString("gui_user", null));
Assert.assertEquals(20, sp.getString("gui_password", null).length());
}
Looper.prepare();
new Handler().postDelayed(() -> {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mService);
Assert.assertNotNull(sp.getString("gui_user", null));
Assert.assertEquals(20, sp.getString("gui_password", null).length());
}, 5000);
}

View File

@ -1,35 +1,42 @@
package com.nutomic.syncthingandroid.test.util;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.test.MockContext;
import com.nutomic.syncthingandroid.util.ConfigXml;
public class ConfigXmlTest extends AndroidTestCase {
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
public class ConfigXmlTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private MockContext mContext;
private ConfigXml mConfig;
@Override
protected void setUp() throws Exception {
super.setUp();
mContext = new MockContext(getContext());
assertFalse(ConfigXml.getConfigFile(mContext).exists());
@Before
public void setUp() throws Exception {
mContext = new MockContext(InstrumentationRegistry.getTargetContext());
Assert.assertFalse(ConfigXml.getConfigFile(mContext).exists());
mConfig = new ConfigXml(mContext);
assertTrue(ConfigXml.getConfigFile(mContext).exists());
Assert.assertTrue(ConfigXml.getConfigFile(mContext).exists());
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
@After
public void tearDown() throws Exception {
ConfigXml.getConfigFile(mContext).delete();
}
@Test
public void testGetWebGuiUrl() {
assertTrue(mConfig.getWebGuiUrl().toString().startsWith("https://127.0.0.1:"));
Assert.assertTrue(mConfig.getWebGuiUrl().toString().startsWith("https://127.0.0.1:"));
}
}

View File

@ -1,19 +1,28 @@
package com.nutomic.syncthingandroid.test.util;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.test.MockContext;
import com.nutomic.syncthingandroid.test.Util;
import com.nutomic.syncthingandroid.util.FolderObserver;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class FolderObserverTest extends AndroidTestCase
implements FolderObserver.OnFolderFileChangeListener {
public class FolderObserverTest implements FolderObserver.OnFolderFileChangeListener {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private File mTestFolder;
@ -21,24 +30,22 @@ public class FolderObserverTest extends AndroidTestCase
private CountDownLatch mLatch;
@Override
protected void setUp() throws Exception {
super.setUp();
mTestFolder = new File(new MockContext(getContext()).getFilesDir(), "observer-test");
@Before
public void setUp() throws Exception {
mTestFolder = new File(new MockContext(InstrumentationRegistry.getTargetContext()).getFilesDir(), "observer-test");
mTestFolder.mkdir();
}
@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
Util.deleteRecursive(mTestFolder);
super.tearDown();
}
@Override
public void onFolderFileChange(String folderId, String relativePath) {
mLatch.countDown();
assertEquals(mCurrentTest, folderId);
assertFalse(relativePath.endsWith("should-not-notifiy"));
Assert.assertEquals(mCurrentTest, folderId);
Assert.assertFalse(relativePath.endsWith("should-not-notifiy"));
}
private RestApi.Folder createFolder(String id) {
@ -48,6 +55,7 @@ public class FolderObserverTest extends AndroidTestCase
return r;
}
@Test
public void testRecursion() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testRecursion";
@ -58,11 +66,12 @@ public class FolderObserverTest extends AndroidTestCase
mLatch = new CountDownLatch(1);
testFile.createNewFile();
mLatch.await(1, TimeUnit.SECONDS);
Assert.assertTrue(mLatch.await(1, TimeUnit.SECONDS));
fo.stopWatching();
}
@Test
public void testRemoveFile() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testRemoveFile";
@ -72,12 +81,13 @@ public class FolderObserverTest extends AndroidTestCase
mLatch = new CountDownLatch(1);
test.delete();
mLatch.await(1, TimeUnit.SECONDS);
assertEquals(0, mLatch.getCount());
Assert.assertTrue(mLatch.await(1, TimeUnit.SECONDS));
Assert.assertEquals(0, mLatch.getCount());
fo.stopWatching();
}
@Test
public void testAddDirectory() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testAddDirectory";
@ -88,21 +98,22 @@ public class FolderObserverTest extends AndroidTestCase
mLatch = new CountDownLatch(1);
testFile.createNewFile();
mLatch.await(1, TimeUnit.SECONDS);
assertEquals(0, mLatch.getCount());
Assert.assertTrue(mLatch.await(1, TimeUnit.SECONDS));
Assert.assertEquals(0, mLatch.getCount());
fo.stopWatching();
}
@Test
public void testNotExisting() throws IOException, InterruptedException {
RestApi.Folder r = new RestApi.Folder();
r.path = new File(new MockContext(getContext()).getFilesDir(), "not-existing").getPath();
r.path = new File(new MockContext(InstrumentationRegistry.getTargetContext()).getFilesDir(), "not-existing").getPath();
r.id = "testNotExisting";
try {
new FolderObserver(this, r);
fail("Expected FolderNotExistingException");
Assert.fail("Expected FolderNotExistingException");
} catch (FolderObserver.FolderNotExistingException e) {
assertTrue(e.getMessage().contains(r.path));
Assert.assertTrue(e.getMessage().contains(r.path));
}
}

View File

@ -128,7 +128,7 @@ public class MainActivity extends SyncthingActivity
* Shows the loading dialog with the correct text ("creating keys" or "loading").
*/
private void showLoadingDialog() {
if (mLoadingDialog != null)
if (isFinishing() || mLoadingDialog != null)
return;
LayoutInflater inflater = getLayoutInflater();