Android basics 12: Android automated testing 06-Instrumentation 04

Source: Internet
Author: User

I have learned a lot about JUnit In the android SDK, but I feel that there have been several problems that have not been solved (I wonder if you feel the same). JUnit testing is automated, there is no need for any operation at all. I have not found any answers to either of the following two questions:
JUnit can be used to test the basic components of Android: Activity, service, and provider. Therefore, how can we control the running of these basic elements requires the Android system to provide some underlying operation interfaces.
Simulate interface operations, such as Clicking buttons on the interface and selecting menus.
For the second question, I seem to have found the answer in the cview. The cview provides some interface operation functions, such as using mclick (), it seems that the android SDK provides some functions to simulate various operations. For the first question, this article provides a detailed description of this question.

First, let's first think about what needs to be done to test the activity? First, you need to create an activity (which is already provided in the android SDK) and then control the lifecycle of the activity. These are the most basic. In the android SDK, the activity is created asynchronously. That is to say, after startactivty () is called, this function returns immediately instead of waiting for the activity to be created, this requires some synchronization operations. In summary, there are two aspects: control the lifecycle and synchronization of the activity. Android SDK provides us
Instrument, in Android. the instrumentationtestcase class in the test package has a function getinstrumentation () and the method for starting the test by using ADB shell am instrument. The following describes the instrumentation in detail.

What is instrumentation?

Instrumentation is the execution of application InstrumentationCode. When the applicationProgramWhen running, instrumentation is enabled. instrumentation is initialized before any application is running. It can be used to monitor the interaction between the system and the application. Instrumentation implementation is described using the <instrumentation> label in androidmanifest. xml.
Instrumentation seems to be somewhat similar to the "hook" function in the window, where a "Bug" is installed between the system and the application ".

Shows the instrumentation class structure in the Android. app package.

It contains two internal classes: activitymoniter and activityresult.

    • Activitymoniter: monitors specific intent. An activitymoniter class instance uses the addmonitor function (instrumentation. activitymonitor) is added to the current instrumentation. Once added, activitymoniter checks every time a new activity is started. If matched, other operations such as its hit count update are performed. An activitymonitor can also be used to find an activity. Through the waitforactivity () method, this function is returned until the matching activity is created.
    • Activityresult: A description of the execution result of an activity, which is returned to the original activity.

Next, let's take a look at the functions of the instrumentation class and list the main functions as follows:

    • Add and delete activitymoniter;

    • Application and activity creation and lifecycle control;

    • Control the operation of instrumentation;

    • Send event messages such as buttons and scroll balls to the current window;

    • Synchronization operations;
    1. Create an activity until the activity starts running;
    2. Execute a call in the main thread. The main thread is blocked until the call ends;
    3. When the main thread is idle (no message is waiting for processing), execute a call;
    4. Synchronization waits for the master thread to be idle

After reading this, we have implemented both the life cycle and synchronization of the activity in the instrumentation class, especially in synchronization operations.
Simple use example of Instrumentation

Public class hellotest extends instrumentationtestcase {Hello mactivitytested; Public hellotest () {}// @ overrideprotected void setup () throws exception {super. setup (); intent = new intent (); intent. setclassname ("com. xmobileapp. hello ", hello. class. getname (); intent. setflags (intent. flag_activity_new_task); mactivitytested = (Hello) getinstrumentation (). startactivitysync (intent);} // @ overrideprotected void teardown () throws exception {mactivitytested. finish (); super. teardown ();}}

Here, we use the getinstrumentation () function of the instrumentationtestcase class to obtain the instrumentation object, start an activity through the startactivitysync () function of the instrumentation, and return the result after avtiation is started.

<Instrumentation> label description in androidmanifest. xml

Open the androidmanifest. xml file and you will see the androidmanifest. XML editing tool. With this tool, we can easily add elements in androidmanifest. xml.

After selecting the instrumentation panel, We can edit the instrumentation tag. The operations on the interface are not described here, which mainly describes the meaning of the attribute values:

Summary
After learning about instrumentation, many problems are lost. You need to study this class in the android SDK to better test the activity object.

References:

Analysis of Android. App. Instrumentation

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.