Java Code Examples for org.robolectric.shadows.ShadowLooper#runUiThreadTasksIncludingDelayedTasks()

The following examples show how to use org.robolectric.shadows.ShadowLooper#runUiThreadTasksIncludingDelayedTasks() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Test
public void queueTestRetryCommand() throws Exception {
    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0);
    BluetoothGattCharacteristic characteristic2 = new BluetoothGattCharacteristic(UUID.fromString("00002A1E-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0);
    service.addCharacteristic(characteristic);
    service.addCharacteristic(characteristic2);
    when(gatt.readCharacteristic(characteristic)).thenReturn(true);
    when(gatt.getServices()).thenReturn(Arrays.asList(service));

    peripheral.readCharacteristic(characteristic);

    verify(gatt).readCharacteristic(characteristic);

    // Trigger bonding to start
    callback.onCharacteristicRead(gatt, characteristic, 5);

    Field field = BluetoothPeripheral.class.getDeclaredField("bondStateReceiver");
    field.setAccessible(true);
    BroadcastReceiver broadcastReceiver = (BroadcastReceiver) field.get(peripheral);

    Intent intent = mock(Intent.class);
    when(intent.getAction()).thenReturn(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    when(intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR)).thenReturn(BOND_BONDED);
    when(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)).thenReturn(device);

    broadcastReceiver.onReceive(context, intent);

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    verify(gatt, times(2)).readCharacteristic(characteristic);
}
 
Example 2
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 5 votes vote down vote up
private BluetoothGattCallback connectAndGetCallback() {
    peripheral.connect();

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    ArgumentCaptor<BluetoothGattCallback> bluetoothGattCallbackCaptor = ArgumentCaptor.forClass(BluetoothGattCallback.class);
    verify(device).connectGatt(any(Context.class), anyBoolean(), bluetoothGattCallbackCaptor.capture(), anyInt());

    List<BluetoothGattCallback> capturedGatts = bluetoothGattCallbackCaptor.getAllValues();
    return capturedGatts.get(0);
}
 
Example 3
Source File: WXDomStatementTest.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  WXSDKEngine.initialize(RuntimeEnvironment.application,new InitConfig.Builder().build());
  ShadowLooper looper = WXBridgeManagerTest.getLooper();
  looper.idle();
  ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
  instance = WXSDKInstanceTest.createInstance();
  rednerManager = new WXRenderManager();
  rednerManager.registerInstance(instance);//
  WXSDKManagerTest.setRenderManager(rednerManager);
  stmt = new DOMActionContextImpl(instance.getInstanceId(),rednerManager );
}
 
Example 4
Source File: WXDomStatementTest.java    From weex-uikit with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  WXSDKEngine.initialize(RuntimeEnvironment.application,new InitConfig.Builder().build());
  ShadowLooper looper = WXBridgeManagerTest.getLooper();
  looper.idle();
  ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
  instance = WXSDKInstanceTest.createInstance();
  rednerManager = new WXRenderManager();
  rednerManager.registerInstance(instance);//
  stmt = new WXDomStatement(instance.getInstanceId(),rednerManager );
}
 
Example 5
Source File: UTest.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
private void testSendAccessibilityAction(boolean serviceEnabled,
                                         boolean hasPermission,
                                         boolean hasRun) {
    PowerMockito.spy(U.class);
    when(U.isAccessibilityServiceEnabled(context)).thenReturn(serviceEnabled);
    when(U.hasWriteSecureSettingsPermission(context)).thenReturn(hasPermission);
    RunnableHooker onComplete = new RunnableHooker();
    U.sendAccessibilityAction(
            context, AccessibilityService.GLOBAL_ACTION_LOCK_SCREEN, onComplete
    );
    // Run all delayed message.
    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
    assertEquals(hasRun, onComplete.hasRun());
}
 
Example 6
Source File: TestANClickThroughAction.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testBannerANClickThroughActionReturnURL() {
    bannerAdView.setClickThroughAction(ANClickThroughAction.RETURN_URL);
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.banner())); // First queue a regular HTML banner response
    executeBannerRequest();
    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    waitUntilExecuted();

    assertTrue(adClickedWithUrl);
    assertFalse(adClicked);
}
 
