summaryrefslogtreecommitdiff
path: root/tests/unittests/src
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittests/src')
-rw-r--r--tests/unittests/src/com/android/car/dialer/livedata/CallStateLiveDataTest.java118
-rw-r--r--tests/unittests/src/com/android/car/dialer/livedata/CallStateLiveDataUnitTest.java36
-rw-r--r--tests/unittests/src/com/android/car/dialer/livedata/HeartBeatLiveDataTest.java62
-rw-r--r--tests/unittests/src/com/android/car/dialer/ui/activecall/OngoingCallFragmentTest.java139
-rw-r--r--tests/unittests/src/com/android/car/dialer/ui/favorite/FavoriteFragmentTest.java138
-rw-r--r--tests/unittests/src/com/android/car/dialer/ui/warning/BluetoothErrorStringLiveDataTest.java145
6 files changed, 602 insertions, 36 deletions
diff --git a/tests/unittests/src/com/android/car/dialer/livedata/CallStateLiveDataTest.java b/tests/unittests/src/com/android/car/dialer/livedata/CallStateLiveDataTest.java
new file mode 100644
index 00000000..11cad651
--- /dev/null
+++ b/tests/unittests/src/com/android/car/dialer/livedata/CallStateLiveDataTest.java
@@ -0,0 +1,118 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.telecom.Call;
+
+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 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;
+
+@RunWith(AndroidJUnit4.class)
+public class CallStateLiveDataTest {
+
+ private CallStateLiveData mCallStateLiveData;
+ private LifecycleRegistry mLifecycleRegistry;
+ @Mock
+ private Call mMockCall;
+ @Mock
+ private LifecycleOwner mMockLifecycleOwner;
+ @Mock
+ private Observer<Integer> mMockObserver;
+ @Captor
+ private ArgumentCaptor<Call.Callback> mCallbackCaptor;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+
+ doNothing().when(mMockCall).registerCallback(mCallbackCaptor.capture());
+ when(mMockCall.getState()).thenReturn(Call.STATE_NEW);
+
+ mCallStateLiveData = new CallStateLiveData(mMockCall);
+ mLifecycleRegistry = new LifecycleRegistry(mMockLifecycleOwner);
+ when(mMockLifecycleOwner.getLifecycle()).thenReturn(mLifecycleRegistry);
+ }
+
+ @Test
+ @UiThreadTest
+ public void testOnActiveRegistry() {
+ mCallStateLiveData.onActive();
+
+ verify(mMockCall).registerCallback(any());
+ }
+
+ @Test
+ @UiThreadTest
+ public void testOnLifecycleStart() {
+ mCallStateLiveData.observe(mMockLifecycleOwner, (value) -> mMockObserver.onChanged(value));
+ verify(mMockObserver, never()).onChanged(any());
+ assertThat(mCallStateLiveData.hasObservers()).isTrue();
+ assertThat(mCallStateLiveData.hasActiveObservers()).isFalse();
+
+ mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
+ verify(mMockObserver).onChanged(any());
+ assertThat(mCallStateLiveData.hasActiveObservers()).isTrue();
+ verify(mMockCall).registerCallback(any());
+ }
+
+ @Test
+ @UiThreadTest
+ public void testOnStateChanged() {
+ ArgumentCaptor<Integer> valueCaptor = ArgumentCaptor.forClass(Integer.class);
+ doNothing().when(mMockObserver).onChanged(valueCaptor.capture());
+
+ mCallStateLiveData.observe(mMockLifecycleOwner, (value) -> mMockObserver.onChanged(value));
+ mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
+
+ assertThat(valueCaptor.getValue()).isEqualTo(Call.STATE_NEW);
+ mCallbackCaptor.getValue().onStateChanged(mMockCall, Call.STATE_ACTIVE);
+ assertThat(valueCaptor.getValue()).isEqualTo(Call.STATE_ACTIVE);
+ }
+
+ @Test
+ @UiThreadTest
+ public void testOnInactiveUnregister() {
+ mCallStateLiveData.observe(mMockLifecycleOwner, (value) -> mMockObserver.onChanged(value));
+ mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
+
+ mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
+
+ verify(mMockCall).unregisterCallback(mCallbackCaptor.getValue());
+ assertThat(mCallStateLiveData.hasObservers()).isFalse();
+ assertThat(mCallStateLiveData.hasActiveObservers()).isFalse();
+ }
+}
diff --git a/tests/unittests/src/com/android/car/dialer/livedata/CallStateLiveDataUnitTest.java b/tests/unittests/src/com/android/car/dialer/livedata/CallStateLiveDataUnitTest.java
deleted file mode 100644
index 9488040c..00000000
--- a/tests/unittests/src/com/android/car/dialer/livedata/CallStateLiveDataUnitTest.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (C) 2020 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.car.dialer.livedata;
-
-import androidx.test.ext.junit.runners.AndroidJUnit4;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-@RunWith(AndroidJUnit4.class)
-public class CallStateLiveDataUnitTest {
-
- @Before
- public void setup() {
- }
-
- @Test
- public void testOnActiveRegistry() {
- }
-}
-
diff --git a/tests/unittests/src/com/android/car/dialer/livedata/HeartBeatLiveDataTest.java b/tests/unittests/src/com/android/car/dialer/livedata/HeartBeatLiveDataTest.java
new file mode 100644
index 00000000..0d2374bd
--- /dev/null
+++ b/tests/unittests/src/com/android/car/dialer/livedata/HeartBeatLiveDataTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.car.dialer.livedata;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import android.text.format.DateUtils;
+
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.Observer;
+import androidx.test.core.app.ActivityScenario;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import com.android.car.dialer.testing.TestActivity;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@RunWith(AndroidJUnit4.class)
+public class HeartBeatLiveDataTest {
+
+ private HeartBeatLiveData mHeartBeatLiveData;
+ @Mock
+ private Observer<Boolean> mMockObserver;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ mHeartBeatLiveData = new HeartBeatLiveData(DateUtils.SECOND_IN_MILLIS);
+ }
+
+ @Test
+ public void active_onLifecycleStart() {
+ ActivityScenario<TestActivity> activityScenario = ActivityScenario.launch(
+ TestActivity.class);
+ activityScenario.moveToState(Lifecycle.State.CREATED);
+ activityScenario.onActivity(activity ->
+ mHeartBeatLiveData.observe(activity, (value) -> mMockObserver.onChanged(value)));
+ verify(mMockObserver, never()).onChanged(any());
+
+ activityScenario.moveToState(Lifecycle.State.STARTED);
+ verify(mMockObserver).onChanged(any());
+ }
+}
diff --git a/tests/unittests/src/com/android/car/dialer/ui/activecall/OngoingCallFragmentTest.java b/tests/unittests/src/com/android/car/dialer/ui/activecall/OngoingCallFragmentTest.java
new file mode 100644
index 00000000..947c5b22
--- /dev/null
+++ b/tests/unittests/src/com/android/car/dialer/ui/activecall/OngoingCallFragmentTest.java
@@ -0,0 +1,139 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.dialer.ui.activecall;
+
+import static androidx.test.espresso.Espresso.onView;
+import static androidx.test.espresso.assertion.ViewAssertions.matches;
+import static androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
+import static androidx.test.espresso.matcher.ViewMatchers.withId;
+
+import static org.mockito.Mockito.when;
+
+import android.telecom.Call;
+import android.telecom.CallAudioState;
+
+import androidx.core.util.Pair;
+import androidx.lifecycle.LiveData;
+import androidx.lifecycle.MutableLiveData;
+import androidx.lifecycle.ViewModelProvider;
+import androidx.test.core.app.ActivityScenario;
+import androidx.test.espresso.matcher.ViewMatchers;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import com.android.car.dialer.R;
+import com.android.car.dialer.livedata.CallDetailLiveData;
+import com.android.car.dialer.testing.TestActivity;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.Collections;
+
+@RunWith(AndroidJUnit4.class)
+public class OngoingCallFragmentTest {
+
+ private OngoingCallFragment mOngoingCallFragment;
+ private Call mMockCall;
+ @Mock
+ private CallAudioState mMockCallAudioState;
+ private CallDetailLiveData mMockCallDetailLiveData;
+ private LiveData<Pair<Integer, Long>> mCallStateAndConnectTimeLiveData;
+ private LiveData<Boolean> mShouldShowOnHoldCall;
+
+ private ActivityScenario<TestActivity> mActivityScenario;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ startFragment();
+ }
+
+ @Test
+ public void testOnCreateView() {
+ onView(withId(R.id.incall_dialpad_fragment))
+ .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
+ onView(withId(R.id.user_profile_container))
+ .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
+ }
+
+ @Test
+ public void testOnOpenDialpad() {
+ mActivityScenario.onActivity(activity -> {
+ mOngoingCallFragment.onOpenDialpad();
+ });
+
+ onView(withId(R.id.incall_dialpad_fragment))
+ .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
+ onView(withId(R.id.user_profile_container))
+ .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
+ }
+
+ @Test
+ public void testOnCloseDialpad() {
+ mActivityScenario.onActivity(activity -> {
+ mOngoingCallFragment.onCloseDialpad();
+ });
+
+ onView(withId(R.id.incall_dialpad_fragment))
+ .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
+ onView(withId(R.id.user_profile_container))
+ .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
+ }
+
+ private void startFragment() {
+ mActivityScenario = ActivityScenario.launch(TestActivity.class);
+ mActivityScenario.onActivity(activity -> {
+ InCallViewModel mockInCallViewModel = new ViewModelProvider(activity).get(
+ InCallViewModel.class);
+ mMockCallDetailLiveData = new CallDetailLiveData();
+ mMockCallDetailLiveData.setTelecomCall(mMockCall);
+ mShouldShowOnHoldCall = new MutableLiveData<>(false);
+ mCallStateAndConnectTimeLiveData =
+ new MutableLiveData<>(new Pair<>(Call.STATE_ACTIVE, 1000L));
+ when(mockInCallViewModel.getPrimaryCallState())
+ .thenReturn(new MutableLiveData<>(Call.STATE_ACTIVE));
+ when(mockInCallViewModel.getPrimaryCallDetail()).thenReturn(mMockCallDetailLiveData);
+ when(mockInCallViewModel.getCallStateAndConnectTime())
+ .thenReturn(mCallStateAndConnectTimeLiveData);
+ when(mockInCallViewModel.shouldShowOnholdCall()).thenReturn(mShouldShowOnHoldCall);
+ when(mockInCallViewModel.getCallAudioState())
+ .thenReturn(new MutableLiveData<>(mMockCallAudioState));
+ when(mockInCallViewModel.getAudioRoute()).thenReturn(new MutableLiveData<>(1));
+ when(mockInCallViewModel.getSupportedAudioRoutes())
+ .thenReturn(new MutableLiveData<>(Collections.EMPTY_LIST));
+ when(mockInCallViewModel.getSecondaryCallDetail()).thenReturn(mMockCallDetailLiveData);
+ when(mockInCallViewModel.getPrimaryCall()).thenReturn(new MutableLiveData<>(mMockCall));
+ when(mockInCallViewModel.getSecondaryCallConnectTime())
+ .thenReturn(new MutableLiveData<>(1000L));
+ when(mockInCallViewModel.getAllCallList())
+ .thenReturn(new MutableLiveData<>(Collections.EMPTY_LIST));
+ when(mockInCallViewModel.getOngoingCallPair())
+ .thenReturn(new MutableLiveData<>(new Pair<>(mMockCall, mMockCall)));
+ when(mockInCallViewModel.getOngoingCallList())
+ .thenReturn(new MutableLiveData<>(Collections.EMPTY_LIST));
+ when(mockInCallViewModel.getDialpadOpenState())
+ .thenReturn(new MutableLiveData<>(false));
+
+ mOngoingCallFragment = new OngoingCallFragment();
+ activity.getSupportFragmentManager().beginTransaction().add(
+ R.id.test_fragment_container, mOngoingCallFragment).commit();
+ });
+ }
+}
diff --git a/tests/unittests/src/com/android/car/dialer/ui/favorite/FavoriteFragmentTest.java b/tests/unittests/src/com/android/car/dialer/ui/favorite/FavoriteFragmentTest.java
new file mode 100644
index 00000000..1c8141ce
--- /dev/null
+++ b/tests/unittests/src/com/android/car/dialer/ui/favorite/FavoriteFragmentTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.dialer.ui.favorite;
+
+import static androidx.test.espresso.Espresso.onView;
+import static androidx.test.espresso.action.ViewActions.click;
+import static androidx.test.espresso.matcher.ViewMatchers.withId;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.any;
+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 androidx.lifecycle.MutableLiveData;
+import androidx.lifecycle.ViewModelProvider;
+import androidx.test.core.app.ActivityScenario;
+import androidx.test.espresso.contrib.RecyclerViewActions;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import com.android.car.arch.common.FutureData;
+import com.android.car.dialer.R;
+import com.android.car.dialer.telecom.UiCallManager;
+import com.android.car.dialer.testing.TestActivity;
+import com.android.car.telephony.common.Contact;
+import com.android.car.telephony.common.PhoneNumber;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.Arrays;
+import java.util.List;
+
+@RunWith(AndroidJUnit4.class)
+public class FavoriteFragmentTest {
+ private static final String RAW_NUMBER = "6502530000";
+
+ private FavoriteFragment mFavoriteFragment;
+ @Mock
+ private UiCallManager mMockUiCallManager;
+ @Mock
+ private Contact mMockContact;
+
+ private FavoriteViewModel mFavoriteViewModel;
+ @Mock
+ private PhoneNumber mMockPhoneNumber;
+
+ ActivityScenario<TestActivity> mActivityScenario;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ startActivity();
+ }
+
+ private void startActivity() {
+ when(mMockPhoneNumber.getRawNumber()).thenReturn(RAW_NUMBER);
+ MutableLiveData<FutureData<List<Object>>> favoriteContacts = new MutableLiveData<>(
+ new FutureData<>(false, Arrays.asList(mMockContact)));
+
+ mActivityScenario = ActivityScenario.launch(TestActivity.class);
+ mActivityScenario.onActivity(activity -> {
+ mFavoriteViewModel = new ViewModelProvider(activity).get(
+ FavoriteViewModel.class);
+ when(mFavoriteViewModel.getFavoriteContacts()).thenReturn(favoriteContacts);
+ when(mFavoriteViewModel.getSortOrderLiveData()).thenReturn(new MutableLiveData<>(1));
+
+ mFavoriteFragment = FavoriteFragment.newInstance();
+
+ activity.getSupportFragmentManager().beginTransaction().add(
+ R.id.test_fragment_container, mFavoriteFragment).commit();
+ });
+
+ mActivityScenario.onActivity(activity -> {
+ mFavoriteFragment.mUiCallManager = mMockUiCallManager;
+ });
+ }
+
+ @Test
+ public void testOnItemClick_contactHasPrimaryNumber_placeCall() {
+ when(mMockContact.getNumbers()).thenReturn(Arrays.asList(mMockPhoneNumber));
+ when(mMockContact.hasPrimaryPhoneNumber()).thenReturn(true);
+ when(mMockContact.getPrimaryPhoneNumber()).thenReturn(mMockPhoneNumber);
+
+ onView(withId(R.id.list_view)).perform(
+ RecyclerViewActions.actionOnItemAtPosition(0, click()));
+
+ ArgumentCaptor<String> mCaptor = ArgumentCaptor.forClass(String.class);
+ verify(mMockUiCallManager).placeCall(mCaptor.capture());
+ assertThat(mCaptor.getValue()).isEqualTo(RAW_NUMBER);
+ }
+
+ @Test
+ public void testOnItemClick_contactHasOnlyOneNumber_placeCall() {
+ when(mMockContact.hasPrimaryPhoneNumber()).thenReturn(false);
+ when(mMockContact.getNumbers()).thenReturn(Arrays.asList(mMockPhoneNumber));
+
+ onView(withId(R.id.list_view)).perform(
+ RecyclerViewActions.actionOnItemAtPosition(0, click()));
+
+ ArgumentCaptor<String> mCaptor = ArgumentCaptor.forClass(String.class);
+ verify(mMockUiCallManager).placeCall(mCaptor.capture());
+ assertThat(mCaptor.getValue()).isEqualTo(RAW_NUMBER);
+ }
+
+ @Test
+ public void testOnItemClick_contactHasMultiNumbers_notPlaceCall() {
+ when(mMockContact.hasPrimaryPhoneNumber()).thenReturn(false);
+ PhoneNumber otherMockPhoneNumber = mock(PhoneNumber.class);
+ when(mMockContact.getNumbers()).thenReturn(
+ Arrays.asList(mMockPhoneNumber, otherMockPhoneNumber));
+
+ onView(withId(R.id.list_view)).perform(
+ RecyclerViewActions.actionOnItemAtPosition(0, click()));
+
+ verify(mMockUiCallManager, never()).placeCall(any());
+ }
+}
diff --git a/tests/unittests/src/com/android/car/dialer/ui/warning/BluetoothErrorStringLiveDataTest.java b/tests/unittests/src/com/android/car/dialer/ui/warning/BluetoothErrorStringLiveDataTest.java
new file mode 100644
index 00000000..14ea4a31
--- /dev/null
+++ b/tests/unittests/src/com/android/car/dialer/ui/warning/BluetoothErrorStringLiveDataTest.java
@@ -0,0 +1,145 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.dialer.ui.warning;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.mock;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.content.Context;
+
+import androidx.lifecycle.MutableLiveData;
+import androidx.lifecycle.Observer;
+import androidx.test.annotation.UiThreadTest;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import com.android.car.dialer.R;
+import com.android.car.dialer.bluetooth.BluetoothState;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+@RunWith(AndroidJUnit4.class)
+public class BluetoothErrorStringLiveDataTest {
+
+ @Mock
+ private Context mContext;
+ @Mock
+ private Observer<String> mMockObserver;
+ private BluetoothAdapter mBluetoothAdapter;
+ private MutableLiveData<List<BluetoothDevice>> mHfpDeviceListLiveData;
+ private MutableLiveData<Set<BluetoothDevice>> mPairedListLiveData;
+ private MutableLiveData<Integer> mBluetoothStateLiveData;
+ private BluetoothErrorStringLiveData mBluetoothErrorStringLiveData;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mHfpDeviceListLiveData = new MutableLiveData<>(Collections.emptyList());
+ mPairedListLiveData = new MutableLiveData<>(Collections.emptySet());
+ mBluetoothStateLiveData = new MutableLiveData<>(0);
+ }
+
+ @Test
+ @UiThreadTest
+ public void testDialerAppState_defaultBluetoothAdapterIsNull_bluetoothError() {
+ mBluetoothAdapter = null;
+
+ initializeBluetoothErrorStringLiveData();
+
+ assertThat(mBluetoothErrorStringLiveData.getValue()).isEqualTo(
+ mContext.getString(R.string.bluetooth_unavailable));
+ }
+
+ @Test
+ @UiThreadTest
+ public void testDialerAppState_bluetoothNotEnabled_bluetoothError() {
+ mBluetoothAdapter = mock(BluetoothAdapter.class);
+ mBluetoothStateLiveData.setValue(BluetoothState.DISABLED);
+
+ initializeBluetoothErrorStringLiveData();
+
+ assertThat(mBluetoothErrorStringLiveData.getValue()).isEqualTo(
+ mContext.getString(R.string.bluetooth_disabled));
+ }
+
+ @Test
+ @UiThreadTest
+ public void testDialerAppState_noPairedDevices_bluetoothError() {
+ mBluetoothAdapter = mock(BluetoothAdapter.class);
+ mBluetoothStateLiveData.setValue(BluetoothState.ENABLED);
+ mPairedListLiveData.setValue(Collections.emptySet());
+
+ initializeBluetoothErrorStringLiveData();
+
+ assertThat(mBluetoothErrorStringLiveData.getValue()).isEqualTo(
+ mContext.getString(R.string.bluetooth_unpaired));
+ }
+
+ @Test
+ @UiThreadTest
+ public void testDialerAppState_hfpNoConnected_bluetoothError() {
+ mBluetoothAdapter = mock(BluetoothAdapter.class);
+ mBluetoothStateLiveData.setValue(BluetoothState.ENABLED);
+ BluetoothDevice mockBluetoothDevice = mock(BluetoothDevice.class);
+ mPairedListLiveData.setValue(Collections.singleton(mockBluetoothDevice));
+ mHfpDeviceListLiveData.setValue(Collections.emptyList());
+
+ initializeBluetoothErrorStringLiveData();
+
+ assertThat(mBluetoothErrorStringLiveData.getValue()).isEqualTo(
+ mContext.getString(R.string.no_hfp));
+ }
+
+ @Test
+ @UiThreadTest
+ public void testDialerAppState_bluetoothAllSet_dialerAppNoError() {
+ mBluetoothAdapter = mock(BluetoothAdapter.class);
+ mBluetoothStateLiveData.setValue(BluetoothState.ENABLED);
+ BluetoothDevice mockBluetoothDevice = mock(BluetoothDevice.class);
+ mPairedListLiveData.setValue(Collections.singleton(mockBluetoothDevice));
+ mHfpDeviceListLiveData.setValue(Collections.singletonList(mockBluetoothDevice));
+
+ initializeBluetoothErrorStringLiveData();
+
+ assertThat(mBluetoothErrorStringLiveData.getValue()).isEqualTo(
+ BluetoothErrorStringLiveData.NO_BT_ERROR);
+ }
+
+ private void initializeBluetoothErrorStringLiveData() {
+ mBluetoothErrorStringLiveData = new BluetoothErrorStringLiveData(mContext,
+ mHfpDeviceListLiveData, mPairedListLiveData, mBluetoothStateLiveData,
+ mBluetoothAdapter);
+ // Observers needed so that the liveData's internal initialization is triggered
+ mBluetoothStateLiveData.observeForever(error -> {
+ });
+ mHfpDeviceListLiveData.observeForever(error -> {
+ });
+ mPairedListLiveData.observeForever(error -> {
+ });
+ mBluetoothErrorStringLiveData.observeForever(mMockObserver);
+ }
+}