diff options
author | Peter Li <pyli@google.com> | 2021-05-25 04:05:21 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2021-05-25 04:05:21 +0000 |
commit | 73c91eb5d54fe9838bf33d996fc35f6727169f72 (patch) | |
tree | 61c1788625b45819839fa6bc5a48e94c9e5a7a6a | |
parent | 525b76b2e583ff7297639ea6c64308b92b9ab878 (diff) | |
parent | de5f57975ca59a67add29cacf34c141970b01aa0 (diff) | |
download | Dialer-73c91eb5d54fe9838bf33d996fc35f6727169f72.tar.gz |
Merge changes I60c71520,I7697d7c6 into sc-dev
* changes:
Migrate BluetoothPairLiveData unit tests
Migrate HfpDeviceListLiveData unit tests
-rw-r--r-- | tests/unittests/src/com/android/car/dialer/bluetooth/BluetoothPairListLiveDataTest.java (renamed from tests/robotests/src/com/android/car/dialer/bluetooth/BluetoothPairListLiveDataTest.java) | 68 | ||||
-rw-r--r-- | tests/unittests/src/com/android/car/dialer/bluetooth/HfpDeviceListLiveDataTest.java (renamed from tests/robotests/src/com/android/car/dialer/bluetooth/HfpDeviceListLiveDataTest.java) | 74 |
2 files changed, 65 insertions, 77 deletions
diff --git a/tests/robotests/src/com/android/car/dialer/bluetooth/BluetoothPairListLiveDataTest.java b/tests/unittests/src/com/android/car/dialer/bluetooth/BluetoothPairListLiveDataTest.java index 924bd395..b83f52e5 100644 --- a/tests/robotests/src/com/android/car/dialer/bluetooth/BluetoothPairListLiveDataTest.java +++ b/tests/unittests/src/com/android/car/dialer/bluetooth/BluetoothPairListLiveDataTest.java @@ -24,20 +24,19 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.robolectric.Shadows.shadowOf; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleRegistry; - -import com.android.car.dialer.CarDialerRobolectricTestRunner; -import com.android.car.dialer.LiveDataObserver; -import com.android.car.dialer.testutils.BroadcastReceiverVerifier; +import androidx.lifecycle.Observer; +import androidx.test.annotation.UiThreadTest; +import androidx.test.ext.junit.runners.AndroidJUnit4; import org.junit.Before; import org.junit.Test; @@ -46,97 +45,88 @@ import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.shadows.ShadowBluetoothAdapter; import java.util.HashSet; import java.util.Set; -@RunWith(CarDialerRobolectricTestRunner.class) +@RunWith(AndroidJUnit4.class) public class BluetoothPairListLiveDataTest { private static final String INTENT_ACTION = BluetoothDevice.ACTION_BOND_STATE_CHANGED; - private static final String BLUETOOTH_DEVICE_ALIAS_1 = "BluetoothDevice 1"; - private static final String BLUETOOTH_DEVICE_ALIAS_2 = "BluetoothDevice 2"; private BluetoothPairListLiveData mBluetoothPairListLiveData; + @Mock + private BluetoothAdapter mBluetoothAdapter; private LifecycleRegistry mLifecycleRegistry; - private BroadcastReceiverVerifier mReceiverVerifier; @Mock private LifecycleOwner mMockLifecycleOwner; @Mock - private LiveDataObserver<Set<BluetoothDevice>> mMockObserver; + private Observer<Set<BluetoothDevice>> mMockObserver; @Captor private ArgumentCaptor<Set<BluetoothDevice>> mValueCaptor; + @Captor + private ArgumentCaptor<BroadcastReceiver> mReceiverCaptor; + @Mock + private Context mContext; @Before public void setup() { MockitoAnnotations.initMocks(this); - mBluetoothPairListLiveData = new BluetoothPairListLiveData(RuntimeEnvironment.application, - BluetoothAdapter.getDefaultAdapter()); + mBluetoothPairListLiveData = new BluetoothPairListLiveData(mContext, mBluetoothAdapter); mLifecycleRegistry = new LifecycleRegistry(mMockLifecycleOwner); when(mMockLifecycleOwner.getLifecycle()).thenReturn(mLifecycleRegistry); - - mReceiverVerifier = new BroadcastReceiverVerifier(RuntimeEnvironment.application); } @Test + @UiThreadTest public void testOnActive() { mBluetoothPairListLiveData.observe(mMockLifecycleOwner, (value) -> mMockObserver.onChanged(value)); verify(mMockObserver, never()).onChanged(any()); mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); - mReceiverVerifier.verifyReceiverRegistered(INTENT_ACTION); + verify(mContext).registerReceiver(mReceiverCaptor.capture(), any()); verify(mMockObserver).onChanged(any()); } @Test + @UiThreadTest public void testOnBluetoothConnected() { // Set up Bluetooth devices - BluetoothDevice bluetoothDevice1 = mock(BluetoothDevice.class); - bluetoothDevice1.setAlias(BLUETOOTH_DEVICE_ALIAS_1); Set<BluetoothDevice> bondedDevices = new HashSet<BluetoothDevice>(); - bondedDevices.add(bluetoothDevice1); - ShadowBluetoothAdapter shadowBluetoothAdapter = shadowOf( - BluetoothAdapter.getDefaultAdapter()); - shadowBluetoothAdapter.setBondedDevices(bondedDevices); - + bondedDevices.add(mock(BluetoothDevice.class)); + when(mBluetoothAdapter.getBondedDevices()).thenReturn(bondedDevices); doNothing().when(mMockObserver).onChanged(mValueCaptor.capture()); mBluetoothPairListLiveData.observe(mMockLifecycleOwner, (value) -> mMockObserver.onChanged(value)); mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); verifyBondedDevices(bondedDevices); + verify(mContext).registerReceiver(mReceiverCaptor.capture(), any()); // Update Bluetooth devices - BluetoothDevice bluetoothDevice2 = mock(BluetoothDevice.class); - bluetoothDevice2.setAlias(BLUETOOTH_DEVICE_ALIAS_2); - bondedDevices.add(bluetoothDevice2); - shadowBluetoothAdapter.setBondedDevices(bondedDevices); - - mReceiverVerifier.getBroadcastReceiverFor(INTENT_ACTION) - .onReceive(mock(Context.class), mock(Intent.class)); + bondedDevices.add(mock(BluetoothDevice.class)); + Intent intent = new Intent(); + intent.setAction(INTENT_ACTION); + mReceiverCaptor.getValue().onReceive(mContext, intent); verifyBondedDevices(bondedDevices); } @Test + @UiThreadTest public void testOnInactiveUnregister() { mBluetoothPairListLiveData.observe(mMockLifecycleOwner, value -> mMockObserver.onChanged(value)); - int preNumber = mReceiverVerifier.getReceiverNumber(); - mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); - mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + verify(mContext).registerReceiver(mReceiverCaptor.capture(), any()); - assertThat(mReceiverVerifier.getReceiverNumber()).isEqualTo(preNumber); + mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + verify(mContext).unregisterReceiver(mReceiverCaptor.getValue()); } private void verifyBondedDevices(Set bondedDevices) { // Verify Bonded Devices for BluetoothAdapter - assertThat(BluetoothAdapter.getDefaultAdapter().getBondedDevices().containsAll( - bondedDevices)).isTrue(); - assertThat(BluetoothAdapter.getDefaultAdapter().getBondedDevices().size()).isEqualTo( - bondedDevices.size()); + assertThat(mBluetoothAdapter.getBondedDevices().containsAll(bondedDevices)).isTrue(); + assertThat(mBluetoothAdapter.getBondedDevices().size()).isEqualTo(bondedDevices.size()); // Verify Bonded Devices for LiveData assertThat(mBluetoothPairListLiveData.getValue().containsAll(bondedDevices)).isTrue(); assertThat(mBluetoothPairListLiveData.getValue().size()).isEqualTo(bondedDevices.size()); diff --git a/tests/robotests/src/com/android/car/dialer/bluetooth/HfpDeviceListLiveDataTest.java b/tests/unittests/src/com/android/car/dialer/bluetooth/HfpDeviceListLiveDataTest.java index b4d5b941..1b68cae0 100644 --- a/tests/robotests/src/com/android/car/dialer/bluetooth/HfpDeviceListLiveDataTest.java +++ b/tests/unittests/src/com/android/car/dialer/bluetooth/HfpDeviceListLiveDataTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 The Android Open Source Project + * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,14 +20,13 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothHeadsetClient; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telecom.TelecomManager; @@ -35,96 +34,95 @@ import android.telecom.TelecomManager; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleRegistry; - -import com.android.car.dialer.CarDialerRobolectricTestRunner; -import com.android.car.dialer.LiveDataObserver; -import com.android.car.dialer.testutils.BroadcastReceiverVerifier; -import com.android.car.dialer.testutils.ShadowBluetoothAdapterForDialer; +import androidx.lifecycle.Observer; +import androidx.test.annotation.UiThreadTest; +import androidx.test.ext.junit.runners.AndroidJUnit4; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; +import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.annotation.Config; -import org.robolectric.shadow.api.Shadow; import java.util.Collections; +import java.util.List; -@RunWith(CarDialerRobolectricTestRunner.class) -@Config(shadows = ShadowBluetoothAdapterForDialer.class) +@RunWith(AndroidJUnit4.class) public class HfpDeviceListLiveDataTest { - private static final String INTENT_ACTION = - BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED; + private static final String INTENT_ACTION_REGISTERED = + TelecomManager.ACTION_PHONE_ACCOUNT_REGISTERED; private HfpDeviceListLiveData mHfpDeviceListLiveData; private LifecycleRegistry mLifecycleRegistry; - private BroadcastReceiverVerifier mReceiverVerifier; @Mock private LifecycleOwner mMockLifecycleOwner; @Mock - private LiveDataObserver<Integer> mMockObserver; + private Observer<Integer> mMockObserver; @Mock - private BluetoothDevice mMockBluetoothDevice; + private PhoneAccountManager mPhoneAccountManager; private BluetoothAdapter mBluetoothAdapter; - private TelecomManager mTelecomManager; + private List<BluetoothDevice> mBluetoothDeviceList; + @Mock + private Context mContext; + @Captor + private ArgumentCaptor<BroadcastReceiver> mReceiverCaptor; @Before public void setup() { MockitoAnnotations.initMocks(this); + mBluetoothDeviceList = Collections.singletonList(null); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - mTelecomManager = RuntimeEnvironment.application.getSystemService(TelecomManager.class); - mHfpDeviceListLiveData = new HfpDeviceListLiveData(RuntimeEnvironment.application, - mBluetoothAdapter, new PhoneAccountManager(mTelecomManager, mBluetoothAdapter)); + mHfpDeviceListLiveData = new HfpDeviceListLiveData( + mContext, mBluetoothAdapter, mPhoneAccountManager); mLifecycleRegistry = new LifecycleRegistry(mMockLifecycleOwner); when(mMockLifecycleOwner.getLifecycle()).thenReturn(mLifecycleRegistry); - - mReceiverVerifier = new BroadcastReceiverVerifier(RuntimeEnvironment.application); + when(mPhoneAccountManager.getHfpDeviceList()).thenReturn(mBluetoothDeviceList); } @Test + @UiThreadTest public void testOnActive() { mHfpDeviceListLiveData.observe(mMockLifecycleOwner, - (value) -> mMockObserver.onChanged(value)); + (value) -> mMockObserver.onChanged(value.size())); verify(mMockObserver, never()).onChanged(any()); - mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); - mReceiverVerifier.verifyReceiverRegistered(INTENT_ACTION); + verify(mContext).registerReceiver(mReceiverCaptor.capture(), any()); verify(mMockObserver).onChanged(any()); } @Test + @UiThreadTest public void testOnBluetoothHfpStateChange() { ArgumentCaptor<Integer> valueCaptor = ArgumentCaptor.forClass(Integer.class); doNothing().when(mMockObserver).onChanged(valueCaptor.capture()); - ShadowBluetoothAdapterForDialer shadowBluetoothAdapter = Shadow.extract( - BluetoothAdapter.getDefaultAdapter()); - - shadowBluetoothAdapter.setHfpDevices(Collections.singletonList(mMockBluetoothDevice)); + when(mPhoneAccountManager.getHfpDeviceList()).thenReturn(mBluetoothDeviceList); mHfpDeviceListLiveData.observe(mMockLifecycleOwner, - (value) -> mMockObserver.onChanged(value)); + (value) -> mMockObserver.onChanged(value.size())); mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); + verify(mContext).registerReceiver(mReceiverCaptor.capture(), any()); assertThat(mHfpDeviceListLiveData.getValue()).isNotEmpty(); - shadowBluetoothAdapter.setHfpDevices(Collections.emptyList()); - mReceiverVerifier.getBroadcastReceiverFor(INTENT_ACTION) - .onReceive(mock(Context.class), mock(Intent.class)); + when(mPhoneAccountManager.getHfpDeviceList()).thenReturn(Collections.emptyList()); + Intent intent = new Intent(); + intent.setAction(INTENT_ACTION_REGISTERED); + mReceiverCaptor.getValue().onReceive(mContext, intent); assertThat(mHfpDeviceListLiveData.getValue()).isEmpty(); } @Test + @UiThreadTest public void testOnInactiveUnregister() { mHfpDeviceListLiveData.observe(mMockLifecycleOwner, - (value) -> mMockObserver.onChanged(value)); + (value) -> mMockObserver.onChanged(value.size())); mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); - int preNumber = mReceiverVerifier.getReceiverNumber(); + verify(mContext).registerReceiver(mReceiverCaptor.capture(), any()); mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); - mReceiverVerifier.verifyReceiverUnregistered(INTENT_ACTION, preNumber); + verify(mContext).unregisterReceiver(mReceiverCaptor.getValue()); } } |