silentsolo / robotium

Automatically exported from code.google.com/p/robotium
0 stars 0 forks source link

WaitForview should check qualifier before sleeping #594

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. wait for any view.
2.
3.

What is the expected output? What do you see instead?
If the view is already up it should return immediately. If not, then start 
waiting.

What version of the product are you using? 5.1 On what operating system?

You should check the conditional before the first sleep so as to avoid 
unnecessary waiting.
So instead of:
public View waitForView(int id){
        ArrayList<View> views = new ArrayList<View>();
        long startTime = System.currentTimeMillis();
        long endTime = startTime + SMALLTIMEOUT;
        while (System.currentTimeMillis() <= endTime) {
            sleeper.sleep();
            views = viewFetcher.getAllViews(false);
            for (View v : views) {
                if (v.getId() == id) {
                    views = null;
                    return v;
                }
            }
        }
        return null;
    }
Do:
public View waitForView(int id){
        ArrayList<View> views = new ArrayList<View>();
        long startTime = System.currentTimeMillis();
        long endTime = startTime + SMALLTIMEOUT;
        while (System.currentTimeMillis() <= endTime) {
            views = viewFetcher.getAllViews(false);
            for (View v : views) {
                if (v.getId() == id) {
                    views = null;
                    return v;
                }
            }
                       sleeper.sleep();
        }
        return null;
    }

Original issue reported on code.google.com by shane...@gmail.com on 1 Apr 2014 at 4:46

GoogleCodeExporter commented 9 years ago
Removing the sleep will affect methods that scroll. E.g. clicking on a list 
item, (expect the list to close) and then using solo.clickOnView(getView(int 
id) will continue the scrolling of the list. With the sleep there the list will 
have a chance to close before getView() will try to click on the underlying 
view. 

Obviously if users would use waitForDialogToClose or similar then this would 
not happen. Unfortunately most users don't do that and removing sleeps like 
this will cause flaky tests. 

In your case you can use getActivity().findviewById(r.id.x) and that will be 
instantaneously. 

Original comment by renasr...@gmail.com on 1 Apr 2014 at 5:17