blob: 523b96197dc7fd7afdc41073f101c2fcbef65942 [file] [log] [blame]
// Copyright (c) 2019, 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.desugar.desugaredlibrary.conversiontests;
import static org.hamcrest.core.StringContains.containsString;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.ToolHelper;
import com.android.tools.r8.desugar.desugaredlibrary.DesugaredLibraryTestBase;
import com.android.tools.r8.utils.AndroidApiLevel;
import com.android.tools.r8.utils.BooleanUtils;
import java.nio.file.Path;
import java.time.Year;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class APIConversionFinalClassTest extends DesugaredLibraryTestBase {
private final TestParameters parameters;
private final boolean shrinkDesugaredLibrary;
private static final AndroidApiLevel MIN_SUPPORTED = AndroidApiLevel.O;
private static Path customLib;
@Parameterized.Parameters(name = "{0}, shrinkDesugaredLibrary: {1}")
public static List<Object[]> data() {
return buildParameters(getTestParameters().withDexRuntimesStartingFromIncluding(ToolHelper.DexVm.Version.V8_1_0).withApiLevelsEndingAtExcluding(AndroidApiLevel.O).build(), BooleanUtils.values());
}
public APIConversionFinalClassTest(TestParameters parameters, boolean shrinkDesugaredLibrary) {
this.shrinkDesugaredLibrary = shrinkDesugaredLibrary;
this.parameters = parameters;
}
@BeforeClass
public static void compileCustomLib() throws Exception {
customLib =
testForD8(getStaticTemp())
.addProgramClasses(CustomLibClass.class)
.setMinApi(MIN_SUPPORTED)
.compile()
.writeToZip();
}
@Test
public void testFinalMethod() throws Exception {
KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
testForD8()
.setMinApi(AndroidApiLevel.B)
.addProgramClasses(Executor.class)
.addLibraryClasses(CustomLibClass.class)
.enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
.compile()
.addDesugaredCoreLibraryRunClassPath(
this::buildDesugaredLibrary,
parameters.getApiLevel(),
keepRuleConsumer.get(),
shrinkDesugaredLibrary)
.addRunClasspathFiles(customLib)
.run(parameters.getRuntime(), Executor.class)
.assertFailureWithErrorThatMatches(containsString("NoSuchMethodError"));
}
static class Executor {
public static void main(String[] args) {
System.out.println(CustomLibClass.libCall(Year.now()));
}
}
// This class will be put at compilation time as library and on the runtime class path.
// This class is convenient for easy testing. Each method plays the role of methods in the
// platform APIs for which argument/return values need conversion.
static class CustomLibClass {
// We use Year because Year is a final class with no custom conversion but Year has been
// unused in the Android library so far.
public static long libCall(Year year) {
return 0L;
}
}
}