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.graphics.Color; 24 import android.os.Bundle; 25 import android.util.TypedValue; 26 import android.view.View; 27 import android.widget.Button; 28 import android.widget.LinearLayout; 29 import android.widget.LinearLayout.LayoutParams; 30 31 import java.lang.reflect.Method; 32 import java.lang.reflect.Modifier; 33 34 /** 35 * Base activity with utility methods to help automate testing. 36 */ 37 public class BaseTestingActivity extends Activity implements View.OnClickListener { 38 39 public static final String SUFFIX_COMMAND = "-command"; 40 public static final String EXTRA_METHOD = "method"; 41 public static final String EXTRA_PARAM = "param_"; 42 43 private static final int MARGIN_DP = 20; 44 45 private final String mAction = this.getClass().getName(); 46 47 private LinearLayout mView; 48 private int mMargin; 49 50 private final BroadcastReceiver mCommandReceiver = new BroadcastReceiver() { 51 52 @Override 53 public void onReceive(Context context, Intent intent) { 54 handleCommand(intent); 55 } 56 }; 57 58 @Override onCreate(Bundle savedInstanceState)59 protected void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 62 mMargin = Math.round(TypedValue.applyDimension( 63 TypedValue.COMPLEX_UNIT_DIP, MARGIN_DP, getResources().getDisplayMetrics())); 64 mView = new LinearLayout(this); 65 mView.setPadding(mMargin, mMargin, mMargin, mMargin); 66 mView.setOrientation(LinearLayout.VERTICAL); 67 mView.setBackgroundColor(Color.BLUE); 68 setContentView(mView); 69 70 registerReceiver(mCommandReceiver, new IntentFilter(mAction + SUFFIX_COMMAND)); 71 } 72 addButton(String title, String method)73 protected void addButton(String title, String method) { 74 Button button = new Button(this); 75 button.setText(title); 76 button.setTag(method); 77 button.setOnClickListener(this); 78 79 LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 80 lp.bottomMargin = mMargin; 81 mView.addView(button, lp); 82 } 83 84 @Override onResume()85 protected void onResume() { 86 super.onResume(); 87 sendBroadcast(new Intent(mAction).putExtra(Intent.EXTRA_INTENT, getIntent())); 88 } 89 90 @Override onDestroy()91 protected void onDestroy() { 92 unregisterReceiver(mCommandReceiver); 93 super.onDestroy(); 94 } 95 96 @Override onClick(View view)97 public void onClick(View view) { 98 handleCommand(new Intent().putExtra(EXTRA_METHOD, (String) view.getTag())); 99 } 100 handleCommand(Intent cmd)101 private void handleCommand(Intent cmd) { 102 String methodName = cmd.getStringExtra(EXTRA_METHOD); 103 try { 104 Method method = null; 105 for (Method m : this.getClass().getDeclaredMethods()) { 106 if (methodName.equals(m.getName()) && 107 !Modifier.isStatic(m.getModifiers()) && 108 Modifier.isPublic(m.getModifiers())) { 109 method = m; 110 break; 111 } 112 } 113 Object[] args = new Object[method.getParameterTypes().length]; 114 Bundle extras = cmd.getExtras(); 115 for (int i = 0; i < args.length; i++) { 116 args[i] = extras.get(EXTRA_PARAM + i); 117 } 118 method.invoke(this, args); 119 } catch (Exception e) { 120 throw new RuntimeException(e); 121 } 122 } 123 getCommandIntent(Class<?> clazz, String method)124 public static Intent getCommandIntent(Class<?> clazz, String method) { 125 return new Intent(clazz.getName() + SUFFIX_COMMAND) 126 .putExtra(EXTRA_METHOD, method); 127 } 128 } 129