diff --git a/build.gradle b/build.gradle index 090bafe68c..f4d7261625 100644 --- a/build.gradle +++ b/build.gradle @@ -157,6 +157,8 @@ dependencies { withoutQuickstepImplementation fileTree(dir: "${FRAMEWORK_PREBUILTS_DIR}/libs", include: 'plugin_core.jar') testImplementation 'junit:junit:4.12' + testImplementation libs.mockitoInlineExtended + androidTestImplementation libs.mockitoInlineExtended androidTestImplementation "org.mockito:mockito-core:1.9.5" androidTestImplementation 'com.google.dexmaker:dexmaker:1.2' androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2' diff --git a/tests/Android.bp b/tests/Android.bp index 96ffa76210..89ebe8cc59 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -83,7 +83,7 @@ android_library { "androidx.test.espresso.contrib", "androidx.test.espresso.intents", "androidx.test.uiautomator_uiautomator", - "mockito-target-inline-minus-junit4", + "mockito-target-extended-minus-junit4", "launcher_log_protos_lite", "truth-prebuilt", "platform-test-rules", @@ -114,6 +114,7 @@ android_test { "android.test.runner", "android.test.mock", ], + // Libraries used by mockito inline extended jni_libs: [ "libdexmakerjvmtiagent", "libstaticjvmtiagent", diff --git a/tests/src/com/android/launcher3/util/rule/StaticMockitoRule.java b/tests/src/com/android/launcher3/util/rule/StaticMockitoRule.java new file mode 100644 index 0000000000..6b91474f02 --- /dev/null +++ b/tests/src/com/android/launcher3/util/rule/StaticMockitoRule.java @@ -0,0 +1,72 @@ +/* + * 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.launcher3.util.rule; + +import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; + +import com.android.dx.mockito.inline.extended.StaticMockitoSession; +import com.android.dx.mockito.inline.extended.StaticMockitoSessionBuilder; + +import org.junit.rules.MethodRule; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.Statement; +import org.mockito.junit.MockitoRule; +import org.mockito.quality.Strictness; + +/** + * Similar to {@link MockitoRule}, but uses {@link StaticMockitoSession}, which allows mocking + * static methods. + */ +public class StaticMockitoRule implements MethodRule { + private Class[] mClasses; + + public StaticMockitoRule(Class... classes) { + mClasses = classes; + } + + @Override + public Statement apply(Statement base, FrameworkMethod method, Object target) { + return new Statement() { + public void evaluate() throws Throwable { + StaticMockitoSessionBuilder builder = + mockitoSession() + .name(target.getClass().getSimpleName() + "." + method.getName()) + .initMocks(target) + .strictness(Strictness.STRICT_STUBS); + + for (Class clazz : mClasses) { + builder.mockStatic(clazz); + } + + StaticMockitoSession session = builder.startMocking(); + Throwable testFailure = evaluateSafely(base); + session.finishMocking(testFailure); + if (testFailure != null) { + throw testFailure; + } + } + + private Throwable evaluateSafely(Statement base) { + try { + base.evaluate(); + return null; + } catch (Throwable throwable) { + return throwable; + } + } + }; + } +}