1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package android.startop.test;
16 
17 import android.content.Context;
18 import androidx.test.InstrumentationRegistry;
19 import android.view.View;
20 import dalvik.system.PathClassLoader;
21 import java.lang.reflect.Method;
22 import org.junit.Assert;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 
26 public class ApkLayoutCompilerTest {
loadDexFile()27     static ClassLoader loadDexFile() throws Exception {
28         Context context = InstrumentationRegistry.getTargetContext();
29         return new PathClassLoader(context.getCodeCacheDir() + "/compiled_view.dex",
30                 ClassLoader.getSystemClassLoader());
31     }
32 
33     @BeforeClass
setup()34     public static void setup() throws Exception {
35         // ensure PackageManager has compiled the layouts.
36         Process pm = Runtime.getRuntime().exec("pm compile --compile-layouts android.startop.test");
37         pm.waitFor();
38     }
39 
40     @Test
loadAndInflateLayout1()41     public void loadAndInflateLayout1() throws Exception {
42         ClassLoader dex_file = loadDexFile();
43         Class compiled_view = dex_file.loadClass("android.startop.test.CompiledView");
44         Method layout1 = compiled_view.getMethod("layout1", Context.class, int.class);
45         Context context = InstrumentationRegistry.getTargetContext();
46         layout1.invoke(null, context, R.layout.layout1);
47     }
48 
49     @Test
loadAndInflateLayout2()50     public void loadAndInflateLayout2() throws Exception {
51         ClassLoader dex_file = loadDexFile();
52         Class compiled_view = dex_file.loadClass("android.startop.test.CompiledView");
53         Method layout2 = compiled_view.getMethod("layout2", Context.class, int.class);
54         Context context = InstrumentationRegistry.getTargetContext();
55         layout2.invoke(null, context, R.layout.layout2);
56     }
57 }
58