Skip to content

Commit 47a4c12

Browse files
committed
Added unit tests for BluetoothLeDeviceStore.java
1 parent 81d984e commit 47a4c12

File tree

3 files changed

+121
-10
lines changed

3 files changed

+121
-10
lines changed

sample_app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ dependencies {
3131
compile 'uk.co.alt236:easycursor-android:1.0.0'
3232
compile fileTree(include: ['*.jar'], dir: 'libs')
3333
compile project(':library')
34+
35+
testCompile 'junit:junit:4.12'
36+
testCompile 'org.mockito:mockito-all:1.9.5'
3437
}
3538

3639
android {
Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package uk.co.alt236.btlescan.containers;
22

3+
import android.support.annotation.NonNull;
4+
35
import java.util.ArrayList;
46
import java.util.Collections;
57
import java.util.Comparator;
@@ -11,14 +13,14 @@
1113
import uk.co.alt236.easycursor.objectcursor.EasyObjectCursor;
1214

1315
public class BluetoothLeDeviceStore {
16+
private static final BluetoothLeDeviceComparator DEFAULT_COMPARATOR = new BluetoothLeDeviceComparator();
1417
private final Map<String, BluetoothLeDevice> mDeviceMap;
1518

16-
1719
public BluetoothLeDeviceStore() {
1820
mDeviceMap = new HashMap<>();
1921
}
2022

21-
public void addDevice(final BluetoothLeDevice device) {
23+
public void addDevice(@NonNull final BluetoothLeDevice device) {
2224
if (mDeviceMap.containsKey(device.getAddress())) {
2325
mDeviceMap.get(device.getAddress()).updateRssiReading(device.getTimestamp(), device.getRssi());
2426
} else {
@@ -30,24 +32,42 @@ public void clear() {
3032
mDeviceMap.clear();
3133
}
3234

35+
public int getSize() {
36+
return mDeviceMap.size();
37+
}
38+
39+
@NonNull
3340
public EasyObjectCursor<BluetoothLeDevice> getDeviceCursor() {
41+
return getDeviceCursor(DEFAULT_COMPARATOR);
42+
}
43+
44+
@NonNull
45+
public EasyObjectCursor<BluetoothLeDevice> getDeviceCursor(@NonNull Comparator<BluetoothLeDevice> comparator) {
3446
return new EasyObjectCursor<>(
3547
BluetoothLeDevice.class,
36-
getDeviceList(),
48+
getDeviceList(comparator),
3749
"address");
3850
}
3951

52+
@NonNull
4053
public List<BluetoothLeDevice> getDeviceList() {
41-
final List<BluetoothLeDevice> methodResult = new ArrayList<>(mDeviceMap.values());
54+
return getDeviceList(DEFAULT_COMPARATOR);
55+
}
4256

43-
Collections.sort(methodResult, new Comparator<BluetoothLeDevice>() {
57+
@NonNull
58+
public List<BluetoothLeDevice> getDeviceList(@NonNull Comparator<BluetoothLeDevice> comparator) {
59+
final List<BluetoothLeDevice> methodResult = new ArrayList<>(mDeviceMap.values());
4460

45-
@Override
46-
public int compare(final BluetoothLeDevice arg0, final BluetoothLeDevice arg1) {
47-
return arg0.getAddress().compareToIgnoreCase(arg1.getAddress());
48-
}
49-
});
61+
Collections.sort(methodResult, comparator);
5062

5163
return methodResult;
5264
}
65+
66+
private static class BluetoothLeDeviceComparator implements Comparator<BluetoothLeDevice> {
67+
68+
@Override
69+
public int compare(final BluetoothLeDevice arg0, final BluetoothLeDevice arg1) {
70+
return arg0.getAddress().compareTo(arg1.getAddress());
71+
}
72+
}
5373
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package uk.co.alt236.btlescan.containers;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.mockito.Mockito;
6+
7+
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertSame;
11+
12+
public class BluetoothLeDeviceStoreTest {
13+
private BluetoothLeDeviceStore cut;
14+
15+
@Before
16+
public void setUp() {
17+
cut = new BluetoothLeDeviceStore();
18+
}
19+
20+
@Test
21+
public void testAddOne() {
22+
assertStoreSize(0);
23+
24+
cut.addDevice(createDevice("foo"));
25+
assertStoreSize(1);
26+
27+
cut.clear();
28+
assertStoreSize(0);
29+
}
30+
31+
@Test
32+
public void testAddTwo() {
33+
assertStoreSize(0);
34+
35+
cut.addDevice(createDevice("foo"));
36+
assertStoreSize(1);
37+
38+
cut.addDevice(createDevice("bar"));
39+
assertStoreSize(2);
40+
41+
cut.clear();
42+
assertStoreSize(0);
43+
}
44+
45+
@Test
46+
public void testUpdateOne() {
47+
assertStoreSize(0);
48+
49+
cut.addDevice(createDevice("foo", 100, 101));
50+
assertStoreSize(1);
51+
final BluetoothLeDevice device1 = cut.getDeviceList().get(0);
52+
assertEquals(100, device1.getTimestamp());
53+
assertEquals(101, device1.getRssi());
54+
55+
cut.addDevice(createDevice("foo", 200, 201));
56+
assertStoreSize(1);
57+
final BluetoothLeDevice device2 = cut.getDeviceList().get(0);
58+
assertSame(device1, device2);
59+
Mockito
60+
.verify(device2, Mockito.times(1))
61+
.updateRssiReading(200, 201);
62+
63+
cut.clear();
64+
assertStoreSize(0);
65+
}
66+
67+
68+
private BluetoothLeDevice createDevice(final String mac) {
69+
final BluetoothLeDevice mock = Mockito.mock(BluetoothLeDevice.class);
70+
return createDevice(mac, 0, 0);
71+
}
72+
73+
private BluetoothLeDevice createDevice(final String mac, long rssiTime, int rssi) {
74+
final BluetoothLeDevice mock = Mockito.mock(BluetoothLeDevice.class);
75+
76+
Mockito.when(mock.getAddress()).thenReturn(mac);
77+
Mockito.when(mock.getTimestamp()).thenReturn(rssiTime);
78+
Mockito.when(mock.getRssi()).thenReturn(rssi);
79+
80+
return mock;
81+
}
82+
83+
private void assertStoreSize(final int expected) {
84+
assertEquals(expected, cut.getSize());
85+
assertEquals(expected, cut.getDeviceCursor().getCount());
86+
assertEquals(expected, cut.getDeviceList().size());
87+
}
88+
}

0 commit comments

Comments
 (0)