Can only be an analysis, dare not say what analysis, compare the online big god detailed analysis, can only from the whole to analyze the relevant things, when can depth of the algorithm level it?
Overview
Before we analyze the source code, let's take a look at what kind of class Robotium gives us.
Entrance
After having a sense of the whole, we go from the solo class, because Solo is the Robotium class and the most important class, let's look at the construction method:
Private Solo(config config, instrumentation instrumentation, activity activity) {if(config.commandlogging) {LOG.D (Config.commandloggingtag,"Solo ("+config+", "+instrumentation+", "+activity+")"); } This. config = (config = =NULL) ?NewConfig (): config; This. instrumentation = instrumentation; This. Sleeper =NewSleeper (); This. Sender =NewSender (instrumentation, sleeper); This. Activityutils =NewActivityutils (config, instrumentation, activity, sleeper); This. Viewfetcher =NewViewfetcher (instrumentation, sleeper); This. Screenshottaker =NewScreenshottaker (config, instrumentation, activityutils, Viewfetcher, sleeper); This. Dialogutils =NewDialogutils (instrumentation, activityutils, Viewfetcher, sleeper); This. Webutils =NewWebutils (config, instrumentation,viewfetcher, sleeper); This. scroller =NewScroller (config, instrumentation, viewfetcher, sleeper); This. Searcher =NewSearcher (Viewfetcher, Webutils, scroller, sleeper); This. Waiter =NewWaiter (instrumentation, activityutils, Viewfetcher, Searcher,scroller, sleeper); This. Getter =NewGetter (instrumentation, activityutils, waiter); This. clicker =NewClicker (Activityutils, Viewfetcher,sender, instrumentation, sleeper, waiter, webutils, dialogutils); This. Setter =NewSetter (Activityutils, Getter, clicker, waiter); This. Asserter =NewAsserter (Activityutils, waiter); This. Checker =NewChecker (Viewfetcher, waiter); This. Zoomer =NewZoomer (instrumentation); This. Swiper =NewSwiper (instrumentation); This. Tapper =NewTapper (instrumentation); This. Illustrator =NewIllustrator (instrumentation); This. Rotator =NewRotator (instrumentation); This. Presser =NewPresser (Viewfetcher, clicker, instrumentation, sleeper, waiter, dialogutils); This. Textenterer =NewTextenterer (instrumentation, clicker, dialogutils); This. Systemutils =NewSystemutils (instrumentation); Initialize (); }
The overloaded construction method is excluded, and you can see that when you initialize a solo object, three parameters are passed in, and the Config object
Instrumentation object, Activity object. The latter two do not say much, in the Learning Instrumentation framework when the instrumentation object is injected into the test application before the operation, and the importance of the activity is not much to say, so the left config, This object is a set of parameters that run Robotium, let's see what we have:
Public Static class Config { /** * The timeout length of the Get, is, set, Assert, enter and click Methods. Default length is ten milliseconds. */ Public intTimeout_small =10000;/** * The timeout length of the waitFor methods. Default length is milliseconds. */ Public intTimeout_large =20000;/** * The screenshot save path. Default save path is/sdcard/robotium-screenshots/. */ PublicString Screenshotsavepath = environment.getexternalstoragedirectory () +"/robotium-screenshots/";/** * The screenshot file type, JPEG or PNG. Use Screenshotfiletype.jpeg or screenshotfiletype.png. Default file type is JPEG. */ PublicScreenshotfiletype screenshotfiletype = screenshotfiletype.jpeg;/** * Set to True if the get, is, set, enter, type and click methods should scroll. Default value is true. */ Public BooleanShouldscroll =true;/** * Set to True if JavaScript should is used to click WebElements. Default value is false. */ Public BooleanUsejavascripttoclickwebelements =false;/** * The screenshot file type, JPEG or PNG. * * @author renas Reda, [email protected] * */ Public enumScreenshotfiletype {JPEG, PNG}/** * Set to TRUE if Activity tracking should is enabled. Default value is true. */ Public BooleanTrackactivities =true;/** * Set the Web frame to being used by Robotium. Default value is document. */ PublicString Webframe ="Document";/** * Set to True if logging should is enabled. Default value is false. */ Public BooleanCommandlogging =false;/** * The command logging tag. Default value is "Robotium". */ PublicString Commandloggingtag ="Robotium"; }
After that is the instantiation of some classes, and the Initialize () method is the time-out initialization.
After completing the initialization analysis, we continue to study the solo class, which is divided into the following categories after reading through the interface:
- List Contents
- Construct initialization
- Assertion
- Clear
- Click – the Clickon method will be called
- Dragging
- Inject text (typetext/entertext)
- Shut down
- Get class –getview
- Fallback
- Check class
- Press pressing class
- Scrolling Scroll class
- Search class
- Set class
- Class
- Wait for class
Robotium Source code class analysis
WebView related did not look, because no use
Asserter:
Robotium gives us two new assert methods, of course defined in the Asserter class:
Assertcurrentactivity and Assertmemorynotlow
Assertcurrentactivity: Check if the current activity is my desired activity, essentially taking out the current activity instance and GetClassName, Finally, the Assert.assertequals method is used to determine
Activity activity = activityUtils.getCurrentActivity(); if(activity != null){Assert.assertEquals(message, name, activity.getClass().getSimpleName());
Assertmemorynotlow: Check whether the current system is out of memory, enough to pass, the principle is through
Activitymanager.memoryinfo object to check
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();((ActivityManager)activityUtils.getCurrentActivity().getSystemService("activity")).getMemoryInfo(mi);
Checker:
This class is used to determine the state of a view, and we give an example of one of the methods:
This is the decision button can be clicked, waiter method is to filter the function, and converted to view, the last call is actually the widget's own property state
Another example of this is the other way
Clicker:
Click Method has always been the focus of automation, directly see Clickonscreen
Because according to the principle, after finding the desired view, it will eventually turn to XY coordinates, and call the above method to click on the screen. The essence of Clickonscreen is to invoke the instrumentation Sendpointsync method to realize the injection point event.
Of course, there are still two ways to call the tap screen.
Clicklongontextandpress
Clickonactionbaritem/clickonactionbarhomebutton
Scroller:
Scrolling classes, including two operation drag and some scrolling actions
The action performed by drag is down–>move–>up, called, or Sendpointsync method
As for the scrolling operation of the list, through the Runonmainsync blocking method, the main thread directly calls the view's
The SetSelection method rolls directly to the nth row.
Searcher:
The class that is responsible for locating and locating the control, which is mainly based on the text to get the text control, and then return a view, mainly see searchfor this method.
So the principle of this method is to get all the controls through the Viewfetcher.getcurrentviews (Viewclass, True) method, and then traverse all the controls, passing the regular matching control, when the match is back to the view, which is the return value above, and again, It is important to note that the lookup will provide automatic rolling operation, only need to set the incoming parameter to true, it is more convenient.
Textenterer:
Robotium supports two types of text injection, and the API has already told us
The former is the SetText method that we often use for unit testing, while the latter calls the instrumentation Sendstringsync method injection, and Sendstringsync divides the string into a single character, Then loop through each call to the Sendkeysync method, so call the second method to inject you can see the input trajectory
Viewfetcher:
This encapsulates all methods for finding the view.
Then through the above can be known through WindowManager related methods to obtain the inside of the decorviews, that is, the control tree, the rest of the method is how to filter the contents of the decorviews inside, do not say much.
Summarize:
So all the way down, you can find this rule.
1. The lookup control is obtained by obtaining the decorviews in the window and then iterating through the comparison
2. After finding the control, the view is converted to XY coordinates, and finally the Instru Sendpointsync method is called to tap the screen
Brief analysis of Robotium whole source code