Merge "Check 'DeviceFoldStateProvider' is already started" into udc-qpr-dev am: 880aef88d3
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/24025613 Change-Id: Ibcc922d99930147c41b3583efc570c25ce206b74 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -82,7 +82,9 @@ constructor(
|
||||
) : LogContextInteractor {
|
||||
|
||||
init {
|
||||
foldProvider.start()
|
||||
applicationScope.launch {
|
||||
foldProvider.start()
|
||||
}
|
||||
}
|
||||
|
||||
override val displayState =
|
||||
|
||||
@@ -20,6 +20,7 @@ import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import android.content.res.Resources
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.testing.AndroidTestingRunner
|
||||
import androidx.core.util.Consumer
|
||||
import androidx.test.filters.SmallTest
|
||||
@@ -38,6 +39,7 @@ import com.android.systemui.util.mockito.any
|
||||
import com.android.systemui.util.mockito.capture
|
||||
import com.android.systemui.util.mockito.mock
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import junit.framework.Assert.fail
|
||||
import java.util.concurrent.Executor
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
@@ -55,16 +57,20 @@ class DeviceFoldStateProviderTest : SysuiTestCase() {
|
||||
|
||||
@Mock private lateinit var activityTypeProvider: ActivityManagerActivityTypeProvider
|
||||
|
||||
@Mock private lateinit var handler: Handler
|
||||
|
||||
@Mock private lateinit var rotationChangeProvider: RotationChangeProvider
|
||||
|
||||
@Mock private lateinit var unfoldKeyguardVisibilityProvider: UnfoldKeyguardVisibilityProvider
|
||||
|
||||
@Mock private lateinit var resources: Resources
|
||||
|
||||
@Mock private lateinit var handler: Handler
|
||||
|
||||
@Mock private lateinit var mainLooper: Looper
|
||||
|
||||
@Mock private lateinit var context: Context
|
||||
|
||||
@Mock private lateinit var thread: Thread
|
||||
|
||||
@Captor private lateinit var rotationListener: ArgumentCaptor<RotationListener>
|
||||
|
||||
private val foldProvider = TestFoldProvider()
|
||||
@@ -89,6 +95,11 @@ class DeviceFoldStateProviderTest : SysuiTestCase() {
|
||||
override val halfFoldedTimeoutMillis: Int
|
||||
get() = HALF_OPENED_TIMEOUT_MILLIS.toInt()
|
||||
}
|
||||
whenever(mainLooper.isCurrentThread).thenReturn(true)
|
||||
whenever(handler.looper).thenReturn(mainLooper)
|
||||
whenever(mainLooper.isCurrentThread).thenReturn(true)
|
||||
whenever(mainLooper.thread).thenReturn(thread)
|
||||
whenever(thread.name).thenReturn("backgroundThread")
|
||||
whenever(context.resources).thenReturn(resources)
|
||||
whenever(context.mainExecutor).thenReturn(mContext.mainExecutor)
|
||||
|
||||
@@ -434,6 +445,26 @@ class DeviceFoldStateProviderTest : SysuiTestCase() {
|
||||
assertThat(foldUpdates).containsExactly(FOLD_UPDATE_START_CLOSING)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun startOnlyOnce_whenStartTriggeredThrice_startOnlyOnce() {
|
||||
foldStateProvider.start()
|
||||
foldStateProvider.start()
|
||||
foldStateProvider.start()
|
||||
|
||||
assertThat(foldProvider.getNumberOfCallbacks()).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError::class)
|
||||
fun startMethod_whileNotOnMainThread_throwsException() {
|
||||
whenever(mainLooper.isCurrentThread).thenReturn(true)
|
||||
try {
|
||||
foldStateProvider.start()
|
||||
fail("Should have thrown AssertionError: should be called from the main thread.")
|
||||
} catch (e: AssertionError) {
|
||||
assertThat(e.message).contains("backgroundThread")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun startClosingEvent_whileNotOnKeyguard_triggersAfterThreshold() {
|
||||
setKeyguardVisibility(visible = false)
|
||||
@@ -658,6 +689,10 @@ class DeviceFoldStateProviderTest : SysuiTestCase() {
|
||||
fun notifyFolded(isFolded: Boolean) {
|
||||
callbacks.forEach { it.onFoldUpdated(isFolded) }
|
||||
}
|
||||
|
||||
fun getNumberOfCallbacks(): Int{
|
||||
return callbacks.size
|
||||
}
|
||||
}
|
||||
|
||||
private class TestScreenOnStatusProvider : ScreenStatusProvider {
|
||||
|
||||
@@ -62,6 +62,7 @@ constructor(
|
||||
private val hingeAngleListener = HingeAngleListener()
|
||||
private val screenListener = ScreenStatusListener()
|
||||
private val foldStateListener = FoldStateListener()
|
||||
private val mainLooper = handler.looper
|
||||
private val timeoutRunnable = Runnable { cancelAnimation() }
|
||||
private val rotationListener = RotationListener {
|
||||
if (isTransitionInProgress) cancelAnimation()
|
||||
@@ -77,22 +78,28 @@ constructor(
|
||||
private var isFolded = false
|
||||
private var isScreenOn = false
|
||||
private var isUnfoldHandled = true
|
||||
private var isStarted = false
|
||||
|
||||
override fun start() {
|
||||
assertMainThread()
|
||||
if (isStarted) return
|
||||
foldProvider.registerCallback(foldStateListener, mainExecutor)
|
||||
screenStatusProvider.addCallback(screenListener)
|
||||
hingeAngleProvider.addCallback(hingeAngleListener)
|
||||
rotationChangeProvider.addCallback(rotationListener)
|
||||
activityTypeProvider.init()
|
||||
isStarted = true
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
assertMainThread()
|
||||
screenStatusProvider.removeCallback(screenListener)
|
||||
foldProvider.unregisterCallback(foldStateListener)
|
||||
hingeAngleProvider.removeCallback(hingeAngleListener)
|
||||
hingeAngleProvider.stop()
|
||||
rotationChangeProvider.removeCallback(rotationListener)
|
||||
activityTypeProvider.uninit()
|
||||
isStarted = false
|
||||
}
|
||||
|
||||
override fun addCallback(listener: FoldUpdatesListener) {
|
||||
@@ -292,6 +299,14 @@ constructor(
|
||||
onHingeAngle(angle)
|
||||
}
|
||||
}
|
||||
|
||||
private fun assertMainThread() {
|
||||
check(mainLooper.isCurrentThread) {
|
||||
("should be called from the main thread." +
|
||||
" sMainLooper.threadName=" + mainLooper.thread.name +
|
||||
" Thread.currentThread()=" + Thread.currentThread().name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun @receiver:FoldUpdate Int.name() =
|
||||
|
||||
Reference in New Issue
Block a user