From cc7474d50de5b0da70bdc94387cd1fb768ff61ea Mon Sep 17 00:00:00 2001 From: Bonian Chen Date: Fri, 22 Apr 2022 22:59:04 +0800 Subject: [PATCH] [Settings] Fix flaky presubmit failure Bug: 229689535 Test: unit test Change-Id: I0fc193d6f34cfff13d15487ad0c1f646f67d4c97 --- .../helper/LifecycleCallbackConverterTest.java | 7 +++++++ .../LifecycleCallbackIntentReceiverTest.java | 14 ++++++++++---- .../LifecycleCallbackTelephonyAdapterTest.java | 10 +++++++--- .../network/helper/ServiceStateStatusTest.java | 5 +++++ .../network/helper/VoiceCallStatusTest.java | 5 +++++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackConverterTest.java b/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackConverterTest.java index 149fd8293db..22377469c22 100644 --- a/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackConverterTest.java +++ b/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackConverterTest.java @@ -43,6 +43,9 @@ public class LifecycleCallbackConverterTest implements LifecycleOwner { public void setUp() { mTestData = new Object(); mConsumer = new TestConsumer(); + } + + private void initEnvPerTestCase() { mConverter = new LifecycleCallbackConverter(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); diff --git a/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackIntentReceiverTest.java b/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackIntentReceiverTest.java index c85937d3a34..780ee55e93a 100644 --- a/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackIntentReceiverTest.java +++ b/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackIntentReceiverTest.java @@ -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); diff --git a/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapterTest.java b/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapterTest.java index be940f2fa95..3968a03cf88 100644 --- a/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapterTest.java +++ b/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapterTest.java @@ -58,17 +58,20 @@ public class LifecycleCallbackTelephonyAdapterTest implements LifecycleOwner { doNothing().when(mTelMgr).registerTelephonyCallback(null, mTestCallback); doNothing().when(mTelMgr).unregisterTelephonyCallback(mTestCallback); - - mAdapter = new LifecycleCallbackTelephonyAdapter(getLifecycle(), mTelMgr, - mTestCallback, null, result -> mResult.set(result)); } public Lifecycle getLifecycle() { return mRegistry; } + private void initEnvPerTestCase() { + mAdapter = new LifecycleCallbackTelephonyAdapter(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()); diff --git a/tests/unit/src/com/android/settings/network/helper/ServiceStateStatusTest.java b/tests/unit/src/com/android/settings/network/helper/ServiceStateStatusTest.java index 1be24f9dcc3..133c338e184 100644 --- a/tests/unit/src/com/android/settings/network/helper/ServiceStateStatusTest.java +++ b/tests/unit/src/com/android/settings/network/helper/ServiceStateStatusTest.java @@ -51,6 +51,9 @@ public class ServiceStateStatusTest implements LifecycleOwner { MockitoAnnotations.initMocks(this); mStatusStorage = new AtomicReference(); + } + + 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); diff --git a/tests/unit/src/com/android/settings/network/helper/VoiceCallStatusTest.java b/tests/unit/src/com/android/settings/network/helper/VoiceCallStatusTest.java index 3d7489275f9..dd4e28a31cf 100644 --- a/tests/unit/src/com/android/settings/network/helper/VoiceCallStatusTest.java +++ b/tests/unit/src/com/android/settings/network/helper/VoiceCallStatusTest.java @@ -48,6 +48,9 @@ public class VoiceCallStatusTest implements LifecycleOwner { MockitoAnnotations.initMocks(this); mStatusStorage = new AtomicReference(); + } + + 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);