標籤:androidmonkey monkey monkeyrunner
在上一篇文章《MonkeyRunner和Android裝置通訊方式源碼分析》中本人提到MonkeyRunner跟目標安卓機器互動的其中一種方式是通過在目標機器端開啟‘monkey -port $port‘來跟monkeyrunner建立串連進行互動的,後來打算下載安卓機器端的monkey原始碼來分析下monkey是如何處理的,在google中打入關鍵字“android monkey source code“後定位到以下串連”https://code.google.com/p/androidmonkey“, 看到是google的然後又是android,以為就是自己想要的monkey了,所以沒有多看就直接下載進行分析。
過程中越看越糊塗,最大的一個疑問是怎麼monkey是用instrumentation來注入event呢?這樣的話它怎麼做到跨進程應用的事件發送呢?因為根據本人的瞭解instrumentation架構是不能跨進程的,而monkey是絕對跨進程的!
@Overridepublic int fireEvent(Instrumentation testRuner) {String note;if (mAction == KeyEvent.ACTION_UP) {note = "ACTION_UP";} else {note = "ACTION_DOWN";}System.out.println(":Typing Key (" + note + "): " + mKeyCode+ " // ");try {//testRuner.sendKeySync(getEvent());testRuner.sendKeyDownUpSync(mKeyCode);} catch (Exception e) {System.out.println("Failed to send key (" + note + "): " + mKeyCode+ " // ");return MonkeyEvent.INJECT_FAIL;}return MonkeyEvent.INJECT_SUCCESS;}糾結了一段時間後返回下載地方看了下概覽,很短,但很清楚的描述了這個不是我想要的monkey,而是原生monkey的一個修改後做成的android庫。既然碰上了就順便翻譯下它的介紹和使用吧,反正就那麼幾句話,待今後有時間再來進一步研究它了,至於它是否真的如我所言不能跨進程,就留待今後或者大家來驗證了。
1. 概覽
官方原文:https://code.google.com/p/androidmonkey
AndroidMonkey is an Android Library. It is, in fact, a copy of the original Android Monkey Toolhttp://developer.android.com/guide/developing/tools/monkey.html and made as a library for testing and analysis (e.g. code coverage) purposes.
AndroidMonkey是一個android庫。事實上它是在基於原生monkey的基礎上做成的一個方便測試和分析(比如:程式碼涵蓋範圍)的一個測試庫。
Tester/User can easily use the library to create random test cases to test android apps with GUI.
測試人員/使用者可以很方便的使用這個庫來建立針對安卓gui應用的隨機測試指令碼
Why this library:
為什麼使用這個庫:
- You can use this library to create random test cases for your application, with just few lines of code
- 使用這個庫的話你可以只用幾行代碼就能編寫出針對你的app的隨機測試案例
- You can add your assertions to access the state of the SUT (you can hardly do this with the Android Monkey Tool)
- 你可以在你的測試代碼中加入斷言從而很方便的去判斷你的SUT(被測系統/應用)的當前情況(如果你用monkey的話是很難做到的)
- You can do Coverage analysis of random testing on Android Application, this is useful for Research Purpose (like what I‘m doing)
- 你可以通過隨即測試對你的app做覆蓋率分析,這對我們的研究是很有用的(像我現在所做的)(譯者註:其實最後這一句話應該這樣子去理解:如果你是一個公司的採購,你想去評估一個應用是否足夠穩定滿足你的需求,那麼你就會先試用做一些前期研究)
Code example/代碼執行個體
public class ContactAdderTest extends ActivityInstrumentationTestCase2<ContactAdder> { private int NUM_EVENTS = 1000; public ContactAdderTest() { super("com.example.android.contactmanager", ContactAdder.class); } @Override protected void setUp() throws Exception { super.setUp(); setActivityInitialTouchMode(false); } public void testMonkeyEvents() { Display display = getActivity().getWindowManager().getDefaultDisplay(); Instrumentation inst = getInstrumentation(); PackageManager pm = getActivity().getPackageManager(); Monkey monkey = new Monkey(display, "com.example.android.contactmanager", inst, pm); // Generate and fire a random event. for (int i = 0; i < NUM_EVENTS; i++) { monkey.nextRandomEvent(); } }}
官方原文:https://code.google.com/p/androidmonkey/wiki/HowToUse
2.使用簡介
HowToUse /使用簡介
Introduction/簡介
Here‘s how to use the library/以下是介紹如何使用這個庫:
Details/詳情
Create an android test project/建立android項目
An Android Test project should be created first, using Android ADT [http://developer.android.com/sdk/eclipse-adt.html] Refer the the AndroidMonkey library (jar or project)
先建立一個Android Test Project項目,通過Android ADT[http://developer.android.com/sdk/eclipse-adt.html] 添加對AndroidMonkey庫的引用(引用jar或者項目)
Create a test class/建立測試類別
Create a normal ActivityInstrumentationTestCase2 test class, and add the following test case:
建立一個普通的ActivityInstrumentationTestCase2測試類別,然後加入如下的測試案例代碼:
public void testMonkeyEvents(){Display display = getActivity().getWindowManager().getDefaultDisplay(); Instrumentation inst = getInstrumentation(); PackageManager pm = getActivity().getPackageManager();Monkey monkey = new Monkey(display, packageToTest, inst, pm);for (int i = 0; i < NUM_EVENTS; i++){monkey.nextRandomEvent();}}
基於Instrumentation Framework的開源項目AndroidMonkey簡介