• 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"); 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(mCommandReceiver, new IntentFilter(mAction + SUFFIX_COMMAND));
73     }
74 
addButton(String title, String method)75     protected void addButton(String title, String method) {
76         Button button = new Button(this);
77         button.setText(title);
78         button.setTag(method);
79         button.setOnClickListener(this);
80 
81         LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
82         lp.bottomMargin = mMargin;
83         mView.addView(button, lp);
84     }
85 
addEditor(String initText, String hint, boolean requestIme)86     protected void addEditor(String initText, String hint, boolean requestIme) {
87         EditText editText = new EditText(this);
88         editText.setHint(hint);
89         editText.setText(initText);
90 
91         LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
92         lp.bottomMargin = mMargin;
93         mView.addView(editText, lp);
94         if (requestIme) {
95             editText.requestFocus();
96             mView.getWindowInsetsController().show(WindowInsets.Type.ime());
97         }
98     }
99 
100     @Override
onResume()101     protected void onResume() {
102         super.onResume();
103         sendBroadcast(new Intent(mAction).putExtra(Intent.EXTRA_INTENT, getIntent()));
104     }
105 
106     @Override
onDestroy()107     protected void onDestroy() {
108         unregisterReceiver(mCommandReceiver);
109         super.onDestroy();
110     }
111 
112     @Override
onClick(View view)113     public void onClick(View view) {
114         handleCommand(new Intent().putExtra(EXTRA_METHOD, (String) view.getTag()));
115     }
116 
handleCommand(Intent cmd)117     private void handleCommand(Intent cmd) {
118         String methodName = cmd.getStringExtra(EXTRA_METHOD);
119         try {
120             Method method = null;
121             for (Method m : this.getClass().getDeclaredMethods()) {
122                 if (methodName.equals(m.getName()) &&
123                         !Modifier.isStatic(m.getModifiers()) &&
124                         Modifier.isPublic(m.getModifiers())) {
125                     method = m;
126                     break;
127                 }
128             }
129             Object[] args = new Object[method.getParameterTypes().length];
130             Bundle extras = cmd.getExtras();
131             for (int i = 0; i < args.length; i++) {
132                 args[i] = extras.get(EXTRA_PARAM + i);
133             }
134             method.invoke(this, args);
135         } catch (Exception e) {
136             throw new RuntimeException(e);
137         }
138     }
139 
getCommandIntent(Class<?> clazz, String method)140     public static Intent getCommandIntent(Class<?> clazz, String method) {
141         return new Intent(clazz.getName() + SUFFIX_COMMAND)
142                 .putExtra(EXTRA_METHOD, method);
143     }
144 }
145