Skip to content

Commit aa7bbd7

Browse files
committed
Android:APIJSONTest 自动化单元测试 记住端口,新增使用 UnitAuto 的提示,解决端口问题导致崩溃
1 parent 4b32396 commit aa7bbd7

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

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

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
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+
115
package apijson.demo.ui;
216

317
import android.app.Activity;
418
import android.app.Application;
519
import android.content.Context;
620
import android.content.Intent;
21+
import android.content.SharedPreferences;
722
import android.content.pm.ActivityInfo;
823
import android.content.res.Configuration;
924
import android.os.Bundle;
@@ -12,6 +27,7 @@
1227
import android.view.View;
1328
import android.view.Window;
1429
import android.widget.TextView;
30+
import android.widget.Toast;
1531

1632
import com.alibaba.fastjson.JSON;
1733
import com.alibaba.fastjson.JSONObject;
@@ -30,9 +46,13 @@
3046
import apijson.demo.application.DemoApplication;
3147
import apijson.demo.server.MethodUtil;
3248

33-
49+
/**自动单元测试,需要用 UnitAuto 发请求到这个设备
50+
* https://github.com/TommyLemon/UnitAuto
51+
* @author Lemon
52+
*/
3453
public class UnitActivity extends Activity implements HttpServerRequestCallback {
3554
private static final String TAG = "UnitActivity";
55+
private static final String KEY_PORT = "KEY_PORT";
3656

3757
/**
3858
* @param context
@@ -71,6 +91,10 @@ protected void onCreate(Bundle savedInstanceState) {
7191
etUnitPort = findViewById(R.id.etUnitPort);
7292
pbUnit = findViewById(R.id.pbUnit);
7393

94+
95+
SharedPreferences sp = getSharedPreferences(TAG, Context.MODE_PRIVATE);
96+
port = sp.getString(KEY_PORT, "");
97+
7498
etUnitPort.setText(port);
7599
pbUnit.setVisibility(View.GONE);
76100

@@ -113,21 +137,33 @@ public void orient(View v) {
113137
private String port = "8080";
114138
public void start(View v) {
115139
v.setEnabled(false);
116-
port = StringUtil.getString(etUnitPort);
117-
startServer(Integer.valueOf(port));
140+
port = StringUtil.getTrimedString(etUnitPort);
141+
142+
try {
143+
startServer(StringUtil.isEmpty(port, true) ? 8080 : Integer.valueOf(port));
118144

145+
etUnitPort.setEnabled(false);
146+
pbUnit.setVisibility(View.VISIBLE);
147+
148+
Toast.makeText(context, R.string.please_send_request_with_unit_auto, Toast.LENGTH_LONG).show();
149+
} catch (Exception e) { // FIXME 端口异常 catch 不到
150+
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
151+
}
119152
v.setEnabled(true);
120-
etUnitPort.setEnabled(false);
121-
pbUnit.setVisibility(View.VISIBLE);
122153
}
123154
public void stop(View v) {
124155
v.setEnabled(false);
125-
server.stop();
126-
mAsyncServer.stop();
127156

157+
try {
158+
server.stop();
159+
mAsyncServer.stop();
160+
161+
etUnitPort.setEnabled(true);
162+
pbUnit.setVisibility(View.GONE);
163+
} catch (Exception e) {
164+
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
165+
}
128166
v.setEnabled(true);
129-
etUnitPort.setEnabled(true);
130-
pbUnit.setVisibility(View.GONE);
131167
}
132168

133169

@@ -268,6 +304,9 @@ public void run() {
268304
protected void onDestroy() {
269305
isAlive = false;
270306
stop(etUnitPort);
307+
308+
getSharedPreferences(TAG, Context.MODE_PRIVATE).edit().remove(KEY_PORT).putString(KEY_PORT, port).apply();
309+
271310
super.onDestroy();
272311
}
273312

APIJSON-Android/APIJSONTest/app/src/main/res/values-en/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@
2929
<string name="screen">Screen: </string>
3030
<string name="horizontal">Horizontal</string>
3131
<string name="vertical">Vertical</string>
32+
<string name="please_send_request_with_unit_auto">Please send request to this device with UnitAuto \nhttp://apijson.org/unit</string>
3233
</resources>

APIJSON-Android/APIJSONTest/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
<string name="screen">屏幕:</string>
3232
<string name="horizontal">横向</string>
3333
<string name="vertical">纵向</string>
34+
<string name="please_send_request_with_unit_auto">请用 UnitAuto 向这个设备发送请求 \nhttp://apijson.org/unit</string>
3435

3536
</resources>

0 commit comments

Comments
 (0)