Merge "Fix ScreenFlashNotificationColorDialogFragmentTest by launching the fragment in modern way." into main

This commit is contained in:
Chun-Ku Lin
2023-09-20 03:42:54 +00:00
committed by Android (Google) Code Review

View File

@@ -39,21 +39,21 @@ import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import androidx.appcompat.app.AlertDialog; 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.R;
import com.android.settings.testutils.FakeTimer; import com.android.settings.testutils.FakeTimer;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowContextWrapper; import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowLooper;
import org.robolectric.util.ReflectionHelpers; import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Timer; import java.util.Timer;
import java.util.function.Consumer; import java.util.function.Consumer;
@@ -61,7 +61,7 @@ import java.util.function.Consumer;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class ScreenFlashNotificationColorDialogFragmentTest { public class ScreenFlashNotificationColorDialogFragmentTest {
private ShadowContextWrapper mShadowContextWrapper; private FragmentScenario<TestScreenFlashNotificationColorDialogFragment> mFragmentScenario;
private ScreenFlashNotificationColorDialogFragment mDialogFragment; private ScreenFlashNotificationColorDialogFragment mDialogFragment;
private AlertDialog mAlertDialog; private AlertDialog mAlertDialog;
private ColorSelectorLayout mColorSelectorLayout; private ColorSelectorLayout mColorSelectorLayout;
@@ -69,35 +69,32 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
@Before @Before
public void setUp() { public void setUp() {
FragmentActivity fragmentActivity = Robolectric.setupActivity(FragmentActivity.class);
mShadowContextWrapper = shadowOf(fragmentActivity);
mCurrentColor = ROSE.mColorInt; 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"); @After
public void cleanUp() {
mAlertDialog = (AlertDialog) mDialogFragment.getDialog(); mFragmentScenario.close();
if (mAlertDialog != null) {
mColorSelectorLayout = mAlertDialog.findViewById(R.id.color_selector_preference);
}
} }
@Test @Test
@Ignore
public void test_assertShow() { public void test_assertShow() {
assertThat(mAlertDialog.isShowing()).isTrue(); assertThat(mAlertDialog.isShowing()).isTrue();
} }
@Test @Test
@Ignore
public void clickNeutral_assertShow() { public void clickNeutral_assertShow() {
performClickOnDialog(BUTTON_NEUTRAL); performClickOnDialog(BUTTON_NEUTRAL);
assertThat(mAlertDialog.isShowing()).isTrue(); assertThat(mAlertDialog.isShowing()).isTrue();
} }
@Test @Test
@Ignore
public void clickNeutral_assertStartPreview() { public void clickNeutral_assertStartPreview() {
performClickOnDialog(BUTTON_NEUTRAL); performClickOnDialog(BUTTON_NEUTRAL);
getTimerFromFragment().runOneTask(); getTimerFromFragment().runOneTask();
@@ -106,7 +103,6 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
} }
@Test @Test
@Ignore
public void clickNeutral_flushAllScheduledTasks_assertStopPreview() { public void clickNeutral_flushAllScheduledTasks_assertStopPreview() {
performClickOnDialog(BUTTON_NEUTRAL); performClickOnDialog(BUTTON_NEUTRAL);
getTimerFromFragment().runAllTasks(); getTimerFromFragment().runAllTasks();
@@ -115,31 +111,28 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
} }
@Test @Test
@Ignore
public void clickNegative_assertNotShow() { public void clickNegative_assertNotShow() {
performClickOnDialog(BUTTON_NEGATIVE); performClickOnDialog(BUTTON_NEGATIVE);
assertThat(mAlertDialog.isShowing()).isFalse(); assertThat(mAlertDialog.isShowing()).isFalse();
} }
@Test @Test
@Ignore
public void clickPositive_assertNotShow() { public void clickPositive_assertNotShow() {
performClickOnDialog(BUTTON_POSITIVE); performClickOnDialog(BUTTON_POSITIVE);
assertThat(mAlertDialog.isShowing()).isFalse(); assertThat(mAlertDialog.isShowing()).isFalse();
} }
@Test @Test
@Ignore
public void clickNeutralAndPause_assertStopPreview() { public void clickNeutralAndPause_assertStopPreview() {
performClickOnDialog(BUTTON_NEUTRAL); performClickOnDialog(BUTTON_NEUTRAL);
getTimerFromFragment().runOneTask(); 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(); assertStopPreview();
} }
@Test @Test
@Ignore
public void clickNeutralAndClickNegative_assertStopPreview() { public void clickNeutralAndClickNegative_assertStopPreview() {
performClickOnDialog(BUTTON_NEUTRAL); performClickOnDialog(BUTTON_NEUTRAL);
getTimerFromFragment().runOneTask(); getTimerFromFragment().runOneTask();
@@ -149,7 +142,6 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
} }
@Test @Test
@Ignore
public void clickNeutralAndClickPositive_assertStopPreview() { public void clickNeutralAndClickPositive_assertStopPreview() {
performClickOnDialog(BUTTON_NEUTRAL); performClickOnDialog(BUTTON_NEUTRAL);
getTimerFromFragment().runOneTask(); getTimerFromFragment().runOneTask();
@@ -159,7 +151,6 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
} }
@Test @Test
@Ignore
public void clickNeutralAndClickColor_assertStartPreview() { public void clickNeutralAndClickColor_assertStartPreview() {
performClickOnDialog(BUTTON_NEUTRAL); performClickOnDialog(BUTTON_NEUTRAL);
getTimerFromFragment().runOneTask(); getTimerFromFragment().runOneTask();
@@ -177,7 +168,6 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
} }
@Test @Test
@Ignore
public void clickColorAndClickNegative_assertColor() { public void clickColorAndClickNegative_assertColor() {
checkColorButton(AZURE); checkColorButton(AZURE);
performClickOnDialog(BUTTON_NEGATIVE); performClickOnDialog(BUTTON_NEGATIVE);
@@ -187,7 +177,6 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
} }
@Test @Test
@Ignore
public void clickColorAndClickPositive_assertColor() { public void clickColorAndClickPositive_assertColor() {
checkColorButton(BLUE); checkColorButton(BLUE);
performClickOnDialog(BUTTON_POSITIVE); performClickOnDialog(BUTTON_POSITIVE);
@@ -201,23 +190,32 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
private void performClickOnDialog(int whichButton) { private void performClickOnDialog(int whichButton) {
mAlertDialog.getButton(whichButton).performClick(); mAlertDialog.getButton(whichButton).performClick();
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
} }
private Intent getLastCapturedIntent() { private Intent getLastCapturedIntent() {
final List<Intent> capturedIntents = new ArrayList<>( final List<Intent> capturedIntents =
mShadowContextWrapper.getBroadcastIntents()); shadowOf(RuntimeEnvironment.getApplication()).getBroadcastIntents();
final int size = capturedIntents.size(); final int size = capturedIntents.size();
return capturedIntents.get(size - 1); return capturedIntents.get(size - 1);
} }
private ScreenFlashNotificationColorDialogFragment createFragment() { private void setupFragment() {
ScreenFlashNotificationColorDialogFragmentWithFakeTimer fragment = mFragmentScenario.onFragment(fragment -> {
new ScreenFlashNotificationColorDialogFragmentWithFakeTimer(); ReflectionHelpers.setField(fragment, "mCurrentColor", mCurrentColor);
ReflectionHelpers.setField(fragment, "mCurrentColor", mCurrentColor); ReflectionHelpers.setField(fragment, "mConsumer",
ReflectionHelpers.setField(fragment, "mConsumer", (Consumer<Integer>) selectedColor -> mCurrentColor = selectedColor);
(Consumer<Integer>) 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() { private FakeTimer getTimerFromFragment() {
@@ -243,7 +241,7 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
* A {@link ScreenFlashNotificationColorDialogFragment} that uses a fake timer so that it won't * A {@link ScreenFlashNotificationColorDialogFragment} that uses a fake timer so that it won't
* create unmanageable timer threads during test. * create unmanageable timer threads during test.
*/ */
public static class ScreenFlashNotificationColorDialogFragmentWithFakeTimer extends public static class TestScreenFlashNotificationColorDialogFragment extends
ScreenFlashNotificationColorDialogFragment { ScreenFlashNotificationColorDialogFragment {
@Override @Override