build/mobile/robocop/FennecNativeElement.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/build/mobile/robocop/FennecNativeElement.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,108 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko;
     1.9 +
    1.10 +import android.app.Activity;
    1.11 +import android.view.View;
    1.12 +import android.view.ViewGroup;
    1.13 +import android.widget.EditText;
    1.14 +import android.widget.TextSwitcher;
    1.15 +import android.widget.TextView;
    1.16 +
    1.17 +public class FennecNativeElement implements Element {
    1.18 +    private final Activity mActivity;
    1.19 +    private Integer mId;
    1.20 +
    1.21 +    public FennecNativeElement(Integer id, Activity activity) {
    1.22 +        mId = id;
    1.23 +        mActivity = activity;
    1.24 +    }
    1.25 +
    1.26 +    public Integer getId() {
    1.27 +        return mId;
    1.28 +    }
    1.29 +
    1.30 +    private boolean mClickSuccess;
    1.31 +
    1.32 +    public boolean click() {
    1.33 +        mClickSuccess = false;
    1.34 +        RobocopUtils.runOnUiThreadSync(mActivity,
    1.35 +            new Runnable() {
    1.36 +                public void run() {
    1.37 +                    View view = (View)mActivity.findViewById(mId);
    1.38 +                    if (view != null) {
    1.39 +                        if (view.performClick()) {
    1.40 +                            mClickSuccess = true;
    1.41 +                        } else {
    1.42 +                            FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
    1.43 +                                "Robocop called click on an element with no listener");
    1.44 +                        }
    1.45 +                    } else {
    1.46 +                        FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
    1.47 +                            "click: unable to find view "+mId);
    1.48 +                    }
    1.49 +                }
    1.50 +            });
    1.51 +        return mClickSuccess;
    1.52 +    }
    1.53 +
    1.54 +    private Object mText;
    1.55 +
    1.56 +    public String getText() {
    1.57 +        mText = null;
    1.58 +        RobocopUtils.runOnUiThreadSync(mActivity,
    1.59 +            new Runnable() {
    1.60 +                public void run() {
    1.61 +                    View v = mActivity.findViewById(mId);
    1.62 +                    if (v instanceof EditText) {
    1.63 +                        EditText et = (EditText)v;
    1.64 +                        mText = et.getEditableText();
    1.65 +                    } else if (v instanceof TextSwitcher) {
    1.66 +                        TextSwitcher ts = (TextSwitcher)v;
    1.67 +                        ts.getNextView();
    1.68 +                        mText = ((TextView)ts.getCurrentView()).getText();
    1.69 +                    } else if (v instanceof ViewGroup) {
    1.70 +                        ViewGroup vg = (ViewGroup)v;
    1.71 +                        for (int i = 0; i < vg.getChildCount(); i++) {
    1.72 +                            if (vg.getChildAt(i) instanceof TextView) {
    1.73 +                                mText = ((TextView)vg.getChildAt(i)).getText();
    1.74 +                            }
    1.75 +                        }
    1.76 +                    } else if (v instanceof TextView) {
    1.77 +                        mText = ((TextView)v).getText(); 
    1.78 +                    } else if (v == null) {
    1.79 +                        FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
    1.80 +                            "getText: unable to find view "+mId);
    1.81 +                    } else {
    1.82 +                        FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
    1.83 +                            "getText: unhandled type for view "+mId);
    1.84 +                    }
    1.85 +                } // end of run() method definition
    1.86 +            } // end of anonymous Runnable object instantiation
    1.87 +        );
    1.88 +        if (mText == null) {
    1.89 +            FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
    1.90 +                "getText: Text is null for view "+mId);
    1.91 +            return null;
    1.92 +        }
    1.93 +        return mText.toString();
    1.94 +    }
    1.95 +
    1.96 +    private boolean mDisplayed;
    1.97 +
    1.98 +    public boolean isDisplayed() {
    1.99 +        mDisplayed = false;
   1.100 +        RobocopUtils.runOnUiThreadSync(mActivity,
   1.101 +            new Runnable() {
   1.102 +                public void run() {
   1.103 +                    View view = (View)mActivity.findViewById(mId);
   1.104 +                    if (view != null) {
   1.105 +                        mDisplayed = true;
   1.106 +                    }
   1.107 +                }
   1.108 +            });
   1.109 +        return mDisplayed;
   1.110 +    }
   1.111 +}

mercurial