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

Commit 26846f5f authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Extract FakeDisplayRepository to allow reuse" into main

parents c143935f fa5fc1e3
Loading
Loading
Loading
Loading
+2 −19
Original line number Diff line number Diff line
@@ -25,14 +25,11 @@ import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.FlowValue
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.display.data.repository.DisplayRepository
import com.android.systemui.display.data.repository.FakeDisplayRepository
import com.android.systemui.display.data.repository.display
import com.android.systemui.display.domain.interactor.ConnectedDisplayInteractor.State
import com.android.systemui.util.mockito.mock
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
@@ -131,18 +128,4 @@ class ConnectedDisplayInteractorTest : SysuiTestCase() {

    private fun TestScope.lastValue(): FlowValue<State?> =
        collectLastValue(connectedDisplayStateProvider.connectedDisplayState)

    private fun display(type: Int, flags: Int = 0): Display {
        return mock<Display>().also { mockDisplay ->
            whenever(mockDisplay.type).thenReturn(type)
            whenever(mockDisplay.flags).thenReturn(flags)
        }
    }

    private class FakeDisplayRepository : DisplayRepository {
        private val flow = MutableSharedFlow<Set<Display>>()
        suspend fun emit(value: Set<Display>) = flow.emit(value)
        override val displays: Flow<Set<Display>>
            get() = flow
    }
}
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.display.data.repository

import android.view.Display
import com.android.systemui.util.mockito.mock
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import org.mockito.Mockito.`when` as whenever

/** Creates a mock display. */
fun display(type: Int, flags: Int = 0, id: Int = 0): Display {
    return mock {
        whenever(this.displayId).thenReturn(id)
        whenever(this.type).thenReturn(type)
        whenever(this.flags).thenReturn(flags)
    }
}

/** Fake [DisplayRepository] implementation for testing. */
class FakeDisplayRepository : DisplayRepository {
    private val flow = MutableSharedFlow<Set<Display>>()

    /** Emits [value] as [displays] flow value. */
    suspend fun emit(value: Set<Display>) = flow.emit(value)

    override val displays: Flow<Set<Display>>
        get() = flow
}