Instrumentation an official profile of Android (personally considered to be the most concise description of Highlevel abstraction)

Source: Internet
Author: User

Official connection: http://developer.android.com/tools/testing/testing_android.html Middle Instrumentation paragraph

(Baidu out of the instrumentation most of the elaboration is not through castration is too lengthy, see people cloud fog, this article translated the official profile, from the top of the instrumentation framework to do the elaboration, in order to q& The idea of a is to understand exactly what instrumentation is all about. Hope to be helpful to instrumentation beginners)

Instrumentation

Android Instrumentation is a set of control methods or "hooks" in the Android system. These hooks control an Android component independently of its normal lifecycle. They also control how Android loads applications.

android Instrumentation is a set of control methods or "hooks" inside the Android system. These hooks control the operation of the Android control beyond the normal life cycle (normally controlled by the operating system). They can also control how Android loads applications.

Normally, an Android component runs with a lifecycle determined by the system. For example, a activity object ' s lifecycle starts when the activity was activated by an Intent. The object ' S 

Normally, the life cycle of an Android control is determined by the operating system. For example, the life cycle of an activity object starts when it is started by a intent, when the OnCreate () method of the activity object is called, followed by the Onresume (); When the user launches another application, The activity's OnPause () method is called, and if the activity's code calls the Finish () method, the OnDestroy () method is called.

The Android API framework does not provide a way for your code to invoke these callback functions directly, but you can do this with instrumentation.

Also, the system runs all the components of a application into the same process. You can allow some components, such as content providers, to run in a separate process, but can ' t force an application To run in the same process as another application, which is already running.

Furthermore, the operating system runs all the controls of an application in the same process. You can allow some (special) controls to run in different processes, such as content providers, but you can't run an app in the same process as another application that is already running.

With Android instrumentation, though, you can invoke callback methods in your test code. This allows your to run through the lifecycle of a component step by step, as if you were debugging the component. The following test code snippet demonstrates how to use this to test, an Activity saves and restores it state:

If you use instrumentation, you can call these callback functions in your test code. This will allow you to run through the entire life cycle of the control as if you were debugging the control in one step. The following code snippet shows how to use instrumentation to test the control of an activity to save and restore its state:

    //Start The main activity of the application under testmactivity=getactivity();    //Get a handle to the Activity object ' s main UI widget, a SpinnerMspinner= (Spinner)mactivity.Findviewbyid(com.Android.Example.Spinner.R.ID.Spinner01);    //Set the Spinner to a known positionmactivity.setspinnerposition(test_state_destroy_position);    //Stop the Activity-the OnDestroy () method should save the state of the Spinnermactivity.Finish();    //Re-start the Activity-the Onresume () method should restore the state of the Spinnermactivity=getactivity();    //Get The Spinner ' s current position    intcurrentposition=mactivity.getspinnerposition();    //Assert that the current position is the same as the starting positionassertequals(test_state_destroy_position,currentposition);

The key method used getActivity() is, which are a part of the instrumentation API. The Activity under test is not started until your call this method. You can set up the "test fixture in advance," and then "this method to start the Activity."

The key method used here is the getactivity () in the instrumentation API, and the activity is not started when you do not call this method. You can also configure the environment that the test requires before you call this method to start the activity.

Also, instrumentation can load both a test package and the application under test into the same process. Since the application components and their tests is in the same process, the tests can invoke methods in the components, and modify and examine the components.

At the same time, instrumentation can load the test package and the application being tested to run in the same process. Now that the controls and the test code are running in the same process, the test code can of course invoke the methods of the controls, while modifying and validating some of these controls.

Instrumentation an official profile of Android (personally considered to be the most concise description of Highlevel abstraction)

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.