diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2021-05-26 01:09:04 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2021-05-26 01:09:04 +0000 |
commit | e144d7b7b387da4350f7e5aacdd59451b126dde2 (patch) | |
tree | 7004d6e6918aa2f207ac8b44f788dacad0f234fa | |
parent | 7120126cb8a9f79531417f2b92fa8933a9bb7441 (diff) | |
parent | 34eb7405f314fe033c7bf5bdce97392e896058b6 (diff) | |
download | Dialer-e144d7b7b387da4350f7e5aacdd59451b126dde2.tar.gz |
Snap for 7398294 from 34eb7405f314fe033c7bf5bdce97392e896058b6 to sc-release
Change-Id: I378da0e761d57920f0348cb7e38f201265109112
-rw-r--r-- | src/com/android/car/dialer/bluetooth/BluetoothStateLiveData.java | 1 | ||||
-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/BluetoothStateLiveDataTest.java (renamed from tests/robotests/src/com/android/car/dialer/bluetooth/BluetoothStateLiveDataTest.java) | 51 | ||||
-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 | ||||
-rw-r--r-- | tests/unittests/src/com/android/car/dialer/livedata/CallDetailLiveDataTest.java (renamed from tests/robotests/src/com/android/car/dialer/livedata/CallDetailLiveDataTest.java) | 58 |
5 files changed, 103 insertions, 149 deletions
diff --git a/src/com/android/car/dialer/bluetooth/BluetoothStateLiveData.java b/src/com/android/car/dialer/bluetooth/BluetoothStateLiveData.java index 3fb44638..4a170af6 100644 --- a/src/com/android/car/dialer/bluetooth/BluetoothStateLiveData.java +++ b/src/com/android/car/dialer/bluetooth/BluetoothStateLiveData.java @@ -66,7 +66,6 @@ class BluetoothStateLiveData extends LiveData<Integer> { updateState(); mContext.registerReceiver(mBluetoothStateReceiver, mIntentFilter); } - } @Override 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/BluetoothStateLiveDataTest.java b/tests/unittests/src/com/android/car/dialer/bluetooth/BluetoothStateLiveDataTest.java index dc501577..00e9ec66 100644 --- a/tests/robotests/src/com/android/car/dialer/bluetooth/BluetoothStateLiveDataTest.java +++ b/tests/unittests/src/com/android/car/dialer/bluetooth/BluetoothStateLiveDataTest.java @@ -20,97 +20,98 @@ 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 static org.robolectric.Shadows.shadowOf; import android.bluetooth.BluetoothAdapter; +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; 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.shadows.ShadowBluetoothAdapter; -@RunWith(CarDialerRobolectricTestRunner.class) +@RunWith(AndroidJUnit4.class) public class BluetoothStateLiveDataTest { private static final String INTENT_ACTION = BluetoothAdapter.ACTION_STATE_CHANGED; private BluetoothStateLiveData mBluetoothStateLiveData; private LifecycleRegistry mLifecycleRegistry; - private BroadcastReceiverVerifier mReceiverVerifier; @Mock private LifecycleOwner mMockLifecycleOwner; @Mock - private LiveDataObserver<Integer> mMockObserver; + private Observer<Integer> mMockObserver; + @Mock + private Context mContext; + @Mock + private BluetoothAdapter mBluetoothAdapter; + @Captor + private ArgumentCaptor<BroadcastReceiver> mReceiverCaptor; @Before public void setup() { MockitoAnnotations.initMocks(this); - mBluetoothStateLiveData = new BluetoothStateLiveData(RuntimeEnvironment.application, - BluetoothAdapter.getDefaultAdapter()); + mBluetoothStateLiveData = new BluetoothStateLiveData(mContext, mBluetoothAdapter); mLifecycleRegistry = new LifecycleRegistry(mMockLifecycleOwner); when(mMockLifecycleOwner.getLifecycle()).thenReturn(mLifecycleRegistry); - - mReceiverVerifier = new BroadcastReceiverVerifier(RuntimeEnvironment.application); } @Test + @UiThreadTest public void testOnActive() { mBluetoothStateLiveData.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 testOnBluetoothAdapterStateChange() { ArgumentCaptor<Integer> valueCaptor = ArgumentCaptor.forClass(Integer.class); doNothing().when(mMockObserver).onChanged(valueCaptor.capture()); - ShadowBluetoothAdapter shadowBluetoothAdapter = shadowOf( - BluetoothAdapter.getDefaultAdapter()); - shadowBluetoothAdapter.setEnabled(false); - + when(mBluetoothAdapter.isEnabled()).thenReturn(false); mBluetoothStateLiveData.observe(mMockLifecycleOwner, (value) -> mMockObserver.onChanged(value)); mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); assertThat(valueCaptor.getValue()).isEqualTo(BluetoothState.DISABLED); + verify(mContext).registerReceiver(mReceiverCaptor.capture(), any()); - shadowBluetoothAdapter.setEnabled(true); - mReceiverVerifier.getBroadcastReceiverFor(INTENT_ACTION) - .onReceive(mock(Context.class), mock(Intent.class)); + when(mBluetoothAdapter.isEnabled()).thenReturn(true); + Intent intent = new Intent(); + intent.setAction(INTENT_ACTION); + mReceiverCaptor.getValue().onReceive(mContext, intent); assertThat(mBluetoothStateLiveData.getValue()).isEqualTo(BluetoothState.ENABLED); assertThat(valueCaptor.getValue()).isEqualTo(BluetoothState.ENABLED); } @Test + @UiThreadTest public void testOnInactiveUnregister() { mBluetoothStateLiveData.observe(mMockLifecycleOwner, (value) -> mMockObserver.onChanged(value)); 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()); } } 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()); } } diff --git a/tests/robotests/src/com/android/car/dialer/livedata/CallDetailLiveDataTest.java b/tests/unittests/src/com/android/car/dialer/livedata/CallDetailLiveDataTest.java index ba4d50d1..29ac861a 100644 --- a/tests/robotests/src/com/android/car/dialer/livedata/CallDetailLiveDataTest.java +++ b/tests/unittests/src/com/android/car/dialer/livedata/CallDetailLiveDataTest.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. @@ -18,12 +18,8 @@ package com.android.car.dialer.livedata; 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.reset; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.net.Uri; @@ -34,9 +30,10 @@ import android.telecom.GatewayInfo; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleRegistry; +import androidx.lifecycle.Observer; +import androidx.test.annotation.UiThreadTest; +import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.android.car.dialer.CarDialerRobolectricTestRunner; -import com.android.car.dialer.LiveDataObserver; import com.android.car.telephony.common.CallDetail; import org.junit.Before; @@ -47,7 +44,7 @@ import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -@RunWith(CarDialerRobolectricTestRunner.class) +@RunWith(AndroidJUnit4.class) public class CallDetailLiveDataTest { private CallDetailLiveData mCallDetailLiveData; @@ -57,7 +54,7 @@ public class CallDetailLiveDataTest { @Mock private LifecycleOwner mMockLifecycleOwner; @Mock - private LiveDataObserver<Integer> mMockObserver; + private Observer<CallDetail> mMockObserver; @Captor private ArgumentCaptor<Call.Callback> mCallbackCaptor; @@ -65,34 +62,20 @@ public class CallDetailLiveDataTest { public void setup() { MockitoAnnotations.initMocks(this); - doNothing().when(mMockCall).registerCallback(mCallbackCaptor.capture()); - mCallDetailLiveData = new CallDetailLiveData(); - mCallDetailLiveData.setTelecomCall(mMockCall); + doNothing().when(mMockCall).registerCallback(mCallbackCaptor.capture()); mLifecycleRegistry = new LifecycleRegistry(mMockLifecycleOwner); when(mMockLifecycleOwner.getLifecycle()).thenReturn(mLifecycleRegistry); } @Test - public void testOnActiveRegistry() { - mCallDetailLiveData.observe(mMockLifecycleOwner, (value) -> mMockObserver.onChanged(value)); - verify(mMockObserver, never()).onChanged(any()); - assertThat(mCallDetailLiveData.hasObservers()).isTrue(); - assertThat(mCallDetailLiveData.hasActiveObservers()).isFalse(); - - reset(mMockCall); - mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); - - assertThat(mCallDetailLiveData.hasActiveObservers()).isTrue(); - verify(mMockObserver).onChanged(any()); - verify(mMockCall).registerCallback(any()); - } - - @Test + @UiThreadTest public void testOnDetailsChanged() { + mCallDetailLiveData.setTelecomCall(mMockCall); ArgumentCaptor<CallDetail> valueCaptor = ArgumentCaptor.forClass(CallDetail.class); doNothing().when(mMockObserver).onChanged(valueCaptor.capture()); - startObserving(); + mCallDetailLiveData.observe(mMockLifecycleOwner, (value) -> mMockObserver.onChanged(value)); + mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); // Set up updated details String number = "6505551234"; @@ -106,8 +89,8 @@ public class CallDetailLiveDataTest { when(updatedDetails.getDisconnectCause()).thenReturn(disconnectCause); when(updatedDetails.getGatewayInfo()).thenReturn(gatewayInfo); when(updatedDetails.getConnectTimeMillis()).thenReturn(connectTimeMillis); - when(mMockCall.getDetails()).thenReturn(updatedDetails); + mCallbackCaptor.getValue().onDetailsChanged(mMockCall, updatedDetails); CallDetail observedValue = valueCaptor.getValue(); @@ -116,21 +99,4 @@ public class CallDetailLiveDataTest { assertThat(observedValue.getDisconnectCause()).isEqualTo(label); assertThat(observedValue.getGatewayInfoOriginalAddress()).isEqualTo(uri); } - - @Test - public void testOnInactiveUnregister() { - startObserving(); - - reset(mMockCall); - mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); - - verify(mMockCall).unregisterCallback(mCallbackCaptor.getValue()); - assertThat(mCallDetailLiveData.hasObservers()).isFalse(); - assertThat(mCallDetailLiveData.hasActiveObservers()).isFalse(); - } - - private void startObserving() { - mCallDetailLiveData.observe(mMockLifecycleOwner, (value) -> mMockObserver.onChanged(value)); - mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); - } } |