• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.compatibility.common.util;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 import android.util.Log;
22 
23 import java.lang.reflect.Method;
24 import java.util.Arrays;
25 import java.util.List;
26 
27 /**
28  * Execute business logic methods for device side test cases
29  */
30 public class BusinessLogicDeviceExecutor extends BusinessLogicExecutor {
31 
32     private Context mContext;
33     private Object mTestObj;
34 
BusinessLogicDeviceExecutor(Context context, Object testObj, List<String> redactionRegexes)35     public BusinessLogicDeviceExecutor(Context context, Object testObj, List<String> redactionRegexes) {
36         mContext = context;
37         mTestObj = testObj;
38         mRedactionRegexes = redactionRegexes;
39     }
40 
41     /**
42      * {@inheritDoc}
43      */
44     @Override
getTestObject()45     protected Object getTestObject() {
46         return mTestObj;
47     }
48 
49     /**
50      * {@inheritDoc}
51      */
52     @Override
logInfo(String format, Object... args)53     public void logInfo(String format, Object... args) {
54         Log.i(LOG_TAG, String.format(format, args));
55     }
56 
57     /**
58      * {@inheritDoc}
59      */
60     @Override
logDebug(String format, Object... args)61     public void logDebug(String format, Object... args) {
62         Log.d(LOG_TAG, String.format(format, args));
63     }
64 
65     /**
66      * {@inheritDoc}
67      */
68     @Override
formatExecutionString(String method, String... args)69     protected String formatExecutionString(String method, String... args) {
70         return String.format("%s(%s)", method, TextUtils.join(", ", formatArgs(args)));
71     }
72 
73     /**
74      * {@inheritDoc}
75      */
76     @Override
getResolvedMethod(Class cls, String methodName, String... args)77     protected ResolvedMethod getResolvedMethod(Class cls, String methodName, String... args)
78             throws ClassNotFoundException {
79         List<Method> nameMatches = getMethodsWithName(cls, methodName);
80         for (Method m : nameMatches) {
81             ResolvedMethod rm = new ResolvedMethod(m);
82             int paramTypesMatched = 0;
83             int argsUsed = 0;
84             Class[] paramTypes = m.getParameterTypes();
85             for (Class paramType : paramTypes) {
86                 if (argsUsed == args.length && paramType.equals(String.class)) {
87                     // We've used up all supplied string args, so this method will not match.
88                     // If paramType is the Context class, we can match a paramType without needing
89                     // more string args. similarly, paramType "String[]" can be matched with zero
90                     // string args. If we add support for more paramTypes, this logic may require
91                     // adjustment.
92                     break;
93                 }
94                 if (paramType.equals(String.class)) {
95                     // Type "String" -- supply the next available arg
96                     rm.addArg(args[argsUsed++]);
97                 } else if (Context.class.isAssignableFrom(paramType)) {
98                     // Type "Context" -- supply the context from the test case
99                     rm.addArg(mContext);
100                 } else if (paramType.equals(Class.forName(STRING_ARRAY_CLASS))) {
101                     // Type "String[]" (or "String...") -- supply all remaining args
102                     rm.addArg(Arrays.copyOfRange(args, argsUsed, args.length));
103                     argsUsed += (args.length - argsUsed);
104                 } else {
105                     break; // Param type is unrecognized, this method will not match.
106                 }
107                 paramTypesMatched++; // A param type has been matched when reaching this point.
108             }
109             if (paramTypesMatched == paramTypes.length && argsUsed == args.length) {
110                 return rm; // Args match, methods match, so return the first method-args pairing.
111             }
112             // Not a match, try args for next method that matches by name.
113         }
114         throw new RuntimeException(String.format(
115                 "BusinessLogic: Failed to invoke action method %s with args: %s", methodName,
116                 Arrays.toString(args)));
117     }
118 }
119