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

Commit b92fa1fd authored by Lucas Silva's avatar Lucas Silva
Browse files

Fix broken DreamPickerControllerTest

The dream infos are cached at the time the controller is created, so
ensure that we mock out the dream infos before building the controller
instance.

Test: make -j64 RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.dream.DreamPickerControllerTest"
Bug: 215660413
Change-Id: Ie86f701a46dfc9a0ef185459a1dbf9016c1194fc
parent c70c0f25
Loading
Loading
Loading
Loading
+13 −10
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ import com.android.settingslib.dream.DreamBackend.DreamInfo;
import com.android.settingslib.widget.LayoutPreference;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
@@ -48,9 +47,7 @@ import java.util.ArrayList;
import java.util.Collections;

@RunWith(RobolectricTestRunner.class)
@Ignore
public class DreamPickerControllerTest {
    private DreamPickerController mController;
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private DreamBackend mBackend;
    private Context mContext;
@@ -65,31 +62,36 @@ public class DreamPickerControllerTest {

        mPreference = new LayoutPreference(mContext, R.layout.dream_picker_layout);
        when(mScreen.findPreference(anyString())).thenReturn(mPreference);
    }

        mController = new DreamPickerController(
    private DreamPickerController buildController() {
        final DreamPickerController controller = new DreamPickerController(
                mContext,
                /* preferenceKey= */ "test",
                mBackend);

        mController.displayPreference(mScreen);
        controller.displayPreference(mScreen);
        return controller;
    }

    @Test
    public void isDisabledIfNoDreamsAvailable() {
        when(mBackend.getDreamInfos()).thenReturn(new ArrayList<>(0));
        assertThat(mController.isAvailable()).isFalse();
        final DreamPickerController controller = buildController();
        assertThat(controller.isAvailable()).isFalse();
    }

    @Test
    public void isEnabledIfDreamsAvailable() {
        when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(new DreamInfo()));
        assertThat(mController.isAvailable()).isTrue();
        final DreamPickerController controller = buildController();
        assertThat(controller.isAvailable()).isTrue();
    }

    @Test
    public void testDreamDisplayedInList() {
        when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(new DreamInfo()));
        mController.updateState(mPreference);
        final DreamPickerController controller = buildController();
        controller.updateState(mPreference);

        RecyclerView view = mPreference.findViewById(R.id.dream_list);
        assertThat(view.getAdapter().getItemCount()).isEqualTo(1);
@@ -102,7 +104,8 @@ public class DreamPickerControllerTest {
        mockDreamInfo.isActive = true;

        when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mockDreamInfo));
        mController.updateState(mPreference);
        final DreamPickerController controller = buildController();
        controller.updateState(mPreference);

        Button view = mPreference.findViewById(R.id.preview_button);
        view.performClick();