1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 03:11:30 +00:00

Delete unit tests (they were terribly written)

This commit is contained in:
Felix Ableitner 2017-10-02 14:59:19 +09:00
parent 4e32d60278
commit 9ad854defb
24 changed files with 2 additions and 1181 deletions

View file

@ -31,4 +31,3 @@ cache:
script:
- ./gradlew clean lint
- ./gradlew buildNative assembleDebug
- ./gradlew compileDebugAndroidTestSources

View file

@ -1,76 +0,0 @@
package com.nutomic.syncthingandroid.test;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Context that saves all received intents, which can be retrieved later by test classes.
*/
public class MockContext extends ContextWrapper {
private final ArrayList<Intent> mReceivedIntents = new ArrayList<>();
private final ArrayList<Intent> mStopServiceIntents = new ArrayList<>();
/**
* Use the actual context for calls that aren't easily mocked. May be null if those
* calls aren't needed.
*/
public MockContext(Context context) {
super(context);
}
@Override
public String getPackageName() {
return null;
}
@Override
public ComponentName startService(Intent intent) {
mReceivedIntents.add(intent);
return null;
}
@Override
public boolean stopService(Intent intent) {
mStopServiceIntents.add(intent);
return true;
}
public List<Intent> getReceivedIntents() {
return mReceivedIntents;
}
public List<Intent> getStopServiceIntents() {
return mStopServiceIntents;
}
public void clearReceivedIntents() {
mReceivedIntents.clear();
}
private BroadcastReceiver mLastUnregistered;
@Override
public void unregisterReceiver(BroadcastReceiver receiver) {
mLastUnregistered = receiver;
}
public BroadcastReceiver getLastUnregistered() {
return mLastUnregistered;
}
@Override
public File getFilesDir() {
File testFilesDir = new File(super.getFilesDir(), "test/");
testFilesDir.mkdir();
return testFilesDir;
}
}

View file

@ -1,91 +0,0 @@
package com.nutomic.syncthingandroid.test;
import android.content.Context;
import com.nutomic.syncthingandroid.model.Connections;
import com.nutomic.syncthingandroid.model.Device;
import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.model.Model;
import com.nutomic.syncthingandroid.model.SystemInfo;
import com.nutomic.syncthingandroid.service.RestApi;
import java.net.URL;
import java.util.List;
public class MockRestApi extends RestApi {
public MockRestApi(Context context, URL url, String apiKey,
OnApiAvailableListener listener) {
super(context, url, apiKey, listener, null);
}
@Override
public void onWebGuiAvailable() {
throw new UnsupportedOperationException();
}
@Override
public String getVersion() {
throw new UnsupportedOperationException();
}
@Override
public void shutdown() {
throw new UnsupportedOperationException();
}
@Override
public List<Device> getDevices(boolean includeLocal) {
throw new UnsupportedOperationException();
}
@Override
public void getSystemInfo(OnResultListener1<SystemInfo> listener) {
throw new UnsupportedOperationException();
}
@Override
public List<Folder> getFolders() {
throw new UnsupportedOperationException();
}
@Override
public void getConnections(OnResultListener1<Connections> listener) {
throw new UnsupportedOperationException();
}
@Override
public void getModel(String folderId, OnResultListener2<String, Model> listener) {
throw new UnsupportedOperationException();
}
@Override
public void editDevice(Device newDevice) {
throw new UnsupportedOperationException();
}
@Override
public void removeDevice(String deviceId) {
throw new UnsupportedOperationException();
}
@Override
public void editFolder(Folder newFolder) {
throw new UnsupportedOperationException();
}
@Override
public void removeFolder(String id) {
throw new UnsupportedOperationException();
}
@Override
public void normalizeDeviceId(String id, OnResultListener1<String> listener, OnResultListener1<String> errorListener) {
throw new UnsupportedOperationException();
}
@Override
public void onFolderFileChange(String folderId, String relativePath) {
throw new UnsupportedOperationException();
}
}

View file

