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            public Class<?>[] getInjectedClasses() {
69                // classes to inject in the final JAR
70                return new Class<?>[0];
71            }
72
73            public String[] getDelegateMethods() {
74                return new String[0];
75            }
76
77            public String[] getDelegateClassNatives() {
78                return new String[0];
79            }
80
81            public String[] getOverriddenMethods() {
82                // methods to force override
83                return new String[0];
84            }
85
86            public String[] getRenamedClasses() {
87                // classes to rename (so that we can replace them)
88                return new String[] {
89                        "mock_android.view.View", "mock_android.view._Original_View",
90                        "not.an.actual.ClassName", "anoter.fake.NewClassName",
91                };
92            }
93
94            public String[] getDeleteReturns() {
95                 // methods deleted from their return type.
96                return new String[0];
97            }
98        };
99
100        AsmGenerator agen = new AsmGenerator(mLog, mOsDestJar, ci);
101
102        AsmAnalyzer aa = new AsmAnalyzer(mLog, mOsJarPath, agen,
103                null,                 // derived from
104                new String[] {        // include classes
105                    "**"
106                });
107        aa.analyze();
108        agen.generate();
109
110        Set<String> notRenamed = agen.getClassesNotRenamed();
111        assertArrayEquals(new String[] { "not/an/actual/ClassName" }, notRenamed.toArray());
112    }
113}
114