Introduction to AndroidMonkey, an open-source project based on the Instrumentation Framework

Source: Internet
Author: User

Introduction to AndroidMonkey, an open-source project based on the Instrumentation Framework

In the previous article "MonkeyRunner and Android device communication methods source code analysis", I mentioned that one of the ways for MonkeyRunner to interact with the target Android machine is to enable 'Monkey-port $ port 'To establish a connection with monkeyrunner for interaction, later, I planned to download the monkey source code of the android machine to analyze how monkey is handled. I typed the keyword "android monkey source code" in google and found the following connection "https://code.google.com/p/androidmonkey". I thought it was google and android, and thought it was the desired monkey. So I downloaded it for analysis without looking at it.

The process is getting confused. The biggest question is, how does monkey use instrumentation to inject events? In this case, how does one send cross-process application events? According to my understanding, the instrumentation framework cannot be cross-process, while the monkey is absolutely cross-process!

@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;}
After struggling for a while, I returned to the download location and looked at the overview. It was very short, but I clearly described this is not the monkey I wanted, instead, it is an android library made of a modified native monkey. Now, let's translate the introduction and use of it by the way. There are just a few words, so I will try to study it later, as to whether it is true, as I have said, it cannot be cross-process, so it will not be verified in the future or by everyone.

1. Overview

Official: 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 is an android library. In fact, it is a Test Library Based on native monkey to facilitate testing and analysis (for example, code coverage.

Tester/User can easily use the library to create random test cases to test android apps with GUI.

Testers/users can easily use this library to create random test scripts for Android gui applications.

Why this library: Why use this library:

  • You can use this library to create random test cases for your application, with just few lines of code
  • With this library, you can write random test cases for your app in just a few lines of code.
  • You can add your assertions to access the state of the SUT (you can hardly do this with the Android Monkey Tool)
  • You can add assertions in your test code to easily judge your SUT (tested system/application) (If you use monkey, it is difficult to do so)
  • You can do Coverage analysis of random testing on Android Application, this is useful for Research Purpose (like what I'm doing)
  • You can immediately test the coverage rate analysis for your app, which is useful for our research (as I do now: in fact, the last sentence should be understood as follows: if you are a company's procurement, you want to assess whether an application is stable enough to meet your needs, then you will first try to do some preliminary research)

Code example/Code instance
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();                }        }}

Official: https://code.google.com/p/androidmonkey/wiki/HowToUse


2. Introduction

HowToUse/Usage

Introduction/Overview

Here's how to use the library/The following describes how to use this library:

Details/Details

Create an android test project/Create an android Project

An Android Test project shoshould be created first, using Android ADT [http://developer.android.com/sdk/eclipse-adt.html] Refer the AndroidMonkey library (jar or project)

First, create an Android Test Project. Use Android ADT [http://developer.android.com/sdk/eclipse-adt.html] to add references to the AndroidMonkey Library (reference jar or Project)

Create a test class/Create a test class

Create a normal ActivityInstrumentationTestCase2 test class, and add the following test case:

Create a common ActivityInstrumentationTestCase2 test class and add the following test case code:

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();}}




For android open-source projects, if you want to join an open-source project to learn and train yourself

You can do it ,,,
 
How to determine the security and stability of open-source projects for android

Why do most server systems on the market use open-source Linux? Instead of Windows with closed source code?
As an open-source system that requires extremely high security and stability, it makes sense. One of the most important aspects is:
Open-source code can be supervised and tested by more people, and system vulnerabilities can be solved more quickly.
There are still many other reasons, but it is undeniable that large open-source projects such as Linux and android are not concerned about their security and stability due to the attention and participation of software working groups around the world.
PS: in fact, the android Project is a feature of Linux.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.