DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
#filter substitution
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package @ANDROID_PACKAGE_NAME@;

import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextSwitcher;
import android.app.Instrumentation;
import com.jayway.android.robotium.solo.Solo;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class FennecNativeElement implements Element {
    private final Activity mActivity;
    private Integer mId;
    private Solo mSolo;
    // max time to wait for thread synchronization
    private static final int MAX_WAIT_MS = 60000;

    public FennecNativeElement(Integer id, Activity activity, Solo solo) {
        mId = id;
        mActivity = activity;
        mSolo = solo;
    }

    public Integer getId() {
        return mId;
    }

    private boolean mClickSuccess;

    public boolean click() {
        final SynchronousQueue syncQueue = new SynchronousQueue();
        mClickSuccess = false;
        mActivity.runOnUiThread(
            new Runnable() {
                public void run() {
                    View view = (View)mActivity.findViewById(mId);
                    if (view != null) {
                        if (view.performClick()) {
                            mClickSuccess = true;
                        } else {
                            FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
                                "Robocop called click on an element with no listener");
                        }
                    } else {
                        FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
                            "click: unable to find view "+mId);
                    }
                    try {
                        syncQueue.put(new Object());
                    } catch (InterruptedException e) {
                        FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
                    }
                }
            });
        try {
            // Wait for the UiThread code to finish running
            if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
                FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, 
                    "click: time-out waiting for UI thread");
                FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
            }
        } catch (InterruptedException e) {
            FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
        }
        return mClickSuccess;
    }

    private Object mText;

    public String getText() {
        final SynchronousQueue syncQueue = new SynchronousQueue();
        mText = null;
        mActivity.runOnUiThread(
            new Runnable() {
                public void run() {
                    View v = mActivity.findViewById(mId);
                    if (v instanceof EditText) {
                        EditText et = (EditText)v;
                        mText = et.getEditableText();
                    } else if (v instanceof TextSwitcher) {
                        TextSwitcher ts = (TextSwitcher)v;
                        ts.getNextView();
                        mText = ((TextView)ts.getCurrentView()).getText();
                    } else if (v instanceof ViewGroup) {
                        ViewGroup vg = (ViewGroup)v;
                        for (int i = 0; i < vg.getChildCount(); i++) {
                            if (vg.getChildAt(i) instanceof TextView) {
                                mText = ((TextView)vg.getChildAt(i)).getText();
                            }
                        }
                    } else if (v instanceof TextView) {
                        mText = ((TextView)v).getText(); 
                    } else if (v == null) {
                        FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
                            "getText: unable to find view "+mId);
                    } else {
                        FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
                            "getText: unhandled type for view "+mId);
                    }
                    try {
                        syncQueue.put(new Object());
                    } catch (InterruptedException e) {
                        FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
                    }
                } // end of run() method definition
            } // end of anonymous Runnable object instantiation
        );
        try {     
            // Wait for the UiThread code to finish running
            if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
                FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, 
                    "getText: time-out waiting for UI thread");
                FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
            }
        } catch (InterruptedException e) {
            FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
        }
        if (mText == null) {
            FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
                "getText: Text is null for view "+mId);
            return null;
        }
        return mText.toString();
    }

    private boolean mDisplayed;

    public boolean isDisplayed() {
        final SynchronousQueue syncQueue = new SynchronousQueue();
        mDisplayed = false;
        mActivity.runOnUiThread(
            new Runnable() {
                public void run() {
                    View view = (View)mActivity.findViewById(mId);
                    if (view != null) {
                        mDisplayed = true;
                    }
                    try {
                        syncQueue.put(new Object());
                    } catch (InterruptedException e) {
                        FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
                    }
                }
            });
        try {
            // Wait for the UiThread code to finish running
            if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
                FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, 
                    "isDisplayed: time-out waiting for UI thread");
                FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
            }
        } catch (InterruptedException e) {
            FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
        }
        return mDisplayed;
    }

}