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