public void takeScreenshot(){
View decorView = viewFetcher.getRecentDecorView(viewFetcher.getWindowDecorViews());
screenshotTaker.takeScreenshot(decorView, null);
}
viewFetcher.getWindowDecorViews()用來獲取當前屏幕上堆積的全部DecorView(能夠把一個DecorView當作是一個畫布,當你打開好多Activity時,至關於有好幾個畫布疊在了一塊兒,每一個畫布上畫着一個activity的畫面)。而viewFetcher.getRecentDecorView()就至關於取最上一層的畫布。進入ViewFetcher.java文件觀察viewFetcher.getRecentDecorView()的源碼,我發現它在排除了一些無用的view後,實際返回的是ViewFetcher.java中的另外一個函數的結果:getRecentContainer(decorViews)。Be patient, 讓咱們看看getRecentContainer(decorViews)裏賣的是什麼藥:
private final View getRecentContainer(View[] views) {
View container = null;
long drawingTime = 0;
View view;
for(int i = 0; i < views.length; i++){
view = views[i]; if (view != null && view.isShown() && view.hasWindowFocus() && view.getDrawingTime() > drawingTime) {
container = view;
drawingTime = view.getDrawingTime();
}
}
return container;
}
getRecentContainer(decorViews)函數判斷哪一個view在最上層的方法,是判斷:
1. view不爲空
view可見
3. view已經被focus上(就是該view是否處於被選中的、被激活的窗口中)
4. 渲染時間排在最後
public void takeScreenshot(){
View decorView = null;
while(decorView == null){
sleep(500);
View[] decorViews = viewFetcher.getWindowDecorViews();
decorView = viewFetcher.getRecentDecorView(decorViews);
}
screenshotTaker.takeScreenshot(decorView, null);
}
https://code.google.com/p/robotium/issues/detail?id=434&colspec=ID%20Type%20Stars%20Status%20Priority%20Milestone%20Owner%20Summary
Solo.java中添加:
public void assertCurrentActivity(String message, String name, int timeout, boolean takeScreenshot)
{
if(takeScreenshot){
asserter.assertCurrentActivity(message, name, timeout, this);
} else
assertCurrentActivity(message, name, timeout);
}
Asserter.java中添加:
public void assertCurrentActivity(String message, String name, int timeout, Solo solo)
{
if(!waiter.waitForActivity(name, timeout)){
solo.takeScreenshot("test_" + message + "_Failure");
Assert.assertTrue(message, false);
} else
solo.takeScreenshot("test_" + message + "_Success");
}
public View getViewByHierarchyViewerId(String id){
Context currContext = instrumentation.getTargetContext();
String packageName = currContext.getPackageName();
int viewId = currContext.getResources().getIdentifier(id, "id", packageName);
return getView(viewId);