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

Commit 3f5e44e0 authored by Fengjiang Li's avatar Fengjiang Li
Browse files

[Test Week] Add ViewOnDrawExecutorTest

Number of new tested methods: 7

Bug: 353303621
Test: This is the unit test
Flag: NONE - unit test
Change-Id: Ic89984fac515924cf9a16ebc6fc7028325bed386
parent f04eeb5e
Loading
Loading
Loading
Loading
+186 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.launcher3.util

import android.view.View
import android.view.ViewTreeObserver
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.launcher3.Launcher
import com.android.launcher3.Workspace
import com.android.launcher3.pageindicators.PageIndicator
import java.util.function.Consumer
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.reset
import org.mockito.kotlin.same
import org.mockito.kotlin.verifyZeroInteractions

@RunWith(AndroidJUnit4::class)
class ViewOnDrawExecutorTest<T> where T : View, T : PageIndicator {

    @Mock private lateinit var runnable: Runnable
    @Mock private lateinit var consumer: Consumer<ViewOnDrawExecutor>
    @Mock private lateinit var launcher: Launcher
    @Mock private lateinit var workspace: Workspace<T>
    @Mock private lateinit var rootView: View
    @Mock private lateinit var viewTreeObserver: ViewTreeObserver

    private lateinit var underTest: ViewOnDrawExecutor
    private lateinit var runnableList: RunnableList

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)
        runnableList = RunnableList()
        runnableList.add(runnable)
        underTest = ViewOnDrawExecutor(runnableList, consumer)

        `when`(launcher.workspace).thenReturn(workspace)
        `when`(workspace.rootView).thenReturn(rootView)
        `when`(workspace.viewTreeObserver).thenReturn(viewTreeObserver)
    }

    @Test
    fun attachToLauncher_alreadyAttachedToWindow() {
        `when`(workspace.isAttachedToWindow).thenReturn(true)

        underTest.attachTo(launcher)

        verify(workspace).addOnAttachStateChangeListener(same(underTest))
        verify(viewTreeObserver).addOnDrawListener(same(underTest))
        verify(rootView).invalidate()
    }

    @Test
    fun attachToLauncher_notAttachedToWindow() {
        `when`(workspace.isAttachedToWindow).thenReturn(false)

        underTest.attachTo(launcher)

        verify(workspace).addOnAttachStateChangeListener(same(underTest))
        verifyZeroInteractions(viewTreeObserver)
        verifyZeroInteractions(rootView)
    }

    @Test
    fun onViewAttachedToWindow_registerObserver() {
        `when`(workspace.isAttachedToWindow).thenReturn(false)
        underTest.attachTo(launcher)

        underTest.onViewAttachedToWindow(rootView)

        verify(viewTreeObserver).addOnDrawListener(same(underTest))
        verify(rootView).invalidate()
    }

    @Test
    fun complete_then_onViewAttachedToWindow_registerObserver() {
        underTest.markCompleted()
        reset(viewTreeObserver)
        reset(rootView)

        underTest.onViewAttachedToWindow(rootView)

        verifyZeroInteractions(viewTreeObserver)
        verifyZeroInteractions(rootView)
    }

    @Test
    fun onDraw_postRunnable() {
        underTest.attachTo(launcher)

        underTest.onDraw()

        verify(workspace).post(same(underTest))
    }

    @Test
    fun run_before_onDraw_noOp() {
        underTest.run()

        verifyZeroInteractions(runnable)
        verifyZeroInteractions(viewTreeObserver)
        verifyZeroInteractions(workspace)
        verifyZeroInteractions(consumer)
    }

    @Test
    fun first_run_executeRunnable() {
        underTest.attachTo(launcher)
        underTest.onDraw()

        underTest.run()

        verify(runnable).run()
        verify(viewTreeObserver).removeOnDrawListener(same(underTest))
        verify(workspace).removeOnAttachStateChangeListener(same(underTest))
        verify(consumer).accept(same(underTest))
    }

    @Test
    fun second_run_noOp() {
        underTest.attachTo(launcher)
        underTest.onDraw()
        underTest.run()
        reset(runnable)
        reset(viewTreeObserver)
        reset(workspace)
        reset(consumer)

        underTest.run()

        verifyZeroInteractions(runnable)
        verifyZeroInteractions(viewTreeObserver)
        verifyZeroInteractions(workspace)
        verifyZeroInteractions(consumer)
    }

    @Test
    fun markCompleted_viewNotAttached() {
        underTest.markCompleted()

        verify(runnable).run()
        verify(consumer).accept(underTest)
        verifyZeroInteractions(workspace)
    }

    @Test
    fun markCompleted_viewAttached() {
        underTest.attachTo(launcher)

        underTest.markCompleted()

        verify(runnable).run()
        verify(consumer).accept(underTest)
        verify(workspace).removeOnAttachStateChangeListener(same(underTest))
        verify(viewTreeObserver).removeOnDrawListener(same(underTest))
    }

    @Test
    fun cancel_notRun() {
        underTest.cancel()

        verifyZeroInteractions(runnable)
        verify(consumer).accept(underTest)
        verifyZeroInteractions(workspace)
    }
}