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

Commit 0050ed80 authored by Fabian Kozynski's avatar Fabian Kozynski Committed by Android (Google) Code Review
Browse files

Merge changes Iff6acb91,I1ac7047d into main

* changes:
  Prevent tiles in bad state by interrupted collectLatest
  Convert CurrentTilesInteractorImplTest to kosmos
parents 3721c179 4fbbb7c6
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -423,5 +423,15 @@ public class TileQueryHelperTest extends SysuiTestCase {

        @Override
        public void destroy() {}

        @Override
        public boolean isDestroyed() {
            return false;
        }

        @Override
        public int getCurrentTileUser() {
            return 0;
        }
    }
}
+126 −114
Original line number Diff line number Diff line
@@ -56,20 +56,27 @@ class TilesAvailabilityInteractorTest(flags: FlagsParameterization) : SysuiTestC

    private val createdTiles = mutableListOf<FakeQSTile>()

    private val kosmos = testKosmos().apply {
    private val kosmos =
        testKosmos().apply {
            tileAvailabilityInteractorsMap = buildMap {
                put(AIRPLANE_MODE_TILE_SPEC, QSTileAvailabilityInteractor.AlwaysAvailableInteractor)
            put(WORK_MODE_TILE_SPEC, FakeTileAvailabilityInteractor(
                    mapOf(
                            fakeUserRepository.getSelectedUserInfo().id to flowOf(true),
                    ).withDefault { flowOf(false) }
            ))
            put(HOTSPOT_TILE_SPEC, FakeTileAvailabilityInteractor(
                put(
                    WORK_MODE_TILE_SPEC,
                    FakeTileAvailabilityInteractor(
                        mapOf(fakeUserRepository.getSelectedUserInfo().id to flowOf(true))
                            .withDefault { flowOf(false) }
                    ),
                )
                put(
                    HOTSPOT_TILE_SPEC,
                    FakeTileAvailabilityInteractor(
                        emptyMap<Int, Flow<Boolean>>().withDefault { flowOf(false) }
            ))
                    ),
                )
            }

        qsTileFactory = constantFactory(
            qsTileFactory =
                constantFactory(
                    tilesForCreator(
                        userRepository.getSelectedUserInfo().id,
                        mapOf(
@@ -78,7 +85,7 @@ class TilesAvailabilityInteractorTest(flags: FlagsParameterization) : SysuiTestC
                            HOTSPOT_TILE_SPEC to true,
                            INTERNET_TILE_SPEC to true,
                            FLASHLIGHT_TILE_SPEC to false,
                        )
                        ),
                    )
                )
        }
@@ -87,83 +94,91 @@ class TilesAvailabilityInteractorTest(flags: FlagsParameterization) : SysuiTestC

    @Test
    @DisableFlags(FLAG_QS_NEW_TILES)
    fun flagOff_usesAvailabilityFromFactoryTiles() = with(kosmos) {
    fun flagOff_usesAvailabilityFromFactoryTiles() =
        with(kosmos) {
            testScope.runTest {
            val unavailableTiles = underTest.getUnavailableTiles(
                val unavailableTiles =
                    underTest.getUnavailableTiles(
                        setOf(
                                AIRPLANE_MODE_TILE_SPEC,
                                WORK_MODE_TILE_SPEC,
                                HOTSPOT_TILE_SPEC,
                                INTERNET_TILE_SPEC,
                                FLASHLIGHT_TILE_SPEC,
                    ).map(TileSpec::create)
                            )
            assertThat(unavailableTiles).isEqualTo(setOf(
                    AIRPLANE_MODE_TILE_SPEC,
                    WORK_MODE_TILE_SPEC,
                    FLASHLIGHT_TILE_SPEC,
            ).mapTo(mutableSetOf(), TileSpec::create))
                            .map(TileSpec::create)
                    )
                assertThat(unavailableTiles)
                    .isEqualTo(
                        setOf(AIRPLANE_MODE_TILE_SPEC, WORK_MODE_TILE_SPEC, FLASHLIGHT_TILE_SPEC)
                            .mapTo(mutableSetOf(), TileSpec::create)
                    )
            }
        }

    @Test
    fun tileCannotBeCreated_isUnavailable() = with(kosmos) {
    fun tileCannotBeCreated_isUnavailable() =
        with(kosmos) {
            testScope.runTest {
                val badSpec = TileSpec.create("unknown")
            val unavailableTiles = underTest.getUnavailableTiles(
                    setOf(
                        badSpec
                    )
            )
                val unavailableTiles = underTest.getUnavailableTiles(setOf(badSpec))
                assertThat(unavailableTiles).contains(badSpec)
            }
        }

    @Test
    @EnableFlags(FLAG_QS_NEW_TILES)
    fun flagOn_defaultsToInteractorTiles_usesFactoryForOthers() = with(kosmos) {
    fun flagOn_defaultsToInteractorTiles_usesFactoryForOthers() =
        with(kosmos) {
            testScope.runTest {
            val unavailableTiles = underTest.getUnavailableTiles(
                val unavailableTiles =
                    underTest.getUnavailableTiles(
                        setOf(
                                AIRPLANE_MODE_TILE_SPEC,
                                WORK_MODE_TILE_SPEC,
                                HOTSPOT_TILE_SPEC,
                                INTERNET_TILE_SPEC,
                                FLASHLIGHT_TILE_SPEC,
                    ).map(TileSpec::create)
                            )
            assertThat(unavailableTiles).isEqualTo(setOf(
                    HOTSPOT_TILE_SPEC,
                    FLASHLIGHT_TILE_SPEC,
            ).mapTo(mutableSetOf(), TileSpec::create))
                            .map(TileSpec::create)
                    )
                assertThat(unavailableTiles)
                    .isEqualTo(
                        setOf(HOTSPOT_TILE_SPEC, FLASHLIGHT_TILE_SPEC)
                            .mapTo(mutableSetOf(), TileSpec::create)
                    )
            }
        }

    @Test
    @EnableFlags(FLAG_QS_NEW_TILES)
    fun flagOn_defaultsToInteractorTiles_usesFactoryForOthers_userChange() = with(kosmos) {
    fun flagOn_defaultsToInteractorTiles_usesFactoryForOthers_userChange() =
        with(kosmos) {
            testScope.runTest {
                fakeUserRepository.asMainUser()
            val unavailableTiles = underTest.getUnavailableTiles(
                val unavailableTiles =
                    underTest.getUnavailableTiles(
                        setOf(
                                AIRPLANE_MODE_TILE_SPEC,
                                WORK_MODE_TILE_SPEC,
                                HOTSPOT_TILE_SPEC,
                                INTERNET_TILE_SPEC,
                                FLASHLIGHT_TILE_SPEC,
                    ).map(TileSpec::create)
                            )
            assertThat(unavailableTiles).isEqualTo(setOf(
                    WORK_MODE_TILE_SPEC,
                    HOTSPOT_TILE_SPEC,
                    FLASHLIGHT_TILE_SPEC,
            ).mapTo(mutableSetOf(), TileSpec::create))
                            .map(TileSpec::create)
                    )
                assertThat(unavailableTiles)
                    .isEqualTo(
                        setOf(WORK_MODE_TILE_SPEC, HOTSPOT_TILE_SPEC, FLASHLIGHT_TILE_SPEC)
                            .mapTo(mutableSetOf(), TileSpec::create)
                    )
            }
        }

    @Test
    @EnableFlags(FLAG_QS_NEW_TILES)
    fun flagOn_onlyNeededTilesAreCreated_andThenDestroyed() = with(kosmos) {
    fun flagOn_onlyNeededTilesAreCreated_andThenDestroyed() =
        with(kosmos) {
            testScope.runTest {
                underTest.getUnavailableTiles(
                    setOf(
@@ -172,19 +187,22 @@ class TilesAvailabilityInteractorTest(flags: FlagsParameterization) : SysuiTestC
                            HOTSPOT_TILE_SPEC,
                            INTERNET_TILE_SPEC,
                            FLASHLIGHT_TILE_SPEC,
                    ).map(TileSpec::create)
                        )
                        .map(TileSpec::create)
                )
                assertThat(createdTiles.map { it.tileSpec })
                    .containsExactly(INTERNET_TILE_SPEC, FLASHLIGHT_TILE_SPEC)
            assertThat(createdTiles.all { it.destroyed }).isTrue()
                assertThat(createdTiles.all { it.isDestroyed }).isTrue()
            }
        }

    @Test
    @DisableFlags(FLAG_QS_NEW_TILES)
    fun flagOn_TilesAreCreatedAndThenDestroyed() = with(kosmos) {
    fun flagOn_TilesAreCreatedAndThenDestroyed() =
        with(kosmos) {
            testScope.runTest {
            val allTiles = setOf(
                val allTiles =
                    setOf(
                        AIRPLANE_MODE_TILE_SPEC,
                        WORK_MODE_TILE_SPEC,
                        HOTSPOT_TILE_SPEC,
@@ -192,30 +210,24 @@ class TilesAvailabilityInteractorTest(flags: FlagsParameterization) : SysuiTestC
                        FLASHLIGHT_TILE_SPEC,
                    )
                underTest.getUnavailableTiles(allTiles.map(TileSpec::create))
            assertThat(createdTiles.map { it.tileSpec })
                    .containsExactlyElementsIn(allTiles)
            assertThat(createdTiles.all { it.destroyed }).isTrue()
                assertThat(createdTiles.map { it.tileSpec }).containsExactlyElementsIn(allTiles)
                assertThat(createdTiles.all { it.isDestroyed }).isTrue()
            }
        }


    private fun constantFactory(creatorTiles: Set<FakeQSTile>): QSFactory {
        return FakeQSFactory { spec ->
            creatorTiles.firstOrNull { it.tileSpec == spec }?.also {
                createdTiles.add(it)
            }
            creatorTiles.firstOrNull { it.tileSpec == spec }?.also { createdTiles.add(it) }
        }
    }

    companion object {
        private fun tilesForCreator(
            user: Int,
                specAvailabilities: Map<String, Boolean>
            specAvailabilities: Map<String, Boolean>,
        ): Set<FakeQSTile> {
            return specAvailabilities.mapTo(mutableSetOf()) {
                FakeQSTile(user, it.value).apply {
                    tileSpec = it.key
                }
                FakeQSTile(user, it.value).apply { tileSpec = it.key }
            }
        }

+505 −468

File changed.

Preview size limit exceeded, changes collapsed.

+6 −0
Original line number Diff line number Diff line
@@ -508,6 +508,12 @@ public class QSTileImplTest extends SysuiTestCase {
        assertThat(mTile.mRefreshes).isEqualTo(1);
    }

    @Test
    public void testIsDestroyedImmediately() {
        mTile.destroy();
        assertThat(mTile.isDestroyed()).isTrue();
    }

    private void assertEvent(UiEventLogger.UiEventEnum eventType,
            UiEventLoggerFake.FakeUiEvent fakeEvent) {
        assertEquals(eventType.getId(), fakeEvent.eventId);
+4 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ import java.util.function.Supplier;
@DependsOn(target = Icon.class)
@DependsOn(target = State.class)
public interface QSTile {
    int VERSION = 4;
    int VERSION = 5;

    String getTileSpec();

@@ -78,6 +78,7 @@ public interface QSTile {
    void longClick(@Nullable Expandable expandable);

    void userSwitch(int currentUser);
    int getCurrentTileUser();

    /**
     * @deprecated not needed as {@link com.android.internal.logging.UiEvent} will use
@@ -150,6 +151,8 @@ public interface QSTile {
        return null;
    }

    boolean isDestroyed();

    @ProvidesInterface(version = Callback.VERSION)
    interface Callback {
        static final int VERSION = 2;
Loading