/** * Create a new rendering session and test that rendering /layout/activity.xml on nexus 5 doesn't * throw any exceptions. */ @Test public void testRendering() throws ClassNotFoundException { // Create the layout pull parser. LayoutPullParser parser = new LayoutPullParser(APP_TEST_RES + "/layout/activity.xml"); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger()); layoutLibCallback.initResources(); // TODO: Set up action bar handler properly to test menu rendering. // Create session params. SessionParams params = getSessionParams(parser, ConfigGenerator.NEXUS_5, layoutLibCallback); RenderSession session = mBridge.createSession(params); if (!session.getResult().isSuccess()) { getLogger().error(session.getResult().getException(), session.getResult().getErrorMessage()); } // Render the session with a timeout of 50s. Result renderResult = session.render(50000); if (!renderResult.isSuccess()) { getLogger().error(session.getResult().getException(), session.getResult().getErrorMessage()); } try { String goldenImagePath = APP_TEST_DIR + "/golden/activity.png"; ImageUtils.requireSimilar(goldenImagePath, session.getImage()); } catch (IOException e) { getLogger().error(e, e.getMessage()); } }
/** * Looks at the parent-chain of the view and if it finds a custom view, or a CalendarView, within * the given distance then it returns true. A ListView within a CalendarView should not be * assigned a custom list view type because it sets its own and then attempts to cast the layout * to its own type which would fail if the normal default list item binding is used. */ private boolean isWithinIllegalParent(Object viewObject, int depth) { String fqcn = viewObject.getClass().getName(); if (fqcn.endsWith(CALENDAR_VIEW) || !fqcn.startsWith(ANDROID_PKG_PREFIX)) { return true; } if (depth > 0) { Result result = mLayoutLib.getViewParent(viewObject); if (result.isSuccess()) { Object parent = result.getData(); if (parent != null) { return isWithinIllegalParent(parent, depth - 1); } } } return false; }
/** Render immediately */ private void renderSync() { GraphicalEditorPart editor = mCanvas.getEditorDelegate().getGraphicalEditor(); if (editor.getReadyLayoutLib(false /*displayError*/) == null) { // Don't attempt to render when there is no ready layout library: most likely // the targets are loading/reloading. return; } disposeThumbnail(); Configuration configuration = mAlternateInput != null && mAlternateConfiguration != null ? mAlternateConfiguration : mConfiguration; ResourceResolver resolver = getResourceResolver(configuration); RenderService renderService = RenderService.create(editor, configuration, resolver); if (mIncludedWithin != null) { renderService.setIncludedWithin(mIncludedWithin); } if (mAlternateInput != null) { IAndroidTarget target = editor.getRenderingTarget(); AndroidTargetData data = null; if (target != null) { Sdk sdk = Sdk.getCurrent(); if (sdk != null) { data = sdk.getTargetData(target); } } // Construct UI model from XML DocumentDescriptor documentDescriptor; if (data == null) { documentDescriptor = new DocumentDescriptor("temp", null); // $NON-NLS-1$ } else { documentDescriptor = data.getLayoutDescriptors().getDescriptor(); } UiDocumentNode model = (UiDocumentNode) documentDescriptor.createUiNode(); model.setEditor(mCanvas.getEditorDelegate().getEditor()); model.setUnknownDescriptorProvider(editor.getModel().getUnknownDescriptorProvider()); Document document = DomUtilities.getDocument(mAlternateInput); if (document == null) { mError = "No document"; createErrorThumbnail(); return; } model.loadFromXmlNode(document); renderService.setModel(model); } else { renderService.setModel(editor.getModel()); } RenderLogger log = new RenderLogger(getDisplayName()); renderService.setLog(log); RenderSession session = renderService.createRenderSession(); Result render = session.render(1000); if (DUMP_RENDER_DIAGNOSTICS) { if (log.hasProblems() || !render.isSuccess()) { AdtPlugin.log( IStatus.ERROR, "Found problems rendering preview " + getDisplayName() + ": " + render.getErrorMessage() + " : " + log.getProblems(false)); Throwable exception = render.getException(); if (exception != null) { AdtPlugin.log(exception, "Failure rendering preview " + getDisplayName()); } } } if (render.isSuccess()) { mError = null; } else { mError = render.getErrorMessage(); if (mError == null) { mError = ""; } } if (render.getStatus() == Status.ERROR_TIMEOUT) { // TODO: Special handling? schedule update again later return; } if (render.isSuccess()) { BufferedImage image = session.getImage(); if (image != null) { createThumbnail(image); } } if (mError != null) { createErrorThumbnail(); } }