Skip to content

Commit 38b684b

Browse files
committed
Android:APIJSONTest 自动 UI 测试初步完成回放触屏操作
1 parent 67f6714 commit 38b684b

File tree

2 files changed

+79
-37
lines changed

2 files changed

+79
-37
lines changed

APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/UIAutoActivity.java

Lines changed: 78 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.graphics.Rect;
2424
import android.os.Bundle;
2525
import android.os.Handler;
26+
import android.os.Message;
2627
import android.support.annotation.RequiresApi;
2728
import android.util.DisplayMetrics;
2829
import android.util.Log;
@@ -76,6 +77,20 @@ public static Intent createIntent(Context context, String list) {
7677
return new Intent(context, UIAutoActivity.class).putExtra(INTENT_TOUCH_LIST, list);
7778
}
7879

80+
81+
private boolean isRecovering = false;
82+
private Handler handler = new Handler() {
83+
@Override
84+
public void handleMessage(Message msg) {
85+
super.handleMessage(msg);
86+
87+
if (isRecovering) {
88+
MotionEvent event = (MotionEvent) msg.obj;
89+
dispatchEventToCurrentActivity(event);
90+
}
91+
}
92+
};
93+
7994
private Activity context;
8095
int screenWidth;
8196
int screenHeight;
@@ -108,11 +123,6 @@ protected void onCreate(Bundle savedInstanceState) {
108123
flowId = getIntent().getLongExtra(INTENT_FLOW_ID, flowId);
109124
touchList = JSON.parseArray(getIntent().getStringExtra(INTENT_TOUCH_LIST));
110125

111-
if (touchList != null && touchList.isEmpty() == false) { //TODO 回放操作
112-
113-
} else { //TODO 录制操作
114-
115-
}
116126

117127
DisplayMetrics outMetrics = new DisplayMetrics();
118128
Display display = getWindowManager().getDefaultDisplay();
@@ -126,6 +136,12 @@ protected void onCreate(Bundle savedInstanceState) {
126136
cache = getSharedPreferences(TAG, Context.MODE_PRIVATE);
127137
dividerY = cache.getFloat(DIVIDER_Y, screenHeight - dip2px(30));
128138

139+
if (touchList != null && touchList.isEmpty() == false) { //TODO 回放操作
140+
recover(touchList);
141+
return;
142+
}
143+
144+
129145
ViewGroup root = (ViewGroup) getWindow().getDecorView();
130146
cover = (ViewGroup) getLayoutInflater().inflate(R.layout.unit_auto_cover_layout, null);
131147
divider = (ViewGroup) getLayoutInflater().inflate(R.layout.unit_auto_divider_layout, null);
@@ -166,6 +182,7 @@ protected void onCreate(Bundle savedInstanceState) {
166182
ivUnitAutoMenu.setOnClickListener(new View.OnClickListener() {
167183
@Override
168184
public void onClick(View v) {
185+
isRecovering = false;
169186
// ((ViewGroup) v.getParent()).removeView(v);
170187

171188
String cacheKey = UIAutoListActivity.CACHE_TOUCH;
@@ -181,7 +198,7 @@ public void onClick(View v) {
181198
cache.edit().remove(cacheKey).putString(cacheKey, JSON.toJSONString(allList)).commit();
182199

183200
// startActivity(UIAutoListActivity.createIntent(DemoApplication.getInstance(), flowId)); // touchList == null ? null : touchList.toJSONString()));
184-
startActivity(UIAutoListActivity.createIntent(DemoApplication.getInstance(), touchList == null ? null : touchList.toJSONString()));
201+
startActivityForResult(UIAutoListActivity.createIntent(DemoApplication.getInstance(), touchList == null ? null : touchList.toJSONString()), REQUEST_UI_AUTO_LIST);
185202

186203
FloatWindow.destroy("v");
187204
FloatWindow.destroy("v_ball");
@@ -232,37 +249,7 @@ else if (event.getAction() == MotionEvent.ACTION_UP) {
232249
public boolean onTouch(View v, MotionEvent event) {
233250
Log.d(TAG, "onTouchEvent " + Calendar.getInstance().getTime().toLocaleString() + " action:" + (event.getAction()) + "; x:" + event.getX() + "; y:" + event.getY());
234251

235-
Activity a = DemoApplication.getInstance().getCurrentActivity();
236-
if (a != null) {
237-
View decorView = a.getWindow().getDecorView();
238-
float y = decorView.getY();
239-
float top = decorView.getTop();
240-
event.offsetLocation(0, decorView.getTop());
241-
View content = decorView.findViewById(android.R.id.content);
242-
float cy = content.getY();
243-
float ctop = content.getTop();
244-
245-
Rect rectangle= new Rect();
246-
decorView.getWindowVisibleDisplayFrame(rectangle);
247-
248-
// event.offsetLocation(0, a.getWindow().getDecorView().findViewById(android.R.id.content).getTop());
249-
250-
if (rectangle.top > 0) {
251-
event = MotionEvent.obtain(event);
252-
event.offsetLocation(0, rectangle.top);
253-
}
254-
a.dispatchTouchEvent(event);
255-
256-
//放到 Application 中 have already added to window manager
257-
new Handler().postDelayed(new Runnable() {
258-
@Override
259-
public void run() {
260-
showCover(true, a);
261-
}
262-
}, 1000);
263-
} else {
264-
//TODO 不是本 APP 的界面
265-
}
252+
dispatchEventToCurrentActivity(event);
266253

267254
if (touchList == null) {
268255
touchList = new JSONArray();
@@ -279,6 +266,9 @@ public void run() {
279266
obj.put("y", (int) relativeY);
280267
obj.put("dividerY", (int) dividerY);
281268
obj.put("time", System.currentTimeMillis());
269+
obj.put("downTime", event.getDownTime());
270+
obj.put("eventTime", event.getEventTime());
271+
obj.put("metaState", event.getMetaState());
282272
touchList.add(obj);
283273

284274
if (isFinishing() || isDestroyed()) {
@@ -476,6 +466,38 @@ private void showCover(boolean show, Activity activity) {
476466
}
477467

478468

469+
public boolean dispatchEventToCurrentActivity(MotionEvent event) {
470+
Activity a = DemoApplication.getInstance().getCurrentActivity();
471+
if (a != null) {
472+
View decorView = a.getWindow().getDecorView();
473+
float y = decorView.getY();
474+
float top = decorView.getTop();
475+
event.offsetLocation(0, decorView.getTop());
476+
View content = decorView.findViewById(android.R.id.content);
477+
float cy = content.getY();
478+
float ctop = content.getTop();
479+
480+
Rect rectangle= new Rect();
481+
decorView.getWindowVisibleDisplayFrame(rectangle);
482+
483+
// event.offsetLocation(0, a.getWindow().getDecorView().findViewById(android.R.id.content).getTop());
484+
485+
if (rectangle.top > 0) {
486+
event = MotionEvent.obtain(event);
487+
event.offsetLocation(0, rectangle.top);
488+
}
489+
a.dispatchTouchEvent(event);
490+
491+
return true;
492+
} else {
493+
//TODO 不是本 APP 的界面
494+
}
495+
496+
return false;
497+
}
498+
499+
500+
479501
/**
480502
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
481503
*/
@@ -511,6 +533,24 @@ protected void onDestroy() {
511533
// }
512534

513535

536+
public void recover(JSONArray touchList) {
537+
isRecovering = true;
538+
539+
JSONObject last = null;
540+
for (int i = 0; i < touchList.size(); i++) {
541+
JSONObject obj = touchList.getJSONObject(i);
542+
543+
MotionEvent event = MotionEvent.obtain(obj.getIntValue("downTime"), obj.getIntValue("eventTime"),
544+
obj.getIntValue("action"), obj.getIntValue("x"), obj.getIntValue("y"), obj.getIntValue("metaState"));
545+
546+
Message msg = handler.obtainMessage();
547+
msg.obj = event;
548+
handler.sendMessageDelayed(msg, last == null ? 0 : obj.getIntValue("eventTime") - last.getIntValue("eventTime"));
549+
550+
last = obj;
551+
}
552+
}
553+
514554
public static final int REQUEST_UI_AUTO_LIST = 1;
515555

516556
@Override
@@ -523,6 +563,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
523563

524564
if (requestCode == REQUEST_UI_AUTO_LIST) {
525565
JSONArray array = data == null ? null : JSON.parseArray(data.getStringExtra(UIAutoListActivity.RESULT_LIST));
566+
recover(array);
526567

527568
Toast.makeText(context, "onActivityResult array = " + JSON.toJSONString(array), Toast.LENGTH_LONG).show();
528569
//TODO 恢复

APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/UIAutoListActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public void send(View v) {
265265
} // Touch >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
266266
request.setTag("Touch");
267267

268+
pbUIAutoList.setVisibility(View.VISIBLE);
268269
HttpManager.getInstance().post(fullUrl, request.toString(), new HttpManager.OnHttpResponseListener() {
269270
@Override
270271
public void onHttpResponse(int requestCode, String resultJson, Exception e) {

0 commit comments

Comments
 (0)