Lao Li Recommendation: 8th Chapter 6, "Monkeyrunner Source Analysis" Monkeyrunner start running process-Start Monkey 2

Source: Internet
Author: User

With the following basic knowledge, we can analyze the code to illustrate how monkey is started in a series of calls that the user calls Monkeyrunner.waitforconnection, Let's take a look at this waitforconnection method of Monkeyrunner:

$ Public static Monkeydevice waitforconnection (pyobject[] args, string[] kws)

63 {

Argparser ap = Jythonutils.createargparser (args, KWS);

Preconditions.checknotnull (AP);

Timeoutms Long;

Try

68 {

Double timeoutinsecs = Jythonutils.getfloat (AP, 0);

TIMEOUTMS = (Timeoutinsecs * 1000.0D);

(pyexception e) {

Timeoutms = Long.max_value;

73}

74

Ichimpdevice device = chimpchat.waitforconnection (Timeoutms, ap.getstring (1, ". *"));

76

Monkeydevice Chimpdevice = new Monkeydevice (device);

Chimpdevice return;

79}

Code 8-6-1 monkeyrunner-waitforconnection

These classes, such as scripts called Monkeyrunner,monkeydevice, are written in Java, and the scripts themselves are written through Jython (which can be called Java Python), so their previous arguments need to have a mechanism for conversion. As to how they are converted is not the focus, so I myself have not studied the implementation principle of Jython, because this does not affect my understanding of the Monkeyrunner framework. OK, let's continue the analysis of the above code:

    • Line 75: Call the Waitforconnection method of the Chimpchat object to get a Adbchimpdevice instance of a high-level abstract device
    • 79-78 rows: Pass the above Adbchimpdevice instance as a parameter into Monkeydevice to construct a Monkedevice object and return it to the test code so that the test code can manipulate the Monkeydevice instance to control the target device.

We analyzed the Waitforconnection method of Chimpchat, and as for the Monkeydevice constructor, we will make a detailed analysis of the operation principle of the whole monkeydevice in the next chapter, so there is no repetition here.

ichimpdevice waitforconnection (long Timeoutms, String deviceId)

90 {

this.mBackend.waitForConnection return (Timeoutms, deviceId);

92}

98 Public Ichimpdevice waitforconnection ()

99 {

return this.mBackend.waitForConnection (2147483647L, ". *");

101}

Code 8-6-2 chimpchat-waitforconnection

Chimpchat provides two waitforconnection methods, one of which is without parameters, equivalent to the user calling Monkeydevice.waitforconnection () directly in the script; the other is a long-type timeout with a parameter and a device serial number. No matter which method Chimpchat is very simple only one row , call is Mbackend waitforconnection, but if the user does not provide parameters, Chimpchat will default initialization time-out and the device serial number parameters, Where the device serial number is initialized to a regular expression ". *", which represents any device first found. The mbackend here is the Adbbackend object that was instantiated in the previous analysis of the "Start Androiddebugbridge" process, so we are going to navigate to the Waitforconnection method of the class.

ichimpdevice waitforconnection (long Timeoutms, String Deviceidregex)

117 {

118 do {

119 IDevice device = Findattacheddevice (Deviceidregex);

120

121 if (device! = null) && (device.getstate () = = IDevice.DeviceState.ONLINE)) {

122 Ichimpdevice Chimpdevice =

New Adbchimpdevice (device);

123 This.devices.add (Chimpdevice);

124 return chimpdevice;

125}

126 Try

127 {

Thread.Sleep (200L);

129} catch (Interruptedexception e) {

LOG.log (Level.severe, "Error sleeping", e);

131}

Timeoutms-= 200L;

133} while (Timeoutms > 0L);

134

135

136 return null;

137}

Code 8-6-3 adbbackend-waitforconnection

The method does a while loop that waits until a device that matches the user's or default serial number knows the timeout, and if it finds the device, it constructs a Adbchimpdevice instance object with the device as a parameter and returns:

    • 133 Line: Enter the while loop, and if the target device is not connected within the timeout period, keep looping
    • 128 and 132 rows: every cycle if no target device is connected, sleep 0.2 seconds before the next cycle
    • 119 Line: Find the target device unit based on the supplied device serial number (regular expression), where can I find it? Actually, it's going to devicemonitor. Monitor thread object maintenance in the latest device devices queue to find
    • 122 rows: After locating the target device, the device object is passed into the Adbchimpdevice constructor to instantiate the Androidchimpdevice object and return. We'll see that Adbchimpdevice will implement the Chimpmanager object to initiate monkey communication during instantiation.

Lao Li Recommendation: 8th Chapter 6, "Monkeyrunner Source Analysis" Monkeyrunner start running process-Start Monkey 2

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.