diff --git a/sample_app/build.gradle b/sample_app/build.gradle index 7e395bf..5cbefef 100644 --- a/sample_app/build.gradle +++ b/sample_app/build.gradle @@ -31,6 +31,9 @@ dependencies { compile 'uk.co.alt236:easycursor-android:1.0.0' compile fileTree(include: ['*.jar'], dir: 'libs') compile project(':library') + + testCompile 'junit:junit:4.12' + testCompile 'org.mockito:mockito-all:1.9.5' } android { diff --git a/sample_app/src/main/java/uk/co/alt236/btlescan/containers/BluetoothLeDeviceStore.java b/sample_app/src/main/java/uk/co/alt236/btlescan/containers/BluetoothLeDeviceStore.java index acc0820..c591de7 100644 --- a/sample_app/src/main/java/uk/co/alt236/btlescan/containers/BluetoothLeDeviceStore.java +++ b/sample_app/src/main/java/uk/co/alt236/btlescan/containers/BluetoothLeDeviceStore.java @@ -1,5 +1,7 @@ package uk.co.alt236.btlescan.containers; +import android.support.annotation.NonNull; + import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -11,14 +13,14 @@ import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice; import uk.co.alt236.easycursor.objectcursor.EasyObjectCursor; public class BluetoothLeDeviceStore { + private static final BluetoothLeDeviceComparator DEFAULT_COMPARATOR = new BluetoothLeDeviceComparator(); private final Map mDeviceMap; - public BluetoothLeDeviceStore() { mDeviceMap = new HashMap<>(); } - public void addDevice(final BluetoothLeDevice device) { + public void addDevice(@NonNull final BluetoothLeDevice device) { if (mDeviceMap.containsKey(device.getAddress())) { mDeviceMap.get(device.getAddress()).updateRssiReading(device.getTimestamp(), device.getRssi()); } else { @@ -30,24 +32,42 @@ public class BluetoothLeDeviceStore { mDeviceMap.clear(); } + public int getSize() { + return mDeviceMap.size(); + } + + @NonNull public EasyObjectCursor getDeviceCursor() { + return getDeviceCursor(DEFAULT_COMPARATOR); + } + + @NonNull + public EasyObjectCursor getDeviceCursor(@NonNull Comparator comparator) { return new EasyObjectCursor<>( BluetoothLeDevice.class, - getDeviceList(), + getDeviceList(comparator), "address"); } + @NonNull public List getDeviceList() { + return getDeviceList(DEFAULT_COMPARATOR); + } + + @NonNull + public List getDeviceList(@NonNull Comparator comparator) { final List methodResult = new ArrayList<>(mDeviceMap.values()); - Collections.sort(methodResult, new Comparator() { - - @Override - public int compare(final BluetoothLeDevice arg0, final BluetoothLeDevice arg1) { - return arg0.getAddress().compareToIgnoreCase(arg1.getAddress()); - } - }); + Collections.sort(methodResult, comparator); return methodResult; } + + private static class BluetoothLeDeviceComparator implements Comparator { + + @Override + public int compare(final BluetoothLeDevice arg0, final BluetoothLeDevice arg1) { + return arg0.getAddress().compareTo(arg1.getAddress()); + } + } } diff --git a/sample_app/src/test/java/uk/co/alt236/btlescan/containers/BluetoothLeDeviceStoreTest.java b/sample_app/src/test/java/uk/co/alt236/btlescan/containers/BluetoothLeDeviceStoreTest.java new file mode 100644 index 0000000..0ab71e0 --- /dev/null +++ b/sample_app/src/test/java/uk/co/alt236/btlescan/containers/BluetoothLeDeviceStoreTest.java @@ -0,0 +1,88 @@ +package uk.co.alt236.btlescan.containers; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +public class BluetoothLeDeviceStoreTest { + private BluetoothLeDeviceStore cut; + + @Before + public void setUp() { + cut = new BluetoothLeDeviceStore(); + } + + @Test + public void testAddOne() { + assertStoreSize(0); + + cut.addDevice(createDevice("foo")); + assertStoreSize(1); + + cut.clear(); + assertStoreSize(0); + } + + @Test + public void testAddTwo() { + assertStoreSize(0); + + cut.addDevice(createDevice("foo")); + assertStoreSize(1); + + cut.addDevice(createDevice("bar")); + assertStoreSize(2); + + cut.clear(); + assertStoreSize(0); + } + + @Test + public void testUpdateOne() { + assertStoreSize(0); + + cut.addDevice(createDevice("foo", 100, 101)); + assertStoreSize(1); + final BluetoothLeDevice device1 = cut.getDeviceList().get(0); + assertEquals(100, device1.getTimestamp()); + assertEquals(101, device1.getRssi()); + + cut.addDevice(createDevice("foo", 200, 201)); + assertStoreSize(1); + final BluetoothLeDevice device2 = cut.getDeviceList().get(0); + assertSame(device1, device2); + Mockito + .verify(device2, Mockito.times(1)) + .updateRssiReading(200, 201); + + cut.clear(); + assertStoreSize(0); + } + + + private BluetoothLeDevice createDevice(final String mac) { + final BluetoothLeDevice mock = Mockito.mock(BluetoothLeDevice.class); + return createDevice(mac, 0, 0); + } + + private BluetoothLeDevice createDevice(final String mac, long rssiTime, int rssi) { + final BluetoothLeDevice mock = Mockito.mock(BluetoothLeDevice.class); + + Mockito.when(mock.getAddress()).thenReturn(mac); + Mockito.when(mock.getTimestamp()).thenReturn(rssiTime); + Mockito.when(mock.getRssi()).thenReturn(rssi); + + return mock; + } + + private void assertStoreSize(final int expected) { + assertEquals(expected, cut.getSize()); + assertEquals(expected, cut.getDeviceCursor().getCount()); + assertEquals(expected, cut.getDeviceList().size()); + } +} \ No newline at end of file