@ -1,70 +0,0 @@
package com.nutomic.syncthingandroid.test;
import android.content.Intent;
import android.os.IBinder;
import com.nutomic.syncthingandroid.service.RestApi;
import com.nutomic.syncthingandroid.service.SyncthingService;
import java.net.URL;
import java.util.LinkedList;
public class MockSyncthingService extends SyncthingService {
private final LinkedList<OnApiChangeListener> mOnApiChangedListeners = new LinkedList<>();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
throw new UnsupportedOperationException();
}
@Override
public void onCreate() {
throw new UnsupportedOperationException();
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException();
}
@Override
public void onDestroy() {
throw new UnsupportedOperationException();
}
@Override
public void registerOnWebGuiAvailableListener(OnWebGuiAvailableListener listener) {
throw new UnsupportedOperationException();
}
@Override
public boolean isFirstStart() {
throw new UnsupportedOperationException();
}
@Override
public RestApi getApi() {
return new MockRestApi(this, null, null, null);
}
@Override
public void registerOnApiChangeListener(OnApiChangeListener listener) {
mOnApiChangedListeners.add(listener);
}
public boolean containsListenerInstance(Class clazz) {
for(OnApiChangeListener l : mOnApiChangedListeners) {
if (l.getClass().equals(clazz)) {
return true;
}
}
return false;
}
@Override
public URL getWebGuiUrl() {
throw new UnsupportedOperationException();
}
}

View file

@ -1,19 +0,0 @@
package com.nutomic.syncthingandroid.test;
import java.io.File;
public class Util {
/**
* Deletes the given folder and all contents.
*/
public static void deleteRecursive(File file) {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
deleteRecursive(f);
}
}
file.delete();
}
}

View file

@ -1,30 +0,0 @@
package com.nutomic.syncthingandroid.test.activities;
import android.support.test.rule.ActivityTestRule;
import com.nutomic.syncthingandroid.activities.MainActivity;
import com.nutomic.syncthingandroid.fragments.DeviceListFragment;
import com.nutomic.syncthingandroid.fragments.FolderListFragment;
import com.nutomic.syncthingandroid.service.SyncthingServiceBinder;
import com.nutomic.syncthingandroid.test.MockSyncthingService;
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();
@Test
public void testOnServiceConnected() {
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

@ -1,119 +0,0 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.receiver.AppConfigReceiver;
import com.nutomic.syncthingandroid.service.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
*/
public class AppConfigReceiverTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private AppConfigReceiver mReceiver;
private MockContext mContext;
@Before
public void setUp() throws Exception {
mReceiver = new AppConfigReceiver();
mContext = new MockContext(InstrumentationRegistry.getTargetContext());
}
@After
public void tearDown() throws Exception {
PreferenceManager.getDefaultSharedPreferences(mContext).edit().clear().apply();
}
/**
* 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)
.apply();
Intent intent = new Intent(new Intent(mContext, AppConfigReceiver.class));
intent.setAction(AppConfigReceiver.ACTION_START);
mReceiver.onReceive(mContext, intent);
Assert.assertEquals("Start SyncthingService Background", 1, mContext.getReceivedIntents().size());
Assert.assertEquals("Start SyncthingService Background", SyncthingService.class.getName(),
mContext.getReceivedIntents().get(0).getComponent().getClassName());
}
/**
* 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)
.apply();
Intent intent = new Intent(new Intent(mContext, AppConfigReceiver.class));
intent.setAction(AppConfigReceiver.ACTION_STOP);
mReceiver.onReceive(mContext, intent);
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)
.apply();
Intent intent = new Intent(new Intent(mContext, AppConfigReceiver.class));
intent.setAction(AppConfigReceiver.ACTION_START);
mReceiver.onReceive(mContext, intent);
Assert.assertEquals("Start SyncthingService Foreround", 1, mContext.getReceivedIntents().size());
Assert.assertEquals("Start SyncthingService Foreround", SyncthingService.class.getName(),
mContext.getReceivedIntents().get(0).getComponent().getClassName());
}
/**
* 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)
.apply();
Intent intent = new Intent(new Intent(mContext, AppConfigReceiver.class));
intent.setAction(AppConfigReceiver.ACTION_STOP);
mReceiver.onReceive(mContext, intent);
Assert.assertEquals("Stop SyncthingService Foreround", 1, mContext.getStopServiceIntents().size());
Intent receivedIntent = mContext.getStopServiceIntents().get(0);
Assert.assertEquals("Stop SyncthingService Foreround", SyncthingService.class.getName(),
receivedIntent.getComponent().getClassName());
}
}

View file

@ -1,83 +0,0 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.receiver.BatteryReceiver;
import com.nutomic.syncthingandroid.service.DeviceStateHolder;
import com.nutomic.syncthingandroid.service.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;
public class BatteryReceiverTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private BatteryReceiver mReceiver;
private MockContext mContext;
@Before
public void setUp() throws Exception {
mReceiver = new BatteryReceiver();
mContext = new MockContext(InstrumentationRegistry.getTargetContext());
}
@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)
.apply();
Intent intent = new Intent(Intent.ACTION_POWER_CONNECTED);
mReceiver.onReceive(mContext, intent);
Assert.assertEquals(1, mContext.getReceivedIntents().size());
Intent receivedIntent = mContext.getReceivedIntents().get(0);
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)
.apply();
Intent intent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
mReceiver.onReceive(mContext, intent);
Assert.assertEquals(1, mContext.getReceivedIntents().size());
Intent receivedIntent = mContext.getReceivedIntents().get(0);
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(InstrumentationRegistry.getTargetContext())
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, false)
.apply();
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_CONNECTED));
Assert.assertEquals(0, mContext.getReceivedIntents().size());
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_DISCONNECTED));
Assert.assertEquals(0, mContext.getReceivedIntents().size());
}
}

View file

@ -1,65 +0,0 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.receiver.BootReceiver;
import com.nutomic.syncthingandroid.service.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 BootReceiver} starts the right service
* ({@link com.nutomic.syncthingandroid.service.SyncthingService}.
*/
public class BootReceiverTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private BootReceiver mReceiver;
private MockContext mContext;
@Before
public void setUp() throws Exception {
mReceiver = new BootReceiver();
mContext = new MockContext(InstrumentationRegistry.getTargetContext());
}
@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)
.apply();
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_BOOT_COMPLETED));
Assert.assertEquals(1, mContext.getReceivedIntents().size());
Intent receivedIntent = mContext.getReceivedIntents().get(0);
Assert.assertEquals(SyncthingService.class.getName(), receivedIntent.getComponent().getClassName());
mContext.clearReceivedIntents();
}
@Test
public void testOnlyRunInForeground() {
PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getTargetContext())
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, false)
.apply();
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_BOOT_COMPLETED));
Assert.assertEquals(0, mContext.getReceivedIntents().size());
}
}

