protected IFile getTestDataFile( IProject project, String sourceName, String destPath, boolean overwrite) throws Exception { String[] split = destPath.split("/"); // $NON-NLS-1$ IContainer parent; String name; if (split.length == 1) { parent = project; name = destPath; } else { IFolder folder = project.getFolder(split[0]); NullProgressMonitor monitor = new NullProgressMonitor(); if (!folder.exists()) { folder.create(true /* force */, true /* local */, monitor); } for (int i = 1, n = split.length; i < n - 1; i++) { IFolder subFolder = folder.getFolder(split[i]); if (!subFolder.exists()) { subFolder.create(true /* force */, true /* local */, monitor); } folder = subFolder; } name = split[split.length - 1]; parent = folder; } IFile file = parent.getFile(new Path(name)); if (overwrite && file.exists()) { String currentContents = AndmoreAndroidPlugin.readFile(file); String newContents = readTestFile(sourceName, true); if (currentContents == null || !currentContents.equals(newContents)) { file.delete(true, new NullProgressMonitor()); } else { return file; } } if (!file.exists()) { String xml = readTestFile(sourceName, true); InputStream bstream = new ByteArrayInputStream(xml.getBytes("UTF-8")); // $NON-NLS-1$ NullProgressMonitor monitor = new NullProgressMonitor(); file.create(bstream, false /* force */, monitor); } return file; }
protected int getCaretOffset(IFile file, String caretLocation) { assertTrue(caretLocation, caretLocation.contains("^")); String fileContent = AndmoreAndroidPlugin.readFile(file); return getCaretOffset(fileContent, caretLocation); }
private void checkResourceFix(String name, String caretLocation, String expectedNewPath) throws Exception { IProject project = getProject(); IFile file = getTestDataFile(project, name, FD_RES + "/" + FD_RES_LAYOUT + "/" + name); // Determine the offset final int offset = getCaretOffset(file, caretLocation); String osRoot = project.getLocation().toOSString(); List<String> errors = new ArrayList<String>(); String fileRelativePath = file.getProjectRelativePath().toPortableString(); String filePath = osRoot + File.separator + fileRelativePath; // Run AaptParser such that markers are added... // When debugging these tests, the project gets a chance to build itself // so // the real aapt errors are there. But when the test is run directly, // aapt has // not yet run. I tried waiting for the build (using the code in // SampleProjectTest) // but this had various adverse effects (exception popups from the // Eclipse debugger // etc) so instead this test just hardcodes the aapt errors that should // be // observed on quickfix1.xml. assertEquals("Unit test is hardcoded to errors for quickfix1.xml", "quickfix1.xml", name); errors.add( filePath + ":7: error: Error: No resource found that matches the given name" + " (at 'text' with value '@string/firststring')."); errors.add( filePath + ":7: error: Error: No resource found that matches the given name" + " (at 'layout_width' with value '@dimen/testdimen')."); errors.add( filePath + ":13: error: Error: No resource found that matches the given name" + " (at 'layout' with value '@layout/testlayout')."); AaptParser.parseOutput(errors, project); AaptQuickFix aaptQuickFix = new AaptQuickFix(); // Open file IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); assertNotNull(page); IEditorPart editor = IDE.openEditor(page, file); assertTrue(editor instanceof AndroidXmlEditor); AndroidXmlEditor layoutEditor = (AndroidXmlEditor) editor; final ISourceViewer viewer = layoutEditor.getStructuredSourceViewer(); // Test marker resolution. IMarker[] markers = file.findMarkers(AndmoreAndroidConstants.MARKER_AAPT_COMPILE, true, IResource.DEPTH_ZERO); for (IMarker marker : markers) { int start = marker.getAttribute(IMarker.CHAR_START, 0); int end = marker.getAttribute(IMarker.CHAR_END, 0); if (offset >= start && offset <= end) { // Found the target marker. Now check the marker resolution of // it. assertTrue(aaptQuickFix.hasResolutions(marker)); IMarkerResolution[] resolutions = aaptQuickFix.getResolutions(marker); assertNotNull(resolutions); assertEquals(1, resolutions.length); IMarkerResolution resolution = resolutions[0]; assertNotNull(resolution); assertTrue(resolution.getLabel().contains("Create resource")); // Not running marker yet -- if we create the files here they // already // exist when the quick assist code runs. (The quick fix and the // quick assist // mostly share code for the implementation anyway.) // resolution.run(marker); break; } } // Next test quick assist. IQuickAssistInvocationContext invocationContext = new IQuickAssistInvocationContext() { @Override public int getLength() { return 0; } @Override public int getOffset() { return offset; } @Override public ISourceViewer getSourceViewer() { return viewer; } }; ICompletionProposal[] proposals = aaptQuickFix.computeQuickAssistProposals(invocationContext); assertNotNull(proposals); assertTrue(proposals.length == 1); ICompletionProposal proposal = proposals[0]; assertNotNull(proposal.getAdditionalProposalInfo()); assertNotNull(proposal.getImage()); assertTrue(proposal.getDisplayString().contains("Create resource")); IDocument document = new Document(); String fileContent = AndmoreAndroidPlugin.readFile(file); document.set(fileContent); // Apply quick fix proposal.apply(document); IPath path = new Path(expectedNewPath); IFile newFile = project.getFile(path); assertNotNull(path.toPortableString(), newFile); // Ensure that the newly created file was opened IEditorPart currentFile = AdtUtils.getActiveEditor(); assertEquals( newFile.getProjectRelativePath(), ((FileEditorInput) currentFile.getEditorInput()).getFile().getProjectRelativePath()); // Look up caret offset assertTrue( currentFile != null ? currentFile.getClass().getName() : "null", currentFile instanceof AndroidXmlEditor); AndroidXmlEditor newEditor = (AndroidXmlEditor) currentFile; ISourceViewer newViewer = newEditor.getStructuredSourceViewer(); Point selectedRange = newViewer.getSelectedRange(); String newFileContents = AndmoreAndroidPlugin.readFile(newFile); // Insert selection markers -- [ ] for the selection range, ^ for the // caret String newFileWithCaret = addSelection(newFileContents, selectedRange); newFileWithCaret = removeSessionData(newFileWithCaret); assertEqualsGolden(name, newFileWithCaret); }