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

Commit 94efd7a8 authored by Anushree Ganjam's avatar Anushree Ganjam
Browse files

Update test build rules to support mocking AConfig methods.

- Add mockito-inline-extended to allow mocking static methods.
- Add StaticMockitoRule to help mock static methods.
This helps in unit tests with mocking the trunk stable flags.

Bug: 294913042
Test: NA
Flag: NA
Change-Id: If53ae1d1bc68a5c6b1824b6c7060cbc14e095b1b
parent fd77e235
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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'
+2 −1
Original line number Diff line number Diff line
@@ -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",
+72 −0
Original line number Diff line number Diff line
/*
 * 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;
                }
            }
        };
    }
}