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