public void testOpenUrl() {
    if (!PlatformUiUtil.hasInternalBrowser()) {
      return;
    }

    TasksUiUtil.openUrl(null);
    assertEquals(1, activePage.getEditorReferences().length);
    IEditorPart editor = activePage.getEditorReferences()[0].getEditor(true);
    assertEquals(WebBrowserEditor.class, editor.getClass());
    assertEquals(WebBrowserEditorInput.class, editor.getEditorInput().getClass());
    assertEquals(null, ((WebBrowserEditorInput) editor.getEditorInput()).getURL());

    TasksUiUtil.openUrl("http://eclipse.org/mylyn");
    assertEquals(2, activePage.getEditorReferences().length);
    editor = activePage.getEditorReferences()[0].getEditor(true);
    assertEquals(WebBrowserEditor.class, editor.getClass());
    assertEquals(WebBrowserEditorInput.class, editor.getEditorInput().getClass());
    assertEquals(null, ((WebBrowserEditorInput) editor.getEditorInput()).getURL());

    IEditorPart editor2 = activePage.getEditorReferences()[1].getEditor(true);
    assertEquals(WebBrowserEditor.class, editor2.getClass());
    assertEquals(WebBrowserEditorInput.class, editor2.getEditorInput().getClass());
    assertNotNull(((WebBrowserEditorInput) editor2.getEditorInput()).getURL());
    assertEquals(
        "http://eclipse.org/mylyn",
        ((WebBrowserEditorInput) editor2.getEditorInput()).getURL().toString());
  }
  public void testFlagNoRichEditor() throws Exception {
    if (!PlatformUiUtil.hasInternalBrowser()) {
      return;
    }

    TasksUiUtil.openUrl(null);
    assertEquals(1, activePage.getEditorReferences().length);
    IEditorPart editor = activePage.getEditorReferences()[0].getEditor(true);
    assertEquals(WebBrowserEditor.class, editor.getClass());
    assertEquals(WebBrowserEditorInput.class, editor.getEditorInput().getClass());
    assertEquals(null, ((WebBrowserEditorInput) editor.getEditorInput()).getURL());
    WebBrowserEditorInput input = ((WebBrowserEditorInput) editor.getEditorInput());
    Field f = input.getClass().getDeclaredField("style");
    f.setAccessible(true);
    int style = (Integer) f.get(input);
    assertFalse((style & BrowserUtil.NO_RICH_EDITOR) == 0);

    TasksUiUtil.openUrl("http://eclipse.org/mylyn");
    assertEquals(2, activePage.getEditorReferences().length);
    editor = activePage.getEditorReferences()[0].getEditor(true);
    assertEquals(WebBrowserEditor.class, editor.getClass());
    assertEquals(WebBrowserEditorInput.class, editor.getEditorInput().getClass());
    assertEquals(null, ((WebBrowserEditorInput) editor.getEditorInput()).getURL());
    input = ((WebBrowserEditorInput) editor.getEditorInput());
    f = input.getClass().getDeclaredField("style");
    f.setAccessible(true);
    style = (Integer) f.get(input);
    assertFalse((style & BrowserUtil.NO_RICH_EDITOR) == 0);

    IEditorPart editor2 = activePage.getEditorReferences()[1].getEditor(true);
    assertEquals(WebBrowserEditor.class, editor2.getClass());
    assertEquals(WebBrowserEditorInput.class, editor2.getEditorInput().getClass());
    assertNotNull(((WebBrowserEditorInput) editor2.getEditorInput()).getURL());
    assertEquals(
        "http://eclipse.org/mylyn",
        ((WebBrowserEditorInput) editor2.getEditorInput()).getURL().toString());
    input = ((WebBrowserEditorInput) editor.getEditorInput());
    f = input.getClass().getDeclaredField("style");
    f.setAccessible(true);
    style = (Integer) f.get(input);
    assertFalse((style & BrowserUtil.NO_RICH_EDITOR) == 0);

    // open task should not set FLAG_NO_RICH_EDITOR
    TasksUiUtil.openTask("http://eclipse.org/mylyn/test");
    assertEquals(3, activePage.getEditorReferences().length);
    editor = activePage.getEditorReferences()[2].getEditor(true);
    assertEquals(WebBrowserEditor.class, editor.getClass());
    assertEquals(WebBrowserEditorInput.class, editor.getEditorInput().getClass());
    assertEquals(
        "http://eclipse.org/mylyn/test",
        ((WebBrowserEditorInput) editor.getEditorInput()).getURL().toString());
    input = ((WebBrowserEditorInput) editor.getEditorInput());
    f = input.getClass().getDeclaredField("style");
    f.setAccessible(true);
    style = (Integer) f.get(input);
    assertTrue((style & BrowserUtil.NO_RICH_EDITOR) == 0);
  }
