1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.launcher3.testcomponent; 17 18 import android.app.Activity; 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.os.Bundle; 24 import android.util.TypedValue; 25 import android.view.View; 26 import android.widget.Button; 27 import android.widget.LinearLayout; 28 import android.widget.LinearLayout.LayoutParams; 29 30 import java.lang.reflect.Method; 31 import java.lang.reflect.Modifier; 32 33 /** 34 * Base activity with utility methods to help automate testing. 35 */ 36 public class BaseTestingActivity extends Activity implements View.OnClickListener { 37 38 public static final String SUFFIX_COMMAND = "-command"; 39 public static final String EXTRA_METHOD = "method"; 40 public static final String EXTRA_PARAM = "param_"; 41 42 private static final int MARGIN_DP = 20; 43 44 private final String mAction = this.getClass().getName(); 45 46 private LinearLayout mView; 47 private int mMargin; 48 49 private final BroadcastReceiver mCommandReceiver = new BroadcastReceiver() { 50 51 @Override 52 public void onReceive(Context context, Intent intent) { 53 handleCommand(intent); 54 } 55 }; 56 57 @Override 58 protected void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 61 mMargin = Math.round(TypedValue.applyDimension( 62 TypedValue.COMPLEX_UNIT_DIP, MARGIN_DP, getResources().getDisplayMetrics())); 63 mView = new LinearLayout(this); 64 mView.setPadding(mMargin, mMargin, mMargin, mMargin); 65 mView.setOrientation(LinearLayout.VERTICAL); 66 setContentView(mView); 67 68 registerReceiver(mCommandReceiver, new IntentFilter(mAction + SUFFIX_COMMAND)); 69 } 70 71 protected void addButton(String title, String method) { 72 Button button = new Button(this); 73 button.setText(title); 74 button.setTag(method); 75 button.setOnClickListener(this); 76 77 LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 78 lp.bottomMargin = mMargin; 79 mView.addView(button, lp); 80 } 81 82 @Override 83 protected void onResume() { 84 super.onResume(); 85 sendBroadcast(new Intent(mAction).putExtra(Intent.EXTRA_INTENT, getIntent())); 86 } 87 88 @Override 89 protected void onDestroy() { 90 unregisterReceiver(mCommandReceiver); 91 super.onDestroy(); 92 } 93 94 @Override 95 public void onClick(View view) { 96 handleCommand(new Intent().putExtra(EXTRA_METHOD, (String) view.getTag())); 97 } 98 99 private void handleCommand(Intent cmd) { 100 String methodName = cmd.getStringExtra(EXTRA_METHOD); 101 try { 102 Method method = null; 103 for (Method m : this.getClass().getDeclaredMethods()) { 104 if (methodName.equals(m.getName()) && 105 !Modifier.isStatic(m.getModifiers()) && 106 Modifier.isPublic(m.getModifiers())) { 107 method = m; 108 break; 109 } 110 } 111 Object[] args = new Object[method.getParameterTypes().length]; 112 Bundle extras = cmd.getExtras(); 113 for (int i = 0; i < args.length; i++) { 114 args[i] = extras.get(EXTRA_PARAM + i); 115 } 116 method.invoke(this, args); 117 } catch (Exception e) { 118 throw new RuntimeException(e); 119 } 120 } 121 122 public static Intent getCommandIntent(Class<?> clazz, String method) { 123 return new Intent(clazz.getName() + SUFFIX_COMMAND) 124 .putExtra(EXTRA_METHOD, method); 125 } 126 } 127