Home | History | Annotate | Download | only in smalidea
      1 /*
      2  * Copyright 2014, Google Inc.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 package org.jf.smalidea;
     33 
     34 import com.intellij.psi.PsiClass;
     35 import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
     36 import org.jf.smalidea.psi.impl.SmaliClass;
     37 import org.jf.smalidea.psi.impl.SmaliExtendsList;
     38 import org.jf.smalidea.psi.impl.SmaliFile;
     39 import org.jf.smalidea.psi.impl.SmaliImplementsList;
     40 import org.junit.Assert;
     41 
     42 public class SmaliImplementsExtendsTest extends LightCodeInsightFixtureTestCase {
     43     public void testNormalClass() {
     44         myFixture.addFileToProject("my/pkg/base.smali",
     45                 ".class public Lmy/pkg/base; .super Ljava/lang/Object;");
     46         myFixture.addFileToProject("my/pkg/iface.smali",
     47                 ".class public Lmy/pkg/iface; .super Ljava/lang/Object;");
     48         myFixture.addFileToProject("my/pkg/iface2.smali",
     49                 ".class public Lmy/pkg/iface2; .super Ljava/lang/Object;");
     50 
     51         SmaliFile file = (SmaliFile)myFixture.addFileToProject("my/pkg/blah.smali",
     52                 ".class public Lmy/pkg/blah; .implements Lmy/pkg/iface; .super Lmy/pkg/base; " +
     53                 ".implements Lmy/pkg/iface2;");
     54 
     55         SmaliClass smaliClass = file.getPsiClass();
     56         SmaliExtendsList extendsList = smaliClass.getExtendsList();
     57         Assert.assertEquals(1, extendsList.getReferencedTypes().length);
     58         Assert.assertEquals("my.pkg.base", extendsList.getReferencedTypes()[0].getCanonicalText());
     59         Assert.assertEquals(1, extendsList.getReferenceNames().length);
     60         Assert.assertEquals("my.pkg.base", extendsList.getReferenceNames()[0]);
     61         Assert.assertEquals(1, smaliClass.getExtendsListTypes().length);
     62         Assert.assertEquals("my.pkg.base", smaliClass.getExtendsListTypes()[0].getCanonicalText());
     63 
     64         PsiClass resolvedSuper = extendsList.getReferencedTypes()[0].resolve();
     65         Assert.assertNotNull(resolvedSuper);
     66         Assert.assertEquals("my.pkg.base", resolvedSuper.getQualifiedName());
     67 
     68         SmaliImplementsList implementsList = smaliClass.getImplementsList();
     69         Assert.assertEquals(2, implementsList.getReferencedTypes().length);
     70         Assert.assertEquals("my.pkg.iface", implementsList.getReferencedTypes()[0].getCanonicalText());
     71         Assert.assertEquals("my.pkg.iface2", implementsList.getReferencedTypes()[1].getCanonicalText());
     72         Assert.assertEquals(2, implementsList.getReferenceNames().length);
     73         Assert.assertEquals("my.pkg.iface", implementsList.getReferenceNames()[0]);
     74         Assert.assertEquals("my.pkg.iface2", implementsList.getReferenceNames()[1]);
     75         Assert.assertEquals(2, smaliClass.getImplementsListTypes().length);
     76         Assert.assertEquals("my.pkg.iface", smaliClass.getImplementsListTypes()[0].getCanonicalText());
     77         Assert.assertEquals("my.pkg.iface2", smaliClass.getImplementsListTypes()[1].getCanonicalText());
     78 
     79         PsiClass resolvedInterface = implementsList.getReferencedTypes()[0].resolve();
     80         Assert.assertNotNull(resolvedInterface);
     81         Assert.assertEquals("my.pkg.iface", resolvedInterface.getQualifiedName());
     82 
     83         resolvedInterface = implementsList.getReferencedTypes()[1].resolve();
     84         Assert.assertNotNull(resolvedInterface);
     85         Assert.assertEquals("my.pkg.iface2", resolvedInterface.getQualifiedName());
     86     }
     87 
     88     public void testInterface() {
     89         myFixture.addFileToProject("my/pkg/iface.smali",
     90                 ".class public Lmy/pkg/iface; .super Ljava/lang/Object;");
     91         myFixture.addFileToProject("my/pkg/iface2.smali",
     92                 ".class public Lmy/pkg/iface2; .super Ljava/lang/Object;");
     93 
     94         SmaliFile file = (SmaliFile)myFixture.addFileToProject("my/pkg/blah.smali",
     95                 ".class public interface Lmy/pkg/blah; .implements Lmy/pkg/iface; .super Ljava/lang/Object; " +
     96                         ".implements Lmy/pkg/iface2;");
     97 
     98         SmaliClass smaliClass = file.getPsiClass();
     99         SmaliExtendsList extendsList = smaliClass.getExtendsList();
    100 
    101         Assert.assertEquals(2, extendsList.getReferencedTypes().length);
    102         Assert.assertEquals("my.pkg.iface", extendsList.getReferencedTypes()[0].getCanonicalText());
    103         Assert.assertEquals("my.pkg.iface2", extendsList.getReferencedTypes()[1].getCanonicalText());
    104         Assert.assertEquals(2, extendsList.getReferenceNames().length);
    105         Assert.assertEquals("my.pkg.iface", extendsList.getReferenceNames()[0]);
    106         Assert.assertEquals("my.pkg.iface2", extendsList.getReferenceNames()[1]);
    107         Assert.assertEquals(2, smaliClass.getExtendsListTypes().length);
    108         Assert.assertEquals("my.pkg.iface", smaliClass.getExtendsListTypes()[0].getCanonicalText());
    109         Assert.assertEquals("my.pkg.iface2", smaliClass.getExtendsListTypes()[1].getCanonicalText());
    110 
    111         PsiClass resolvedInterface = extendsList.getReferencedTypes()[0].resolve();
    112         Assert.assertNotNull(resolvedInterface);
    113         Assert.assertEquals("my.pkg.iface", resolvedInterface.getQualifiedName());
    114 
    115         resolvedInterface = extendsList.getReferencedTypes()[1].resolve();
    116         Assert.assertNotNull(resolvedInterface);
    117         Assert.assertEquals("my.pkg.iface2", resolvedInterface.getQualifiedName());
    118 
    119         SmaliImplementsList implementsList = smaliClass.getImplementsList();
    120         Assert.assertEquals(0, implementsList.getReferencedTypes().length);
    121         Assert.assertEquals(0, implementsList.getReferenceNames().length);
    122         Assert.assertEquals(0, smaliClass.getImplementsListTypes().length);
    123     }
    124 }
    125