View file

@ -1,54 +0,0 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.os.BatteryManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.service.DeviceStateHolder;
import com.nutomic.syncthingandroid.test.MockContext;
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;
@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);
Assert.assertFalse(mReceiver.isCharging());
i.putExtra(DeviceStateHolder.EXTRA_IS_CHARGING, true);
mReceiver.update(i);
Assert.assertTrue(mReceiver.isCharging());
}
@Test
public void testWifiConnected() {
Intent i = new Intent();
i.putExtra(DeviceStateHolder.EXTRA_IS_ALLOWED_NETWORK_CONNECTION, false);
mReceiver.update(i);
Assert.assertFalse(mReceiver.isAllowedNetworkConnection());
i.putExtra(DeviceStateHolder.EXTRA_IS_ALLOWED_NETWORK_CONNECTION, true);
mReceiver.update(i);
Assert.assertTrue(mReceiver.isAllowedNetworkConnection());
}
}

View file

@ -1,72 +0,0 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.preference.PreferenceManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.service.DeviceStateHolder;
import com.nutomic.syncthingandroid.receiver.NetworkReceiver;
import com.nutomic.syncthingandroid.service.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 NetworkReceiver}.
*
* 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 {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private NetworkReceiver mReceiver;
private MockContext mContext;
@Before
public void setUp() throws Exception {
mReceiver = new NetworkReceiver();
mContext = new MockContext(InstrumentationRegistry.getTargetContext());
}
@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)
.apply();
mReceiver.onReceive(mContext, new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
Assert.assertEquals(1, mContext.getReceivedIntents().size());
Intent receivedIntent = mContext.getReceivedIntents().get(0);
Assert.assertEquals(SyncthingService.class.getName(), receivedIntent.getComponent().getClassName());
Assert.assertNull(receivedIntent.getAction());
Assert.assertTrue(receivedIntent.hasExtra(DeviceStateHolder.EXTRA_IS_ALLOWED_NETWORK_CONNECTION));
mContext.clearReceivedIntents();
}
@Test
public void testOnlyRunInForeground() {
PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getTargetContext())
.edit()
.putBoolean(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND, false)
.apply();
mReceiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_CONNECTED));
Assert.assertEquals(0, mContext.getReceivedIntents().size());
}
}

