aboutsummaryrefslogtreecommitdiff
path: root/bt/src/test/java/io/trygvis/android/bt/BtSequencerTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'bt/src/test/java/io/trygvis/android/bt/BtSequencerTest.java')
-rw-r--r--bt/src/test/java/io/trygvis/android/bt/BtSequencerTest.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/bt/src/test/java/io/trygvis/android/bt/BtSequencerTest.java b/bt/src/test/java/io/trygvis/android/bt/BtSequencerTest.java
new file mode 100644
index 0000000..862eda5
--- /dev/null
+++ b/bt/src/test/java/io/trygvis/android/bt/BtSequencerTest.java
@@ -0,0 +1,96 @@
+package io.trygvis.android.bt;
+
+import android.bluetooth.BluetoothGatt;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static io.trygvis.android.bt.BtSequence.SequenceResult.continueDirectly;
+import static io.trygvis.android.bt.BtSequence.SequenceResult.detour;
+import static io.trygvis.android.bt.BtSequence.SequenceResult.stop;
+import static org.fest.assertions.Assertions.assertThat;
+
+@RunWith(JUnit4.class)
+public class BtSequencerTest {
+
+ public static final String ADDRESS = "AA:BB:CC:DD:EE:FF";
+
+ @Test
+ public void eventAndFinally() {
+ List<String> events = new ArrayList<>();
+
+ BtSequence seq = new BtSequence().onConnectionStateChange((gatt, newState, status) -> {
+ events.add("onConnectionStateChange");
+ return stop();
+ }).onFinally(success -> events.add("finally: " + success));
+
+ BtSequencer sequencer = new BtSequencer(ADDRESS, seq);
+
+ sequencer.onEvent(BtSequencer.EventType.onConnectionStateChange, null, null, null, null,
+ BluetoothGatt.GATT_SUCCESS, BluetoothGatt.STATE_CONNECTED);
+
+ assertThat(events).containsSequence(
+ "onConnectionStateChange",
+ "finally: true"
+ );
+ }
+
+ @Test
+ public void detours() {
+ List<String> events = new ArrayList<>();
+
+ BtSequence tour = new BtSequence().onDirect(gatt -> {
+ events.add("tour: onDirect");
+ return stop();
+ }).onFinally(success -> events.add("tour: finally: " + success));
+
+ BtSequence seq = new BtSequence().onConnectionStateChange((gatt, newState, status) -> {
+ events.add("onConnectionStateChange");
+ return detour(tour.andThen(tour));
+ }).onFinally(success -> events.add("finally: " + success));
+
+ BtSequencer sequencer = new BtSequencer(ADDRESS, seq);
+
+ sequencer.onEvent(BtSequencer.EventType.onConnectionStateChange, null, null, null, null,
+ BluetoothGatt.GATT_SUCCESS, BluetoothGatt.STATE_CONNECTED);
+
+ assertThat(events).containsSequence(
+ "onConnectionStateChange",
+ "tour: onDirect",
+ "tour: finally: true",
+ "finally: true"
+ );
+ }
+
+ @Test
+ public void andThen() {
+ List<String> events = new ArrayList<>();
+
+ BtSequence a = new BtSequence().onConnectionStateChange((gatt, newState, status) -> {
+ events.add("a");
+ return continueDirectly();
+ }).onFinally(success -> events.add("a: finally: " + success));
+ BtSequence b = new BtSequence().onDirect(gatt -> {
+ events.add("b");
+ return stop();
+ }).onFinally(success -> events.add("b: finally: " + success));
+
+ BtSequence seq = a.andThen(b);
+
+ BtSequencer sequencer = new BtSequencer(ADDRESS, seq);
+
+ sequencer.onEvent(BtSequencer.EventType.onConnectionStateChange, null, null, null, null,
+ BluetoothGatt.GATT_SUCCESS, BluetoothGatt.STATE_CONNECTED);
+
+ assertThat(events).containsSequence(
+ "a",
+ "a: finally: true",
+ "b",
+ "b: finally: true"
+ );
+ }
+}