Skip to content

Commit f97b33c

Browse files
author
朱辉\harry
committed
JS脚本测试
1 parent 517797a commit f97b33c

6 files changed

Lines changed: 188 additions & 1 deletion

File tree

‎build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
apply plugin: 'java'// UTF-8支持tasks.withType(JavaCompile) { options.encoding = "UTF-8"}task "java-mkdirs" <<{ sourceSets*.java.srcDirs*.each{it.mkdirs()} sourceSets*.resources.srcDirs*.each{it.mkdirs()}}// 依赖库仓库repositories { mavenLocal() mavenCentral() }dependencies { compile group: 'com.android.tools.ddms', name: 'ddmlib', version: '25.3.0' compile group: 'io.netty', name: 'netty-all', version: '4.1.9.Final' compile group: 'com.alibaba', name: 'fastjson', version: '1.2.31' compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4' compile group: 'com.neovisionaries', name: 'nv-websocket-client', version: '1.31'} jar { manifest { attributes("Main-Class": "com.yeetor.Main") } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } doLast { def s = file("$libsDir/resources") s.mkdirs() copy { from "$projectDir/resources" into "$libsDir/resources" } }}apply plugin: 'application'run { if ( project.hasProperty("appArgs") ) { args Eval.me(appArgs) } doLast { def s = file("$buildDir/classes/resources") s.mkdirs() copy { from "$projectDir/resources" into "$buildDir/classes/resources" } }}mainClassName= 'com.yeetor.Main'
1+
apply plugin: 'java'// UTF-8支持tasks.withType(JavaCompile) { options.encoding = "UTF-8"}task "java-mkdirs" <<{ sourceSets*.java.srcDirs*.each{it.mkdirs()} sourceSets*.resources.srcDirs*.each{it.mkdirs()}}// 依赖库仓库repositories { mavenLocal() mavenCentral() }dependencies { compile group: 'com.android.tools.ddms', name: 'ddmlib', version: '25.3.0' compile group: 'io.netty', name: 'netty-all', version: '4.1.9.Final' compile group: 'com.alibaba', name: 'fastjson', version: '1.2.31' compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4' compile group: 'com.neovisionaries', name: 'nv-websocket-client', version: '1.31'} jar { manifest { attributes("Main-Class": "com.yeetor.Main") } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } doLast { def s = file("$libsDir/resources") s.mkdirs() copy { from "$projectDir/resources" into "$libsDir/resources" } }}apply plugin: 'application'run { if ( project.hasProperty("appArgs") ) { args Eval.me(appArgs) } doFirst { def s = file("$buildDir/classes/resources") s.mkdirs() copy { from "$projectDir/resources" into "$buildDir/classes/resources" } }}mainClassName= 'com.yeetor.Main'

