diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-28 23:45:38 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-28 23:45:38 +0100 |
commit | 3e619a735e63a1222e71060d9e65b354a156b158 (patch) | |
tree | 2f31cdc65d0d773874800c114530eb620bcca536 /bt/src/test | |
parent | c4685214d8db34166213ffa373a16af1a99401a5 (diff) | |
download | io.trygvis.soilmoisture-android-3e619a735e63a1222e71060d9e65b354a156b158.tar.gz io.trygvis.soilmoisture-android-3e619a735e63a1222e71060d9e65b354a156b158.tar.bz2 io.trygvis.soilmoisture-android-3e619a735e63a1222e71060d9e65b354a156b158.tar.xz io.trygvis.soilmoisture-android-3e619a735e63a1222e71060d9e65b354a156b158.zip |
o Major refactoring on the BtPromise, mainly internal. Renaming BtPromise to BtSequence and BtSequencer.
Diffstat (limited to 'bt/src/test')
-rw-r--r-- | bt/src/test/java/io/trygvis/android/bt/BtSequencerTest.java | 96 |
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" + ); + } +} |