blob: 368d16987ae1d940884dd8ab6d43090e251b4c86 [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.desugaredlibrary.jdk11;
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.transformers.MethodTransformer;
import com.android.tools.r8.utils.AndroidApiLevel;
import com.android.tools.r8.utils.BooleanUtils;
import com.android.tools.r8.utils.StringUtils;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.objectweb.asm.Opcodes;
@RunWith(Parameterized.class)
public class StandardCharsetTest extends DesugaredLibraryTestBase {
private static final String EXPECTED_RESULT =
StringUtils.lines("%E3%81%8B", "%82%A0%82%A9%97%43%24%E3%81%8B", "true");
private final TestParameters parameters;
private final boolean shrinkDesugaredLibrary;
@Parameters(name = "{1}, shrinkDesugaredLibrary: {0}")
public static List<Object[]> data() {
return buildParameters(
BooleanUtils.values(), getTestParameters().withDexRuntimes().withAllApiLevels().build());
}
public StandardCharsetTest(boolean shrinkDesugaredLibrary, TestParameters parameters) {
this.shrinkDesugaredLibrary = shrinkDesugaredLibrary;
this.parameters = parameters;
}
@Test
public void testD8() throws Exception {
Assume.assumeTrue(isJDK11DesugaredLibrary());
KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
testForD8(parameters.getBackend())
.addLibraryFiles(ToolHelper.getAndroidJar(AndroidApiLevel.P))
.addProgramClassFileData(getProgramClassFileData())
.setMinApi(parameters.getApiLevel())
.enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
.compile()
.addDesugaredCoreLibraryRunClassPath(
this::buildDesugaredLibrary,
parameters.getApiLevel(),
keepRuleConsumer.get(),
shrinkDesugaredLibrary)
.run(parameters.getRuntime(), TestClass.class)
.assertSuccessWithOutput(EXPECTED_RESULT);
}
@Test
public void testR8() throws Exception {
Assume.assumeTrue(isJDK11DesugaredLibrary());
KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
testForR8(Backend.DEX)
.addLibraryFiles(ToolHelper.getAndroidJar(AndroidApiLevel.P))
.addProgramClassFileData(getProgramClassFileData())
.setMinApi(parameters.getApiLevel())
.addKeepMainRule(TestClass.class)
.enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
.compile()
.addDesugaredCoreLibraryRunClassPath(
this::buildDesugaredLibrary,
parameters.getApiLevel(),
keepRuleConsumer.get(),
shrinkDesugaredLibrary)
.run(parameters.getRuntime(), TestClass.class)
.assertSuccessWithOutput(EXPECTED_RESULT);
}
private Collection<byte[]> getProgramClassFileData() throws IOException {
return ImmutableList.of(
transformer(TestClass.class)
.addMethodTransformer(
new MethodTransformer() {
@Override
public void visitMethodInsn(
int opcode,
String owner,
String name,
String descriptor,
boolean isInterface) {
if (opcode == Opcodes.INVOKESTATIC && name.equals("encode")) {
super.visitMethodInsn(
opcode, "java/net/URLEncoder", name, descriptor, isInterface);
return;
}
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
})
.transform());
}
public static class TestClass {
public static void main(String[] args) {
System.out.println(encode("か", StandardCharsets.UTF_8));
System.out.println(
encode("あ", "Shift_JIS")
+ encode("か", "Shift_JIS")
+ encode("佑", "Shift_JIS")
+ encode("$", "Shift_JIS")
+ encode("か", StandardCharsets.UTF_8));
System.out.println(Character.isBmpCodePoint('か'));
}
// Replaced in the transformer by JDK 11 URLEncoder#encode.
public static String encode(String s, Charset set) {
return null;
}
// Replaced in the transformer by JDK 11 URLEncoder#encode.
public static String encode(String s, String set) {
return null;
}
}
}