1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package com.android.tools.layoutlib.create;
19
20
21import static org.junit.Assert.assertArrayEquals;
22
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26
27import java.io.File;
28import java.io.IOException;
29import java.net.URL;
30import java.util.ArrayList;
31import java.util.Set;
32
33/**
34 * Unit tests for some methods of {@link AsmGenerator}.
35 */
36public class AsmGeneratorTest {
37
38    private MockLog mLog;
39    private ArrayList<String> mOsJarPath;
40    private String mOsDestJar;
41    private File mTempFile;
42
43    @Before
44    public void setUp() throws Exception {
45        mLog = new MockLog();
46        URL url = this.getClass().getClassLoader().getResource("data/mock_android.jar");
47
48        mOsJarPath = new ArrayList<String>();
49        mOsJarPath.add(url.getFile());
50
51        mTempFile = File.createTempFile("mock", "jar");
52        mOsDestJar = mTempFile.getAbsolutePath();
53        mTempFile.deleteOnExit();
54    }
55
56    @After
57    public void tearDown() throws Exception {
58        if (mTempFile != null) {
59            mTempFile.delete();
60            mTempFile = null;
61        }
62    }
63
64    @Test
65    public void testClassRenaming() throws IOException, LogAbortException {
66
67        ICreateInfo ci = new ICreateInfo() {
68            @Override
69            public Class<?>[] getInjectedClasses() {
70                // classes to inject in the final JAR
71                return new Class<?>[0];
72            }
73
74            @Override
75            public String[] getDelegateMethods() {
76                return new String[0];
77            }
78
79            @Override
80            public String[] getDelegateClassNatives() {
81                return new String[0];
82            }
83
84            @Override
85            public String[] getOverriddenMethods() {
86                // methods to force override
87                return new String[0];
88            }
89
90            @Override
91            public String[] getRenamedClasses() {
92                // classes to rename (so that we can replace them)
93                return new String[] {
94                        "mock_android.view.View", "mock_android.view._Original_View",
95                        "not.an.actual.ClassName", "anoter.fake.NewClassName",
96                };
97            }
98
99            @Override
100            public String[] getDeleteReturns() {
101                 // methods deleted from their return type.
102                return new String[0];
103            }
104        };
105
106        AsmGenerator agen = new AsmGenerator(mLog, mOsDestJar, ci);
107
108        AsmAnalyzer aa = new AsmAnalyzer(mLog, mOsJarPath, agen,
109                null,                 // derived from
110                new String[] {        // include classes
111                    "**"
112                });
113        aa.analyze();
114        agen.generate();
115
116        Set<String> notRenamed = agen.getClassesNotRenamed();
117        assertArrayEquals(new String[] { "not/an/actual/ClassName" }, notRenamed.toArray());
118    }
119}
120