[Settings] Fix flaky presubmit failure

Bug: 229689535
Test: unit test
Change-Id: I0fc193d6f34cfff13d15487ad0c1f646f67d4c97
This commit is contained in:
Bonian Chen
2022-04-22 22:59:04 +08:00
parent a95ee31cc8
commit cc7474d50d
5 changed files with 34 additions and 7 deletions

View File

@@ -43,6 +43,9 @@ public class LifecycleCallbackConverterTest implements LifecycleOwner {
public void setUp() {
mTestData = new Object();
mConsumer = new TestConsumer();
}
private void initEnvPerTestCase() {
mConverter = new LifecycleCallbackConverter<Object>(getLifecycle(), mConsumer);
}
@@ -52,6 +55,7 @@ public class LifecycleCallbackConverterTest implements LifecycleOwner {
@Test
public void converter_dropResult_whenInActive() {
initEnvPerTestCase();
mConverter.postResult(mTestData);
assertThat(mConsumer.getCallbackCount()).isEqualTo(0);
@@ -59,6 +63,7 @@ public class LifecycleCallbackConverterTest implements LifecycleOwner {
@Test
public void converter_callbackResult_whenActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
@@ -69,6 +74,7 @@ public class LifecycleCallbackConverterTest implements LifecycleOwner {
@Test
public void converter_dropResult_whenBackToInActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
@@ -79,6 +85,7 @@ public class LifecycleCallbackConverterTest implements LifecycleOwner {
@Test
public void converter_passResultToUiThread_whenActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);

View File

@@ -64,18 +64,21 @@ public class LifecycleCallbackIntentReceiverTest implements LifecycleOwner {
mHandler = new Handler(thread.getLooper());
mConsumer = new TestConsumer();
mTarget = new TestObj(getLifecycle(), mContext,
mIntentFilter, TEST_INTENT_PERMISSION,
mHandler, mConsumer);
}
public Lifecycle getLifecycle() {
return mRegistry;
}
private void initEnvPerTestCase() {
mTarget = new TestObj(getLifecycle(), mContext,
mIntentFilter, TEST_INTENT_PERMISSION,
mHandler, mConsumer);
}
@Test
public void receiver_register_whenActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
assertThat(mTarget.getCallbackActiveCount(true)
@@ -98,6 +101,7 @@ public class LifecycleCallbackIntentReceiverTest implements LifecycleOwner {
@Test
public void receiver_unregister_whenInActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
@@ -111,6 +115,7 @@ public class LifecycleCallbackIntentReceiverTest implements LifecycleOwner {
@Test
public void receiver_register_whenReActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
@@ -126,6 +131,7 @@ public class LifecycleCallbackIntentReceiverTest implements LifecycleOwner {
@Test
public void receiver_close_whenDestroy() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);

View File

@@ -58,17 +58,20 @@ public class LifecycleCallbackTelephonyAdapterTest implements LifecycleOwner {
doNothing().when(mTelMgr).registerTelephonyCallback(null, mTestCallback);
doNothing().when(mTelMgr).unregisterTelephonyCallback(mTestCallback);
mAdapter = new LifecycleCallbackTelephonyAdapter<Object>(getLifecycle(), mTelMgr,
mTestCallback, null, result -> mResult.set(result));
}
public Lifecycle getLifecycle() {
return mRegistry;
}
private void initEnvPerTestCase() {
mAdapter = new LifecycleCallbackTelephonyAdapter<Object>(getLifecycle(), mTelMgr,
mTestCallback, null, result -> mResult.set(result));
}
@Test
public void telephonyCallback_register_whenActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
verify(mTelMgr, never()).registerTelephonyCallback(anyObject(), anyObject());
@@ -80,6 +83,7 @@ public class LifecycleCallbackTelephonyAdapterTest implements LifecycleOwner {
@Test
public void telephonyCallback_unregister_whenInActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
verify(mTelMgr, never()).unregisterTelephonyCallback(anyObject());

View File

@@ -51,6 +51,9 @@ public class ServiceStateStatusTest implements LifecycleOwner {
MockitoAnnotations.initMocks(this);
mStatusStorage = new AtomicReference<ServiceState>();
}
private void initEnvPerTestCase() {
mServiceStateStatus = new ServiceStateStatus(getLifecycle(), mTelMgr, null) {
@Override
protected void setValue(ServiceState status) {
@@ -65,6 +68,7 @@ public class ServiceStateStatusTest implements LifecycleOwner {
@Test
public void telephonyCallback_updateStatus_whenActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mServiceStateStatus.mServiceStateProducer.onServiceStateChanged(mTestData);
@@ -80,6 +84,7 @@ public class ServiceStateStatusTest implements LifecycleOwner {
@Test
public void telephonyCallback_updateStatusToNull_whenInActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);

View File

@@ -48,6 +48,9 @@ public class VoiceCallStatusTest implements LifecycleOwner {
MockitoAnnotations.initMocks(this);
mStatusStorage = new AtomicReference<Integer>();
}
private void initEnvPerTestCase() {
mVoiceCallStatus = new VoiceCallStatus(getLifecycle(), mTelMgr, null) {
//ArchTaskExecutor.getMainThreadExecutor()) {
@Override
@@ -63,6 +66,7 @@ public class VoiceCallStatusTest implements LifecycleOwner {
@Test
public void telephonyCallback_updateStatus_whenActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mVoiceCallStatus.mCallStateProducer.onCallStateChanged(
@@ -80,6 +84,7 @@ public class VoiceCallStatusTest implements LifecycleOwner {
@Test
public void telephonyCallback_updateStatusToNull_whenInActive() {
initEnvPerTestCase();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);