Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 74cf9db4 authored by Chun-Ku Lin's avatar Chun-Ku Lin Committed by Android (Google) Code Review
Browse files

Merge "Fix ScreenFlashNotificationColorDialogFragmentTest by launching the...

Merge "Fix ScreenFlashNotificationColorDialogFragmentTest by launching the fragment in modern way." into main
parents 09b7b127 186644aa
Loading
Loading
Loading
Loading
+38 −40
Original line number Diff line number Diff line
@@ -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<TestScreenFlashNotificationColorDialogFragment> 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();

        mDialogFragment.show(fragmentActivity.getSupportFragmentManager(), "test");

        mAlertDialog = (AlertDialog) mDialogFragment.getDialog();
        if (mAlertDialog != null) {
            mColorSelectorLayout = mAlertDialog.findViewById(R.id.color_selector_preference);
        mFragmentScenario = FragmentScenario.launch(
                TestScreenFlashNotificationColorDialogFragment.class,
                /* fragmentArgs= */ null,
                R.style.Theme_AlertDialog_SettingsLib,
                Lifecycle.State.INITIALIZED);
        setupFragment();
    }

    @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<Intent> capturedIntents = new ArrayList<>(
                mShadowContextWrapper.getBroadcastIntents());
        final List<Intent> capturedIntents =
                shadowOf(RuntimeEnvironment.getApplication()).getBroadcastIntents();
        final int size = capturedIntents.size();
        return capturedIntents.get(size - 1);
    }

    private ScreenFlashNotificationColorDialogFragment createFragment() {
        ScreenFlashNotificationColorDialogFragmentWithFakeTimer fragment =
                new ScreenFlashNotificationColorDialogFragmentWithFakeTimer();
    private void setupFragment() {
        mFragmentScenario.onFragment(fragment -> {
            ReflectionHelpers.setField(fragment, "mCurrentColor", mCurrentColor);
            ReflectionHelpers.setField(fragment, "mConsumer",
                    (Consumer<Integer>) selectedColor -> mCurrentColor = selectedColor);

        return fragment;
        });
        mFragmentScenario.moveToState(Lifecycle.State.RESUMED);

        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