From 186644aa6e59a39a5a9b183ec94575870bfec145 Mon Sep 17 00:00:00 2001 From: Chun-Ku Lin Date: Tue, 19 Sep 2023 08:13:00 +0000 Subject: [PATCH] Fix ScreenFlashNotificationColorDialogFragmentTest by launching the fragment in modern way. Fix: 300158485 Test: atest ScreenFlashNotificationColorDialogFragmentTest Change-Id: I3396b05ad78501b38e68360e25389966d379aacb --- ...shNotificationColorDialogFragmentTest.java | 76 +++++++++---------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/tests/robotests/src/com/android/settings/accessibility/ScreenFlashNotificationColorDialogFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/ScreenFlashNotificationColorDialogFragmentTest.java index 04b48c02694..f3fa69dd208 100644 --- a/tests/robotests/src/com/android/settings/accessibility/ScreenFlashNotificationColorDialogFragmentTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/ScreenFlashNotificationColorDialogFragmentTest.java @@ -39,21 +39,21 @@ import android.content.Intent; import android.graphics.Color; import androidx.appcompat.app.AlertDialog; -import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.testing.FragmentScenario; +import androidx.lifecycle.Lifecycle; import com.android.settings.R; import com.android.settings.testutils.FakeTimer; +import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; -import org.robolectric.shadows.ShadowContextWrapper; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.shadows.ShadowLooper; import org.robolectric.util.ReflectionHelpers; -import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.function.Consumer; @@ -61,7 +61,7 @@ import java.util.function.Consumer; @RunWith(RobolectricTestRunner.class) public class ScreenFlashNotificationColorDialogFragmentTest { - private ShadowContextWrapper mShadowContextWrapper; + private FragmentScenario mFragmentScenario; private ScreenFlashNotificationColorDialogFragment mDialogFragment; private AlertDialog mAlertDialog; private ColorSelectorLayout mColorSelectorLayout; @@ -69,35 +69,32 @@ public class ScreenFlashNotificationColorDialogFragmentTest { @Before public void setUp() { - FragmentActivity fragmentActivity = Robolectric.setupActivity(FragmentActivity.class); - mShadowContextWrapper = shadowOf(fragmentActivity); - mCurrentColor = ROSE.mColorInt; - mDialogFragment = createFragment(); + mFragmentScenario = FragmentScenario.launch( + TestScreenFlashNotificationColorDialogFragment.class, + /* fragmentArgs= */ null, + R.style.Theme_AlertDialog_SettingsLib, + Lifecycle.State.INITIALIZED); + setupFragment(); + } - mDialogFragment.show(fragmentActivity.getSupportFragmentManager(), "test"); - - mAlertDialog = (AlertDialog) mDialogFragment.getDialog(); - if (mAlertDialog != null) { - mColorSelectorLayout = mAlertDialog.findViewById(R.id.color_selector_preference); - } + @After + public void cleanUp() { + mFragmentScenario.close(); } @Test - @Ignore public void test_assertShow() { assertThat(mAlertDialog.isShowing()).isTrue(); } @Test - @Ignore public void clickNeutral_assertShow() { performClickOnDialog(BUTTON_NEUTRAL); assertThat(mAlertDialog.isShowing()).isTrue(); } @Test - @Ignore public void clickNeutral_assertStartPreview() { performClickOnDialog(BUTTON_NEUTRAL); getTimerFromFragment().runOneTask(); @@ -106,7 +103,6 @@ public class ScreenFlashNotificationColorDialogFragmentTest { } @Test - @Ignore public void clickNeutral_flushAllScheduledTasks_assertStopPreview() { performClickOnDialog(BUTTON_NEUTRAL); getTimerFromFragment().runAllTasks(); @@ -115,31 +111,28 @@ public class ScreenFlashNotificationColorDialogFragmentTest { } @Test - @Ignore public void clickNegative_assertNotShow() { performClickOnDialog(BUTTON_NEGATIVE); assertThat(mAlertDialog.isShowing()).isFalse(); } @Test - @Ignore public void clickPositive_assertNotShow() { performClickOnDialog(BUTTON_POSITIVE); assertThat(mAlertDialog.isShowing()).isFalse(); } @Test - @Ignore public void clickNeutralAndPause_assertStopPreview() { performClickOnDialog(BUTTON_NEUTRAL); getTimerFromFragment().runOneTask(); - mDialogFragment.onPause(); + // move the state from RESUMED to CREATED to make fragment's onPause() to be called + mFragmentScenario.moveToState(Lifecycle.State.CREATED); assertStopPreview(); } @Test - @Ignore public void clickNeutralAndClickNegative_assertStopPreview() { performClickOnDialog(BUTTON_NEUTRAL); getTimerFromFragment().runOneTask(); @@ -149,7 +142,6 @@ public class ScreenFlashNotificationColorDialogFragmentTest { } @Test - @Ignore public void clickNeutralAndClickPositive_assertStopPreview() { performClickOnDialog(BUTTON_NEUTRAL); getTimerFromFragment().runOneTask(); @@ -159,7 +151,6 @@ public class ScreenFlashNotificationColorDialogFragmentTest { } @Test - @Ignore public void clickNeutralAndClickColor_assertStartPreview() { performClickOnDialog(BUTTON_NEUTRAL); getTimerFromFragment().runOneTask(); @@ -177,7 +168,6 @@ public class ScreenFlashNotificationColorDialogFragmentTest { } @Test - @Ignore public void clickColorAndClickNegative_assertColor() { checkColorButton(AZURE); performClickOnDialog(BUTTON_NEGATIVE); @@ -187,7 +177,6 @@ public class ScreenFlashNotificationColorDialogFragmentTest { } @Test - @Ignore public void clickColorAndClickPositive_assertColor() { checkColorButton(BLUE); performClickOnDialog(BUTTON_POSITIVE); @@ -201,23 +190,32 @@ public class ScreenFlashNotificationColorDialogFragmentTest { private void performClickOnDialog(int whichButton) { mAlertDialog.getButton(whichButton).performClick(); + ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); } private Intent getLastCapturedIntent() { - final List capturedIntents = new ArrayList<>( - mShadowContextWrapper.getBroadcastIntents()); + final List capturedIntents = + shadowOf(RuntimeEnvironment.getApplication()).getBroadcastIntents(); final int size = capturedIntents.size(); return capturedIntents.get(size - 1); } - private ScreenFlashNotificationColorDialogFragment createFragment() { - ScreenFlashNotificationColorDialogFragmentWithFakeTimer fragment = - new ScreenFlashNotificationColorDialogFragmentWithFakeTimer(); - ReflectionHelpers.setField(fragment, "mCurrentColor", mCurrentColor); - ReflectionHelpers.setField(fragment, "mConsumer", - (Consumer) selectedColor -> mCurrentColor = selectedColor); + private void setupFragment() { + mFragmentScenario.onFragment(fragment -> { + ReflectionHelpers.setField(fragment, "mCurrentColor", mCurrentColor); + ReflectionHelpers.setField(fragment, "mConsumer", + (Consumer) selectedColor -> mCurrentColor = selectedColor); + }); + mFragmentScenario.moveToState(Lifecycle.State.RESUMED); - return fragment; + mFragmentScenario.onFragment(fragment -> { + assertThat(fragment.getDialog()).isNotNull(); + assertThat(fragment.requireDialog().isShowing()).isTrue(); + assertThat(fragment.requireDialog()).isInstanceOf(AlertDialog.class); + mAlertDialog = (AlertDialog) fragment.requireDialog(); + mDialogFragment = fragment; + mColorSelectorLayout = mAlertDialog.findViewById(R.id.color_selector_preference); + }); } private FakeTimer getTimerFromFragment() { @@ -243,7 +241,7 @@ public class ScreenFlashNotificationColorDialogFragmentTest { * A {@link ScreenFlashNotificationColorDialogFragment} that uses a fake timer so that it won't * create unmanageable timer threads during test. */ - public static class ScreenFlashNotificationColorDialogFragmentWithFakeTimer extends + public static class TestScreenFlashNotificationColorDialogFragment extends ScreenFlashNotificationColorDialogFragment { @Override