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.openapi.projectRoots.Sdk;
     35 import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl;
     36 import com.intellij.psi.JavaResolveResult;
     37 import com.intellij.psi.PsiClass;
     38 import com.intellij.psi.PsiReference;
     39 import com.intellij.testFramework.ResolveTestCase;
     40 import org.jf.smalidea.psi.impl.SmaliClass;
     41 import org.jf.smalidea.psi.impl.SmaliClassTypeElement;
     42 import org.junit.Assert;
     43 
     44 public class ClassReferenceTest extends ResolveTestCase {
     45     /**
     46      * Test a reference to a java class from a smali class
     47      */
     48     public void testJavaReferenceFromSmali() throws Exception {
     49         SmaliClassTypeElement typeElement = (SmaliClassTypeElement)configureByFileText(
     50                 ".class public Lblah; .super L<ref>java/lang/Object;", "blah.smali");
     51 
     52         Assert.assertNotNull(typeElement);
     53         Assert.assertEquals("Object", typeElement.getName());
     54 
     55         PsiClass psiClass = typeElement.resolve();
     56         Assert.assertNotNull(psiClass);
     57         Assert.assertEquals("java.lang.Object", psiClass.getQualifiedName());
     58 
     59         JavaResolveResult resolveResult = typeElement.advancedResolve(false);
     60         Assert.assertNotNull(resolveResult.getElement());
     61         Assert.assertEquals("java.lang.Object", ((PsiClass)resolveResult.getElement()).getQualifiedName());
     62 
     63         JavaResolveResult[] resolveResults = typeElement.multiResolve(false);
     64         Assert.assertEquals(1, resolveResults.length);
     65         Assert.assertNotNull(resolveResults[0].getElement());
     66         Assert.assertEquals("java.lang.Object", ((PsiClass)resolveResults[0].getElement()).getQualifiedName());
     67     }
     68 
     69     /**
     70      * Test a reference to a smali class from a smali class
     71      */
     72     public void testSmaliReferenceFromSmali() throws Exception {
     73         createFile("blarg.smali", ".class public Lblarg; .super Ljava/lang/Object;");
     74 
     75         SmaliClassTypeElement typeElement = (SmaliClassTypeElement)configureByFileText(
     76                 ".class public Lblah; .super L<ref>blarg;", "blah.smali");
     77 
     78         Assert.assertEquals("blarg", typeElement.getName());
     79 
     80         SmaliClass smaliClass = (SmaliClass)typeElement.resolve();
     81         Assert.assertNotNull(smaliClass);
     82         Assert.assertEquals("blarg", smaliClass.getQualifiedName());
     83 
     84         JavaResolveResult resolveResult = typeElement.advancedResolve(false);
     85         Assert.assertNotNull(resolveResult.getElement());
     86         Assert.assertEquals("blarg", ((PsiClass)resolveResult.getElement()).getQualifiedName());
     87 
     88         JavaResolveResult[] resolveResults = typeElement.multiResolve(false);
     89         Assert.assertEquals(1, resolveResults.length);
     90         Assert.assertNotNull(resolveResults[0].getElement());
     91         Assert.assertEquals("blarg", ((PsiClass)resolveResults[0].getElement()).getQualifiedName());
     92     }
     93 
     94     /**
     95      * Test a reference to a smali class from a java class
     96      */
     97     public void testSmaliReferenceFromJava() throws Exception {
     98         createFile("blarg.smali", ".class public Lblarg; .super Ljava/lang/Object;");
     99 
    100         PsiReference reference = configureByFileText(
    101                 "public class blah extends bla<ref>rg { }", "blah.java");
    102 
    103         SmaliClass smaliClass = (SmaliClass)reference.resolve();
    104         Assert.assertNotNull(smaliClass);
    105         Assert.assertEquals("blarg", smaliClass.getQualifiedName());
    106     }
    107 
    108     @Override
    109     protected Sdk getTestProjectJdk() {
    110         return JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
    111     }
    112 }
    113