View file

@ -1,133 +0,0 @@
package com.nutomic.syncthingandroid.test.syncthing;
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.service.RestApi;
import com.nutomic.syncthingandroid.service.SyncthingRunnable;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.test.MockContext;
import com.nutomic.syncthingandroid.util.ConfigXml;
import com.nutomic.syncthingandroid.util.Util;
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 {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private RestApi mApi;
@Before
public void setUp() throws Exception {
Context context = InstrumentationRegistry.getTargetContext();
new SyncthingRunnable(context, SyncthingRunnable.Command.main);
ConfigXml config = new ConfigXml(context);
String httpsCertPath = context.getFilesDir() + "/" + SyncthingService.HTTPS_CERT_FILE;
final CountDownLatch latch = new CountDownLatch(2);
new PollWebGuiAvailableTask(context, config.getWebGuiUrl(), httpsCertPath, config.getApiKey(), result -> {
mApi.onWebGuiAvailable();
latch.countDown();
});
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));
}
@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() {
Assert.assertNotNull(mApi.getDevices(false));
}
@Test
public void testGetSystemInfo() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mApi.getSystemInfo((info) -> {
Assert.assertNotNull(info);
latch.countDown();
});
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Test
public void testGetSystemVersion() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mApi.getSystemVersion(info -> {
Assert.assertNotNull(info);
latch.countDown();
});
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Test
public void testGetFolders() {
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) {
Assert.assertNotSame("", Util.readableFileSize(InstrumentationRegistry.getTargetContext(), l));
Assert.assertNotSame("", Util.readableTransferRate(InstrumentationRegistry.getTargetContext(), l));
}
}
@Test
public void testGetConnections() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mApi.getConnections(connections -> {
Assert.assertNotNull(connections);
latch.countDown();
});
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Test
public void testGetModel() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mApi.getModel("camera", (folderId, model) -> {
Assert.assertNotNull(model);
latch.countDown();
});
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", id -> {
Assert.assertEquals("P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2", id);
latch.countDown();
}, error -> Assert.fail());
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
}

View file

@ -1,35 +0,0 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.service.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 {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
@Test
public void testRunning() throws InterruptedException {
MockContext context = new MockContext(InstrumentationRegistry.getTargetContext());
File testFile = new File(context.getFilesDir(), SyncthingRunnable.UNIT_TEST_PATH);
Assert.assertFalse(testFile.exists());
// Inject a different command instead of the Syncthing binary for testing.
new SyncthingRunnable(context, new String[]{"touch", testFile.getAbsolutePath()}).run();
Assert.assertTrue(testFile.exists());
testFile.delete();
}
}

View file

@ -1,24 +0,0 @@
package com.nutomic.syncthingandroid.test.syncthing;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.service.SyncthingServiceBinder;
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);
Assert.assertEquals(service, binder.getService());
}
}

View file

