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

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

Merge "Cleanup displaylib tests" into main

parents 091025ed e9c4bbfd
Loading
Loading
Loading
Loading
+13 −26
Original line number Diff line number Diff line
@@ -17,13 +17,13 @@
package com.android.systemui.display.data.repository

import android.hardware.display.DisplayManager
import android.os.Looper
import android.os.fakeHandler
import android.testing.TestableLooper
import android.view.Display
import android.view.Display.DEFAULT_DISPLAY
import android.view.Display.TYPE_EXTERNAL
import android.view.Display.TYPE_INTERNAL
import android.view.IWindowManager
import android.view.mockIWindowManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.app.displaylib.DisplayRepository.PendingDisplay
@@ -31,13 +31,14 @@ import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.FlowValue
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.coroutines.collectValues
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.statusbar.CommandQueue
import com.android.systemui.statusbar.mockCommandQueue
import com.android.systemui.testKosmos
import com.android.systemui.util.mockito.kotlinArgumentCaptor
import com.android.systemui.util.mockito.mock
import com.android.systemui.utils.os.FakeHandler
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
@@ -54,17 +55,18 @@ import org.mockito.kotlin.whenever
@TestableLooper.RunWithLooper
@SmallTest
class DisplayRepositoryTest : SysuiTestCase() {
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()

    private val displayManager = mock<DisplayManager>()
    private val commandQueue = mock<CommandQueue>()
    private val windowManager = mock<IWindowManager>()
    private val displayManager = kosmos.mockDisplayManager
    private val commandQueue = kosmos.mockCommandQueue
    private val windowManager = kosmos.mockIWindowManager

    private val displayListener = kotlinArgumentCaptor<DisplayManager.DisplayListener>()
    private val commandQueueCallbacks = kotlinArgumentCaptor<CommandQueue.Callbacks>()
    private val connectedDisplayListener = kotlinArgumentCaptor<DisplayManager.DisplayListener>()

    private val testHandler = FakeHandler(Looper.getMainLooper())
    private val testScope = TestScope(UnconfinedTestDispatcher())
    private val testHandler = kosmos.fakeHandler
    private val testScope = kosmos.testScope
    private val defaultDisplay =
        display(type = TYPE_INTERNAL, id = DEFAULT_DISPLAY, state = Display.STATE_ON)

@@ -72,22 +74,7 @@ class DisplayRepositoryTest : SysuiTestCase() {
    // that the initial state (soon after construction) contains the expected ones set in every
    // test.
    private val displayRepository: DisplayRepositoryImpl by lazy {
        // TODO b/401305290 - move this to kosmos
        val displayRepositoryFromLib =
            com.android.app.displaylib.DisplayRepositoryImpl(
                displayManager,
                testHandler,
                testScope.backgroundScope,
                UnconfinedTestDispatcher(),
            )
        val displaysWithDecorRepository =
            DisplaysWithDecorationsRepositoryImpl(
                commandQueue,
                windowManager,
                testScope.backgroundScope,
                displayRepositoryFromLib,
            )
        DisplayRepositoryImpl(displayRepositoryFromLib, displaysWithDecorRepository).also {
        kosmos.realDisplayRepository.also {
            verify(displayManager, never()).registerDisplayListener(any(), any())
            // It needs to be called, just once, for the initial value.
            verify(displayManager).getDisplays()
+0 −45
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.display.data.repository

import com.android.app.displaylib.PerDisplayRepository
import java.util.function.Consumer

// TODO b/401305290 - move to displaylib
class FakePerDisplayRepository<T> : PerDisplayRepository<T> {

    private val instances = mutableMapOf<Int, T>()

    fun add(displayId: Int, instance: T) {
        instances[displayId] = instance
    }

    fun remove(displayId: Int) {
        instances.remove(displayId)
    }

    override fun get(displayId: Int): T? {
        return instances[displayId]
    }

    override val debugName: String
        get() = "FakePerDisplayRepository"

    override fun forEach(createIfAbsent: Boolean, action: Consumer<T>) {
        instances.forEach { (_, t) -> action.accept(t) }
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -19,5 +19,7 @@ package android.os
import com.android.systemui.concurrency.fakeExecutor
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.util.concurrency.mockExecutorHandler
import com.android.systemui.utils.os.FakeHandler

val Kosmos.fakeExecutorHandler by Kosmos.Fixture { mockExecutorHandler(fakeExecutor) }
val Kosmos.fakeHandler by Kosmos.Fixture { FakeHandler(Looper.getMainLooper()) }
+29 −0
Original line number Diff line number Diff line
@@ -16,12 +16,19 @@

package com.android.systemui.display.data.repository

import android.hardware.display.DisplayManager
import android.os.fakeHandler
import android.view.Display
import android.view.mockIWindowManager
import com.android.app.displaylib.fakes.FakePerDisplayRepository
import com.android.systemui.display.dagger.SystemUIDisplaySubcomponent
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.Kosmos.Fixture
import com.android.systemui.kosmos.testScope
import com.android.systemui.statusbar.mockCommandQueue
import com.android.systemui.util.mockito.mock
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher

val Kosmos.displayRepository by Fixture { FakeDisplayRepository() }

@@ -51,3 +58,25 @@ val Kosmos.displaySubcomponentPerDisplayRepository by Fixture {
        add(Display.DEFAULT_DISPLAY, sysuiDefaultDisplaySubcomponent)
    }
}

val Kosmos.mockDisplayManager by Fixture { mock<DisplayManager>() }
val Kosmos.displayRepositoryFromDisplayLib by Fixture {
    com.android.app.displaylib.DisplayRepositoryImpl(
        mockDisplayManager,
        fakeHandler,
        testScope.backgroundScope,
        UnconfinedTestDispatcher(),
    )
}
val Kosmos.displayWithDecorationsRepository by Fixture {
    DisplaysWithDecorationsRepositoryImpl(
        mockCommandQueue,
        mockIWindowManager,
        testScope.backgroundScope,
        displayRepositoryFromDisplayLib,
    )
}

val Kosmos.realDisplayRepository by Fixture {
    DisplayRepositoryImpl(displayRepositoryFromDisplayLib, displayWithDecorationsRepository)
}
+1 −1
Original line number Diff line number Diff line
@@ -17,8 +17,8 @@
package com.android.systemui.model

import android.view.Display
import com.android.app.displaylib.fakes.FakePerDisplayRepository
import com.android.systemui.common.domain.interactor.SysUIStateDisplaysInteractor
import com.android.systemui.display.data.repository.FakePerDisplayRepository
import com.android.systemui.display.data.repository.displayRepository
import com.android.systemui.dump.dumpManager
import com.android.systemui.kosmos.Kosmos