idx
int64
0
41.2k
question
stringlengths
74
4.21k
target
stringlengths
5
888
22,600
public boolean scrollWebView ( final WebView webView , int direction , final boolean allTheWay ) { if ( direction == DOWN ) { inst . runOnMainSync ( new Runnable ( ) { public void run ( ) { canScroll = webView . pageDown ( allTheWay ) ; } } ) ; } if ( direction == UP ) { inst . runOnMainSync ( new Runnable ( ) { public void run ( ) { canScroll = webView . pageUp ( allTheWay ) ; } } ) ; } return canScroll ; }
Scrolls a WebView .
22,601
public < T extends AbsListView > boolean scrollList ( T absListView , int direction , boolean allTheWay ) { if ( absListView == null ) { return false ; } if ( direction == DOWN ) { int listCount = absListView . getCount ( ) ; int lastVisiblePosition = absListView . getLastVisiblePosition ( ) ; if ( allTheWay ) { scrollListToLine ( absListView , listCount - 1 ) ; return false ; } if ( lastVisiblePosition >= listCount - 1 ) { if ( lastVisiblePosition > 0 ) { scrollListToLine ( absListView , lastVisiblePosition ) ; } return false ; } int firstVisiblePosition = absListView . getFirstVisiblePosition ( ) ; if ( firstVisiblePosition != lastVisiblePosition ) scrollListToLine ( absListView , lastVisiblePosition ) ; else scrollListToLine ( absListView , firstVisiblePosition + 1 ) ; } else if ( direction == UP ) { int firstVisiblePosition = absListView . getFirstVisiblePosition ( ) ; if ( allTheWay || firstVisiblePosition < 2 ) { scrollListToLine ( absListView , 0 ) ; return false ; } int lastVisiblePosition = absListView . getLastVisiblePosition ( ) ; final int lines = lastVisiblePosition - firstVisiblePosition ; int lineToScrollTo = firstVisiblePosition - lines ; if ( lineToScrollTo == lastVisiblePosition ) lineToScrollTo -- ; if ( lineToScrollTo < 0 ) lineToScrollTo = 0 ; scrollListToLine ( absListView , lineToScrollTo ) ; } sleeper . sleep ( ) ; return true ; }
Scrolls a list .
22,602
public < T extends AbsListView > void scrollListToLine ( final T view , final int line ) { if ( view == null ) Assert . fail ( "AbsListView is null!" ) ; final int lineToMoveTo ; if ( view instanceof GridView ) { lineToMoveTo = line + 1 ; } else { lineToMoveTo = line ; } inst . runOnMainSync ( new Runnable ( ) { public void run ( ) { view . setSelection ( lineToMoveTo ) ; } } ) ; }
Scroll the list to a given line
22,603
public void scrollViewToSide ( View view , Side side , float scrollPosition , int stepCount ) { int [ ] corners = new int [ 2 ] ; view . getLocationOnScreen ( corners ) ; int viewHeight = view . getHeight ( ) ; int viewWidth = view . getWidth ( ) ; float x = corners [ 0 ] + viewWidth * scrollPosition ; float y = corners [ 1 ] + viewHeight / 2.0f ; if ( side == Side . LEFT ) drag ( corners [ 0 ] , x , y , y , stepCount ) ; else if ( side == Side . RIGHT ) drag ( x , corners [ 0 ] , y , y , stepCount ) ; }
Scrolls view horizontally .
22,604
private View getScreenshotView ( ) { View decorView = viewFetcher . getRecentDecorView ( viewFetcher . getWindowDecorViews ( ) ) ; final long endTime = SystemClock . uptimeMillis ( ) + Timeout . getSmallTimeout ( ) ; while ( decorView == null ) { final boolean timedOut = SystemClock . uptimeMillis ( ) > endTime ; if ( timedOut ) { return null ; } sleeper . sleepMini ( ) ; decorView = viewFetcher . getRecentDecorView ( viewFetcher . getWindowDecorViews ( ) ) ; } wrapAllGLViews ( decorView ) ; return decorView ; }
Gets the proper view to use for a screenshot .
22,605
private void wrapAllGLViews ( View decorView ) { ArrayList < GLSurfaceView > currentViews = viewFetcher . getCurrentViews ( GLSurfaceView . class , true , decorView ) ; final CountDownLatch latch = new CountDownLatch ( currentViews . size ( ) ) ; for ( GLSurfaceView glView : currentViews ) { Object renderContainer = new Reflect ( glView ) . field ( "mGLThread" ) . type ( GLSurfaceView . class ) . out ( Object . class ) ; Renderer renderer = new Reflect ( renderContainer ) . field ( "mRenderer" ) . out ( Renderer . class ) ; if ( renderer == null ) { renderer = new Reflect ( glView ) . field ( "mRenderer" ) . out ( Renderer . class ) ; renderContainer = glView ; } if ( renderer == null ) { latch . countDown ( ) ; continue ; } if ( renderer instanceof GLRenderWrapper ) { GLRenderWrapper wrapper = ( GLRenderWrapper ) renderer ; wrapper . setTakeScreenshot ( ) ; wrapper . setLatch ( latch ) ; } else { GLRenderWrapper wrapper = new GLRenderWrapper ( glView , renderer , latch ) ; new Reflect ( renderContainer ) . field ( "mRenderer" ) . in ( wrapper ) ; } } try { latch . await ( ) ; } catch ( InterruptedException ex ) { ex . printStackTrace ( ) ; } }
Extract and wrap the all OpenGL ES Renderer .
22,606
private Bitmap getBitmapOfWebView ( final WebView webView ) { Picture picture = webView . capturePicture ( ) ; Bitmap b = Bitmap . createBitmap ( picture . getWidth ( ) , picture . getHeight ( ) , Bitmap . Config . ARGB_8888 ) ; Canvas c = new Canvas ( b ) ; picture . draw ( c ) ; return b ; }
Returns a bitmap of a given WebView .
22,607
private Bitmap getBitmapOfView ( final View view ) { view . destroyDrawingCache ( ) ; view . buildDrawingCache ( false ) ; Bitmap orig = view . getDrawingCache ( ) ; Bitmap . Config config = null ; if ( orig == null ) { return null ; } config = orig . getConfig ( ) ; if ( config == null ) { config = Bitmap . Config . ARGB_8888 ; } Bitmap b = orig . copy ( config , false ) ; orig . recycle ( ) ; view . destroyDrawingCache ( ) ; return b ; }
Returns a bitmap of a given View .
22,608
private String getFileName ( final String name ) { SimpleDateFormat sdf = new SimpleDateFormat ( "ddMMyy-hhmmss" ) ; String fileName = null ; if ( name == null ) { if ( config . screenshotFileType == ScreenshotFileType . JPEG ) { fileName = sdf . format ( new Date ( ) ) . toString ( ) + ".jpg" ; } else { fileName = sdf . format ( new Date ( ) ) . toString ( ) + ".png" ; } } else { if ( config . screenshotFileType == ScreenshotFileType . JPEG ) { fileName = name + ".jpg" ; } else { fileName = name + ".png" ; } } return fileName ; }
Returns a proper filename depending on if name is given or not .
22,609
private void initScreenShotSaver ( ) { if ( screenShotSaverThread == null || screenShotSaver == null ) { screenShotSaverThread = new HandlerThread ( "ScreenShotSaver" ) ; screenShotSaverThread . start ( ) ; screenShotSaver = new ScreenShotSaver ( screenShotSaverThread ) ; } }
This method initializes the aysnc screenshot saving logic
22,610
private void createStackAndPushStartActivity ( ) { activityStack = new Stack < WeakReference < Activity > > ( ) ; if ( activity != null && config . trackActivities ) { WeakReference < Activity > weakReference = new WeakReference < Activity > ( activity ) ; activity = null ; activityStack . push ( weakReference ) ; } }
Creates a new activity stack and pushes the start activity .
22,611
private void setupActivityMonitor ( ) { if ( config . trackActivities ) { try { IntentFilter filter = null ; activityMonitor = inst . addMonitor ( filter , null , false ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } }
This is were the activityMonitor is set up . The monitor will keep check for the currently active activity .
22,612
private void removeActivityFromStack ( Activity activity ) { Iterator < WeakReference < Activity > > activityStackIterator = activityStack . iterator ( ) ; while ( activityStackIterator . hasNext ( ) ) { Activity activityFromWeakReference = activityStackIterator . next ( ) . get ( ) ; if ( activityFromWeakReference == null ) { activityStackIterator . remove ( ) ; } if ( activity != null && activityFromWeakReference != null && activityFromWeakReference . equals ( activity ) ) { activityStackIterator . remove ( ) ; } } }
Removes a given activity from the activity stack
22,613
private void addActivityToStack ( Activity activity ) { activitiesStoredInActivityStack . push ( activity . toString ( ) ) ; weakActivityReference = new WeakReference < Activity > ( activity ) ; activity = null ; activityStack . push ( weakActivityReference ) ; }
Adds an activity to the stack
22,614
private final void waitForActivityIfNotAvailable ( ) { if ( activityStack . isEmpty ( ) || activityStack . peek ( ) . get ( ) == null ) { if ( activityMonitor != null ) { Activity activity = activityMonitor . getLastActivity ( ) ; while ( activity == null ) { sleeper . sleepMini ( ) ; activity = activityMonitor . getLastActivity ( ) ; } addActivityToStack ( activity ) ; } else if ( config . trackActivities ) { sleeper . sleepMini ( ) ; setupActivityMonitor ( ) ; waitForActivityIfNotAvailable ( ) ; } } }
Waits for an activity to be started if one is not provided by the constructor .
22,615
public String getString ( int resId ) { Activity activity = getCurrentActivity ( false ) ; if ( activity == null ) { return "" ; } return activity . getString ( resId ) ; }
Returns a localized string .
22,616
private void stopActivityMonitor ( ) { try { if ( activityMonitor != null ) { inst . removeMonitor ( activityMonitor ) ; activityMonitor = null ; } } catch ( Exception ignored ) { } }
Removes the ActivityMonitor
22,617
public void finishOpenedActivities ( ) { activitySyncTimer . cancel ( ) ; if ( ! config . trackActivities ) { useGoBack ( 3 ) ; return ; } ArrayList < Activity > activitiesOpened = getAllOpenedActivities ( ) ; for ( int i = activitiesOpened . size ( ) - 1 ; i >= 0 ; i -- ) { sleeper . sleep ( MINISLEEP ) ; finishActivity ( activitiesOpened . get ( i ) ) ; } activitiesOpened = null ; sleeper . sleep ( MINISLEEP ) ; finishActivity ( getCurrentActivity ( true , false ) ) ; stopActivityMonitor ( ) ; setRegisterActivities ( false ) ; this . activity = null ; sleeper . sleepMini ( ) ; useGoBack ( 1 ) ; clearActivityStack ( ) ; }
All activites that have been opened are finished .
22,618
private void useGoBack ( int numberOfTimes ) { for ( int i = 0 ; i < numberOfTimes ; i ++ ) { try { inst . sendKeyDownUpSync ( KeyEvent . KEYCODE_BACK ) ; sleeper . sleep ( MINISLEEP ) ; inst . sendKeyDownUpSync ( KeyEvent . KEYCODE_BACK ) ; } catch ( Throwable ignored ) { } } }
Sends the back button command a given number of times
22,619
private void finishActivity ( Activity activity ) { if ( activity != null ) { try { activity . finish ( ) ; } catch ( Throwable e ) { e . printStackTrace ( ) ; } } }
Finishes an activity .
22,620
public View getScrollOrListParent ( View view ) { if ( ! ( view instanceof android . widget . AbsListView ) && ! ( view instanceof android . widget . ScrollView ) && ! ( view instanceof WebView ) ) { try { return getScrollOrListParent ( ( View ) view . getParent ( ) ) ; } catch ( Exception e ) { return null ; } } else { return view ; } }
Returns the scroll or list parent view
22,621
public ArrayList < View > getAllViews ( boolean onlySufficientlyVisible ) { final View [ ] views = getWindowDecorViews ( ) ; final ArrayList < View > allViews = new ArrayList < View > ( ) ; final View [ ] nonDecorViews = getNonDecorViews ( views ) ; View view = null ; if ( nonDecorViews != null ) { for ( int i = 0 ; i < nonDecorViews . length ; i ++ ) { view = nonDecorViews [ i ] ; try { addChildren ( allViews , ( ViewGroup ) view , onlySufficientlyVisible ) ; } catch ( Exception ignored ) { } if ( view != null ) allViews . add ( view ) ; } } if ( views != null && views . length > 0 ) { view = getRecentDecorView ( views ) ; try { addChildren ( allViews , ( ViewGroup ) view , onlySufficientlyVisible ) ; } catch ( Exception ignored ) { } if ( view != null ) allViews . add ( view ) ; } return allViews ; }
Returns views from the shown DecorViews .
22,622
public final View getRecentDecorView ( View [ ] views ) { if ( views == null ) return null ; final View [ ] decorViews = new View [ views . length ] ; int i = 0 ; View view ; for ( int j = 0 ; j < views . length ; j ++ ) { view = views [ j ] ; if ( isDecorView ( view ) ) { decorViews [ i ] = view ; i ++ ; } } return getRecentContainer ( decorViews ) ; }
Returns the most recent DecorView
22,623
private final View getRecentContainer ( View [ ] views ) { View container = null ; long drawingTime = 0 ; View view ; for ( int i = 0 ; i < views . length ; i ++ ) { view = views [ i ] ; if ( view != null && view . isShown ( ) && view . hasWindowFocus ( ) && view . getDrawingTime ( ) > drawingTime ) { container = view ; drawingTime = view . getDrawingTime ( ) ; } } return container ; }
Returns the most recent view container
22,624
private final View [ ] getNonDecorViews ( View [ ] views ) { View [ ] decorViews = null ; if ( views != null ) { decorViews = new View [ views . length ] ; int i = 0 ; View view ; for ( int j = 0 ; j < views . length ; j ++ ) { view = views [ j ] ; if ( ! isDecorView ( view ) ) { decorViews [ i ] = view ; i ++ ; } } } return decorViews ; }
Returns all views that are non DecorViews
22,625
private boolean isDecorView ( View view ) { if ( view == null ) { return false ; } final String nameOfClass = view . getClass ( ) . getName ( ) ; return ( nameOfClass . equals ( "com.android.internal.policy.impl.PhoneWindow$DecorView" ) || nameOfClass . equals ( "com.android.internal.policy.impl.MultiPhoneWindow$MultiPhoneDecorView" ) || nameOfClass . equals ( "com.android.internal.policy.PhoneWindow$DecorView" ) ) ; }
Returns whether a view is a DecorView
22,626
@ SuppressWarnings ( "deprecation" ) public float getScrollListWindowHeight ( View view ) { final int [ ] xyParent = new int [ 2 ] ; View parent = getScrollOrListParent ( view ) ; final float windowHeight ; if ( parent == null ) { WindowManager windowManager = ( WindowManager ) instrumentation . getTargetContext ( ) . getSystemService ( Context . WINDOW_SERVICE ) ; windowHeight = windowManager . getDefaultDisplay ( ) . getHeight ( ) ; } else { parent . getLocationOnScreen ( xyParent ) ; windowHeight = xyParent [ 1 ] + parent . getHeight ( ) ; } parent = null ; return windowHeight ; }
Returns the height of the scroll or list view parent
22,627
public final < T extends View > T getFreshestView ( ArrayList < T > views ) { final int [ ] locationOnScreen = new int [ 2 ] ; T viewToReturn = null ; long drawingTime = 0 ; if ( views == null ) { return null ; } for ( T view : views ) { if ( view != null ) { view . getLocationOnScreen ( locationOnScreen ) ; if ( locationOnScreen [ 0 ] < 0 || ! ( view . getHeight ( ) > 0 ) ) { continue ; } if ( view . getDrawingTime ( ) > drawingTime ) { drawingTime = view . getDrawingTime ( ) ; viewToReturn = view ; } else if ( view . getDrawingTime ( ) == drawingTime ) { if ( view . isFocused ( ) ) { viewToReturn = view ; } } } } views = null ; return viewToReturn ; }
Tries to guess which view is the most likely to be interesting . Returns the most recently drawn view which presumably will be the one that the user was most recently interacting with .
22,628
public < T extends View > ViewGroup getRecyclerView ( int recyclerViewIndex , int timeOut ) { final long endTime = SystemClock . uptimeMillis ( ) + timeOut ; while ( SystemClock . uptimeMillis ( ) < endTime ) { View recyclerView = getRecyclerView ( true , recyclerViewIndex ) ; if ( recyclerView != null ) { return ( ViewGroup ) recyclerView ; } } return null ; }
Waits for a RecyclerView and returns it .
22,629
public View getRecyclerView ( boolean shouldSleep , int recyclerViewIndex ) { Set < View > uniqueViews = new HashSet < View > ( ) ; if ( shouldSleep ) { sleeper . sleep ( ) ; } @ SuppressWarnings ( "unchecked" ) ArrayList < View > views = RobotiumUtils . filterViewsToSet ( new Class [ ] { ViewGroup . class } , getAllViews ( false ) ) ; views = RobotiumUtils . removeInvisibleViews ( views ) ; for ( View view : views ) { if ( isViewType ( view . getClass ( ) , "widget.RecyclerView" ) ) { uniqueViews . add ( view ) ; } if ( uniqueViews . size ( ) > recyclerViewIndex ) { return ( ViewGroup ) view ; } } return null ; }
Returns a RecyclerView or null if none is found
22,630
public List < View > getScrollableSupportPackageViews ( boolean shouldSleep ) { List < View > viewsToReturn = new ArrayList < View > ( ) ; if ( shouldSleep ) { sleeper . sleep ( ) ; } @ SuppressWarnings ( "unchecked" ) ArrayList < View > views = RobotiumUtils . filterViewsToSet ( new Class [ ] { ViewGroup . class } , getAllViews ( true ) ) ; views = RobotiumUtils . removeInvisibleViews ( views ) ; for ( View view : views ) { if ( isViewType ( view . getClass ( ) , "widget.RecyclerView" ) || isViewType ( view . getClass ( ) , "widget.NestedScrollView" ) ) { viewsToReturn . add ( view ) ; } } return viewsToReturn ; }
Returns a Set of all RecyclerView or empty Set if none is found
22,631
public View getIdenticalView ( View view ) { if ( view == null ) { return null ; } View viewToReturn = null ; List < ? extends View > visibleViews = RobotiumUtils . removeInvisibleViews ( getCurrentViews ( view . getClass ( ) , true ) ) ; for ( View v : visibleViews ) { if ( areViewsIdentical ( v , view ) ) { viewToReturn = v ; break ; } } return viewToReturn ; }
Returns an identical View to the one specified .
22,632
@ SuppressWarnings ( "unchecked" ) public View [ ] getWindowDecorViews ( ) { Field viewsField ; Field instanceField ; try { viewsField = windowManager . getDeclaredField ( "mViews" ) ; instanceField = windowManager . getDeclaredField ( windowManagerString ) ; viewsField . setAccessible ( true ) ; instanceField . setAccessible ( true ) ; Object instance = instanceField . get ( null ) ; View [ ] result ; if ( android . os . Build . VERSION . SDK_INT >= 19 ) { result = ( ( ArrayList < View > ) viewsField . get ( instance ) ) . toArray ( new View [ 0 ] ) ; } else { result = ( View [ ] ) viewsField . get ( instance ) ; } return result ; } catch ( Exception e ) { e . printStackTrace ( ) ; } return null ; }
Returns the WindorDecorViews shown on the screen .
22,633
private void setWindowManagerString ( ) { if ( android . os . Build . VERSION . SDK_INT >= 17 ) { windowManagerString = "sDefaultWindowManager" ; } else if ( android . os . Build . VERSION . SDK_INT >= 13 ) { windowManagerString = "sWindowManager" ; } else { windowManagerString = "mWindowManager" ; } }
Sets the window manager string .
22,634
public static < T extends View > ArrayList < T > removeInvisibleViews ( Iterable < T > viewList ) { ArrayList < T > tmpViewList = new ArrayList < T > ( ) ; for ( T view : viewList ) { if ( view != null && view . isShown ( ) ) { tmpViewList . add ( view ) ; } } return tmpViewList ; }
Removes invisible Views .
22,635
public static < T > ArrayList < T > filterViews ( Class < T > classToFilterBy , Iterable < ? > viewList ) { ArrayList < T > filteredViews = new ArrayList < T > ( ) ; for ( Object view : viewList ) { if ( view != null && classToFilterBy . isAssignableFrom ( view . getClass ( ) ) ) { filteredViews . add ( classToFilterBy . cast ( view ) ) ; } } viewList = null ; return filteredViews ; }
Filters Views based on the given class type .
22,636
public static ArrayList < View > filterViewsToSet ( Class < View > classSet [ ] , Iterable < View > viewList ) { ArrayList < View > filteredViews = new ArrayList < View > ( ) ; for ( View view : viewList ) { if ( view == null ) continue ; for ( Class < View > filter : classSet ) { if ( filter . isAssignableFrom ( view . getClass ( ) ) ) { filteredViews . add ( view ) ; break ; } } } return filteredViews ; }
Filters all Views not within the given set .
22,637
public static void sortViewsByLocationOnScreen ( List < ? extends View > views , boolean yAxisFirst ) { Collections . sort ( views , new ViewLocationComparator ( yAxisFirst ) ) ; }
Orders Views by their location on - screen .
22,638
public static int getNumberOfMatches ( String regex , TextView view , Set < TextView > uniqueTextViews ) { if ( view == null ) { return uniqueTextViews . size ( ) ; } Pattern pattern = null ; try { pattern = Pattern . compile ( regex ) ; } catch ( PatternSyntaxException e ) { pattern = Pattern . compile ( regex , Pattern . LITERAL ) ; } Matcher matcher = pattern . matcher ( view . getText ( ) . toString ( ) ) ; if ( matcher . find ( ) ) { uniqueTextViews . add ( view ) ; } if ( view . getError ( ) != null ) { matcher = pattern . matcher ( view . getError ( ) . toString ( ) ) ; if ( matcher . find ( ) ) { uniqueTextViews . add ( view ) ; } } if ( view . getText ( ) . toString ( ) . equals ( "" ) && view . getHint ( ) != null ) { matcher = pattern . matcher ( view . getHint ( ) . toString ( ) ) ; if ( matcher . find ( ) ) { uniqueTextViews . add ( view ) ; } } return uniqueTextViews . size ( ) ; }
Checks if a View matches a certain string and returns the amount of total matches .
22,639
public void clickOnScreen ( float x , float y , View view ) { boolean successfull = false ; int retry = 0 ; SecurityException ex = null ; while ( ! successfull && retry < 20 ) { long downTime = SystemClock . uptimeMillis ( ) ; long eventTime = SystemClock . uptimeMillis ( ) ; MotionEvent event = MotionEvent . obtain ( downTime , eventTime , MotionEvent . ACTION_DOWN , x , y , 0 ) ; MotionEvent event2 = MotionEvent . obtain ( downTime , eventTime , MotionEvent . ACTION_UP , x , y , 0 ) ; try { inst . sendPointerSync ( event ) ; inst . sendPointerSync ( event2 ) ; successfull = true ; } catch ( SecurityException e ) { ex = e ; dialogUtils . hideSoftKeyboard ( null , false , true ) ; sleeper . sleep ( MINI_WAIT ) ; retry ++ ; View identicalView = viewFetcher . getIdenticalView ( view ) ; if ( identicalView != null ) { float [ ] xyToClick = getClickCoordinates ( identicalView ) ; x = xyToClick [ 0 ] ; y = xyToClick [ 1 ] ; } } } if ( ! successfull ) { Assert . fail ( "Click at (" + x + ", " + y + ") can not be completed! (" + ( ex != null ? ex . getClass ( ) . getName ( ) + ": " + ex . getMessage ( ) : "null" ) + ")" ) ; } }
Clicks on a given coordinate on the screen .
22,640
public void clickLongOnScreen ( float x , float y , int time , View view ) { boolean successfull = false ; int retry = 0 ; SecurityException ex = null ; long downTime = SystemClock . uptimeMillis ( ) ; long eventTime = SystemClock . uptimeMillis ( ) ; MotionEvent event = MotionEvent . obtain ( downTime , eventTime , MotionEvent . ACTION_DOWN , x , y , 0 ) ; while ( ! successfull && retry < 20 ) { try { inst . sendPointerSync ( event ) ; successfull = true ; sleeper . sleep ( MINI_WAIT ) ; } catch ( SecurityException e ) { ex = e ; dialogUtils . hideSoftKeyboard ( null , false , true ) ; sleeper . sleep ( MINI_WAIT ) ; retry ++ ; View identicalView = viewFetcher . getIdenticalView ( view ) ; if ( identicalView != null ) { float [ ] xyToClick = getClickCoordinates ( identicalView ) ; x = xyToClick [ 0 ] ; y = xyToClick [ 1 ] ; } } } if ( ! successfull ) { Assert . fail ( "Long click at (" + x + ", " + y + ") can not be completed! (" + ( ex != null ? ex . getClass ( ) . getName ( ) + ": " + ex . getMessage ( ) : "null" ) + ")" ) ; } eventTime = SystemClock . uptimeMillis ( ) ; event = MotionEvent . obtain ( downTime , eventTime , MotionEvent . ACTION_MOVE , x + 1.0f , y + 1.0f , 0 ) ; inst . sendPointerSync ( event ) ; if ( time > 0 ) sleeper . sleep ( time ) ; else sleeper . sleep ( ( int ) ( ViewConfiguration . getLongPressTimeout ( ) * 2.5f ) ) ; eventTime = SystemClock . uptimeMillis ( ) ; event = MotionEvent . obtain ( downTime , eventTime , MotionEvent . ACTION_UP , x , y , 0 ) ; inst . sendPointerSync ( event ) ; sleeper . sleep ( ) ; }
Long clicks a given coordinate on the screen .
22,641
public void clickOnScreen ( View view , boolean longClick , int time ) { if ( view == null ) Assert . fail ( "View is null and can therefore not be clicked!" ) ; float [ ] xyToClick = getClickCoordinates ( view ) ; float x = xyToClick [ 0 ] ; float y = xyToClick [ 1 ] ; if ( x == 0 || y == 0 ) { sleeper . sleepMini ( ) ; try { view = viewFetcher . getIdenticalView ( view ) ; } catch ( Exception ignored ) { } if ( view != null ) { xyToClick = getClickCoordinates ( view ) ; x = xyToClick [ 0 ] ; y = xyToClick [ 1 ] ; } } sleeper . sleep ( 300 ) ; if ( longClick ) clickLongOnScreen ( x , y , time , view ) ; else clickOnScreen ( x , y , view ) ; }
Private method used to click on a given view .
22,642
private float [ ] getClickCoordinates ( View view ) { int [ ] xyLocation = new int [ 2 ] ; float [ ] xyToClick = new float [ 2 ] ; int trialCount = 0 ; view . getLocationOnScreen ( xyLocation ) ; while ( xyLocation [ 0 ] == 0 && xyLocation [ 1 ] == 0 && trialCount < 10 ) { sleeper . sleep ( 300 ) ; view . getLocationOnScreen ( xyLocation ) ; trialCount ++ ; } final int viewWidth = view . getWidth ( ) ; final int viewHeight = view . getHeight ( ) ; final float x = xyLocation [ 0 ] + ( viewWidth / 2.0f ) ; float y = xyLocation [ 1 ] + ( viewHeight / 2.0f ) ; xyToClick [ 0 ] = x ; xyToClick [ 1 ] = y ; return xyToClick ; }
Returns click coordinates for the specified view .
22,643
private void openMenu ( ) { sleeper . sleepMini ( ) ; if ( ! dialogUtils . waitForDialogToOpen ( MINI_WAIT , false ) ) { try { sender . sendKeyCode ( KeyEvent . KEYCODE_MENU ) ; dialogUtils . waitForDialogToOpen ( WAIT_TIME , true ) ; } catch ( SecurityException e ) { Assert . fail ( "Can not open the menu!" ) ; } } }
Opens the menu and waits for it to open .
22,644
public void clickOnMenuItem ( String text , boolean subMenu ) { sleeper . sleepMini ( ) ; TextView textMore = null ; int [ ] xy = new int [ 2 ] ; int x = 0 ; int y = 0 ; if ( ! dialogUtils . waitForDialogToOpen ( MINI_WAIT , false ) ) { try { sender . sendKeyCode ( KeyEvent . KEYCODE_MENU ) ; dialogUtils . waitForDialogToOpen ( WAIT_TIME , true ) ; } catch ( SecurityException e ) { Assert . fail ( "Can not open the menu!" ) ; } } boolean textShown = waiter . waitForText ( text , 1 , WAIT_TIME , true ) != null ; if ( subMenu && ( viewFetcher . getCurrentViews ( TextView . class , true ) . size ( ) > 5 ) && ! textShown ) { for ( TextView textView : viewFetcher . getCurrentViews ( TextView . class , true ) ) { x = xy [ 0 ] ; y = xy [ 1 ] ; textView . getLocationOnScreen ( xy ) ; if ( xy [ 0 ] > x || xy [ 1 ] > y ) textMore = textView ; } } if ( textMore != null ) clickOnScreen ( textMore ) ; clickOnText ( text , false , 1 , true , 0 ) ; }
Clicks on a menu item with a given text .
22,645
public void clickOnActionBarItem ( int resourceId ) { sleeper . sleep ( ) ; Activity activity = activityUtils . getCurrentActivity ( ) ; if ( activity != null ) { inst . invokeMenuActionSync ( activity , resourceId , 0 ) ; } }
Clicks on an ActionBar item with a given resource id
22,646
public void clickOnWebElement ( By by , int match , boolean scroll , boolean useJavaScriptToClick ) { WebElement webElement = null ; if ( useJavaScriptToClick ) { webElement = waiter . waitForWebElement ( by , match , Timeout . getSmallTimeout ( ) , false ) ; if ( webElement == null ) { Assert . fail ( "WebElement with " + webUtils . splitNameByUpperCase ( by . getClass ( ) . getSimpleName ( ) ) + ": '" + by . getValue ( ) + "' is not found!" ) ; } webUtils . executeJavaScript ( by , true ) ; return ; } WebElement webElementToClick = waiter . waitForWebElement ( by , match , Timeout . getSmallTimeout ( ) , scroll ) ; if ( webElementToClick == null ) { if ( match > 1 ) { Assert . fail ( match + " WebElements with " + webUtils . splitNameByUpperCase ( by . getClass ( ) . getSimpleName ( ) ) + ": '" + by . getValue ( ) + "' are not found!" ) ; } else { Assert . fail ( "WebElement with " + webUtils . splitNameByUpperCase ( by . getClass ( ) . getSimpleName ( ) ) + ": '" + by . getValue ( ) + "' is not found!" ) ; } } clickOnScreen ( webElementToClick . getLocationX ( ) , webElementToClick . getLocationY ( ) , null ) ; }
Clicks on a web element using the given By method .
22,647
private View getViewOnAbsListLine ( AbsListView absListView , int index , int lineIndex ) { final long endTime = SystemClock . uptimeMillis ( ) + Timeout . getSmallTimeout ( ) ; View view = absListView . getChildAt ( lineIndex ) ; while ( view == null ) { final boolean timedOut = SystemClock . uptimeMillis ( ) > endTime ; if ( timedOut ) { Assert . fail ( "View is null and can therefore not be clicked!" ) ; } sleeper . sleep ( ) ; absListView = ( AbsListView ) viewFetcher . getIdenticalView ( absListView ) ; if ( absListView == null ) { absListView = waiter . waitForAndGetView ( index , AbsListView . class ) ; } view = absListView . getChildAt ( lineIndex ) ; } return view ; }
Returns the view in the specified list line
22,648
private View getViewOnRecyclerItemIndex ( ViewGroup recyclerView , int recyclerViewIndex , int itemIndex ) { final long endTime = SystemClock . uptimeMillis ( ) + Timeout . getSmallTimeout ( ) ; View view = recyclerView . getChildAt ( itemIndex ) ; while ( view == null ) { final boolean timedOut = SystemClock . uptimeMillis ( ) > endTime ; if ( timedOut ) { Assert . fail ( "View is null and can therefore not be clicked!" ) ; } sleeper . sleep ( ) ; recyclerView = ( ViewGroup ) viewFetcher . getIdenticalView ( recyclerView ) ; if ( recyclerView == null ) { recyclerView = ( ViewGroup ) viewFetcher . getRecyclerView ( false , recyclerViewIndex ) ; } if ( recyclerView != null ) { view = recyclerView . getChildAt ( itemIndex ) ; } } return view ; }
Returns the view in the specified item index
22,649
public String getAttribute ( String attributeName ) { if ( attributeName != null ) { return this . attributes . get ( attributeName ) ; } return null ; }
Returns the value for the specified attribute .
22,650
public void assertMemoryNotLow ( ) { ActivityManager . MemoryInfo mi = new ActivityManager . MemoryInfo ( ) ; ( ( ActivityManager ) activityUtils . getCurrentActivity ( ) . getSystemService ( "activity" ) ) . getMemoryInfo ( mi ) ; Assert . assertFalse ( "Low memory available: " + mi . availMem + " bytes!" , mi . lowMemory ) ; }
Asserts that the available memory is not considered low by the system .
22,651
public void setSlidingDrawer ( final SlidingDrawer slidingDrawer , final int status ) { if ( slidingDrawer != null ) { Activity activity = activityUtils . getCurrentActivity ( false ) ; if ( activity != null ) { activity . runOnUiThread ( new Runnable ( ) { public void run ( ) { try { switch ( status ) { case CLOSED : slidingDrawer . close ( ) ; break ; case OPENED : slidingDrawer . open ( ) ; break ; } } catch ( Exception ignored ) { } } } ) ; } } }
Sets the status of a given SlidingDrawer . Examples are Solo . CLOSED and Solo . OPENED .
22,652
public void setNavigationDrawer ( final int status ) { final View homeView = getter . getView ( "home" , 0 ) ; final View leftDrawer = getter . getView ( "left_drawer" , 0 ) ; try { switch ( status ) { case CLOSED : if ( leftDrawer != null && homeView != null && leftDrawer . isShown ( ) ) { clicker . clickOnScreen ( homeView ) ; } break ; case OPENED : if ( leftDrawer != null && homeView != null && ! leftDrawer . isShown ( ) ) { clicker . clickOnScreen ( homeView ) ; Condition condition = new Condition ( ) { public boolean isSatisfied ( ) { return leftDrawer . isShown ( ) ; } } ; waiter . waitForCondition ( condition , Timeout . getSmallTimeout ( ) ) ; } break ; } } catch ( Exception ignored ) { } }
Sets the status of the NavigationDrawer . Examples are Solo . CLOSED and Solo . OPENED .
22,653
private boolean isDialogOpen ( ) { final Activity activity = activityUtils . getCurrentActivity ( false ) ; final View [ ] views = viewFetcher . getWindowDecorViews ( ) ; View view = viewFetcher . getRecentDecorView ( views ) ; if ( ! isDialog ( activity , view ) ) { for ( View v : views ) { if ( isDialog ( activity , v ) ) { return true ; } } } else { return true ; } return false ; }
Checks if a dialog is open .
22,654
private boolean isDialog ( Activity activity , View decorView ) { if ( decorView == null || ! decorView . isShown ( ) || activity == null ) { return false ; } Context viewContext = null ; if ( decorView != null ) { viewContext = decorView . getContext ( ) ; } if ( viewContext instanceof ContextThemeWrapper ) { ContextThemeWrapper ctw = ( ContextThemeWrapper ) viewContext ; viewContext = ctw . getBaseContext ( ) ; } Context activityContext = activity ; Context activityBaseContext = activity . getBaseContext ( ) ; return ( activityContext . equals ( viewContext ) || activityBaseContext . equals ( viewContext ) ) && ( decorView != activity . getWindow ( ) . getDecorView ( ) ) ; }
Checks that the specified DecorView and the Activity DecorView are not equal .
22,655
public void hideSoftKeyboard ( EditText editText , boolean shouldSleepFirst , boolean shouldSleepAfter ) { InputMethodManager inputMethodManager ; Activity activity = activityUtils . getCurrentActivity ( shouldSleepFirst ) ; if ( activity == null ) { inputMethodManager = ( InputMethodManager ) instrumentation . getTargetContext ( ) . getSystemService ( Context . INPUT_METHOD_SERVICE ) ; } else { inputMethodManager = ( InputMethodManager ) activity . getSystemService ( Activity . INPUT_METHOD_SERVICE ) ; } if ( editText != null ) { inputMethodManager . hideSoftInputFromWindow ( editText . getWindowToken ( ) , 0 ) ; return ; } View focusedView = activity . getCurrentFocus ( ) ; if ( ! ( focusedView instanceof EditText ) ) { EditText freshestEditText = viewFetcher . getFreshestView ( viewFetcher . getCurrentViews ( EditText . class , true ) ) ; if ( freshestEditText != null ) { focusedView = freshestEditText ; } } if ( focusedView != null ) { inputMethodManager . hideSoftInputFromWindow ( focusedView . getWindowToken ( ) , 0 ) ; } if ( shouldSleepAfter ) { sleeper . sleep ( ) ; } }
Hides the soft keyboard
22,656
private boolean isActivityMatching ( Activity currentActivity , String name ) { if ( currentActivity != null && currentActivity . getClass ( ) . getSimpleName ( ) . equals ( name ) ) { return true ; } return false ; }
Compares Activity names .
22,657
private boolean isActivityMatching ( Class < ? extends Activity > activityClass , Activity currentActivity ) { if ( currentActivity != null && currentActivity . getClass ( ) . equals ( activityClass ) ) { return true ; } return false ; }
Compares Activity classes .
22,658
private ActivityMonitor getActivityMonitor ( ) { IntentFilter filter = null ; ActivityMonitor activityMonitor = instrumentation . addMonitor ( filter , null , false ) ; return activityMonitor ; }
Creates a new ActivityMonitor and returns it
22,659
public < T extends View > boolean waitForViews ( boolean scrollMethod , Class < ? extends T > ... classes ) { final long endTime = SystemClock . uptimeMillis ( ) + Timeout . getSmallTimeout ( ) ; while ( SystemClock . uptimeMillis ( ) < endTime ) { for ( Class < ? extends T > classToWaitFor : classes ) { if ( waitForView ( classToWaitFor , 0 , false , false ) ) { return true ; } } if ( scrollMethod ) { scroller . scroll ( Scroller . DOWN ) ; } else { scroller . scrollDown ( ) ; } sleeper . sleep ( ) ; } return false ; }
Waits for two views to be shown .
22,660
public boolean waitForView ( View view ) { View viewToWaitFor = waitForView ( view , Timeout . getLargeTimeout ( ) , true , true ) ; if ( viewToWaitFor != null ) { return true ; } return false ; }
Waits for a given view . Default timeout is 20 seconds .
22,661
public WebElement waitForWebElement ( final By by , int minimumNumberOfMatches , int timeout , boolean scroll ) { final long endTime = SystemClock . uptimeMillis ( ) + timeout ; while ( true ) { final boolean timedOut = SystemClock . uptimeMillis ( ) > endTime ; if ( timedOut ) { searcher . logMatchesFound ( by . getValue ( ) ) ; return null ; } sleeper . sleep ( ) ; WebElement webElementToReturn = searcher . searchForWebElement ( by , minimumNumberOfMatches ) ; if ( webElementToReturn != null ) return webElementToReturn ; if ( scroll ) { scroller . scrollDown ( ) ; } } }
Waits for a web element .
22,662
public < T extends View > T waitForAndGetView ( int index , Class < T > classToFilterBy ) { long endTime = SystemClock . uptimeMillis ( ) + Timeout . getSmallTimeout ( ) ; while ( SystemClock . uptimeMillis ( ) <= endTime && ! waitForView ( classToFilterBy , index , true , true ) ) ; int numberOfUniqueViews = searcher . getNumberOfUniqueViews ( ) ; ArrayList < T > views = RobotiumUtils . removeInvisibleViews ( viewFetcher . getCurrentViews ( classToFilterBy , true ) ) ; if ( views . size ( ) < numberOfUniqueViews ) { int newIndex = index - ( numberOfUniqueViews - views . size ( ) ) ; if ( newIndex >= 0 ) index = newIndex ; } T view = null ; try { view = views . get ( index ) ; } catch ( IndexOutOfBoundsException exception ) { int match = index + 1 ; if ( match > 1 ) { Assert . fail ( match + " " + classToFilterBy . getSimpleName ( ) + "s" + " are not found!" ) ; } else { Assert . fail ( classToFilterBy . getSimpleName ( ) + " is not found!" ) ; } } views = null ; return view ; }
Waits for and returns a View .
22,663
public boolean waitForFragment ( String tag , int id , int timeout ) { long endTime = SystemClock . uptimeMillis ( ) + timeout ; while ( SystemClock . uptimeMillis ( ) <= endTime ) { if ( getSupportFragment ( tag , id ) != null ) return true ; if ( getFragment ( tag , id ) != null ) return true ; } return false ; }
Waits for a Fragment with a given tag or id to appear .
22,664
private Fragment getSupportFragment ( String tag , int id ) { FragmentActivity fragmentActivity = null ; try { fragmentActivity = ( FragmentActivity ) activityUtils . getCurrentActivity ( false ) ; } catch ( Throwable ignored ) { } if ( fragmentActivity != null ) { try { if ( tag == null ) return fragmentActivity . getSupportFragmentManager ( ) . findFragmentById ( id ) ; else return fragmentActivity . getSupportFragmentManager ( ) . findFragmentByTag ( tag ) ; } catch ( NoSuchMethodError ignored ) { } } return null ; }
Returns a SupportFragment with a given tag or id .
22,665
private StringBuilder getLog ( StringBuilder stringBuilder ) { Process p = null ; BufferedReader reader = null ; String line = null ; try { p = Runtime . getRuntime ( ) . exec ( "logcat -d" ) ; reader = new BufferedReader ( new InputStreamReader ( p . getInputStream ( ) ) ) ; stringBuilder . setLength ( 0 ) ; while ( ( line = reader . readLine ( ) ) != null ) { stringBuilder . append ( line ) ; } reader . close ( ) ; StringBuilder errorLog = new StringBuilder ( ) ; reader = new BufferedReader ( new InputStreamReader ( p . getErrorStream ( ) ) ) ; errorLog . append ( "logcat returns error: " ) ; while ( ( line = reader . readLine ( ) ) != null ) { errorLog . append ( line ) ; } reader . close ( ) ; p . waitFor ( ) ; if ( p . exitValue ( ) != 0 ) { destroy ( p , reader ) ; throw new Exception ( errorLog . toString ( ) ) ; } } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( InterruptedException e ) { e . printStackTrace ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } destroy ( p , reader ) ; return stringBuilder ; }
Returns the log in the given stringBuilder .
22,666
public void clearLog ( ) { Process p = null ; try { p = Runtime . getRuntime ( ) . exec ( "logcat -c" ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } }
Clears the log .
22,667
private void destroy ( Process p , BufferedReader reader ) { p . destroy ( ) ; try { reader . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } }
Destroys the process and closes the BufferedReader .
22,668
private android . app . Fragment getFragment ( String tag , int id ) { try { if ( tag == null ) return activityUtils . getCurrentActivity ( ) . getFragmentManager ( ) . findFragmentById ( id ) ; else return activityUtils . getCurrentActivity ( ) . getFragmentManager ( ) . findFragmentByTag ( tag ) ; } catch ( Throwable ignored ) { } return null ; }
Returns a Fragment with a given tag or id .
22,669
private ArrayList < TextView > createAndReturnTextViewsFromWebElements ( boolean javaScriptWasExecuted ) { ArrayList < TextView > webElementsAsTextViews = new ArrayList < TextView > ( ) ; if ( javaScriptWasExecuted ) { for ( WebElement webElement : webElementCreator . getWebElementsFromWebViews ( ) ) { if ( isWebElementSufficientlyShown ( webElement ) ) { RobotiumTextView textView = new RobotiumTextView ( inst . getContext ( ) , webElement . getText ( ) , webElement . getLocationX ( ) , webElement . getLocationY ( ) ) ; webElementsAsTextViews . add ( textView ) ; } } } return webElementsAsTextViews ; }
Creates and returns TextView objects based on WebElements
22,670
public ArrayList < WebElement > getWebElements ( final By by , boolean onlySufficientlyVisbile ) { boolean javaScriptWasExecuted = executeJavaScript ( by , false ) ; if ( config . useJavaScriptToClickWebElements ) { if ( ! javaScriptWasExecuted ) { return new ArrayList < WebElement > ( ) ; } return webElementCreator . getWebElementsFromWebViews ( ) ; } return getWebElements ( javaScriptWasExecuted , onlySufficientlyVisbile ) ; }
Returns an ArrayList of WebElements of the specified By object currently shown in the active WebView .
22,671
private ArrayList < WebElement > getWebElements ( boolean javaScriptWasExecuted , boolean onlySufficientlyVisbile ) { ArrayList < WebElement > webElements = new ArrayList < WebElement > ( ) ; if ( javaScriptWasExecuted ) { for ( WebElement webElement : webElementCreator . getWebElementsFromWebViews ( ) ) { if ( ! onlySufficientlyVisbile ) { webElements . add ( webElement ) ; } else if ( isWebElementSufficientlyShown ( webElement ) ) { webElements . add ( webElement ) ; } } } return webElements ; }
Returns the sufficiently shown WebElements
22,672
private String prepareForStartOfJavascriptExecution ( List < WebView > webViews ) { webElementCreator . prepareForStart ( ) ; WebChromeClient currentWebChromeClient = getCurrentWebChromeClient ( ) ; if ( currentWebChromeClient != null && ! currentWebChromeClient . getClass ( ) . isAssignableFrom ( RobotiumWebClient . class ) ) { originalWebChromeClient = currentWebChromeClient ; } robotiumWebCLient . enableJavascriptAndSetRobotiumWebClient ( webViews , originalWebChromeClient ) ; return getJavaScriptAsString ( ) ; }
Prepares for start of JavaScript execution
22,673
private WebChromeClient getCurrentWebChromeClient ( ) { WebChromeClient currentWebChromeClient = null ; Object currentWebView = viewFetcher . getFreshestView ( viewFetcher . getCurrentViews ( WebView . class , true ) ) ; if ( android . os . Build . VERSION . SDK_INT >= 16 ) { try { currentWebView = new Reflect ( currentWebView ) . field ( "mProvider" ) . out ( Object . class ) ; } catch ( IllegalArgumentException ignored ) { } } try { if ( android . os . Build . VERSION . SDK_INT >= 19 ) { Object mClientAdapter = new Reflect ( currentWebView ) . field ( "mContentsClientAdapter" ) . out ( Object . class ) ; currentWebChromeClient = new Reflect ( mClientAdapter ) . field ( "mWebChromeClient" ) . out ( WebChromeClient . class ) ; } else { Object mCallbackProxy = new Reflect ( currentWebView ) . field ( "mCallbackProxy" ) . out ( Object . class ) ; currentWebChromeClient = new Reflect ( mCallbackProxy ) . field ( "mWebChromeClient" ) . out ( WebChromeClient . class ) ; } } catch ( Exception ignored ) { } return currentWebChromeClient ; }
Returns the current WebChromeClient through reflection
22,674
public void enterTextIntoWebElement ( final By by , final String text ) { if ( by instanceof By . Id ) { executeJavaScriptFunction ( "enterTextById(\"" + by . getValue ( ) + "\", \"" + text + "\");" ) ; } else if ( by instanceof By . Xpath ) { executeJavaScriptFunction ( "enterTextByXpath(\"" + by . getValue ( ) + "\", \"" + text + "\");" ) ; } else if ( by instanceof By . CssSelector ) { executeJavaScriptFunction ( "enterTextByCssSelector(\"" + by . getValue ( ) + "\", \"" + text + "\");" ) ; } else if ( by instanceof By . Name ) { executeJavaScriptFunction ( "enterTextByName(\"" + by . getValue ( ) + "\", \"" + text + "\");" ) ; } else if ( by instanceof By . ClassName ) { executeJavaScriptFunction ( "enterTextByClassName(\"" + by . getValue ( ) + "\", \"" + text + "\");" ) ; } else if ( by instanceof By . Text ) { executeJavaScriptFunction ( "enterTextByTextContent(\"" + by . getValue ( ) + "\", \"" + text + "\");" ) ; } else if ( by instanceof By . TagName ) { executeJavaScriptFunction ( "enterTextByTagName(\"" + by . getValue ( ) + "\", \"" + text + "\");" ) ; } }
Enters text into a web element using the given By method
22,675
public boolean executeJavaScript ( final By by , boolean shouldClick ) { if ( by instanceof By . Id ) { return executeJavaScriptFunction ( "id(\"" + by . getValue ( ) + "\", \"" + String . valueOf ( shouldClick ) + "\");" ) ; } else if ( by instanceof By . Xpath ) { return executeJavaScriptFunction ( "xpath(\"" + by . getValue ( ) + "\", \"" + String . valueOf ( shouldClick ) + "\");" ) ; } else if ( by instanceof By . CssSelector ) { return executeJavaScriptFunction ( "cssSelector(\"" + by . getValue ( ) + "\", \"" + String . valueOf ( shouldClick ) + "\");" ) ; } else if ( by instanceof By . Name ) { return executeJavaScriptFunction ( "name(\"" + by . getValue ( ) + "\", \"" + String . valueOf ( shouldClick ) + "\");" ) ; } else if ( by instanceof By . ClassName ) { return executeJavaScriptFunction ( "className(\"" + by . getValue ( ) + "\", \"" + String . valueOf ( shouldClick ) + "\");" ) ; } else if ( by instanceof By . Text ) { return executeJavaScriptFunction ( "textContent(\"" + by . getValue ( ) + "\", \"" + String . valueOf ( shouldClick ) + "\");" ) ; } else if ( by instanceof By . TagName ) { return executeJavaScriptFunction ( "tagName(\"" + by . getValue ( ) + "\", \"" + String . valueOf ( shouldClick ) + "\");" ) ; } return false ; }
Executes JavaScript determined by the given By object
22,676
private boolean executeJavaScriptFunction ( final String function ) { List < WebView > webViews = viewFetcher . getCurrentViews ( WebView . class , true ) ; final WebView webView = viewFetcher . getFreshestView ( ( ArrayList < WebView > ) webViews ) ; if ( webView == null ) { return false ; } final String javaScript = setWebFrame ( prepareForStartOfJavascriptExecution ( webViews ) ) ; inst . runOnMainSync ( new Runnable ( ) { public void run ( ) { if ( webView != null ) { webView . loadUrl ( "javascript:" + javaScript + function ) ; } } } ) ; return true ; }
Executes the given JavaScript function
22,677
public String splitNameByUpperCase ( String name ) { String [ ] texts = name . split ( "(?=\\p{Upper})" ) ; StringBuilder stringToReturn = new StringBuilder ( ) ; for ( String string : texts ) { if ( stringToReturn . length ( ) > 0 ) { stringToReturn . append ( " " + string . toLowerCase ( ) ) ; } else { stringToReturn . append ( string . toLowerCase ( ) ) ; } } return stringToReturn . toString ( ) ; }
Splits a name by upper case .
22,678
private String getJavaScriptAsString ( ) { InputStream fis = getClass ( ) . getResourceAsStream ( "RobotiumWeb.js" ) ; StringBuffer javaScript = new StringBuffer ( ) ; try { BufferedReader input = new BufferedReader ( new InputStreamReader ( fis ) ) ; String line = null ; while ( ( line = input . readLine ( ) ) != null ) { javaScript . append ( line ) ; javaScript . append ( "\n" ) ; } input . close ( ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } return javaScript . toString ( ) ; }
Returns the JavaScript file RobotiumWeb . js as a String
22,679
public < T extends View > boolean searchFor ( Set < T > uniqueViews , Class < T > viewClass , final int index ) { ArrayList < T > allViews = RobotiumUtils . removeInvisibleViews ( viewFetcher . getCurrentViews ( viewClass , true ) ) ; int uniqueViewsFound = ( getNumberOfUniqueViews ( uniqueViews , allViews ) ) ; if ( uniqueViewsFound > 0 && index < uniqueViewsFound ) { return true ; } if ( uniqueViewsFound > 0 && index == 0 ) { return true ; } return false ; }
Searches for a view class .
22,680
public < T extends View > boolean searchFor ( View view ) { ArrayList < View > views = viewFetcher . getAllViews ( true ) ; for ( View v : views ) { if ( v . equals ( view ) ) { return true ; } } return false ; }
Searches for a given view .
22,681
public WebElement searchForWebElement ( final By by , int minimumNumberOfMatches ) { if ( minimumNumberOfMatches < 1 ) { minimumNumberOfMatches = 1 ; } List < WebElement > viewsFromScreen = webUtils . getWebElements ( by , true ) ; addViewsToList ( webElements , viewsFromScreen ) ; return getViewFromList ( webElements , minimumNumberOfMatches ) ; }
Searches for a web element .
22,682
private void addViewsToList ( List < WebElement > allWebElements , List < WebElement > webElementsOnScreen ) { int [ ] xyViewFromSet = new int [ 2 ] ; int [ ] xyViewFromScreen = new int [ 2 ] ; for ( WebElement textFromScreen : webElementsOnScreen ) { boolean foundView = false ; textFromScreen . getLocationOnScreen ( xyViewFromScreen ) ; for ( WebElement textFromList : allWebElements ) { textFromList . getLocationOnScreen ( xyViewFromSet ) ; if ( textFromScreen . getText ( ) . equals ( textFromList . getText ( ) ) && xyViewFromScreen [ 0 ] == xyViewFromSet [ 0 ] && xyViewFromScreen [ 1 ] == xyViewFromSet [ 1 ] ) { foundView = true ; } } if ( ! foundView ) { allWebElements . add ( textFromScreen ) ; } } }
Adds views to a given list .
22,683
private WebElement getViewFromList ( List < WebElement > webElements , int match ) { WebElement webElementToReturn = null ; if ( webElements . size ( ) >= match ) { try { webElementToReturn = webElements . get ( -- match ) ; } catch ( Exception ignored ) { } } if ( webElementToReturn != null ) webElements . clear ( ) ; return webElementToReturn ; }
Returns a text view with a given match .
22,684
public < T extends View > int getNumberOfUniqueViews ( Set < T > uniqueViews , ArrayList < T > views ) { for ( int i = 0 ; i < views . size ( ) ; i ++ ) { uniqueViews . add ( views . get ( i ) ) ; } numberOfUniqueViews = uniqueViews . size ( ) ; return numberOfUniqueViews ; }
Returns the number of unique views .
22,685
public void setMobileData ( Boolean turnedOn ) { ConnectivityManager dataManager = ( ConnectivityManager ) instrumentation . getTargetContext ( ) . getSystemService ( Context . CONNECTIVITY_SERVICE ) ; Method dataClass = null ; try { dataClass = ConnectivityManager . class . getDeclaredMethod ( "setMobileDataEnabled" , boolean . class ) ; dataClass . setAccessible ( true ) ; dataClass . invoke ( dataManager , turnedOn ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } }
Sets if mobile data should be turned on or off . Requires android . permission . CHANGE_NETWORK_STATE in the AndroidManifest . xml of the application under test .
22,686
public void setWiFiData ( Boolean turnedOn ) { WifiManager wifiManager = ( WifiManager ) instrumentation . getTargetContext ( ) . getSystemService ( Context . WIFI_SERVICE ) ; try { wifiManager . setWifiEnabled ( turnedOn ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } }
Sets if wifi data should be turned on or off . Requires android . permission . CHANGE_WIFI_STATE in the AndroidManifest . xml of the application under test .
22,687
public ActivityMonitor getActivityMonitor ( ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "getActivityMonitor()" ) ; } return activityUtils . getActivityMonitor ( ) ; }
Returns the ActivityMonitor used by Robotium .
22,688
public ArrayList < View > getViews ( ) { try { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "getViews()" ) ; } return viewFetcher . getViews ( null , false ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; return null ; } }
Returns an ArrayList of all the View objects located in the focused Activity or Dialog .
22,689
public View getTopParent ( View view ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "getTopParent(" + view + ")" ) ; } View topParent = viewFetcher . getTopParent ( view ) ; return topParent ; }
Returns the absolute top parent View of the specified View .
22,690
public boolean waitForText ( String text , int minimumNumberOfMatches , long timeout , boolean scroll , boolean onlyVisible ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "waitForText(\"" + text + "\", " + minimumNumberOfMatches + ", " + timeout + ", " + scroll + ", " + onlyVisible + ")" ) ; } return ( waiter . waitForText ( text , minimumNumberOfMatches , timeout , scroll , onlyVisible , true ) != null ) ; }
Waits for the specified text to appear .
22,691
public boolean waitForView ( int id ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "waitForView(" + id + ")" ) ; } return waitForView ( id , 0 , Timeout . getLargeTimeout ( ) , true ) ; }
Waits for a View matching the specified resource id . Default timeout is 20 seconds .
22,692
public boolean waitForView ( Object tag ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "waitForView(" + tag + ")" ) ; } return waitForView ( tag , 0 , Timeout . getLargeTimeout ( ) , true ) ; }
Waits for a View matching the specified tag . Default timeout is 20 seconds .
22,693
public boolean waitForView ( Object tag , int minimumNumberOfMatches , int timeout ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "waitForView(" + tag + ", " + minimumNumberOfMatches + ", " + timeout + ")" ) ; } return waitForView ( tag , minimumNumberOfMatches , timeout , true ) ; }
Waits for a View matching the specified tag .
22,694
public < T extends View > boolean waitForView ( final Class < T > viewClass ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "waitForView(" + viewClass + ")" ) ; } return waiter . waitForView ( viewClass , 0 , Timeout . getLargeTimeout ( ) , true ) ; }
Waits for a View matching the specified class . Default timeout is 20 seconds .
22,695
public < T extends View > boolean waitForView ( View view ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "waitForView(" + view + ")" ) ; } return waiter . waitForView ( view ) ; }
Waits for the specified View . Default timeout is 20 seconds .
22,696
public < T extends View > boolean waitForView ( View view , int timeout , boolean scroll ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "waitForView(" + view + ", " + timeout + ", " + scroll + ")" ) ; } boolean checkIsShown = false ; if ( ! scroll ) { checkIsShown = true ; } View viewToWaitFor = waiter . waitForView ( view , timeout , scroll , checkIsShown ) ; if ( viewToWaitFor != null ) return true ; return false ; }
Waits for the specified View .
22,697
public < T extends View > boolean waitForView ( final Class < T > viewClass , final int minimumNumberOfMatches , final int timeout ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "waitForView(" + viewClass + ", " + minimumNumberOfMatches + ", " + timeout + ")" ) ; } int index = minimumNumberOfMatches - 1 ; if ( index < 1 ) index = 0 ; return waiter . waitForView ( viewClass , index , timeout , true ) ; }
Waits for a View matching the specified class .
22,698
public boolean waitForWebElement ( By by ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "waitForWebElement(" + by + ")" ) ; } return ( waiter . waitForWebElement ( by , 0 , Timeout . getLargeTimeout ( ) , true ) != null ) ; }
Waits for a WebElement matching the specified By object . Default timeout is 20 seconds .
22,699
public boolean waitForWebElement ( By by , int timeout , boolean scroll ) { if ( config . commandLogging ) { Log . d ( config . commandLoggingTag , "waitForWebElement(" + by + ", " + timeout + ", " + scroll + ")" ) ; } return ( waiter . waitForWebElement ( by , 0 , timeout , scroll ) != null ) ; }
Waits for a WebElement matching the specified By object .