Home | History | Annotate | Download | only in compat
      1 /*
      2  * Copyright (C) 2015 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 package com.android.contacts.compat;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 @SmallTest
     23 public class CompatUtilsTest extends AndroidTestCase {
     24 
     25     public void testIsClassAvailable_NullClassName() {
     26         assertFalse(CompatUtils.isClassAvailable(null));
     27     }
     28 
     29     public void testIsClassAvailable_EmptyClassName() {
     30         assertFalse(CompatUtils.isClassAvailable(""));
     31     }
     32 
     33     public void testIsClassAvailable_NonexistentClass() {
     34         assertFalse(CompatUtils.isClassAvailable("com.android.contacts.common.NonexistentClass"));
     35     }
     36 
     37     public void testIsClassAvailable() {
     38         assertTrue(CompatUtils.isClassAvailable(BaseClass.class.getName()));
     39     }
     40 
     41     public void testIsMethodAvailable_NullClassName() {
     42         assertFalse(CompatUtils.isMethodAvailable(null, "methodName"));
     43     }
     44 
     45     public void testIsMethodAvailable_EmptyClassName() {
     46         assertFalse(CompatUtils.isMethodAvailable("", "methodName"));
     47     }
     48 
     49     public void testIsMethodAvailable_NullMethodName() {
     50         assertFalse(CompatUtils.isMethodAvailable("className", null));
     51     }
     52 
     53     public void testIsMethodAvailable_EmptyMethodName() {
     54         assertFalse(CompatUtils.isMethodAvailable("className", ""));
     55     }
     56 
     57     public void testIsMethodAvailable_NonexistentClass() {
     58         assertFalse(CompatUtils.isMethodAvailable("com.android.contacts.common.NonexistentClass",
     59                 ""));
     60     }
     61 
     62     public void testIsMethodAvailable_NonexistentMethod() {
     63         assertFalse(CompatUtils.isMethodAvailable(BaseClass.class.getName(), "derivedMethod"));
     64     }
     65 
     66     public void testIsMethodAvailable() {
     67         assertTrue(CompatUtils.isMethodAvailable(BaseClass.class.getName(), "baseMethod"));
     68     }
     69 
     70     public void testIsMethodAvailable_InheritedMethod() {
     71         assertTrue(CompatUtils.isMethodAvailable(DerivedClass.class.getName(), "baseMethod"));
     72     }
     73 
     74     public void testIsMethodAvailable_OverloadedMethod() {
     75         assertTrue(CompatUtils.isMethodAvailable(DerivedClass.class.getName(), "overloadedMethod"));
     76         assertTrue(CompatUtils.isMethodAvailable(DerivedClass.class.getName(), "overloadedMethod",
     77                 Integer.TYPE));
     78     }
     79 
     80     public void testIsMethodAvailable_NonexistentOverload() {
     81         assertFalse(CompatUtils.isMethodAvailable(DerivedClass.class.getName(), "overloadedMethod",
     82                 Boolean.TYPE));
     83     }
     84 
     85     public void testInvokeMethod_NullMethodName() {
     86         assertNull(CompatUtils.invokeMethod(new BaseClass(), null, null, null));
     87     }
     88 
     89     public void testInvokeMethod_EmptyMethodName() {
     90         assertNull(CompatUtils.invokeMethod(new BaseClass(), "", null, null));
     91     }
     92 
     93     public void testInvokeMethod_NullClassInstance() {
     94         assertNull(CompatUtils.invokeMethod(null, "", null, null));
     95     }
     96 
     97     public void testInvokeMethod_NonexistentMethod() {
     98         assertNull(CompatUtils.invokeMethod(new BaseClass(), "derivedMethod", null, null));
     99     }
    100 
    101     public void testInvokeMethod_MethodWithNoParameters() {
    102         assertEquals(1, CompatUtils.invokeMethod(new DerivedClass(), "overloadedMethod", null, null));
    103     }
    104 
    105     public void testInvokeMethod_MethodWithNoParameters_WithParameters() {
    106         assertNull(CompatUtils.invokeMethod(new DerivedClass(), "derivedMethod",
    107                 new Class<?>[] {Integer.TYPE}, new Object[] {1}));
    108     }
    109 
    110     public void testInvokeMethod_MethodWithParameters_WithEmptyParameterList() {
    111         assertNull(CompatUtils.invokeMethod(new DerivedClass(), "overloadedMethod",
    112                 new Class<?>[] {Integer.TYPE}, new Object[] {}));
    113     }
    114 
    115     public void testInvokeMethod_InvokeSimpleMethod() {
    116         assertEquals(2, CompatUtils.invokeMethod(new DerivedClass(), "overloadedMethod",
    117                 new Class<?>[] {Integer.TYPE}, new Object[] {2}));
    118     }
    119 
    120     private class BaseClass {
    121         public void baseMethod() {}
    122     }
    123 
    124     private class DerivedClass extends BaseClass {
    125         public int derivedMethod() {
    126             // This method needs to return something to differentiate a successful invocation from
    127             // an unsuccessful one.
    128             return 0;
    129         }
    130 
    131         public int overloadedMethod() {
    132             return 1;
    133         }
    134 
    135         public int overloadedMethod(int i) {
    136             return i;
    137         }
    138     }
    139 }
    140