@ -1,105 +0,0 @@
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;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.service.SyncthingServiceBinder;
import com.nutomic.syncthingandroid.util.ConfigXml;
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.TimeoutException;
/**
* These tests assume that syncthing keys have already been generated. If not, tests may fail
* because startup takes too long.
*/
public class SyncthingServiceTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private SyncthingService mService;
@Before
public void setup() throws TimeoutException {
Intent intent =
new Intent(InstrumentationRegistry.getTargetContext(), SyncthingService.class);
// 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
public void testFirstStart() {
Assert.assertTrue(mService.isFirstStart());
}
@Test
public void testNotFirstStart() throws IOException {
new File(mService.getFilesDir(), SyncthingService.PUBLIC_KEY_FILE).createNewFile();
Assert.assertFalse(mService.isFirstStart());
}
@Test
public void testImportExportConfig() {
File config = new File(mService.getFilesDir(), ConfigXml.CONFIG_FILE);
File privateKey = new File(mService.getFilesDir(), SyncthingService.PRIVATE_KEY_FILE);
File publicKey = new File(mService.getFilesDir(), SyncthingService.PUBLIC_KEY_FILE);
try {
config.createNewFile();
privateKey.createNewFile();
publicKey.createNewFile();
} catch (IOException e) {
Assert.fail();
}
mService.exportConfig();
config.delete();
privateKey.delete();
publicKey.delete();
Assert.assertTrue(mService.importConfig());
Assert.assertTrue(config.exists());
Assert.assertTrue(privateKey.exists());
Assert.assertTrue(publicKey.exists());
}
@Test
public void testPassword() throws InterruptedException {
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,42 +0,0 @@
package com.nutomic.syncthingandroid.test.util;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.test.MockContext;
import com.nutomic.syncthingandroid.util.ConfigXml;
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;
@Before
public void setUp() throws Exception {
mContext = new MockContext(InstrumentationRegistry.getTargetContext());
Assert.assertFalse(ConfigXml.getConfigFile(mContext).exists());
mConfig = new ConfigXml(mContext);
Assert.assertTrue(ConfigXml.getConfigFile(mContext).exists());
}
@After
public void tearDown() throws Exception {
ConfigXml.getConfigFile(mContext).delete();
}
@Test
public void testGetWebGuiUrl() {
Assert.assertTrue(mConfig.getWebGuiUrl().toString().startsWith("https://127.0.0.1:"));
}
}

View file

@ -1,120 +0,0 @@
package com.nutomic.syncthingandroid.test.util;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import com.nutomic.syncthingandroid.model.Folder;
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 implements FolderObserver.OnFolderFileChangeListener {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private File mTestFolder;
private String mCurrentTest;
private CountDownLatch mLatch;
@Before
public void setUp() throws Exception {
mTestFolder = new File(new MockContext(InstrumentationRegistry.getTargetContext()).getFilesDir(), "observer-test");
mTestFolder.mkdir();
}
@After
public void tearDown() throws Exception {
Util.deleteRecursive(mTestFolder);
}
@Override
public void onFolderFileChange(String folderId, String relativePath) {
mLatch.countDown();
Assert.assertEquals(mCurrentTest, folderId);
Assert.assertFalse(relativePath.endsWith("should-not-notifiy"));
}
private Folder createFolder(String id) {
Folder r = new Folder();
r.path = mTestFolder.getPath();
r.id = id;
return r;
}
@Test
public void testRecursion() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testRecursion";
File subFolder = new File(mTestFolder, "subfolder");
subFolder.mkdir();
FolderObserver fo = new FolderObserver(this, createFolder(mCurrentTest));
File testFile = new File(subFolder, "test");
mLatch = new CountDownLatch(1);
testFile.createNewFile();
Assert.assertTrue(mLatch.await(1, TimeUnit.SECONDS));
fo.stopWatching();
}
@Test
public void testRemoveFile() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testRemoveFile";
File test = new File(mTestFolder, "test");
test.createNewFile();
FolderObserver fo = new FolderObserver(this, createFolder(mCurrentTest));
mLatch = new CountDownLatch(1);
test.delete();
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";
File subFolder = new File(mTestFolder, "subfolder");
subFolder.mkdir();
File testFile = new File(subFolder, "test");
FolderObserver fo = new FolderObserver(this, createFolder(mCurrentTest));
mLatch = new CountDownLatch(1);
testFile.createNewFile();
Assert.assertTrue(mLatch.await(1, TimeUnit.SECONDS));
Assert.assertEquals(0, mLatch.getCount());
fo.stopWatching();
}
@Test
public void testNotExisting() throws IOException, InterruptedException {
Folder r = new Folder();
r.path = new File(new MockContext(InstrumentationRegistry.getTargetContext()).getFilesDir(), "not-existing").getPath();
r.id = "testNotExisting";
try {
new FolderObserver(this, r);
Assert.fail("Expected FolderNotExistingException");
} catch (FolderObserver.FolderNotExistingException e) {
Assert.assertTrue(e.getMessage().contains(r.path));
}
}
}

View file

@ -10,7 +10,6 @@ import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.util.Log;

