diff options
author | Winson <chiuwinson@google.com> | 2019-10-24 16:32:20 -0700 |
---|---|---|
committer | Winson <chiuwinson@google.com> | 2019-12-09 10:46:59 -0800 |
commit | 3f46dbd7a091b5e594be6055ab11d0e0cfe25a18 (patch) | |
tree | d303f72bf4a8f6cd2106ef7851e716c707a622bb /tests/utils | |
parent | 6571c8a46154b800d16a2fe850c4b2129b209a2a (diff) |
Overlay, actor, and target app visibility handling
Hooks AppsFilter to support exposing a target and any overlays
targeting it to the actor specified in its overlayable block.
Sacrifices some install-time performance in favor of less memory
usage and easier to follow code by doing a full search/rebuild
on each change.
Benchmarks TBD
Bug: 143096091
Test: atest OverlayReferenceMapperTests
Change-Id: Ic832818b9aa383f1167ca3e69a11b8459fa9db97
Diffstat (limited to 'tests/utils')
-rw-r--r-- | tests/utils/testutils/Android.bp | 7 | ||||
-rw-r--r-- | tests/utils/testutils/java/test/package-info.java | 21 | ||||
-rw-r--r-- | tests/utils/testutils/java/test/util/MockitoUtils.kt | 70 |
3 files changed, 97 insertions, 1 deletions
diff --git a/tests/utils/testutils/Android.bp b/tests/utils/testutils/Android.bp index f71be7b0b7d3..027b1d6799fd 100644 --- a/tests/utils/testutils/Android.bp +++ b/tests/utils/testutils/Android.bp @@ -17,11 +17,16 @@ java_library { name: "frameworks-base-testutils", - srcs: ["java/**/*.java"], + srcs: [ + "java/**/*.java", + "java/**/*.kt", + ], static_libs: [ "junit", "hamcrest-library", + "truth-prebuilt", + "mockito-target-minus-junit4", ], libs: [ diff --git a/tests/utils/testutils/java/test/package-info.java b/tests/utils/testutils/java/test/package-info.java new file mode 100644 index 000000000000..c34d7b21ab84 --- /dev/null +++ b/tests/utils/testutils/java/test/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2019 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. + */ + +/** + * This package separated from android. because placing classes under android.'s .test/.util + * may be confused with tests for that actual android subpackage. + **/ +package test; diff --git a/tests/utils/testutils/java/test/util/MockitoUtils.kt b/tests/utils/testutils/java/test/util/MockitoUtils.kt new file mode 100644 index 000000000000..5151abe54108 --- /dev/null +++ b/tests/utils/testutils/java/test/util/MockitoUtils.kt @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2019 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 test.util + +import org.mockito.Answers +import org.mockito.Mockito +import org.mockito.invocation.InvocationOnMock +import org.mockito.stubbing.Answer +import org.mockito.stubbing.Stubber + +object MockitoUtils { + val ANSWER_THROWS = Answer<Any?> { + when (val name = it.method.name) { + "toString" -> return@Answer Answers.CALLS_REAL_METHODS.answer(it) + else -> { + val arguments = it.arguments + ?.takeUnless { it.isEmpty() } + ?.joinToString() + ?.let { + "with $it" + } + .orEmpty() + + throw UnsupportedOperationException("${it.mock::class.java.simpleName}#$name " + + "$arguments should not be called") + } + } + } +} + +inline fun <reified T> mock(block: T.() -> Unit = {}) = Mockito.mock(T::class.java).apply(block) + +fun <Type> Stubber.whenever(mock: Type) = Mockito.`when`(mock) +fun <Type : Any?> whenever(mock: Type) = Mockito.`when`(mock) + +@Suppress("UNCHECKED_CAST") +fun <Type : Any?> whenever(mock: Type, block: InvocationOnMock.() -> Any?) = + Mockito.`when`(mock).thenAnswer { block(it) } + +fun whenever(mock: Unit) = Mockito.`when`(mock).thenAnswer { } + +inline fun <reified T> mockThrowOnUnmocked(block: T.() -> Unit): T { + val swappingAnswer = object : Answer<Any?> { + var delegate: Answer<*> = Answers.RETURNS_DEFAULTS + + override fun answer(invocation: InvocationOnMock?): Any? { + return delegate.answer(invocation) + } + } + + return Mockito.mock(T::class.java, swappingAnswer).apply(block) + .also { + // To allow when() usage inside block, only swap to throwing afterwards + swappingAnswer.delegate = MockitoUtils.ANSWER_THROWS + } +} |