| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.content.browser; | |
| 6 | |
| 7 import android.test.InstrumentationTestCase; | |
| 8 import android.test.suitebuilder.annotation.SmallTest; | |
| 9 | |
| 10 import org.chromium.base.test.util.Feature; | |
| 11 import org.chromium.content.browser.DownloadInfo.Builder; | |
| 12 | |
| 13 import java.lang.reflect.InvocationTargetException; | |
| 14 import java.lang.reflect.Method; | |
| 15 import java.util.HashMap; | |
| 16 import java.util.Map; | |
| 17 import java.util.Random; | |
| 18 | |
| 19 /** | |
| 20 * Tests for (@link DownloadInfo}. | |
| 21 */ | |
| 22 public class DownloadInfoTest extends InstrumentationTestCase { | |
| 23 /** | |
| 24 * Class to store getter/setter information. Stores the method name of a get
ter and setter | |
| 25 * without prefix, so "int getFoo()" and "void setFoo(int)" both have same A
ccessorSignature | |
| 26 * {"Foo", int}. | |
| 27 */ | |
| 28 static class AccessorSignature { | |
| 29 final String mMethodNameWithoutPrefix; | |
| 30 final Class<?> mReturnTypeOrParam; | |
| 31 | |
| 32 AccessorSignature(String methodNameWithoutPrefix, Class<?> returnTypeOrP
aram) { | |
| 33 mMethodNameWithoutPrefix = methodNameWithoutPrefix; | |
| 34 mReturnTypeOrParam = returnTypeOrParam; | |
| 35 } | |
| 36 | |
| 37 @Override | |
| 38 public int hashCode() { | |
| 39 return mMethodNameWithoutPrefix.hashCode() * 31 | |
| 40 + mReturnTypeOrParam.hashCode(); | |
| 41 } | |
| 42 | |
| 43 @Override | |
| 44 public boolean equals(Object obj) { | |
| 45 if (obj == this) { | |
| 46 return true; | |
| 47 } | |
| 48 if (obj instanceof AccessorSignature) { | |
| 49 AccessorSignature other = (AccessorSignature) obj; | |
| 50 return other.mReturnTypeOrParam == mReturnTypeOrParam | |
| 51 && other.mMethodNameWithoutPrefix.equals(mMethodNameWitho
utPrefix); | |
| 52 } | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 @Override | |
| 57 public String toString() { | |
| 58 return "{ " + mMethodNameWithoutPrefix + ", " + mReturnTypeOrParam.g
etName() + "}"; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * Returns an AccessorInfo object for the method if it is a getter. A getter
for the class has | |
| 64 * signature: Type getFoo(), where Type is a String or a primitive type othe
r than boolean. | |
| 65 * Boolean getters are an exception and have special prefixes. In case of a
boolean getter, the | |
| 66 * signature is 'boolean isFoo()' or 'boolean hasFoo', i.e. boolean getters
start with "is" or | |
| 67 * "has". | |
| 68 * @param method the method that is a getter | |
| 69 * @return AccessorInfo for the method if it is a getter, null otherwise. | |
| 70 */ | |
| 71 AccessorSignature getGetterInfo(Method method) { | |
| 72 // A getter is of format Type getFoo(): i.e. 0 params and one | |
| 73 // return type. | |
| 74 if (method.getParameterTypes().length == 0) { | |
| 75 // Based on return type extract the name of the getter. | |
| 76 Class<?> returnType = method.getReturnType(); | |
| 77 if (returnType.isPrimitive() || returnType == String.class) { | |
| 78 String methodName = method.getName(); | |
| 79 if (returnType.equals(Boolean.TYPE)) { | |
| 80 if (methodName.matches("(is|has).*")) { | |
| 81 return new AccessorSignature(methodName.replaceFirst("is
|has", ""), | |
| 82 returnType); | |
| 83 } | |
| 84 } else { | |
| 85 if (methodName.startsWith("get")) { | |
| 86 return new AccessorSignature(methodName.substring(3), re
turnType); | |
| 87 } | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 return null; | |
| 92 } | |
| 93 | |
| 94 /** | |
| 95 * Returns an AccessorInfo object for the method if it is a setter. A setter
for the class has | |
| 96 * signature: Type setFoo(), where Type is a String or a primitive type. | |
| 97 * @param method the method that is a getter | |
| 98 * @return AccessorInfo for the method if it is a getter, null otherwise. | |
| 99 */ | |
| 100 AccessorSignature getSetterInfo(Method method) { | |
| 101 if (method.getParameterTypes().length == 1) { | |
| 102 Class<?> parameter = method.getParameterTypes()[0]; | |
| 103 String methodName = method.getName(); | |
| 104 if (methodName.startsWith("set")) { | |
| 105 if (parameter.equals(Boolean.TYPE)) { | |
| 106 // Boolean setters are of form setIsFoo or setHasFoo. | |
| 107 return new AccessorSignature( | |
| 108 methodName.replaceFirst("set(Is|Has)", ""), | |
| 109 parameter); | |
| 110 } else { | |
| 111 return new AccessorSignature(methodName.substring(3), parame
ter); | |
| 112 } | |
| 113 } | |
| 114 } | |
| 115 return null; | |
| 116 } | |
| 117 | |
| 118 /** | |
| 119 * Invoke a method via reflection and rethrow the exception. Makes findbugs
happy. | |
| 120 * @param method Method to invoke. | |
| 121 * @param instance class instance on which method should be invoked. | |
| 122 * @return return value of invocation. | |
| 123 */ | |
| 124 Object invokeMethod(Method method, Object instance, Object... args) throws E
xception { | |
| 125 try { | |
| 126 return method.invoke(instance, args); | |
| 127 } catch (IllegalArgumentException e) { | |
| 128 throw e; | |
| 129 } catch (IllegalAccessException e) { | |
| 130 throw e; | |
| 131 } catch (InvocationTargetException e) { | |
| 132 throw e; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 @SmallTest | |
| 137 @Feature({"Downloads"}) | |
| 138 public void testBuilderHasCorrectSetters() { | |
| 139 HashMap<AccessorSignature, Method> downloadInfoGetters = | |
| 140 new HashMap<AccessorSignature, Method>(); | |
| 141 HashMap<AccessorSignature, Method> builderSetters = | |
| 142 new HashMap<AccessorSignature, Method>(); | |
| 143 for (Method m : DownloadInfo.class.getMethods()) { | |
| 144 AccessorSignature info = getGetterInfo(m); | |
| 145 if (info != null) { | |
| 146 downloadInfoGetters.put(info, m); | |
| 147 } | |
| 148 } | |
| 149 assertTrue("There should be at least one getter.", | |
| 150 downloadInfoGetters.size() > 0); | |
| 151 for (Method m : Builder.class.getMethods()) { | |
| 152 AccessorSignature info = getSetterInfo(m); | |
| 153 if (info != null) { | |
| 154 builderSetters.put(info, m); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 // Make sure we have a setter for each getter and vice versa. | |
| 159 assertEquals("Mismatch between getters and setters.", | |
| 160 downloadInfoGetters.keySet(), builderSetters.keySet()); | |
| 161 | |
| 162 // Generate specific values for fields and verify that they are correctl
y set. For boolean | |
| 163 // fields set them all to true. For integers generate random numbers. | |
| 164 Random random = new Random(); | |
| 165 HashMap<AccessorSignature, Object> valuesForBuilder = | |
| 166 new HashMap<DownloadInfoTest.AccessorSignature, Object>(); | |
| 167 for (AccessorSignature signature : builderSetters.keySet()) { | |
| 168 if (signature.mReturnTypeOrParam.equals(String.class)) { | |
| 169 String value = signature.mMethodNameWithoutPrefix | |
| 170 + Integer.toString(random.nextInt()); | |
| 171 valuesForBuilder.put(signature, value); | |
| 172 } else if (signature.mReturnTypeOrParam.equals(Boolean.TYPE)) { | |
| 173 valuesForBuilder.put(signature, Boolean.TRUE); | |
| 174 } else { | |
| 175 // This is a primitive type that is not boolean, probably an int
eger. | |
| 176 valuesForBuilder.put(signature, Integer.valueOf(random.nextInt(1
00))); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 Builder builder = new Builder(); | |
| 181 // Create a DownloadInfo object with these values. | |
| 182 for (Map.Entry<AccessorSignature, Method> builderSetter : builderSetters
.entrySet()) { | |
| 183 AccessorSignature signature = builderSetter.getKey(); | |
| 184 Method setter = builderSetter.getValue(); | |
| 185 try { | |
| 186 invokeMethod(setter, builder, valuesForBuilder.get(signature)); | |
| 187 } catch (Exception e) { | |
| 188 fail("Exception while setting value in the setter. Signature: "
+ signature | |
| 189 + " value:" + valuesForBuilder.get(signature) + ":" + e)
; | |
| 190 } | |
| 191 } | |
| 192 DownloadInfo downloadInfo = builder.build(); | |
| 193 for (Map.Entry<AccessorSignature, Method> downloadInfoGetter : | |
| 194 downloadInfoGetters.entrySet()) { | |
| 195 AccessorSignature signature = downloadInfoGetter.getKey(); | |
| 196 Method getter = downloadInfoGetter.getValue(); | |
| 197 try { | |
| 198 Object returnValue = invokeMethod(getter, downloadInfo); | |
| 199 assertEquals(signature.toString(), | |
| 200 valuesForBuilder.get(signature).toString(), returnValue.
toString()); | |
| 201 } catch (Exception e) { | |
| 202 fail("Exception while getting value from getter. Signature: " +
signature | |
| 203 + " value:" + valuesForBuilder.get(signature)); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 // Test DownloadInfo.fromDownloadInfo copies all fields. | |
| 208 DownloadInfo newDownloadInfo = Builder.fromDownloadInfo(downloadInfo).bu
ild(); | |
| 209 for (Map.Entry<AccessorSignature, Method> downloadInfoGetter : | |
| 210 downloadInfoGetters.entrySet()) { | |
| 211 AccessorSignature signature = downloadInfoGetter.getKey(); | |
| 212 Method getter = downloadInfoGetter.getValue(); | |
| 213 try { | |
| 214 Object returnValue1 = invokeMethod(getter, downloadInfo); | |
| 215 Object returnValue2 = invokeMethod(getter, newDownloadInfo); | |
| 216 assertEquals(signature.toString(), returnValue1, returnValue2); | |
| 217 } catch (Exception e) { | |
| 218 fail("Exception while getting value from getter. Signature: " +
signature | |
| 219 + " value:" + valuesForBuilder.get(signature) + ":" + e)
; | |
| 220 } | |
| 221 } | |
| 222 } | |
| 223 } | |
| OLD | NEW |