blob: f10a6e9ea002045f2e62ae342d4b702b979642dd [file] [log] [blame]
// Copyright (c) 2021, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.apioutlining;
import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import com.android.tools.r8.TestCompilerBuilder;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.ThrowableConsumer;
import com.android.tools.r8.references.Reference;
import com.android.tools.r8.utils.AndroidApiLevel;
import com.android.tools.r8.utils.ThrowingConsumer;
import com.android.tools.r8.utils.codeinspector.CodeInspector;
import com.android.tools.r8.utils.codeinspector.CodeMatchers;
import com.android.tools.r8.utils.codeinspector.MethodSubject;
import java.lang.reflect.Method;
public abstract class ApiOutliningTestHelper {
static <T extends TestCompilerBuilder<?, ?, ?, ?, ?>>
ThrowableConsumer<T> setMockApiLevelForMethod(Method method, AndroidApiLevel apiLevel) {
return compilerBuilder -> {
compilerBuilder.addOptionsModification(
options -> {
options.methodApiMapping.put(Reference.methodFromMethod(method), apiLevel);
});
};
}
static ApiOutliningMethodVerificationHelper verifyThat(TestParameters parameters, Method method) {
return new ApiOutliningMethodVerificationHelper(parameters, method);
}
public static class ApiOutliningMethodVerificationHelper {
private final Method methodOfInterest;
private final TestParameters parameters;
public ApiOutliningMethodVerificationHelper(
TestParameters parameters, Method methodOfInterest) {
this.methodOfInterest = methodOfInterest;
this.parameters = parameters;
}
protected ThrowingConsumer<CodeInspector, Exception> inlinedIntoFromApiLevel(
Method method, AndroidApiLevel apiLevel) {
return parameters.getApiLevel().isGreaterThanOrEqualTo(apiLevel)
? inlinedInto(method)
: notInlinedInto(method);
}
private ThrowingConsumer<CodeInspector, Exception> notInlinedInto(Method method) {
return inspector -> {
MethodSubject candidate = inspector.method(methodOfInterest);
assertThat(candidate, isPresent());
MethodSubject target = inspector.method(method);
assertThat(target, isPresent());
assertThat(target, CodeMatchers.invokesMethod(candidate));
};
}
private ThrowingConsumer<CodeInspector, Exception> inlinedInto(Method method) {
return inspector -> {
MethodSubject candidate = inspector.method(methodOfInterest);
if (!candidate.isPresent()) {
return;
}
MethodSubject target = inspector.method(method);
assertThat(target, isPresent());
assertThat(target, not(CodeMatchers.invokesMethod(candidate)));
};
}
}
}