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 testIsClassAvailable_NullClassName()25 public void testIsClassAvailable_NullClassName() { 26 assertFalse(CompatUtils.isClassAvailable(null)); 27 } 28 testIsClassAvailable_EmptyClassName()29 public void testIsClassAvailable_EmptyClassName() { 30 assertFalse(CompatUtils.isClassAvailable("")); 31 } 32 testIsClassAvailable_NonexistentClass()33 public void testIsClassAvailable_NonexistentClass() { 34 assertFalse(CompatUtils.isClassAvailable("com.android.contacts.common.NonexistentClass")); 35 } 36 testIsClassAvailable()37 public void testIsClassAvailable() { 38 assertTrue(CompatUtils.isClassAvailable(BaseClass.class.getName())); 39 } 40 testIsMethodAvailable_NullClassName()41 public void testIsMethodAvailable_NullClassName() { 42 assertFalse(CompatUtils.isMethodAvailable(null, "methodName")); 43 } 44 testIsMethodAvailable_EmptyClassName()45 public void testIsMethodAvailable_EmptyClassName() { 46 assertFalse(CompatUtils.isMethodAvailable("", "methodName")); 47 } 48 testIsMethodAvailable_NullMethodName()49 public void testIsMethodAvailable_NullMethodName() { 50 assertFalse(CompatUtils.isMethodAvailable("className", null)); 51 } 52 testIsMethodAvailable_EmptyMethodName()53 public void testIsMethodAvailable_EmptyMethodName() { 54 assertFalse(CompatUtils.isMethodAvailable("className", "")); 55 } 56 testIsMethodAvailable_NonexistentClass()57 public void testIsMethodAvailable_NonexistentClass() { 58 assertFalse(CompatUtils.isMethodAvailable("com.android.contacts.common.NonexistentClass", 59 "")); 60 } 61 testIsMethodAvailable_NonexistentMethod()62 public void testIsMethodAvailable_NonexistentMethod() { 63 assertFalse(CompatUtils.isMethodAvailable(BaseClass.class.getName(), "derivedMethod")); 64 } 65 testIsMethodAvailable()66 public void testIsMethodAvailable() { 67 assertTrue(CompatUtils.isMethodAvailable(BaseClass.class.getName(), "baseMethod")); 68 } 69 testIsMethodAvailable_InheritedMethod()70 public void testIsMethodAvailable_InheritedMethod() { 71 assertTrue(CompatUtils.isMethodAvailable(DerivedClass.class.getName(), "baseMethod")); 72 } 73 testIsMethodAvailable_OverloadedMethod()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 testIsMethodAvailable_NonexistentOverload()80 public void testIsMethodAvailable_NonexistentOverload() { 81 assertFalse(CompatUtils.isMethodAvailable(DerivedClass.class.getName(), "overloadedMethod", 82 Boolean.TYPE)); 83 } 84 testInvokeMethod_NullMethodName()85 public void testInvokeMethod_NullMethodName() { 86 assertNull(CompatUtils.invokeMethod(new BaseClass(), null, null, null)); 87 } 88 testInvokeMethod_EmptyMethodName()89 public void testInvokeMethod_EmptyMethodName() { 90 assertNull(CompatUtils.invokeMethod(new BaseClass(), "", null, null)); 91 } 92 testInvokeMethod_NullClassInstance()93 public void testInvokeMethod_NullClassInstance() { 94 assertNull(CompatUtils.invokeMethod(null, "", null, null)); 95 } 96 testInvokeMethod_NonexistentMethod()97 public void testInvokeMethod_NonexistentMethod() { 98 assertNull(CompatUtils.invokeMethod(new BaseClass(), "derivedMethod", null, null)); 99 } 100 testInvokeMethod_MethodWithNoParameters()101 public void testInvokeMethod_MethodWithNoParameters() { 102 assertEquals(1, CompatUtils.invokeMethod(new DerivedClass(), "overloadedMethod", null, null)); 103 } 104 testInvokeMethod_MethodWithNoParameters_WithParameters()105 public void testInvokeMethod_MethodWithNoParameters_WithParameters() { 106 assertNull(CompatUtils.invokeMethod(new DerivedClass(), "derivedMethod", 107 new Class<?>[] {Integer.TYPE}, new Object[] {1})); 108 } 109 testInvokeMethod_MethodWithParameters_WithEmptyParameterList()110 public void testInvokeMethod_MethodWithParameters_WithEmptyParameterList() { 111 assertNull(CompatUtils.invokeMethod(new DerivedClass(), "overloadedMethod", 112 new Class<?>[] {Integer.TYPE}, new Object[] {})); 113 } 114 testInvokeMethod_InvokeSimpleMethod()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 { baseMethod()121 public void baseMethod() {} 122 } 123 124 private class DerivedClass extends BaseClass { derivedMethod()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 overloadedMethod()131 public int overloadedMethod() { 132 return 1; 133 } 134 overloadedMethod(int i)135 public int overloadedMethod(int i) { 136 return i; 137 } 138 } 139 } 140