/** * Test that disconnection from server is properly recognized. */ @Test public void testServerEventDisconnected() throws InterruptedException { ShadowBluetoothSocket.setTransmittedString(SERVER_VERSION_SUCCESS); final CountDownLatch messageReceived = new CountDownLatch(1); control = new BluetoothPresenterControl(new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == RemoteControl.ServiceState.NONE.ordinal()) { messageReceived.countDown(); } } }); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTED); ShadowBluetoothSocket.setFailReading(true); waitForServiceStateChanged(control, RemoteControl.ServiceState.NONE); ShadowLooper.runUiThreadTasks(); assertThat("Handler was not called", messageReceived.await(MESSAGE_RECEIVING_TIMEOUT, TimeUnit.MILLISECONDS), is(true)); }
/** * Test that writing data using the presenter control works without errors. */ @Test public void testWriteCommand() throws InterruptedException { ShadowBluetoothSocket.setTransmittedString(SERVER_VERSION_SUCCESS); control = new BluetoothPresenterControl(new Handler() {}); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTED); ShadowBluetoothSocket.resetLastTransmittedString(); control.sendCommand(Command.NEXT_SLIDE); assertThat(ShadowBluetoothSocket.getLastTransmittedString(), is("{ \"type\": \"command\", " + "\"data\": \"" + Command.NEXT_SLIDE.getCommand() + "\"}\n\n")); }
@Test public void turnOnSuccess() { final ShadowBluetoothAdapter shadowAdapter = getShadowBluetoothAdapter(); shadowAdapter.setEnabled(false); final NativeBluetoothStack stack = new NativeBluetoothStack(getContext(), errorListener, loggerFacade); // NativeBluetoothStack#turnOn() returns a mirror, so this is fine. Observable<Void> turnOn = stack.turnOn(); Intent stateChange = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED) .putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_OFF) .putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_TURNING_ON); getContext().sendBroadcast(stateChange); stateChange.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_TURNING_ON) .putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON); getContext().sendBroadcast(stateChange); Sync.wrap(turnOn) .assertThat(is(nullValue())); }
@Test public void turnOnFailure() { final ShadowBluetoothAdapter shadowAdapter = getShadowBluetoothAdapter(); shadowAdapter.setEnabled(false); final NativeBluetoothStack stack = new NativeBluetoothStack(getContext(), errorListener, loggerFacade); // NativeBluetoothStack#turnOn() returns a mirror, so this is fine. Observable<Void> turnOn = stack.turnOn(); Intent stateChange = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); getContext().sendBroadcast(stateChange); Sync.wrap(turnOn) .assertThrows(ChangePowerStateException.class); }
@Test public void turnOffSuccess() { final ShadowBluetoothAdapter shadowAdapter = getShadowBluetoothAdapter(); shadowAdapter.setEnabled(false); final NativeBluetoothStack stack = new NativeBluetoothStack(getContext(), errorListener, loggerFacade); // NativeBluetoothStack#turnOff() returns a mirror, so this is fine. Observable<Void> turnOff = stack.turnOff(); Intent stateChange = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED) .putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_ON) .putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_TURNING_OFF); getContext().sendBroadcast(stateChange); stateChange.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_TURNING_OFF) .putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF); getContext().sendBroadcast(stateChange); Sync.wrap(turnOff) .assertThat(is(nullValue())); }
@Test public void turnOffFailure() { final ShadowBluetoothAdapter shadowAdapter = getShadowBluetoothAdapter(); shadowAdapter.setEnabled(false); final NativeBluetoothStack stack = new NativeBluetoothStack(getContext(), errorListener, loggerFacade); // NativeBluetoothStack#turnOff() returns a mirror, so this is fine. Observable<Void> turnOff = stack.turnOff(); Intent stateChange = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); getContext().sendBroadcast(stateChange); Sync.wrap(turnOff) .assertThrows(ChangePowerStateException.class); }
/** * Test that we can successfully initiate a connection to another device. */ @Test public void testConnectingState() { control = new BluetoothPresenterControl(new Handler() {}); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); assertThat(control.getState(), is(RemoteControl.ServiceState.CONNECTING)); }
/** * Test that we can successfully connect to another device. */ @Test public void testConnectedStateSuccess() throws InterruptedException { ShadowBluetoothSocket.setTransmittedString(SERVER_VERSION_SUCCESS); control = new BluetoothPresenterControl(new Handler() {}); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTED); }
/** * Test that if connection fails, we get the correct state of the service. */ @Test public void testConnectedStateFailure() throws InterruptedException { ShadowBluetoothSocket.setConnectionSucceed(false); control = new BluetoothPresenterControl(new Handler() {}); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.NONE); }
/** * Test that if connection fails due to invalid version information, * we get the correct state of the service. */ @Test public void testConnectedStateFailureInvalidVersion() throws InterruptedException { ShadowBluetoothSocket.setTransmittedString(SERVER_VERSION_FAILURE); control = new BluetoothPresenterControl(new Handler() {}); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTING); waitForServiceStateChanged(control, RemoteControl.ServiceState.NONE); }
/** * Test that "connecting" and "connected" event are sent successfully if the * connection succeeded. */ @Test public void testConnectedEventSuccess() throws InterruptedException { final CountDownLatch connectingMessageReceived = new CountDownLatch(1); final CountDownLatch connectedMessageReceived = new CountDownLatch(1); control = new BluetoothPresenterControl(new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == RemoteControl.ServiceState.CONNECTING.ordinal()) { connectingMessageReceived.countDown(); } else if (msg.what == RemoteControl.ServiceState.CONNECTED.ordinal()) { assertThat("Got wrong connection result", msg.getData().getBoolean(RemoteControl.RESULT_VALUES[0]), is(true)); assertThat("Got wrong device to which we are connected", msg.getData().getString(RemoteControl.RESULT_VALUES[1]), is(DEVICE_NAME)); connectedMessageReceived.countDown(); } } }); ShadowBluetoothSocket.setTransmittedString(SERVER_VERSION_SUCCESS); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); shadowOf(bluetoothDevice).setName(DEVICE_NAME); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTED); ShadowLooper.runUiThreadTasks(); assertThat("Did not receive 'connecting' message", connectingMessageReceived.await(MESSAGE_RECEIVING_TIMEOUT, TimeUnit.MILLISECONDS), is(true)); assertThat("Did not receive 'connected' message", connectedMessageReceived.await(MESSAGE_RECEIVING_TIMEOUT, TimeUnit.MILLISECONDS), is(true)); }
/** * Test that the "connecting" and "connected" event are sent successfully if the * connection fails and that the result state is correct. */ @Test public void testConnectedEventFailure() throws InterruptedException { ShadowBluetoothSocket.setConnectionSucceed(false); final CountDownLatch connectingMessageReceived = new CountDownLatch(1); final CountDownLatch connectedMessageReceived = new CountDownLatch(1); control = new BluetoothPresenterControl(new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == RemoteControl.ServiceState.CONNECTING.ordinal()) { connectingMessageReceived.countDown(); } else if (msg.what == RemoteControl.ServiceState.CONNECTED.ordinal()) { assertThat("Got wrong connection result", msg.getData().getBoolean(RemoteControl.RESULT_VALUES[0]), is(false)); connectedMessageReceived.countDown(); } } }); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTING); waitForServiceStateChanged(control, RemoteControl.ServiceState.NONE); ShadowLooper.runUiThreadTasks(); assertThat("Did not receive 'connecting' event", connectingMessageReceived.await(MESSAGE_RECEIVING_TIMEOUT, TimeUnit.MILLISECONDS), is(true)); assertThat("Did not receive 'connected' event", connectedMessageReceived.await(MESSAGE_RECEIVING_TIMEOUT, TimeUnit.MILLISECONDS), is(true)); }
/** * Test that the "error" event is sent successfully if the server sends incompatible version * information. */ @Test public void testConnectedEventIncompatibleVersion() throws InterruptedException { ShadowBluetoothSocket.setTransmittedString(SERVER_VERSION_FAILURE); final CountDownLatch messageReceived = new CountDownLatch(1); control = new BluetoothPresenterControl(new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == RemoteControl.ServiceState.ERROR.ordinal()) { assertThat("Got wrong error type", msg.getData().getString(RemoteControl.RESULT_VALUES[2]), is(RemoteControl.ERROR_TYPES.VERSION.toString())); messageReceived.countDown(); } } }); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTING); waitForServiceStateChanged(control, RemoteControl.ServiceState.NONE); ShadowLooper.runUiThreadTasks(); assertThat("Did not receive 'error' event", messageReceived.await(MESSAGE_RECEIVING_TIMEOUT, TimeUnit.MILLISECONDS), is(true)); }
/** * Test that the "error" event is sent successfully if the server sends invalid data. */ @Test public void testConnectedEventInvalidData() throws InterruptedException { ShadowBluetoothSocket.setTransmittedString("This is invalid json data\n\n"); final CountDownLatch messageReceived = new CountDownLatch(1); control = new BluetoothPresenterControl(new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == RemoteControl.ServiceState.ERROR.ordinal()) { assertThat("Got wrong error type", msg.getData().getString(RemoteControl.RESULT_VALUES[2]), is(RemoteControl.ERROR_TYPES.PARSING.toString())); messageReceived.countDown(); } } }); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTING); long startTime = System.currentTimeMillis(); while (System.currentTimeMillis() < startTime + MESSAGE_RECEIVING_TIMEOUT) { Thread.sleep(MESSAGE_CHECK_TIME); ShadowLooper.runUiThreadTasks(); if (messageReceived.await(MESSAGE_CHECK_TIME, TimeUnit.MILLISECONDS)) { return; } } fail("Did not receive 'error' event"); }
/** * Test that disconnection from client works fine. */ @Test public void testClientDisconnected() throws InterruptedException { ShadowBluetoothSocket.setTransmittedString(SERVER_VERSION_SUCCESS); control = new BluetoothPresenterControl(new Handler() {}); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTING); control.stop(); assertThat(control.getState(), is(RemoteControl.ServiceState.NONE)); }
/** * Test that disconnection from server is properly recognized while connection is still * establishing and initial information is not exchanged (e.g. version information). */ @Test public void testServerDisconnectedDuringInformationExchange() throws InterruptedException { control = new BluetoothPresenterControl(new Handler() {}); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTING); ShadowBluetoothSocket.setFailReading(true); // Wait some time until the thread really stopped waitForServiceStateChanged(control, RemoteControl.ServiceState.NONE); }
/** * Test that disconnection from server is properly recognized. */ @Test public void testServerDisconnectedAfterConnectionEstablished() throws InterruptedException { ShadowBluetoothSocket.setTransmittedString(SERVER_VERSION_SUCCESS); control = new BluetoothPresenterControl(new Handler() {}); BluetoothDevice bluetoothDevice = ShadowBluetoothAdapter.getDefaultAdapter() .getRemoteDevice(DEVICE_ADDRESS); control.connect(bluetoothDevice); waitForServiceStateChanged(control, RemoteControl.ServiceState.CONNECTED); ShadowBluetoothSocket.setFailReading(true); // Wait some time until the thread really stopped waitForServiceStateChanged(control, RemoteControl.ServiceState.NONE); }
@Test public void isEnabled() { final ShadowBluetoothAdapter shadowAdapter = getShadowBluetoothAdapter(); final NativeBluetoothStack stack = new NativeBluetoothStack(getContext(), errorListener, loggerFacade); shadowAdapter.setEnabled(false); assertThat(stack.isEnabled(), is(false)); shadowAdapter.setEnabled(true); assertThat(stack.isEnabled(), is(true)); }
@Test public void enabledOn() { final ShadowBluetoothAdapter shadowAdapter = getShadowBluetoothAdapter(); shadowAdapter.setEnabled(false); final NativeBluetoothStack stack = new NativeBluetoothStack(getContext(), errorListener, loggerFacade); assertThat(Sync.last(stack.enabled().take(1)), is(false)); Intent stateChange = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED) .putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON); getContext().sendBroadcast(stateChange); assertThat(Sync.last(stack.enabled().take(1)), is(true)); }
@Test public void enabledOff() { final ShadowBluetoothAdapter shadowAdapter = getShadowBluetoothAdapter(); shadowAdapter.setEnabled(true); final NativeBluetoothStack stack = new NativeBluetoothStack(getContext(), errorListener, loggerFacade); assertThat(Sync.last(stack.enabled().take(1)), is(true)); Intent stateChange = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED) .putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF); getContext().sendBroadcast(stateChange); assertThat(Sync.last(stack.enabled().take(1)), is(false)); }
@Test public void enabledError() { final ShadowBluetoothAdapter shadowAdapter = getShadowBluetoothAdapter(); shadowAdapter.setEnabled(true); final NativeBluetoothStack stack = new NativeBluetoothStack(getContext(), errorListener, loggerFacade); assertThat(Sync.last(stack.enabled().take(1)), is(true)); Intent stateChange = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED) .putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); getContext().sendBroadcast(stateChange); assertThat(Sync.last(stack.enabled().take(1)), is(false)); }
public static ShadowBluetoothAdapter shadowOf(BluetoothAdapter other) { return (ShadowBluetoothAdapter) Robolectric.shadowOf_(other); }