From 2e83305b93a24344e89342bddd168cccbc42c1de Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 22 Aug 2014 14:38:11 +0200 Subject: [PATCH] Added unit tests for BroadcastReceivers. --- build.gradle | 4 ++ .../syncthingandroid/test/TestContext.java | 53 +++++++++++++++++++ .../test/syncthing/BatteryReceiverTest.java | 49 +++++++++++++++++ .../test/syncthing/BootReceiverTest.java | 36 +++++++++++++ .../test/syncthing/NetworkReceiverTest.java | 43 +++++++++++++++ .../syncthing/SyncthingServiceBinderTest.java | 7 +++ .../syncthing/BootReceiver.java | 2 + 7 files changed, 194 insertions(+) create mode 100644 src/androidTest/java/com/nutomic/syncthingandroid/test/TestContext.java create mode 100644 src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/BatteryReceiverTest.java create mode 100644 src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/BootReceiverTest.java create mode 100644 src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/NetworkReceiverTest.java create mode 100644 src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingServiceBinderTest.java diff --git a/build.gradle b/build.gradle index 5ffdae23..80993b9c 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,10 @@ android { defaultConfig { versionCode getVersionCodeFromManifest() + testApplicationId 'com.nutomic.syncthingandroid.test' + testInstrumentationRunner 'android.test.InstrumentationTestRunner' + testHandleProfiling true + testFunctionalTest true } sourceSets { diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/TestContext.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/TestContext.java new file mode 100644 index 00000000..d9097124 --- /dev/null +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/TestContext.java @@ -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 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 getReceivedIntents() { + return mReceivedIntents; + } + + public void clearReceivedIntents() { + mReceivedIntents.clear(); + } + +} diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/BatteryReceiverTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/BatteryReceiverTest.java new file mode 100644 index 00000000..901af5ab --- /dev/null +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/BatteryReceiverTest.java @@ -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(); + } + +} diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/BootReceiverTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/BootReceiverTest.java new file mode 100644 index 00000000..f608a009 --- /dev/null +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/BootReceiverTest.java @@ -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(); + } +} diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/NetworkReceiverTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/NetworkReceiverTest.java new file mode 100644 index 00000000..5cf15889 --- /dev/null +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/NetworkReceiverTest.java @@ -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(); + } + +} diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingServiceBinderTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingServiceBinderTest.java new file mode 100644 index 00000000..264995b4 --- /dev/null +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingServiceBinderTest.java @@ -0,0 +1,7 @@ +package com.nutomic.syncthingandroid.test.syncthing; + +/** + * Created by felix on 22.08.14. + */ +public class SyncthingServiceBinderTest { +} diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/BootReceiver.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/BootReceiver.java index 82769616..5cfc756f 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/BootReceiver.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/BootReceiver.java @@ -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)); } + }