Пример #3
0
 /**
  * Returns the list of identifiers that should not be shown for the palette.
  *
  * @param part the editor for which the palette is shown
  * @return a list of identifiers of palettes that should not be shown.
  */
 public static List<String> getHiddenPalettes(IEditorPart part) {
   // retrieve preferences for the given editor
   XMLMemento rootMemento = getExistingCustomizations();
   IMemento editorMemento = getEditorMemento(rootMemento, part.getClass().getName());
   IMemento hiddenPalettesMemento = getEditorHiddenPalettesMemento(editorMemento);
   return getHiddenPalettesList(hiddenPalettesMemento);
 }
  /**
   * the command has been executed, so extract extract the needed information from the application
   * context.
   */
  public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (!ITextEditor.class.isAssignableFrom(activeEditor.getClass())) {
      return null;
    }
    ITextEditor iTextEditor = (ITextEditor) activeEditor;
    ISelection selection = iTextEditor.getSelectionProvider().getSelection();
    if (!ITextSelection.class.isAssignableFrom(selection.getClass())) {
      return null;
    }
    ITextSelection textSelection = (ITextSelection) selection;

    IHandlerService service =
        (IHandlerService) HandlerUtil.getActiveSite(event).getService(IHandlerService.class);
    try {
      if (textSelection.getLength() == 0) {
        service.executeCommand("org.eclipse.ui.edit.text.cut.line", null);
      } else {
        service.executeCommand("org.eclipse.ui.edit.cut", null);
      }
    } catch (NotDefinedException e) {
      throw new ExecutionException("Failed to cut line or selection", e);
    } catch (NotEnabledException e) {
      throw new ExecutionException("Failed to cut line or selection", e);
    } catch (NotHandledException e) {
      throw new ExecutionException("Failed to cut line or selection", e);
    }

    return null;
  }
Пример #5
0
 /** @return python editor. */
 protected PyEdit getPyEdit() {
   if (targetEditor instanceof PyEdit) {
     return (PyEdit) targetEditor;
   } else {
     throw new RuntimeException(
         "Expecting PyEdit editor. Found:" + targetEditor.getClass().getName());
   }
 }
Пример #6
0
 /** This function returns the text editor. */
 protected ITextEditor getTextEditor() {
   if (targetEditor instanceof ITextEditor) {
     return (ITextEditor) targetEditor;
   } else {
     throw new RuntimeException(
         "Expecting text editor. Found:" + targetEditor.getClass().getName());
   }
 }
Пример #7
0
 /** Selects and reveals the given offset and length in the given editor part. */
 public static void revealInEditor(final IEditorPart editor, final int offset, final int length) {
   if (editor instanceof ITextEditor) {
     ((ITextEditor) editor).selectAndReveal(offset, length);
     return;
   }
   ErlLogger.warn(
       "EditorUtility.revealInEditor should only be called on an ErlangEditor; it was an %s",
       editor.getClass().getName());
 }
 public void testOpenTaskFromString() {
   if (!PlatformUiUtil.hasInternalBrowser()) {
     return;
   }
   TasksUiUtil.openTask((String) null);
   assertEquals(1, activePage.getEditorReferences().length);
   IEditorPart editor = activePage.getEditorReferences()[0].getEditor(true);
   assertEquals(WebBrowserEditor.class, editor.getClass());
 }
Пример #9
0
 private void setActivePart(final String simpleClassNameOfPage) {
   for (int i = 0; i < getPageCount(); i++) {
     final IEditorPart editorPart = getEditor(i);
     if (editorPart.getClass().getSimpleName().equals(simpleClassNameOfPage)) {
       setActivePage(i);
       return;
     }
   }
   setActivePage(0);
 }
