• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 
5 package com.android.tools.r8.smali;
6 
7 import static org.junit.Assert.assertEquals;
8 
9 import com.android.tools.r8.ToolHelper;
10 import com.android.tools.r8.graph.DexApplication;
11 import com.android.tools.r8.utils.AndroidApp;
12 import com.android.tools.r8.utils.DexInspector;
13 import com.android.tools.r8.utils.DexInspector.ClassSubject;
14 import com.android.tools.r8.utils.InternalOptions;
15 import java.io.IOException;
16 import java.nio.file.Paths;
17 import org.antlr.runtime.RecognitionException;
18 import org.junit.Test;
19 
20 public class SmaliBuildTest extends SmaliTestBase {
21 
checkJavaLangString(DexApplication application, boolean present)22   private void checkJavaLangString(DexApplication application, boolean present) {
23     DexInspector inspector = new DexInspector(application);
24     ClassSubject clazz = inspector.clazz("java.lang.String");
25     assertEquals(present, clazz.isPresent());
26   }
27 
28   @Test
buildWithoutLibrary()29   public void buildWithoutLibrary() {
30     // Build simple "Hello, world!" application.
31     SmaliBuilder builder = new SmaliBuilder(DEFAULT_CLASS_NAME);
32     builder.addMainMethod(
33         2,
34         "    sget-object         v0, Ljava/lang/System;->out:Ljava/io/PrintStream;",
35         "    const-string        v1, \"Hello, world!\"",
36         "    invoke-virtual      { v0, v1 }, Ljava/io/PrintStream;->println(Ljava/lang/String;)V",
37         "    return-void"
38     );
39 
40     InternalOptions options = new InternalOptions();
41     // No libraries added - java.lang.String is not present.
42     DexApplication originalApplication = buildApplication(builder, options);
43     checkJavaLangString(originalApplication, false);
44 
45     DexApplication processedApplication = processApplication(originalApplication, options);
46     checkJavaLangString(processedApplication, false);
47   }
48 
49   @Test
buildWithLibrary()50   public void buildWithLibrary() throws IOException, RecognitionException {
51     // Build simple "Hello, world!" application.
52     SmaliBuilder builder = new SmaliBuilder(DEFAULT_CLASS_NAME);
53     builder.addMainMethod(
54         2,
55         "    sget-object         v0, Ljava/lang/System;->out:Ljava/io/PrintStream;",
56         "    const-string        v1, \"Hello, world!\"",
57         "    invoke-virtual      { v0, v1 }, Ljava/io/PrintStream;->println(Ljava/lang/String;)V",
58         "    return-void"
59     );
60 
61     InternalOptions options = new InternalOptions();
62     AndroidApp app = AndroidApp.builder()
63         .addDexProgramData(builder.compile())
64         .addLibraryFiles(Paths.get(ToolHelper.getDefaultAndroidJar()))
65         .build();
66 
67     // Java standard library added - java.lang.String is present.
68     DexApplication originalApplication = buildApplication(app, options);
69     checkJavaLangString(originalApplication, true);
70 
71     DexApplication processedApplication = processApplication(originalApplication, options);
72     checkJavaLangString(processedApplication, true);
73   }
74 }
75