Files
Lawnchair/tests/multivalentTests/src/com/android/launcher3/util/ExecutorRunnableTest.kt
T
Uwais Ashraf 77b97c0729 Replace existing Robolectric test task with functioning one.
This CL does the following:
- Creates a dir for multivalentTests
- Creates symlinks for the dir to keep Android Studio happy
- Moves many files to the multivalentTests dir
- Adjusts gradle and soong build files to use the new dir as part of
their source sets.

Test: ./gradlew :NexusLauncher:testGoogleWithQuickstepDebugUnitTest
Test: atest Launcher3RoboTests
Fix: 316553886
Bug: 316553889
Flag: NA
Change-Id: Iae28fd0c0191b3ecf9bd2950800875950cca2622
2024-01-19 21:39:23 +00:00

138 lines
4.1 KiB
Kotlin

/*
* Copyright (C) 2023 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 androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.launcher3.util.rule.TestStabilityRule
import java.util.concurrent.ExecutorService
import java.util.concurrent.locks.ReentrantLock
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertFalse
import junit.framework.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
/** Unit test for [ExecutorRunnable] */
@SmallTest
@RunWith(AndroidJUnit4::class)
class ExecutorRunnableTest {
private lateinit var underTest: ExecutorRunnable<Int>
private val lock = ReentrantLock()
private var result: Int = -1
private var isTaskExecuted = false
private var isCallbackExecuted = false
@get:Rule(order = 0) val testStabilityRule = TestStabilityRule()
@Before
fun setup() {
reset()
submitJob()
}
private fun submitJob() {
underTest =
ExecutorRunnable.createAndExecute(
Executors.UI_HELPER_EXECUTOR,
{
isTaskExecuted = true
1
},
Executors.VIEW_PREINFLATION_EXECUTOR,
{
isCallbackExecuted = true
result = it + 1
}
)
}
@Test
fun run_and_complete() {
awaitAllExecutorCompleted()
assertTrue("task should be executed", isTaskExecuted)
assertTrue("callback should be executed", isCallbackExecuted)
assertEquals(2, result)
}
@Test
fun run_and_cancel_cancelTaskAndCallback() {
awaitAllExecutorCompleted()
reset()
lock.lock()
Executors.UI_HELPER_EXECUTOR.submit { lock.lock() }
submitJob()
underTest.cancel(false)
lock.unlock() // unblock task on UI_HELPER_EXECUTOR
awaitAllExecutorCompleted()
assertFalse("task should not be executed.", isTaskExecuted)
assertFalse("callback should not be executed.", isCallbackExecuted)
assertEquals(0, result)
}
@Test
fun run_and_cancel_cancelCallback() {
awaitAllExecutorCompleted()
reset()
lock.lock()
Executors.VIEW_PREINFLATION_EXECUTOR.submit { lock.lock() }
submitJob()
awaitExecutorCompleted(Executors.UI_HELPER_EXECUTOR)
assertTrue("task should be executed.", isTaskExecuted)
underTest.cancel(false)
lock.unlock() // unblock callback on VIEW_PREINFLATION_EXECUTOR
awaitExecutorCompleted(Executors.VIEW_PREINFLATION_EXECUTOR)
assertFalse("callback should not be executed.", isCallbackExecuted)
assertEquals(0, result)
}
@Test
fun run_and_cancelAfterCompletion_executeAll() {
awaitAllExecutorCompleted()
underTest.cancel(false)
assertTrue("task should be executed", isTaskExecuted)
assertTrue("callback should be executed", isCallbackExecuted)
assertEquals(2, result)
}
private fun awaitExecutorCompleted(executor: ExecutorService) {
executor.submit<Any> { null }.get()
}
private fun awaitAllExecutorCompleted() {
awaitExecutorCompleted(Executors.UI_HELPER_EXECUTOR)
awaitExecutorCompleted(Executors.VIEW_PREINFLATION_EXECUTOR)
}
private fun reset() {
result = 0
isTaskExecuted = false
isCallbackExecuted = false
}
}