Пример #10
0
  public static void checkRegions(
      IProject project,
      String fileName,
      List<TestRegion> regionList,
      AbstractHyperlinkDetector elDetector)
      throws Exception {
    IFile file = project.getFile(fileName);

    assertNotNull("The file \"" + fileName + "\" is not found", file);
    assertTrue("The file \"" + fileName + "\" is not found", file.isAccessible());

    FileEditorInput editorInput = new FileEditorInput(file);

    IDocumentProvider documentProvider = null;
    try {
      documentProvider = DocumentProviderRegistry.getDefault().getDocumentProvider(editorInput);
    } catch (Exception x) {
      x.printStackTrace();
      fail("An exception caught: " + x.getMessage());
    }

    assertNotNull(
        "The document provider for the file \"" + fileName + "\" is not loaded", documentProvider);

    try {
      documentProvider.connect(editorInput);
    } catch (Exception x) {
      x.printStackTrace();
      fail(
          "The document provider is not able to be initialized with the editor input\nAn exception caught: "
              + x.getMessage());
    }

    IDocument document = documentProvider.getDocument(editorInput);

    assertNotNull("The document for the file \"" + fileName + "\" is not loaded", document);

    if (regionList.get(0).region == null) loadRegions(regionList, document);

    int expected = 0;
    for (TestRegion testRegion : regionList) expected += testRegion.region.getLength() + 1;

    IEditorPart part = openFileInEditor(file);
    ISourceViewer viewer = null;
    if (part instanceof JavaEditor) {
      viewer = ((JavaEditor) part).getViewer();
      elDetector.setContext(new TestContext((ITextEditor) part));
    } else if (part instanceof EditorPartWrapper) {
      if (((EditorPartWrapper) part).getEditor() instanceof WebCompoundEditor) {
        WebCompoundEditor wce = (WebCompoundEditor) ((EditorPartWrapper) part).getEditor();
        viewer = wce.getSourceEditor().getTextViewer();
        elDetector.setContext(new TestContext(wce.getSourceEditor()));
      } else if (((EditorPartWrapper) part).getEditor() instanceof XMLTextEditorStandAlone) {
        XMLTextEditorStandAlone xtesa =
            (XMLTextEditorStandAlone) ((EditorPartWrapper) part).getEditor();
        viewer = xtesa.getTextViewer();
        elDetector.setContext(new TestContext(xtesa));
      } else fail("unsupported editor type - " + ((EditorPartWrapper) part).getEditor().getClass());
    } else if (part instanceof JSPMultiPageEditor) {
      viewer = ((JSPMultiPageEditor) part).getJspEditor().getTextViewer();
      elDetector.setContext(new TestContext(((JSPMultiPageEditor) part).getJspEditor()));
    } else fail("unsupported editor type - " + part.getClass());

    int counter = 0;
    for (int i = 0; i < document.getLength(); i++) {
      int lineNumber = document.getLineOfOffset(i);
      int position = i - document.getLineOffset(lineNumber) + 1;
      lineNumber++;

      TestData testData = new TestData(document, i);
      IHyperlink[] links = elDetector.detectHyperlinks(viewer, testData.getHyperlinkRegion(), true);

      boolean recognized = links != null;

      if (recognized) {
        counter++;
        TestRegion testRegion = findOffsetInRegions(i, regionList);
        if (testRegion == null) {
          String information = findRegionInformation(document, i, regionList);
          fail(
              "Wrong detection for offset - "
                  + i
                  + " (line - "
                  + lineNumber
                  + " position - "
                  + position
                  + ") "
                  + information);
        } else {
          checkTestRegion(links, testRegion);
        }
      } else {
        for (TestRegion testRegion : regionList) {
          if (i >= testRegion.region.getOffset()
              && i <= testRegion.region.getOffset() + testRegion.region.getLength()) {
            fail(
                "Wrong detection for region - "
                    + getRegionInformation(document, testRegion)
                    + " offset - "
                    + i
                    + " (line - "
                    + lineNumber
                    + " position - "
                    + position
                    + ")");
          }
        }
      }
    }

    assertEquals("Wrong recognized region count: ", expected, counter);

    documentProvider.disconnect(editorInput);
  }
Пример #11
0
 /**
  * Returns the name of the class of the editor
  *
  * @param editorClass the editor for which the class name is searched
  * @return the class name
  */
 protected static String getEditorClassName(IEditorPart editorClass) {
   return editorClass.getClass().getName();
 }
Пример #12
0
  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);
  }
Пример #13
0
 @Override
 protected void pageChange(final int newPageIndex) {
   super.pageChange(newPageIndex);
   final IEditorPart activeEditor = getActiveEditor();
   saveActivePage(activeEditor.getClass().getSimpleName());
 }