View file

@ -5,7 +5,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.util.Log;
import com.nutomic.syncthingandroid.service.DeviceStateHolder;
import com.nutomic.syncthingandroid.service.SyncthingService;

View file

@ -7,7 +7,6 @@ import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;
import com.nutomic.syncthingandroid.service.DeviceStateHolder;
import com.nutomic.syncthingandroid.service.SyncthingService;
@ -17,8 +16,6 @@ import com.nutomic.syncthingandroid.service.SyncthingService;
*/
public class NetworkReceiver extends BroadcastReceiver {
private static final String TAG = "NetworkReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (!ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))

View file

@ -54,14 +54,6 @@ public class DeviceStateHolder {
NetworkReceiver.updateNetworkStatus(mContext);
}
public boolean isCharging() {
return mIsCharging;
}
public boolean isAllowedNetworkConnection() {
return mIsAllowedNetworkConnection;
}
public void update(Intent intent) {
mIsAllowedNetworkConnection =
intent.getBooleanExtra(EXTRA_IS_ALLOWED_NETWORK_CONNECTION, mIsAllowedNetworkConnection);
@ -98,7 +90,7 @@ public class DeviceStateHolder {
boolean prefStopMobileData = mPreferences.getBoolean(SyncthingService.PREF_SYNC_ONLY_WIFI, false);
boolean prefStopNotCharging = mPreferences.getBoolean(SyncthingService.PREF_SYNC_ONLY_CHARGING, false);
return (isCharging() || !prefStopNotCharging) &&
return (mIsCharging || !prefStopNotCharging) &&
(!prefStopMobileData || isWhitelistedNetworkConnection());
}
else {
@ -107,7 +99,7 @@ public class DeviceStateHolder {
}
private boolean isWhitelistedNetworkConnection() {
boolean wifiConnected = isAllowedNetworkConnection();
boolean wifiConnected = mIsAllowedNetworkConnection;
if (wifiConnected) {
Set<String> ssids = mPreferences.getStringSet(SyncthingService.PREF_SYNC_ONLY_WIFI_SSIDS, new HashSet<>());
if (ssids.isEmpty()) {

View file

@ -45,7 +45,6 @@ public class SyncthingRunnable implements Runnable {
private static final String TAG_NATIVE = "SyncthingNativeCode";
private static final String TAG_NICE = "SyncthingRunnableIoNice";
private static final String TAG_KILL = "SyncthingRunnableKill";
public static final String UNIT_TEST_PATH = "was running";
private static final String BINARY_NAME = "libsyncthing.so";
private static final int LOG_FILE_MAX_LINES = 10;
private static final int NOTIFICATION_ID_CRASH = 9;
@ -90,20 +89,6 @@ public class SyncthingRunnable implements Runnable {
}
}
/**
* Constructs instance.
*
* @param manualCommand The exact command to be executed on the shell. Used for tests only.
*/
public SyncthingRunnable(Context context, String[] manualCommand) {
mContext = context;
mSyncthingBinary = mContext.getApplicationInfo().nativeLibraryDir + "/" + BINARY_NAME;
mCommand = manualCommand;
mLogFile = new File(mContext.getExternalFilesDir(null), "syncthing.log");
mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
mUseRoot = false;
}
@Override
public void run() {
trimLogFile();

View file

@ -38,7 +38,6 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Holds the native syncthing instance and provides an API to access it.
@ -443,15 +442,6 @@ public class SyncthingService extends Service implements
}
}
/**
* Returns true if this service has not been started before (ie config.xml does not exist).
*
* This will return true until the public key file has been generated.
*/
public boolean isFirstStart() {
return !new File(getFilesDir(), PUBLIC_KEY_FILE).exists();
}
public @Nullable RestApi getApi() {
return mApi;
}

View file

@ -11,7 +11,6 @@ import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import com.nutomic.syncthingandroid.R;
@ -27,7 +26,6 @@ import java.util.TreeMap;
* Based on https://gitlab.com/fdroid/fdroidclient/blob/master/app/src/main/java/org/fdroid/fdroid/Languages.java
*/
public final class Languages {
private static final String TAG = "Languages";
public static final String USE_SYSTEM_DEFAULT = "";