From 3e619a735e63a1222e71060d9e65b354a156b158 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 28 Jan 2015 23:45:38 +0100 Subject: o Major refactoring on the BtPromise, mainly internal. Renaming BtPromise to BtSequence and BtSequencer. --- .../io/trygvis/android/bt/BtSequencerTest.java | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 bt/src/test/java/io/trygvis/android/bt/BtSequencerTest.java (limited to 'bt/src/test') 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 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 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 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" + ); + } +} -- cgit v1.2.3