Robotium searchText源码分析

一路查找最终的实现的代码乳腺

/**

* Searches for a {@code View} with the given regex string and returns {@code true} if the
* searched {@code View} is found a given number of times.
*
* @param viewClass what kind of {@code View} to search for, e.g. {@code Button.class} or {@code TextView.class} //搜索的类
* @param regex the text to search for. The parameter <strong>will</strong> be interpreted as a regular expression.//搜查的关键字
* @param expectedMinimumNumberOfMatches the minimum number of matches expected to be found. {@code 0} matches means that one or more //期望最小搜索到的数量
* matches are expected to be found.
* @param timeout the amount of time in milliseconds to wait //设置搜索的等待时间,当时间结束后停止搜索
* @param scroll whether scrolling should be performed //滑动滚动条查找
* @param onlyVisible {@code true} if only texts visible on the screen should be searched //是否只查找页面显示的关键字

* @return {@code true} if a view of the specified class with the given text is found a given number of times.
* {@code false} if it is not found.
*/


public <T extends TextView> T searchFor(final Class<T> viewClass, final String regex, int expectedMinimumNumberOfMatches, final long timeout, final boolean scroll, final boolean onlyVisible) {

//默认搜索个数为1

if(expectedMinimumNumberOfMatches < 1) {
expectedMinimumNumberOfMatches = 1;
}

//Callable为定义的接口

final Callable<Collection<T>> viewFetcherCallback = new Callable<Collection<T>>() {
@SuppressWarnings("unchecked")
public Collection<T> call() throws Exception {
sleeper.sleep();
/**
 *  isAssignableFrom 是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的超类或接口。
       *通常调用格式是
       *Class1.isAssignableFrom(Class2)
 */
ArrayList<T> viewsToReturn = viewFetcher.getCurrentViews(viewClass);

//OnlyVisible为true时移除搜索的InvisibleView对象
if(onlyVisible){
viewsToReturn = RobotiumUtils.removeInvisibleViews(viewsToReturn);
}
//返回结果
if(viewClass.isAssignableFrom(TextView.class)) {
viewsToReturn.addAll((Collection<? extends T>) webUtils.getTextViewsFromWebView());
}
return viewsToReturn;
}
};


try {
return searchFor(viewFetcherCallback, regex, expectedMinimumNumberOfMatches, timeout, scroll);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值