[DO NOT MERGE] Separate Region Sampling Instance
This change abstracts out the creation of a region sampling helper and the initialization of all its fields. It adds test coverage for the new class. Bug: 202758428 Test: atest RegionSamplingInstanceTest Change-Id: Ife2aafccc20466e81f481d1764210806dffc3123
This commit is contained in:
@@ -128,6 +128,13 @@ public class RegionSamplingHelper implements View.OnAttachStateChangeListener,
|
|||||||
mCallback = samplingCallback;
|
mCallback = samplingCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make callback accessible
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
public SamplingCallback getCallback() {
|
||||||
|
return mCallback;
|
||||||
|
}
|
||||||
private void onDraw() {
|
private void onDraw() {
|
||||||
if (mWaitingOnDraw) {
|
if (mWaitingOnDraw) {
|
||||||
mWaitingOnDraw = false;
|
mWaitingOnDraw = false;
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.android.systemui.shared.regionsampling
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enum for whether clock region is dark or light.
|
||||||
|
*/
|
||||||
|
enum class RegionDarkness(val isDark: Boolean) {
|
||||||
|
DEFAULT(false),
|
||||||
|
DARK(true),
|
||||||
|
LIGHT(false)
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 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.shared.regionsampling
|
||||||
|
|
||||||
|
import android.graphics.Rect
|
||||||
|
import android.view.View
|
||||||
|
import androidx.annotation.VisibleForTesting
|
||||||
|
import com.android.systemui.shared.navigationbar.RegionSamplingHelper
|
||||||
|
import com.android.systemui.shared.navigationbar.RegionSamplingHelper.SamplingCallback
|
||||||
|
import java.io.PrintWriter
|
||||||
|
import java.util.concurrent.Executor
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for instance of RegionSamplingHelper
|
||||||
|
*/
|
||||||
|
open class RegionSamplingInstance(
|
||||||
|
sampledView: View?,
|
||||||
|
mainExecutor: Executor?,
|
||||||
|
bgExecutor: Executor?,
|
||||||
|
regionSamplingEnabled: Boolean,
|
||||||
|
updateFun: UpdateColorCallback
|
||||||
|
) {
|
||||||
|
private var isDark = RegionDarkness.DEFAULT
|
||||||
|
private var samplingBounds = Rect()
|
||||||
|
@VisibleForTesting var regionSampler: RegionSamplingHelper? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for method to be passed into RegionSamplingHelper
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
interface UpdateColorCallback {
|
||||||
|
/**
|
||||||
|
* Method to update the text colors after clock darkness changed.
|
||||||
|
*/
|
||||||
|
fun updateColors()
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
open fun createRegionSamplingHelper(
|
||||||
|
sampledView: View,
|
||||||
|
callback: SamplingCallback,
|
||||||
|
mainExecutor: Executor?,
|
||||||
|
bgExecutor: Executor?
|
||||||
|
): RegionSamplingHelper {
|
||||||
|
return RegionSamplingHelper(sampledView, callback, mainExecutor, bgExecutor)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun convertToClockDarkness(isRegionDark: Boolean): RegionDarkness {
|
||||||
|
return if (isRegionDark) {
|
||||||
|
RegionDarkness.DARK
|
||||||
|
} else {
|
||||||
|
RegionDarkness.LIGHT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun currentRegionDarkness(): RegionDarkness {
|
||||||
|
return isDark
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start region sampler
|
||||||
|
*/
|
||||||
|
fun startRegionSampler() {
|
||||||
|
regionSampler?.start(samplingBounds)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop region sampler
|
||||||
|
*/
|
||||||
|
fun stopRegionSampler() {
|
||||||
|
regionSampler?.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dump region sampler
|
||||||
|
*/
|
||||||
|
fun dump(pw: PrintWriter) {
|
||||||
|
regionSampler?.dump(pw)
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
if (regionSamplingEnabled && sampledView != null) {
|
||||||
|
regionSampler = createRegionSamplingHelper(sampledView,
|
||||||
|
object : SamplingCallback {
|
||||||
|
override fun onRegionDarknessChanged(isRegionDark: Boolean) {
|
||||||
|
isDark = convertToClockDarkness(isRegionDark)
|
||||||
|
updateFun.updateColors()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getSampledRegion(sampledView: View): Rect {
|
||||||
|
samplingBounds = Rect(sampledView.left, sampledView.top,
|
||||||
|
sampledView.right, sampledView.bottom)
|
||||||
|
return samplingBounds
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isSamplingEnabled(): Boolean {
|
||||||
|
return regionSamplingEnabled
|
||||||
|
}
|
||||||
|
}, mainExecutor, bgExecutor)
|
||||||
|
}
|
||||||
|
regionSampler?.setWindowVisible(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package com.android.systemui.shared.regionsampling
|
||||||
|
|
||||||
|
import android.graphics.Rect
|
||||||
|
import android.testing.AndroidTestingRunner
|
||||||
|
import android.view.View
|
||||||
|
import androidx.test.filters.SmallTest
|
||||||
|
import com.android.systemui.SysuiTestCase
|
||||||
|
import com.android.systemui.shared.navigationbar.RegionSamplingHelper
|
||||||
|
import java.io.PrintWriter
|
||||||
|
import java.util.concurrent.Executor
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.Mock
|
||||||
|
import org.mockito.Mockito.clearInvocations
|
||||||
|
import org.mockito.Mockito.verify
|
||||||
|
import org.mockito.Mockito.`when` as whenever
|
||||||
|
import org.mockito.junit.MockitoJUnit
|
||||||
|
|
||||||
|
@RunWith(AndroidTestingRunner::class)
|
||||||
|
@SmallTest
|
||||||
|
class RegionSamplingInstanceTest : SysuiTestCase() {
|
||||||
|
|
||||||
|
@JvmField @Rule
|
||||||
|
val mockito = MockitoJUnit.rule()
|
||||||
|
|
||||||
|
@Mock private lateinit var sampledView: View
|
||||||
|
@Mock private lateinit var mainExecutor: Executor
|
||||||
|
@Mock private lateinit var bgExecutor: Executor
|
||||||
|
@Mock private lateinit var regionSampler: RegionSamplingHelper
|
||||||
|
@Mock private lateinit var updateFun: RegionSamplingInstance.UpdateColorCallback
|
||||||
|
@Mock private lateinit var pw: PrintWriter
|
||||||
|
@Mock private lateinit var callback: RegionSamplingHelper.SamplingCallback
|
||||||
|
|
||||||
|
private lateinit var regionSamplingInstance: RegionSamplingInstance
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setUp() {
|
||||||
|
whenever(sampledView.isAttachedToWindow).thenReturn(true)
|
||||||
|
whenever(regionSampler.callback).thenReturn(this@RegionSamplingInstanceTest.callback)
|
||||||
|
|
||||||
|
regionSamplingInstance = object : RegionSamplingInstance(
|
||||||
|
sampledView,
|
||||||
|
mainExecutor,
|
||||||
|
bgExecutor,
|
||||||
|
true,
|
||||||
|
updateFun
|
||||||
|
) {
|
||||||
|
override fun createRegionSamplingHelper(
|
||||||
|
sampledView: View,
|
||||||
|
callback: RegionSamplingHelper.SamplingCallback,
|
||||||
|
mainExecutor: Executor?,
|
||||||
|
bgExecutor: Executor?
|
||||||
|
): RegionSamplingHelper {
|
||||||
|
return this@RegionSamplingInstanceTest.regionSampler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testStartRegionSampler() {
|
||||||
|
regionSamplingInstance.startRegionSampler()
|
||||||
|
|
||||||
|
verify(regionSampler).start(Rect(0, 0, 0, 0))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testStopRegionSampler() {
|
||||||
|
regionSamplingInstance.stopRegionSampler()
|
||||||
|
|
||||||
|
verify(regionSampler).stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testDump() {
|
||||||
|
regionSamplingInstance.dump(pw)
|
||||||
|
|
||||||
|
verify(regionSampler).dump(pw)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testUpdateColorCallback() {
|
||||||
|
regionSampler.callback.onRegionDarknessChanged(false)
|
||||||
|
verify(regionSampler.callback).onRegionDarknessChanged(false)
|
||||||
|
clearInvocations(regionSampler.callback)
|
||||||
|
regionSampler.callback.onRegionDarknessChanged(true)
|
||||||
|
verify(regionSampler.callback).onRegionDarknessChanged(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFlagFalse() {
|
||||||
|
regionSamplingInstance = object : RegionSamplingInstance(
|
||||||
|
sampledView,
|
||||||
|
mainExecutor,
|
||||||
|
bgExecutor,
|
||||||
|
false,
|
||||||
|
updateFun
|
||||||
|
) {
|
||||||
|
override fun createRegionSamplingHelper(
|
||||||
|
sampledView: View,
|
||||||
|
callback: RegionSamplingHelper.SamplingCallback,
|
||||||
|
mainExecutor: Executor?,
|
||||||
|
bgExecutor: Executor?
|
||||||
|
): RegionSamplingHelper {
|
||||||
|
return this@RegionSamplingInstanceTest.regionSampler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals(regionSamplingInstance.regionSampler, null)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user