michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import android.app.Activity; michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: import android.widget.EditText; michael@0: import android.widget.TextSwitcher; michael@0: import android.widget.TextView; michael@0: michael@0: public class FennecNativeElement implements Element { michael@0: private final Activity mActivity; michael@0: private Integer mId; michael@0: michael@0: public FennecNativeElement(Integer id, Activity activity) { michael@0: mId = id; michael@0: mActivity = activity; michael@0: } michael@0: michael@0: public Integer getId() { michael@0: return mId; michael@0: } michael@0: michael@0: private boolean mClickSuccess; michael@0: michael@0: public boolean click() { michael@0: mClickSuccess = false; michael@0: RobocopUtils.runOnUiThreadSync(mActivity, michael@0: new Runnable() { michael@0: public void run() { michael@0: View view = (View)mActivity.findViewById(mId); michael@0: if (view != null) { michael@0: if (view.performClick()) { michael@0: mClickSuccess = true; michael@0: } else { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN, michael@0: "Robocop called click on an element with no listener"); michael@0: } michael@0: } else { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, michael@0: "click: unable to find view "+mId); michael@0: } michael@0: } michael@0: }); michael@0: return mClickSuccess; michael@0: } michael@0: michael@0: private Object mText; michael@0: michael@0: public String getText() { michael@0: mText = null; michael@0: RobocopUtils.runOnUiThreadSync(mActivity, michael@0: new Runnable() { michael@0: public void run() { michael@0: View v = mActivity.findViewById(mId); michael@0: if (v instanceof EditText) { michael@0: EditText et = (EditText)v; michael@0: mText = et.getEditableText(); michael@0: } else if (v instanceof TextSwitcher) { michael@0: TextSwitcher ts = (TextSwitcher)v; michael@0: ts.getNextView(); michael@0: mText = ((TextView)ts.getCurrentView()).getText(); michael@0: } else if (v instanceof ViewGroup) { michael@0: ViewGroup vg = (ViewGroup)v; michael@0: for (int i = 0; i < vg.getChildCount(); i++) { michael@0: if (vg.getChildAt(i) instanceof TextView) { michael@0: mText = ((TextView)vg.getChildAt(i)).getText(); michael@0: } michael@0: } michael@0: } else if (v instanceof TextView) { michael@0: mText = ((TextView)v).getText(); michael@0: } else if (v == null) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, michael@0: "getText: unable to find view "+mId); michael@0: } else { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, michael@0: "getText: unhandled type for view "+mId); michael@0: } michael@0: } // end of run() method definition michael@0: } // end of anonymous Runnable object instantiation michael@0: ); michael@0: if (mText == null) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN, michael@0: "getText: Text is null for view "+mId); michael@0: return null; michael@0: } michael@0: return mText.toString(); michael@0: } michael@0: michael@0: private boolean mDisplayed; michael@0: michael@0: public boolean isDisplayed() { michael@0: mDisplayed = false; michael@0: RobocopUtils.runOnUiThreadSync(mActivity, michael@0: new Runnable() { michael@0: public void run() { michael@0: View view = (View)mActivity.findViewById(mId); michael@0: if (view != null) { michael@0: mDisplayed = true; michael@0: } michael@0: } michael@0: }); michael@0: return mDisplayed; michael@0: } michael@0: }