summaryrefslogtreecommitdiff
path: root/tests/unittests/src/com/android/car/dialer/livedata
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittests/src/com/android/car/dialer/livedata')
-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
3 files changed, 180 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());
+ }
+}