mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-11-22 20:31:16 +00:00
Added unit tests for BroadcastReceivers.
This commit is contained in:
parent
b1749ce7cb
commit
2e83305b93
7 changed files with 194 additions and 0 deletions
|
@ -33,6 +33,10 @@ android {
|
|||
|
||||
defaultConfig {
|
||||
versionCode getVersionCodeFromManifest()
|
||||
testApplicationId 'com.nutomic.syncthingandroid.test'
|
||||
testInstrumentationRunner 'android.test.InstrumentationTestRunner'
|
||||
testHandleProfiling true
|
||||
testFunctionalTest true
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package com.nutomic.syncthingandroid.test;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.test.mock.MockContext;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Context that saves all received intents, which can be retrieved later by test classes.
|
||||
*/
|
||||
public class TestContext extends MockContext {
|
||||
|
||||
private Context mContext;
|
||||
private ArrayList<Intent> mReceivedIntents = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Use the actual context for calls that aren't easily mocked. May be null if those
|
||||
* calls aren't needed.
|
||||
*/
|
||||
public TestContext(Context context) {
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPackageName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getSystemService(String name) {
|
||||
return mContext.getSystemService(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ComponentName startService(Intent intent) {
|
||||
mReceivedIntents.add(intent);
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Intent> getReceivedIntents() {
|
||||
return mReceivedIntents;
|
||||
}
|
||||
|
||||
public void clearReceivedIntents() {
|
||||
mReceivedIntents.clear();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.nutomic.syncthingandroid.test.syncthing;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.test.suitebuilder.annotation.MediumTest;
|
||||
|
||||
import com.nutomic.syncthingandroid.syncthing.BatteryReceiver;
|
||||
import com.nutomic.syncthingandroid.syncthing.DeviceStateHolder;
|
||||
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
|
||||
import com.nutomic.syncthingandroid.test.TestContext;
|
||||
|
||||
public class BatteryReceiverTest extends AndroidTestCase {
|
||||
|
||||
private BatteryReceiver mReceiver;
|
||||
private TestContext mContext;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
mReceiver = new BatteryReceiver();
|
||||
mContext = new TestContext(null);
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testOnReceiveCharging() {
|
||||
Intent intent = new Intent(Intent.ACTION_POWER_CONNECTED);
|
||||
|
||||
mReceiver.onReceive(mContext, intent);
|
||||
assertEquals(1, mContext.getReceivedIntents().size());
|
||||
|
||||
Intent receivedIntent = mContext.getReceivedIntents().get(0);
|
||||
assertTrue(receivedIntent.getBooleanExtra(DeviceStateHolder.EXTRA_IS_CHARGING, false));
|
||||
mContext.clearReceivedIntents();
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testOnReceiveNotCharging() {
|
||||
Intent intent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
|
||||
|
||||
mReceiver.onReceive(mContext, intent);
|
||||
assertEquals(1, mContext.getReceivedIntents().size());
|
||||
|
||||
Intent receivedIntent = mContext.getReceivedIntents().get(0);
|
||||
assertEquals(receivedIntent.getComponent().getClassName(), SyncthingService.class.getName());
|
||||
assertFalse(receivedIntent.getBooleanExtra(DeviceStateHolder.EXTRA_IS_CHARGING, true));
|
||||
mContext.clearReceivedIntents();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.nutomic.syncthingandroid.test.syncthing;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.test.suitebuilder.annotation.MediumTest;
|
||||
|
||||
import com.nutomic.syncthingandroid.syncthing.BootReceiver;
|
||||
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
|
||||
import com.nutomic.syncthingandroid.test.TestContext;
|
||||
|
||||
/**
|
||||
* Tests that {@link com.nutomic.syncthingandroid.syncthing.BootReceiver} starts the right service
|
||||
* ({@link com.nutomic.syncthingandroid.syncthing.SyncthingService}.
|
||||
*/
|
||||
public class BootReceiverTest extends AndroidTestCase {
|
||||
|
||||
private BootReceiver mReceiver;
|
||||
private TestContext mContext;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
mReceiver = new BootReceiver();
|
||||
mContext = new TestContext(null);
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testOnReceiveCharging() {
|
||||
mReceiver.onReceive(mContext, null);
|
||||
assertEquals(1, mContext.getReceivedIntents().size());
|
||||
|
||||
Intent receivedIntent = mContext.getReceivedIntents().get(0);
|
||||
assertEquals(receivedIntent.getComponent().getClassName(), SyncthingService.class.getName());
|
||||
mContext.clearReceivedIntents();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.nutomic.syncthingandroid.test.syncthing;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.test.suitebuilder.annotation.MediumTest;
|
||||
|
||||
import com.nutomic.syncthingandroid.syncthing.NetworkReceiver;
|
||||
import com.nutomic.syncthingandroid.syncthing.DeviceStateHolder;
|
||||
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
|
||||
import com.nutomic.syncthingandroid.test.TestContext;
|
||||
|
||||
/**
|
||||
* Tests for correct extras on the Intent sent by
|
||||
* {@link com.nutomic.syncthingandroid.syncthing.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 extends AndroidTestCase {
|
||||
|
||||
private NetworkReceiver mReceiver;
|
||||
private TestContext mContext;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
mReceiver = new NetworkReceiver();
|
||||
mContext = new TestContext(getContext());
|
||||
}
|
||||
|
||||
@MediumTest
|
||||
public void testOnReceive() {
|
||||
mReceiver.onReceive(mContext, null);
|
||||
assertEquals(1, mContext.getReceivedIntents().size());
|
||||
|
||||
Intent receivedIntent = mContext.getReceivedIntents().get(0);
|
||||
assertEquals(receivedIntent.getComponent().getClassName(), SyncthingService.class.getName());
|
||||
assertNull(receivedIntent.getAction());
|
||||
assertTrue(receivedIntent.hasExtra(DeviceStateHolder.EXTRA_HAS_WIFI));
|
||||
mContext.clearReceivedIntents();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.nutomic.syncthingandroid.test.syncthing;
|
||||
|
||||
/**
|
||||
* Created by felix on 22.08.14.
|
||||
*/
|
||||
public class SyncthingServiceBinderTest {
|
||||
}
|
|
@ -5,8 +5,10 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
|
||||
public class BootReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
context.startService(new Intent(context, SyncthingService.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue