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

Commit 77b5fc5a authored by Julia Tuttle's avatar Julia Tuttle
Browse files

Fix mismapped args in 9-arg combine

And add a test for them.

Bug: 369151941
Flag: EXEMPT bugfix
Test: FlowTest
Change-Id: Ic42b76968123db8dc08f15c512f5187f22247350
parent 5bf5afe0
Loading
Loading
Loading
Loading
+100 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.util.kotlin

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class FlowTest : SysuiTestCase() {
    val kosmos = testKosmos()
    val testScope = kosmos.testScope

    @Test
    fun combine6() {
        testScope.runTest {
            val result by collectLastValue(combine(f0, f1, f2, f3, f4, f5, ::listOf6))
            assertItemsEqualIndices(result)
        }
    }

    @Test
    fun combine7() {
        testScope.runTest {
            val result by collectLastValue(combine(f0, f1, f2, f3, f4, f5, f6, ::listOf7))
            assertItemsEqualIndices(result)
        }
    }

    @Test
    fun combine8() {
        testScope.runTest {
            val result by collectLastValue(combine(f0, f1, f2, f3, f4, f5, f6, f7, ::listOf8))
            assertItemsEqualIndices(result)
        }
    }

    @Test
    fun combine9() {
        testScope.runTest {
            val result by collectLastValue(combine(f0, f1, f2, f3, f4, f5, f6, f7, f8, ::listOf9))
            assertItemsEqualIndices(result)
        }
    }

    private fun assertItemsEqualIndices(list: List<Int>?) {
        assertThat(list).isNotNull()
        list ?: return

        for (index in list.indices) {
            assertThat(list[index]).isEqualTo(index)
        }
    }

    private val f0: Flow<Int> = flowOf(0)
    private val f1: Flow<Int> = flowOf(1)
    private val f2: Flow<Int> = flowOf(2)
    private val f3: Flow<Int> = flowOf(3)
    private val f4: Flow<Int> = flowOf(4)
    private val f5: Flow<Int> = flowOf(5)
    private val f6: Flow<Int> = flowOf(6)
    private val f7: Flow<Int> = flowOf(7)
    private val f8: Flow<Int> = flowOf(8)
}

private fun <T> listOf6(a0: T, a1: T, a2: T, a3: T, a4: T, a5: T): List<T> =
    listOf(a0, a1, a2, a3, a4, a5)

private fun <T> listOf7(a0: T, a1: T, a2: T, a3: T, a4: T, a5: T, a6: T): List<T> =
    listOf(a0, a1, a2, a3, a4, a5, a6)

private fun <T> listOf8(a0: T, a1: T, a2: T, a3: T, a4: T, a5: T, a6: T, a7: T): List<T> =
    listOf(a0, a1, a2, a3, a4, a5, a6, a7)

private fun <T> listOf9(a0: T, a1: T, a2: T, a3: T, a4: T, a5: T, a6: T, a7: T, a8: T): List<T> =
    listOf(a0, a1, a2, a3, a4, a5, a6, a7, a8)
+24 −24
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui.util.kotlin

import com.android.app.tracing.coroutines.launchTraced as launch
import com.android.systemui.util.time.SystemClock
import com.android.systemui.util.time.SystemClockImpl
import java.util.concurrent.atomic.AtomicReference
@@ -33,7 +34,6 @@ import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import com.android.app.tracing.coroutines.launchTraced as launch

/**
 * Returns a new [Flow] that combines the two most recent emissions from [this] using [transform].
@@ -252,10 +252,10 @@ inline fun <T1, T2, T3, T4, T5, T6, R> combine(
    flow4: Flow<T4>,
    flow5: Flow<T5>,
    flow6: Flow<T6>,
        crossinline transform: suspend (T1, T2, T3, T4, T5, T6) -> R
    crossinline transform: suspend (T1, T2, T3, T4, T5, T6) -> R,
): Flow<R> {
    return kotlinx.coroutines.flow.combine(flow, flow2, flow3, flow4, flow5, flow6) {
        args: Array<*> ->
    return kotlinx.coroutines.flow.combine(flow, flow2, flow3, flow4, flow5, flow6) { args: Array<*>
        ->
        @Suppress("UNCHECKED_CAST")
        transform(
            args[0] as T1,
@@ -276,7 +276,7 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, R> combine(
    flow5: Flow<T5>,
    flow6: Flow<T6>,
    flow7: Flow<T7>,
    crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7) -> R
    crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7) -> R,
): Flow<R> {
    return kotlinx.coroutines.flow.combine(flow, flow2, flow3, flow4, flow5, flow6, flow7) {
        args: Array<*> ->
@@ -288,7 +288,7 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, R> combine(
            args[3] as T4,
            args[4] as T5,
            args[5] as T6,
            args[6] as T7
            args[6] as T7,
        )
    }
}
@@ -302,7 +302,7 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, T8, R> combine(
    flow6: Flow<T6>,
    flow7: Flow<T7>,
    flow8: Flow<T8>,
    crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8) -> R
    crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8) -> R,
): Flow<R> {
    return kotlinx.coroutines.flow.combine(flow, flow2, flow3, flow4, flow5, flow6, flow7, flow8) {
        args: Array<*> ->
@@ -315,7 +315,7 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, T8, R> combine(
            args[4] as T5,
            args[5] as T6,
            args[6] as T7,
            args[7] as T8
            args[7] as T8,
        )
    }
}
@@ -330,7 +330,7 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, T8, T9, R> combine(
    flow7: Flow<T7>,
    flow8: Flow<T8>,
    flow9: Flow<T9>,
    crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R
    crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R,
): Flow<R> {
    return kotlinx.coroutines.flow.combine(
        flow,
@@ -341,7 +341,7 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, T8, T9, R> combine(
        flow6,
        flow7,
        flow8,
        flow9
        flow9,
    ) { args: Array<*> ->
        @Suppress("UNCHECKED_CAST")
        transform(
@@ -352,8 +352,8 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, T8, T9, R> combine(
            args[4] as T5,
            args[5] as T6,
            args[6] as T7,
            args[6] as T8,
            args[6] as T9,
            args[7] as T8,
            args[8] as T9,
        )
    }
}