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

Commit a744e112 authored by Archisha Baranwal's avatar Archisha Baranwal Committed by Android (Google) Code Review
Browse files

Merge "Modifying ViewCaptureAwareWindowManager to add param to check if...

Merge "Modifying ViewCaptureAwareWindowManager to add param to check if ViewCapture tracing is enabled." into main
parents 178535d7 e14bb5fb
Loading
Loading
Loading
Loading
+13 −8
Original line number Diff line number Diff line
@@ -32,16 +32,19 @@ private const val TAG = "ViewCaptureWindowManager"
class ViewCaptureAwareWindowManager(
    private val windowManager: WindowManager,
    private val lazyViewCapture: Lazy<ViewCapture>,
    private val isViewCaptureEnabled: Boolean,
) : WindowManager by windowManager {

    private var viewCaptureCloseableMap: MutableMap<View, SafeCloseable> = mutableMapOf()

    override fun addView(view: View, params: ViewGroup.LayoutParams?) {
        windowManager.addView(view, params)
        if (isViewCaptureEnabled) {
            val viewCaptureCloseable: SafeCloseable =
                lazyViewCapture.value.startCapture(view, getViewName(view))
            viewCaptureCloseableMap[view] = viewCaptureCloseable
        }
    }

    override fun removeView(view: View?) {
        removeViewFromCloseableMap(view)
@@ -56,6 +59,7 @@ class ViewCaptureAwareWindowManager(
    private fun getViewName(view: View) = "." + view.javaClass.name

    private fun removeViewFromCloseableMap(view: View?) {
        if (isViewCaptureEnabled) {
            if (viewCaptureCloseableMap.containsKey(view)) {
                viewCaptureCloseableMap[view]?.close()
                viewCaptureCloseableMap.remove(view)
@@ -64,3 +68,4 @@ class ViewCaptureAwareWindowManager(
            }
        }
    }
}
+19 −3
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mockito.doAnswer
import org.mockito.Mockito.spy
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.invocation.InvocationOnMock
@@ -56,14 +57,29 @@ class ViewCaptureAwareWindowManagerTest {
            .`when`(windowManager)
            .addView(any(View::class.java), any(WindowManager.LayoutParams::class.java))
        `when`(mockRootView.context).thenReturn(context)
    }

    @Test
    fun testAddView_viewCaptureEnabled_verifyStartCaptureCall() {
        mViewCaptureAwareWindowManager =
            ViewCaptureAwareWindowManager(windowManager, lazyViewCapture)
            ViewCaptureAwareWindowManager(
                windowManager,
                lazyViewCapture,
                isViewCaptureEnabled = true
            )
        mViewCaptureAwareWindowManager?.addView(mockRootView, mockRootView.layoutParams)
        verify(viewCaptureSpy).startCapture(any(), anyString())
    }

    @Test
    fun testAddView_verifyStartCaptureCall() {
    fun testAddView_viewCaptureNotEnabled_verifyStartCaptureCall() {
        mViewCaptureAwareWindowManager =
            ViewCaptureAwareWindowManager(
                windowManager,
                lazyViewCapture,
                isViewCaptureEnabled = false
            )
        mViewCaptureAwareWindowManager?.addView(mockRootView, mockRootView.layoutParams)
        verify(viewCaptureSpy).startCapture(any(), anyString())
        verify(viewCaptureSpy, times(0)).startCapture(any(), anyString())
    }
}