Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 75b16ca6 authored by Babak's avatar Babak Committed by Babak Bostan
Browse files

Fix ConnectedDeviceVoiceRecognitoinNotifierTest

- Adds bluetooth device to the intent since it is required by other
receivers which can cause issues if test is running while other receivers
are listening for the bluetooth broadcast.
- Fixes a potential flakiness by making sure the looper processes all the
messages.

Fix: 154515909
Test: Manual

Change-Id: Ib9c6f9eed20336cafcf9d984edd60eb311195e5e
parent 87389006
Loading
Loading
Loading
Loading
+21 −6
Original line number Original line Diff line number Diff line
@@ -24,6 +24,8 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verify;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadsetClient;
import android.bluetooth.BluetoothHeadsetClient;
import android.content.Intent;
import android.content.Intent;
import android.os.Handler;
import android.os.Handler;
@@ -44,14 +46,19 @@ import org.junit.runner.RunWith;
public class ConnectedDeviceVoiceRecognitionNotifierTest extends SysuiTestCase {
public class ConnectedDeviceVoiceRecognitionNotifierTest extends SysuiTestCase {


    private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
    private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
    private static final String BLUETOOTH_REMOTE_ADDRESS = "00:11:22:33:44:55";


    private ConnectedDeviceVoiceRecognitionNotifier mVoiceRecognitionNotifier;
    private ConnectedDeviceVoiceRecognitionNotifier mVoiceRecognitionNotifier;
    private TestableLooper mTestableLooper;
    private Handler mTestHandler;
    private Handler mTestHandler;
    private BluetoothDevice mBluetoothDevice;


    @Before
    @Before
    public void setUp() throws Exception {
    public void setUp() throws Exception {
        TestableLooper testableLooper = TestableLooper.get(this);
        mTestableLooper = TestableLooper.get(this);
        mTestHandler = spy(new Handler(testableLooper.getLooper()));
        mTestHandler = spy(new Handler(mTestableLooper.getLooper()));
        mBluetoothDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(
                BLUETOOTH_REMOTE_ADDRESS);
        mVoiceRecognitionNotifier = new ConnectedDeviceVoiceRecognitionNotifier(
        mVoiceRecognitionNotifier = new ConnectedDeviceVoiceRecognitionNotifier(
                mContext, mTestHandler);
                mContext, mTestHandler);
        mVoiceRecognitionNotifier.onBootCompleted();
        mVoiceRecognitionNotifier.onBootCompleted();
@@ -61,8 +68,10 @@ public class ConnectedDeviceVoiceRecognitionNotifierTest extends SysuiTestCase {
    public void testReceiveIntent_started_showToast() {
    public void testReceiveIntent_started_showToast() {
        Intent intent = new Intent(BluetoothHeadsetClient.ACTION_AG_EVENT);
        Intent intent = new Intent(BluetoothHeadsetClient.ACTION_AG_EVENT);
        intent.putExtra(BluetoothHeadsetClient.EXTRA_VOICE_RECOGNITION, VOICE_RECOGNITION_STARTED);
        intent.putExtra(BluetoothHeadsetClient.EXTRA_VOICE_RECOGNITION, VOICE_RECOGNITION_STARTED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);

        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
        waitForIdleSync();
        mTestableLooper.processAllMessages();


        verify(mTestHandler).post(any());
        verify(mTestHandler).post(any());
    }
    }
@@ -71,8 +80,10 @@ public class ConnectedDeviceVoiceRecognitionNotifierTest extends SysuiTestCase {
    public void testReceiveIntent_invalidExtra_noToast() {
    public void testReceiveIntent_invalidExtra_noToast() {
        Intent intent = new Intent(BluetoothHeadsetClient.ACTION_AG_EVENT);
        Intent intent = new Intent(BluetoothHeadsetClient.ACTION_AG_EVENT);
        intent.putExtra(BluetoothHeadsetClient.EXTRA_VOICE_RECOGNITION, INVALID_VALUE);
        intent.putExtra(BluetoothHeadsetClient.EXTRA_VOICE_RECOGNITION, INVALID_VALUE);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);

        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
        waitForIdleSync();
        mTestableLooper.processAllMessages();


        verify(mTestHandler, never()).post(any());
        verify(mTestHandler, never()).post(any());
    }
    }
@@ -80,8 +91,10 @@ public class ConnectedDeviceVoiceRecognitionNotifierTest extends SysuiTestCase {
    @Test
    @Test
    public void testReceiveIntent_noExtra_noToast() {
    public void testReceiveIntent_noExtra_noToast() {
        Intent intent = new Intent(BluetoothHeadsetClient.ACTION_AG_EVENT);
        Intent intent = new Intent(BluetoothHeadsetClient.ACTION_AG_EVENT);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);

        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
        waitForIdleSync();
        mTestableLooper.processAllMessages();


        verify(mTestHandler, never()).post(any());
        verify(mTestHandler, never()).post(any());
    }
    }
@@ -89,8 +102,10 @@ public class ConnectedDeviceVoiceRecognitionNotifierTest extends SysuiTestCase {
    @Test
    @Test
    public void testReceiveIntent_invalidIntent_noToast() {
    public void testReceiveIntent_invalidIntent_noToast() {
        Intent intent = new Intent(BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED);
        Intent intent = new Intent(BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);

        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
        waitForIdleSync();
        mTestableLooper.processAllMessages();


        verify(mTestHandler, never()).post(any());
        verify(mTestHandler, never()).post(any());
    }
    }