Search in sources :

Example 1 with UiHierarchyXmlLoader

use of com.android.uiautomator.tree.UiHierarchyXmlLoader in project uiautomatorviewer by yangzaiCN.

the class UiAutomatorModel method loadScreenshotAndXmlDump.

public boolean loadScreenshotAndXmlDump(File screenshotFile, File xmlDumpFile) {
    if (screenshotFile != null && xmlDumpFile != null && screenshotFile.isFile() && xmlDumpFile.isFile()) {
        ImageData[] data = null;
        Image img = null;
        try {
            // use SWT's ImageLoader to read png from path
            data = new ImageLoader().load(screenshotFile.getAbsolutePath());
        } catch (SWTException e) {
            e.printStackTrace();
            return false;
        }
        // i.e. gifs or icons, we just care if it has at least one here
        if (data.length < 1)
            return false;
        BasicTreeNode rootNode = new UiHierarchyXmlLoader().parseXml(xmlDumpFile.getAbsolutePath());
        if (rootNode == null) {
            System.err.println("null rootnode after parsing.");
            return false;
        }
        try {
            // Image is tied to ImageData and a Display, so we only need to create once
            // per new image
            img = new Image(mView.getShell().getDisplay(), data[0]);
        } catch (SWTException e) {
            e.printStackTrace();
            return false;
        }
        // only update screenhot and xml if both are loaded successfully
        if (mScreenshot != null) {
            mScreenshot.dispose();
        }
        mScreenshot = img;
        if (mRootNode != null) {
            mRootNode.clearAllChildren();
        }
        // TODO: we should verify here if the coordinates in the XML matches the png
        // or not: think loading a phone screenshot with a tablet XML dump
        mRootNode = rootNode;
        mScreenshotFile = screenshotFile;
        mXmlDumpFile = xmlDumpFile;
        mExploreMode = true;
        mView.loadScreenshotAndXml();
        return true;
    }
    return false;
}
Also used : UiHierarchyXmlLoader(com.android.uiautomator.tree.UiHierarchyXmlLoader) SWTException(org.eclipse.swt.SWTException) ImageData(org.eclipse.swt.graphics.ImageData) Image(org.eclipse.swt.graphics.Image) ImageLoader(org.eclipse.swt.graphics.ImageLoader) BasicTreeNode(com.android.uiautomator.tree.BasicTreeNode)

Aggregations

BasicTreeNode (com.android.uiautomator.tree.BasicTreeNode)1 UiHierarchyXmlLoader (com.android.uiautomator.tree.UiHierarchyXmlLoader)1 SWTException (org.eclipse.swt.SWTException)1 Image (org.eclipse.swt.graphics.Image)1 ImageData (org.eclipse.swt.graphics.ImageData)1 ImageLoader (org.eclipse.swt.graphics.ImageLoader)1