blob: 8e3a5e99a2d2b50bf9719f4f78fc059849516854 [file] [log] [blame]
// Copyright (c) 2023, 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.androidresources;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import com.android.tools.r8.R8TestBuilder;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.androidresources.AndroidResourceTestingUtils.AndroidTestResource;
import com.android.tools.r8.androidresources.AndroidResourceTestingUtils.AndroidTestResourceBuilder;
import com.android.tools.r8.androidresources.AndroidResourceTestingUtils.ResourceTableInspector;
import com.android.tools.r8.utils.BooleanUtils;
import com.android.tools.r8.utils.FileUtils;
import com.android.tools.r8.utils.ZipUtils;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class SimpleNoCodeReferenceAndroidResourceTest extends TestBase {
@Parameter(0)
public TestParameters parameters;
@Parameter(1)
public boolean optimized;
@Parameters(name = "{0}, elementType: {1}")
public static List<Object[]> data() {
return buildParameters(
getTestParameters()
.withDefaultDexRuntime()
.withAllApiLevels()
.withIncludeAllPartialCompilation()
.build(),
BooleanUtils.values());
}
@Test
public void testR8() throws Exception {
// We don't support running R8Partial with non optimized resource shrinking.
assumeTrue(optimized || parameters.getPartialCompilationTestParameters().isNone());
String manifestPath = "AndroidManifest.xml";
String resourcePath = "resources.pb";
String pngPath = "res/drawable/foo.png";
AndroidTestResource testResource =
new AndroidTestResourceBuilder()
.withSimpleManifestAndAppNameString()
.addDrawable("foo.png", AndroidResourceTestingUtils.TINY_PNG)
.addDrawable("bar.png", AndroidResourceTestingUtils.TINY_PNG)
.build(temp);
Path resources = testResource.getResourceZip();
Path output = temp.newFile("resources_out.zip").toPath();
testForR8(parameters)
.addInnerClasses(getClass())
.setMinApi(parameters)
.addAndroidResources(testResource, output)
.applyIf(optimized, R8TestBuilder::enableOptimizedShrinking)
.addKeepMainRule(FooBar.class)
.compile()
.inspectShrunkenResources(
shrunkenInspector -> {
// Reachable from the manifest
shrunkenInspector.assertContainsResourceWithName("string", "app_name");
// Not reachable from anything
shrunkenInspector.assertDoesNotContainResourceWithName("drawable", "foo");
shrunkenInspector.assertDoesNotContainResourceWithName("drawable", "bar");
try {
assertFalse(ZipUtils.containsEntry(output, pngPath));
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.run(parameters.getRuntime(), FooBar.class)
.assertSuccessWithOutputLines("Hello World");
// We don't touch the manifest
assertArrayEquals(
ZipUtils.readSingleEntry(output, manifestPath),
ZipUtils.readSingleEntry(resources, manifestPath));
String rClassContent =
FileUtils.readTextFile(
testResource.getRClass().getJavaFilePath(), Charset.defaultCharset());
assertFalse(
Arrays.equals(
ZipUtils.readSingleEntry(output, resourcePath),
ZipUtils.readSingleEntry(resources, resourcePath)));
assertThat(rClassContent, containsString("app_name"));
assertThat(rClassContent, containsString("foo"));
assertThat(rClassContent, containsString("bar"));
assertTrue(ZipUtils.containsEntry(resources, pngPath));
ResourceTableInspector resourceTableInspector =
new ResourceTableInspector(
ZipUtils.readSingleEntry(testResource.getResourceZip(), resourcePath));
resourceTableInspector.assertContainsResourceWithName("string", "app_name");
resourceTableInspector.assertContainsResourceWithName("drawable", "foo");
resourceTableInspector.assertContainsResourceWithName("drawable", "bar");
}
public static class FooBar {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
}