blob: 1624c29de83ae01fe0cc2966232eac086550f27b [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.desugar.records;
import static com.android.tools.r8.utils.InternalOptions.TestingOptions;
import static org.junit.Assume.assumeTrue;
import com.android.tools.r8.R8FullTestBuilder;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.TestRuntime.CfVm;
import com.android.tools.r8.utils.AndroidApiLevel;
import com.android.tools.r8.utils.StringUtils;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class EmptyRecordAnnotationTest extends TestBase {
private static final String RECORD_NAME = "EmptyRecordAnnotation";
private static final byte[][] PROGRAM_DATA = RecordTestUtils.getProgramData(RECORD_NAME);
private static final String MAIN_TYPE = RecordTestUtils.getMainType(RECORD_NAME);
private static final String EXPECTED_RESULT_CF =
StringUtils.lines("class java.lang.Record", "class records.EmptyRecordAnnotation$Empty");
private static final String EXPECTED_RESULT_DEX =
StringUtils.lines(
"class com.android.tools.r8.RecordTag", "class records.EmptyRecordAnnotation$Empty");
private final TestParameters parameters;
public EmptyRecordAnnotationTest(TestParameters parameters) {
this.parameters = parameters;
}
@Parameterized.Parameters(name = "{0}")
public static List<Object[]> data() {
return buildParameters(
getTestParameters()
.withCfRuntimesStartingFromIncluding(CfVm.JDK17)
.withDexRuntimes()
.withAllApiLevelsAlsoForCf()
.build());
}
private boolean isDefaultCfParameters() {
return parameters.isCfRuntime() && parameters.getApiLevel().equals(AndroidApiLevel.B);
}
@Test
public void testD8AndJvm() throws Exception {
if (isDefaultCfParameters()) {
testForJvm()
.addProgramClassFileData(PROGRAM_DATA)
.run(parameters.getRuntime(), MAIN_TYPE)
.assertSuccessWithOutput(EXPECTED_RESULT_CF);
}
testForD8(parameters.getBackend())
.addProgramClassFileData(PROGRAM_DATA)
.setMinApi(parameters.getApiLevel())
.addOptionsModification(TestingOptions::allowExperimentClassFileVersion)
.compile()
.run(parameters.getRuntime(), MAIN_TYPE)
.assertSuccessWithOutput(EXPECTED_RESULT_DEX);
}
@Test
public void testR8() throws Exception {
assumeTrue(parameters.isDexRuntime() || isDefaultCfParameters());
R8FullTestBuilder builder =
testForR8(parameters.getBackend())
.addProgramClassFileData(PROGRAM_DATA)
.setMinApi(parameters.getApiLevel())
.addKeepRules("-keep class records.EmptyRecordAnnotation { *; }")
.addKeepRules("-keepattributes *Annotation*")
.addKeepRules("-keep class records.EmptyRecordAnnotation$Empty")
.addKeepMainRule(MAIN_TYPE)
.addOptionsModification(TestingOptions::allowExperimentClassFileVersion);
if (parameters.isCfRuntime()) {
builder
.addLibraryFiles(RecordTestUtils.getJdk15LibraryFiles(temp))
.compile()
.inspect(RecordTestUtils::assertRecordsAreRecords)
.enableJVMPreview()
.run(parameters.getRuntime(), MAIN_TYPE)
.assertSuccessWithOutput(EXPECTED_RESULT_CF);
return;
}
builder
.addKeepRules("-keep class java.lang.Record")
.run(parameters.getRuntime(), MAIN_TYPE)
.assertSuccessWithOutput(EXPECTED_RESULT_DEX);
}
}