|
| 1 | +/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package apijson.demo.ui; |
| 16 | + |
| 17 | +import android.app.Activity; |
| 18 | +import android.content.Context; |
| 19 | +import android.content.Intent; |
| 20 | +import android.os.Bundle; |
| 21 | +import android.os.Handler; |
| 22 | +import android.view.Display; |
| 23 | +import android.view.MotionEvent; |
| 24 | +import android.view.View; |
| 25 | +import android.widget.ScrollView; |
| 26 | +import android.widget.TextView; |
| 27 | +import android.widget.Toast; |
| 28 | + |
| 29 | +import java.util.Calendar; |
| 30 | + |
| 31 | +import apijson.demo.R; |
| 32 | + |
| 33 | + |
| 34 | +/**自动 UI 测试,需要用 UIAuto 发请求到这个设备 |
| 35 | + * https://github.com/TommyLemon/UIAuto |
| 36 | + * @author Lemon |
| 37 | + */ |
| 38 | +public class UIAutoActivity extends Activity { |
| 39 | + /** |
| 40 | + * @param context |
| 41 | + * @return |
| 42 | + */ |
| 43 | + public static Intent createIntent(Context context) { |
| 44 | + return new Intent(context, UIAutoActivity.class); |
| 45 | + } |
| 46 | + |
| 47 | + private Activity context; |
| 48 | + @Override |
| 49 | + protected void onCreate(Bundle savedInstanceState) { |
| 50 | + super.onCreate(savedInstanceState); |
| 51 | + setContentView(R.layout.ui_auto_activity); |
| 52 | + context = this; |
| 53 | + |
| 54 | + final Display d = getWindowManager().getDefaultDisplay(); |
| 55 | + |
| 56 | + final ScrollView svTouch = findViewById(R.id.svTouch); |
| 57 | + final TextView tvTouch = findViewById(R.id.tvTouch); |
| 58 | + |
| 59 | + final View tvButton = findViewById(R.id.tvButton); |
| 60 | + final View btnButton = findViewById(R.id.btnButton); |
| 61 | + |
| 62 | + final View vTouch = findViewById(R.id.vTouch); |
| 63 | + final View llTouch = findViewById(R.id.llTouch); |
| 64 | + final View rlRoot = findViewById(R.id.rlRoot); |
| 65 | + final View vDispatchTouch = findViewById(R.id.vDispatchTouch); |
| 66 | + |
| 67 | + |
| 68 | + View.OnTouchListener listener = new View.OnTouchListener() { |
| 69 | + @Override |
| 70 | + public boolean onTouch(View v, MotionEvent event) { |
| 71 | + vDispatchTouch.dispatchTouchEvent(event); |
| 72 | + |
| 73 | +// vTouch.setOnTouchListener(new View.OnTouchListener() { |
| 74 | +// @Override |
| 75 | +// public boolean onTouch(View v, MotionEvent event) { |
| 76 | +// return false; |
| 77 | +// } |
| 78 | +// }); |
| 79 | +// |
| 80 | + CharSequence s = tvTouch.getText(); |
| 81 | + if (s == null) { |
| 82 | + s = ""; |
| 83 | + } |
| 84 | + |
| 85 | + tvTouch.setText(Calendar.getInstance().getTime().toLocaleString() + " action:" + (event.getAction()) + "; x:" + event.getX() + "; y:" + event.getY() + "\n" + s); |
| 86 | +// Toast.makeText(context, "vTouch.action:" + (event.getAction()) + "; x:" + event.getX() + "; y:" + event.getY(), Toast.LENGTH_SHORT).show(); |
| 87 | + |
| 88 | +//死循环 llTouch.dispatchTouchEvent(event); |
| 89 | +// vDispatchTouch.dispatchTouchEvent(event); |
| 90 | +// vDispatchTouch.dispatchTouchEvent(event); |
| 91 | + //onTouchEvent 不能处理事件 vDispatchTouch.onTouchEvent(event); |
| 92 | +// vTouch.setOnTouchListener(this); |
| 93 | + return true; //连续记录只能 return true |
| 94 | + } |
| 95 | + }; |
| 96 | + vTouch.setOnTouchListener(listener); |
| 97 | + |
| 98 | + tvButton.setOnClickListener(new View.OnClickListener() { |
| 99 | + @Override |
| 100 | + public void onClick(View v) { |
| 101 | + Toast.makeText(context, "tvButton.onClick", Toast.LENGTH_SHORT).show(); |
| 102 | + } |
| 103 | + }); |
| 104 | + tvButton.setOnTouchListener(new View.OnTouchListener() { |
| 105 | + @Override |
| 106 | + public boolean onTouch(View v, MotionEvent event) { |
| 107 | + Toast.makeText(context, "tvButton.action:" + (event.getAction()) + "; x:" + event.getX() + "; y:" + event.getY(), Toast.LENGTH_SHORT).show(); |
| 108 | + return false; //连续记录只能 return true |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + btnButton.setOnLongClickListener(new View.OnLongClickListener() { |
| 113 | + @Override |
| 114 | + public boolean onLongClick(View v) { |
| 115 | + Toast.makeText(context, "btnMain.onLongClick", Toast.LENGTH_SHORT).show(); |
| 116 | + return true; |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + |
| 121 | + new Handler().postDelayed(new Runnable() { |
| 122 | + @Override |
| 123 | + public void run() { |
| 124 | + |
| 125 | + MotionEvent e = MotionEvent.obtain(1000, 1000, |
| 126 | + MotionEvent.ACTION_DOWN, d.getWidth()/2, d.getHeight()/2, 0); |
| 127 | + |
| 128 | + //TODO onTouchEvent ? |
| 129 | + vTouch.dispatchTouchEvent(e); |
| 130 | + e.setAction(MotionEvent.ACTION_UP); |
| 131 | + vTouch.dispatchTouchEvent(e); |
| 132 | + |
| 133 | + } |
| 134 | + }, 1000); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + public void onClick(View v) { |
| 139 | + Toast.makeText(context, "onClick BUTTON", Toast.LENGTH_SHORT).show(); |
| 140 | + } |
| 141 | +} |
0 commit comments