Example 7
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 4 votes vote down vote up
@Test
public void autoconnectTest() throws Exception {
    peripheral.autoConnect();

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    ArgumentCaptor<BluetoothGattCallback> captor = ArgumentCaptor.forClass(BluetoothGattCallback.class);
    ArgumentCaptor<Boolean> autoConnectCaptor = ArgumentCaptor.forClass(Boolean.class);

    verify(device).connectGatt(any(Context.class), autoConnectCaptor.capture(), captor.capture(), anyInt());

    boolean autoconnect = autoConnectCaptor.getValue();
    assertTrue(autoconnect);

    BluetoothGattCallback callback = captor.getValue();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    assertEquals(STATE_CONNECTED,  peripheral.getState());
}
 
Example 8
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 4 votes vote down vote up
@Test
public void cancelConnectionTest() throws Exception {
    peripheral.connect();

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    ArgumentCaptor<BluetoothGattCallback> captor = ArgumentCaptor.forClass(BluetoothGattCallback.class);

    verify(device).connectGatt(any(Context.class), anyBoolean(), captor.capture(), anyInt());

    BluetoothGattCallback callback = captor.getValue();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    peripheral.cancelConnection();

    verify(gatt).disconnect();

    assertEquals(STATE_DISCONNECTING,  peripheral.getState());

    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_DISCONNECTED);

    verify(gatt).close();
}
 
Example 9
Source File: TestANClickThroughAction.java    From mobile-sdk-android with Apache License 2.0 4 votes vote down vote up
private void waitUntilExecuted() {
    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
}
 
Example 10
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 3 votes vote down vote up
@Test
public void connectTest() throws Exception {
    peripheral.connect();

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    ArgumentCaptor<BluetoothGattCallback> captor = ArgumentCaptor.forClass(BluetoothGattCallback.class);

    verify(device).connectGatt(any(Context.class), anyBoolean(), captor.capture(), anyInt());

    BluetoothGattCallback callback = captor.getValue();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    assertEquals(STATE_CONNECTED,  peripheral.getState());
}
 
Example 11
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 3 votes vote down vote up
@Test
public void readCharacteristicNoReadPropertyTest() throws Exception {
    peripheral.connect();

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"), 0,0);

    peripheral.readCharacteristic(characteristic);

    verify(gatt, never()).readCharacteristic(any(BluetoothGattCharacteristic.class));
}
 
Example 12
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 3 votes vote down vote up
@Test
public void writeCharacteristicNotConnectedTest() throws Exception {
    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"), PROPERTY_WRITE,0);

    when(gatt.writeCharacteristic(characteristic)).thenReturn(true);

    peripheral.writeCharacteristic(characteristic, new byte[]{0}, WRITE_TYPE_DEFAULT);

    verify(gatt, never()).writeCharacteristic(any(BluetoothGattCharacteristic.class));
}
 
Example 13
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 3 votes vote down vote up
@Test
public void writeCharacteristicNotWritePropertyTest() throws Exception {
    peripheral.connect();

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"), 0,0);

    peripheral.writeCharacteristic(characteristic, new byte[] { 0, 0 }, WRITE_TYPE_DEFAULT);

    verify(gatt, never()).writeCharacteristic(any(BluetoothGattCharacteristic.class));
}
 
Example 14
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 3 votes vote down vote up
@Test
public void writeCharacteristicNoValueTest() throws Exception {
    peripheral.connect();

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"), PROPERTY_WRITE,0);

    when(gatt.writeCharacteristic(characteristic)).thenReturn(true);

    peripheral.writeCharacteristic(characteristic, null, WRITE_TYPE_DEFAULT);

    verify(gatt, never()).writeCharacteristic(any(BluetoothGattCharacteristic.class));
}
 
Example 15
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 3 votes vote down vote up
@Test
public void onConnectionStateChangedConnectedAlreadyBondedTest() throws Exception {
    when(device.getBondState()).thenReturn(BOND_BONDED);

    BluetoothGattCallback callback = connectAndGetCallback();

    callback.onConnectionStateChange(gatt, GATT_SUCCESS, BluetoothProfile.STATE_CONNECTED);

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    verify(gatt).discoverServices();
}
 
Example 16
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 3 votes vote down vote up
@Test
public void onConnectionStateChangedConnectedBondedingTest() throws Exception {
    when(device.getBondState()).thenReturn(BOND_BONDING);

    BluetoothGattCallback callback = connectAndGetCallback();

    callback.onConnectionStateChange(gatt, GATT_SUCCESS, BluetoothProfile.STATE_CONNECTED);

    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

    verify(gatt, never()).discoverServices();
}