‎resources/init.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Common functions
3+
*/
4+
5+
/**
6+
* Sleep In Java Thread
7+
* @param millisecond
8+
*/
9+
function sleep(millisecond) {
10+
java.lang.Thread.sleep(millisecond);
11+
}
12+
13+
/**
14+
* 获取已安装包名
15+
* @param device
16+
* @returns {*}
17+
*/
18+
function getPKGList(device) {
19+
return device.executeShellAndGetString("pm list packages");
20+
}
21+
22+
var device = com.yeetor.engine.EngineDevice.getDevice("3HX5T16C17040892");
23+
24+
25+
function 画圆() {
26+
device.touchDown(540, 0);
27+
var r = 450;
28+
for (var i = 0; i < 360; i++) {
29+
var y = Math.cos(i) * r;
30+
var x = Math.sin(i) * r;
31+
32+
x += 540;
33+
y += 960;
34+
35+
print(x + ":" + y);
36+
37+
device.touchMove(x, y);
38+
}
39+
device.touchUp();
40+
}
41+
画圆();
42+
43+
44+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.yeetor.engine;
2+
3+
import com.android.ddmlib.IDevice;
4+
import com.google.common.util.concurrent.ListenableFuture;
5+
import com.google.common.util.concurrent.SettableFuture;
6+
import com.sun.org.apache.xpath.internal.operations.Bool;
7+
import com.yeetor.adb.AdbServer;
8+
import com.yeetor.minitouch.Minitouch;
9+
import com.yeetor.minitouch.MinitouchListener;
10+
11+
import java.util.concurrent.ExecutionException;
12+
13+
/**
14+
* Created by harry on 2017/5/11.
15+
*/
16+
public class EngineDevice {
17+
18+
private IDevice iDevice;
19+
private Minitouch minitouch;
20+
private boolean minitouchOpen = false;
21+
22+
public static EngineDevice getDevice(String serialNumber) {
23+
IDevice iDevice = AdbServer.server().getDevice(serialNumber);
24+
if (iDevice != null) {
25+
EngineDevice device = new EngineDevice(iDevice);
26+
if (device.isMinitouchOpen()) {
27+
return device;
28+
}
29+
}
30+
return null;
31+
}
32+
33+
public EngineDevice(IDevice iDevice) {
34+
this.iDevice = iDevice;
35+
minitouch = new Minitouch(iDevice);
36+
SettableFuture future = SettableFuture.create();
37+
38+
minitouch.addEventListener(new MinitouchListener() {
39+
@Override
40+
public void onStartup(Minitouch minitouch, boolean success) {
41+
future.set(success);
42+
}
43+
44+
@Override
45+
public void onClose(Minitouch minitouch) {
46+
minitouchOpen = false;
47+
}
48+
});
49+
minitouch.start();
50+
51+
// 等待,指导Minitouch启动完毕
52+
try {
53+
Boolean success = (Boolean)future.get();
54+
if (success) {
55+
minitouchOpen = true;
56+
}
57+
} catch (InterruptedException e) {
58+
e.printStackTrace();
59+
} catch (ExecutionException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
64+
protected boolean isMinitouchOpen() {
65+
return minitouchOpen;
66+
}
67+
68+
public void touchDown(int x, int y) {
69+
minitouch.sendEvent("d 0 " + x + " " + y + " 50\nc\n");
70+
}
71+
72+
public void touchMove(int x, int y) {
73+
minitouch.sendEvent("m 0 " + x + " " + y + " 50\nc\n");
74+
}
75+
76+
public void touchUp() {
77+
minitouch.sendEvent("u 0\nc\n");
78+
79+
}
80+
81+
public String executeShellAndGetString(String command) {
82+
return AdbServer.executeShellCommand(iDevice, command);
83+
}
84+
85+
public void startApp(String str) {
86+
AdbServer.executeShellCommand(iDevice, "am start " + str);
87+
}
88+
89+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.yeetor.engine;
2+
3+
import com.yeetor.engine.js.Functions;
4+
import com.yeetor.util.Constant;
5+
import jdk.nashorn.api.scripting.NashornScriptEngine;
6+
import jdk.nashorn.internal.objects.annotations.ScriptClass;
7+
import jdk.nashorn.internal.runtime.ScriptFunction;
8+
import jdk.nashorn.internal.runtime.ScriptRuntime;
9+
10+
import javax.script.*;
11+
import java.io.*;
12+
13+
/**
14+
* Created by harry on 2017/5/11.
15+
*/
16+
public class JavaScripts {
17+
18+
public static void test() {
19+
ScriptEngineManager manager = new ScriptEngineManager();
20+
ScriptEngine engine = manager.getEngineByName("JavaScript");
21+
NashornScriptEngine nashornScriptEngine = (NashornScriptEngine) engine;
22+
23+
24+
try {
25+
Reader fReader = new InputStreamReader(new FileInputStream(Constant.getResourceFile("init.js")));
26+
engine.eval(fReader);
27+
} catch (ScriptException e) {
28+
e.printStackTrace();
29+
} catch (FileNotFoundException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
public void show() {
35+
System.out.println("我是傻逼");
36+
}
37+
38+
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.yeetor.engine.js;
2+
3+
/**
4+
* Created by harry on 2017/5/11.
5+
*/
6+
public class Functions {
7+
public static void hello() {
8+
System.out.println("sss");
9+
}
10+
}

‎src/main/java/com/yeetor/util/Constant.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public static File getResourceDir() {
2222
return resources;
2323
}
2424

25+
public static File getResourceFile(String name) {
26+
File dir = getResourceDir();
27+
return new File(dir, name);
28+
}
29+
2530
public static File getMinicap(String abi) {
2631
File resources = getResourceDir();
2732
if (resources.exists()) {

0 commit comments

Comments
 (0)