|
| 1 | +package com.shimmerresearch.shimmer3.communication; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import com.shimmerresearch.bluetooth.ShimmerBluetooth; |
| 13 | +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE; |
| 14 | +import com.shimmerresearch.driver.ShimmerObject; |
| 15 | +import com.shimmerresearch.pcDriver.ShimmerPC; |
| 16 | + |
| 17 | +/** |
| 18 | + * DEV-1275: the connect sequence reads the unit's status part way through, and |
| 19 | + * ShimmerPC.eventLogAndStreamStatusChanged() moved CONNECTING to CONNECTED on |
| 20 | + * that reply. ShimmerDeviceCallbackAdapter marks every CONNECTED initialised, |
| 21 | + * so an app waiting for CONNECTED and initialised was told the unit was ready |
| 22 | + * while the rest of the sequence (battery, calibration dump, inquiry) was still |
| 23 | + * queued. Whatever it did next went out in among that sequence: streaming |
| 24 | + * started part way through it, and a configure cut the calibration dump short, |
| 25 | + * which left a Shimmer3's accel NaN. The sequence's own end, inquiryDone() and |
| 26 | + * then isReadyForStreaming(), is what moves CONNECTING to CONNECTED. |
| 27 | + * <p> |
| 28 | + * No hardware: ByteCommunicationSimulatorS3 answers the whole connect sequence, |
| 29 | + * the status read included. |
| 30 | + */ |
| 31 | +public class API_00031_ConnectReadyAfterConnectSequenceTest { |
| 32 | + |
| 33 | + private ShimmerPC mDevice; |
| 34 | + private volatile boolean mStatusRead; |
| 35 | + private volatile boolean mSequenceEnded; |
| 36 | + /** What had happened when the state first became CONNECTED; null until then */ |
| 37 | + private volatile Boolean mStatusReadAtFirstConnected; |
| 38 | + private volatile Boolean mSequenceEndedAtFirstConnected; |
| 39 | + private volatile List<String> mQueuedAtFirstConnected; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + mDevice = new ShimmerPC("COM99") { |
| 44 | + @Override |
| 45 | + public void startTimerReadStatus() { |
| 46 | + // As in ByteCommunicationTestUtils.newShimmerPCWithoutStatusTimer: only the |
| 47 | + // connect sequence's own status read should run |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void eventLogAndStreamStatusChanged(byte currentCommand) { |
| 52 | + mStatusRead = true; |
| 53 | + super.eventLogAndStreamStatusChanged(currentCommand); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void isReadyForStreaming() { |
| 58 | + mSequenceEnded = true; |
| 59 | + super.isReadyForStreaming(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean setBluetoothRadioState(BT_STATE state) { |
| 64 | + if(state==BT_STATE.CONNECTED && mSequenceEndedAtFirstConnected==null){ |
| 65 | + mStatusReadAtFirstConnected = mStatusRead; |
| 66 | + mSequenceEndedAtFirstConnected = mSequenceEnded; |
| 67 | + List<String> queued = new ArrayList<>(); |
| 68 | + for(byte[] instruction:new ArrayList<>(getListofInstructions())){ |
| 69 | + if(instruction!=null){ |
| 70 | + queued.add(btCommandToString(instruction[0])); |
| 71 | + } |
| 72 | + } |
| 73 | + mQueuedAtFirstConnected = queued; |
| 74 | + } |
| 75 | + return super.setBluetoothRadioState(state); |
| 76 | + } |
| 77 | + }; |
| 78 | + mDevice.setTestRadio(new ByteCommunicationSimulatorS3("COM99")); |
| 79 | + } |
| 80 | + |
| 81 | + @After |
| 82 | + public void tearDown() { |
| 83 | + mDevice.disconnectNoException(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void theUnitIsReportedReadyOnlyOnceTheConnectSequenceHasEnded() { |
| 88 | + ByteCommunicationTestUtils.connectAndWaitUntilInitialised(mDevice); |
| 89 | + |
| 90 | + assertNotNull("the state became CONNECTED", mSequenceEndedAtFirstConnected); |
| 91 | + assertTrue("the connect sequence read the status before the end, so the case is covered", mStatusReadAtFirstConnected); |
| 92 | + assertTrue("CONNECTED came only once the connect sequence had ended (inquiryDone, isReadyForStreaming); still queued: " |
| 93 | + + mQueuedAtFirstConnected, mSequenceEndedAtFirstConnected); |
| 94 | + // Every read of the unit's configuration (status, battery, calibration dump, |
| 95 | + // inquiry) is answered. What may remain: the inquiry itself, still at the head |
| 96 | + // while its reply is handled, and the RTC write that the status reply queues |
| 97 | + // when the unit's clock is not set. That write runs before anything the app |
| 98 | + // queues, so it never lands in a stream. |
| 99 | + List<String> allowed = new ArrayList<>(); |
| 100 | + allowed.add(ShimmerBluetooth.btCommandToString(ShimmerObject.INQUIRY_COMMAND)); |
| 101 | + allowed.add(ShimmerBluetooth.btCommandToString(ShimmerObject.SET_RWC_COMMAND)); |
| 102 | + for(String queued:mQueuedAtFirstConnected){ |
| 103 | + assertTrue("no read from the connect sequence is still queued at CONNECTED, but " + queued + " is (queued: " |
| 104 | + + mQueuedAtFirstConnected + ")", allowed.contains(queued)); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments