FileDocCategorySizeDatePackage
AsmAnalyzerTest.javaAPI DocAndroid 1.5 API8559Wed May 06 22:42:02 BST 2009com.android.tools.layoutlib.create

AsmAnalyzerTest

public class AsmAnalyzerTest extends Object
Unit tests for some methods of {@link AsmAnalyzer}.

Fields Summary
private com.android.tools.layoutlib.create.LogTest.MockLog
mLog
private ArrayList
mOsJarPath
private AsmAnalyzer
mAa
Constructors Summary
Methods Summary
public voidsetUp()

        mLog = new LogTest.MockLog();
        URL url = this.getClass().getClassLoader().getResource("data/mock_android.jar");
        
        mOsJarPath = new ArrayList<String>();
        mOsJarPath.add(url.getFile());

        mAa = new AsmAnalyzer(mLog, mOsJarPath, null /* gen */,
                null /* deriveFrom */, null /* includeGlobs */ );
    
public voidtearDown()

    
public voidtestDependencyVisitor()

        Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
        TreeMap<String, ClassReader> keep = new TreeMap<String, ClassReader>();
        TreeMap<String, ClassReader> new_keep = new TreeMap<String, ClassReader>();
        TreeMap<String, ClassReader> in_deps = new TreeMap<String, ClassReader>();
        TreeMap<String, ClassReader> out_deps = new TreeMap<String, ClassReader>();

        ClassReader cr = mAa.findClass("mock_android.widget.TableLayout", zipClasses, keep);
        DependencyVisitor visitor = mAa.getVisitor(zipClasses, keep, new_keep, in_deps, out_deps);
        
        // get first level dependencies
        cr.accept(visitor, 0 /* flags */);

        assertArrayEquals(new String[] {
                "mock_android.view.ViewGroup",
                "mock_android.widget.TableLayout$LayoutParams",
            },
            out_deps.keySet().toArray());

        in_deps.putAll(out_deps);
        out_deps.clear();

        // get second level dependencies
        for (ClassReader cr2 : in_deps.values()) {
            cr2.accept(visitor, 0 /* flags */);
        }

        assertArrayEquals(new String[] {
                "mock_android.view.View",
                "mock_android.view.ViewGroup$LayoutParams",
                "mock_android.view.ViewGroup$MarginLayoutParams",
            },
            out_deps.keySet().toArray());

        in_deps.putAll(out_deps);
        out_deps.clear();

        // get third level dependencies (there are none)
        for (ClassReader cr2 : in_deps.values()) {
            cr2.accept(visitor, 0 /* flags */);
        }

        assertArrayEquals(new String[] { }, out_deps.keySet().toArray());
    
public voidtestFindClass()

        Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
        TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();

        ClassReader cr = mAa.findClass("mock_android.view.ViewGroup$LayoutParams",
                zipClasses, found);
        
        assertNotNull(cr);
        assertEquals("mock_android/view/ViewGroup$LayoutParams", cr.getClassName());
        assertArrayEquals(new String[] { "mock_android.view.ViewGroup$LayoutParams" },
                found.keySet().toArray());
        assertArrayEquals(new ClassReader[] { cr }, found.values().toArray());
    
public voidtestFindClassesDerivingFrom()

        Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
        TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();

        mAa.findClassesDerivingFrom("mock_android.view.View", zipClasses, found);

        assertArrayEquals(new String[] {
                "mock_android.view.View",
                "mock_android.view.ViewGroup",
                "mock_android.widget.LinearLayout",
                "mock_android.widget.TableLayout",
            },
            found.keySet().toArray());
        
        for (String key : found.keySet()) {
            ClassReader value = found.get(key);
            assertNotNull("No value for " + key, value);
            assertEquals(key, AsmAnalyzer.classReaderToClassName(value));
        }
    
public voidtestFindGlobs()

        Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
        TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();

        // this matches classes, a package match returns nothing
        found.clear();
        mAa.findGlobs("mock_android.view", zipClasses, found);

        assertArrayEquals(new String[] { },
            found.keySet().toArray());

        // a complex glob search. * is a search pattern that matches names, not dots
        mAa.findGlobs("mock_android.*.*Group$*Layout*", zipClasses, found);

        assertArrayEquals(new String[] {
                "mock_android.view.ViewGroup$LayoutParams",
                "mock_android.view.ViewGroup$MarginLayoutParams"
            },
            found.keySet().toArray());

        // a complex glob search. ** is a search pattern that matches names including dots
        mAa.findGlobs("mock_android.**Group*", zipClasses, found);

        assertArrayEquals(new String[] {
                "mock_android.view.ViewGroup",
                "mock_android.view.ViewGroup$LayoutParams",
                "mock_android.view.ViewGroup$MarginLayoutParams"
            },
            found.keySet().toArray());

        // matches a single class
        found.clear();
        mAa.findGlobs("mock_android.view.View", zipClasses, found);

        assertArrayEquals(new String[] {
                "mock_android.view.View"
            },
            found.keySet().toArray());

        // matches everyting inside the given package but not sub-packages
        found.clear();
        mAa.findGlobs("mock_android.view.*", zipClasses, found);

        assertArrayEquals(new String[] {
                "mock_android.view.View",
                "mock_android.view.ViewGroup",
                "mock_android.view.ViewGroup$LayoutParams",
                "mock_android.view.ViewGroup$MarginLayoutParams"
            },
            found.keySet().toArray());

        for (String key : found.keySet()) {
            ClassReader value = found.get(key);
            assertNotNull("No value for " + key, value);
            assertEquals(key, AsmAnalyzer.classReaderToClassName(value));
        }
    
public voidtestParseZip()

        Map<String, ClassReader> map = mAa.parseZip(mOsJarPath);

        assertArrayEquals(new String[] {
                "mock_android.dummy.InnerTest",
                "mock_android.dummy.InnerTest$DerivingClass",
                "mock_android.dummy.InnerTest$MyGenerics1",
                "mock_android.dummy.InnerTest$MyIntEnum",
                "mock_android.dummy.InnerTest$MyStaticInnerClass",   
                "mock_android.dummy.InnerTest$NotStaticInner1", 
                "mock_android.dummy.InnerTest$NotStaticInner2",  
                "mock_android.view.View",
                "mock_android.view.ViewGroup",
                "mock_android.view.ViewGroup$LayoutParams",
                "mock_android.view.ViewGroup$MarginLayoutParams",
                "mock_android.widget.LinearLayout",
                "mock_android.widget.LinearLayout$LayoutParams",
                "mock_android.widget.TableLayout",
                "mock_android.widget.TableLayout$LayoutParams"
            },
            map.keySet().toArray());