Search in sources :

Example 1 with SetBreakpointHandler

use of com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudBreakpointHandler method registerBreakpoint.

/**
 * Called by IntelliJ when the user has enabled or created a new breakpoint. Creates the server
 * breakpoint.
 *
 * @param ideBreakpoint breakpoint to register
 */
@Override
public void registerBreakpoint(@NotNull final XLineBreakpoint<CloudLineBreakpointProperties> ideBreakpoint) {
    if (ideBreakpoint.getSourcePosition() == null || !ideBreakpoint.isEnabled() || !(ideBreakpoint.getType() instanceof CloudLineBreakpointType)) {
        return;
    }
    com.intellij.debugger.ui.breakpoints.Breakpoint cloudIdeBreakpoint = BreakpointManager.getJavaBreakpoint(ideBreakpoint);
    if (!(cloudIdeBreakpoint instanceof CloudLineBreakpointType.CloudLineBreakpoint)) {
        LOG.error("breakpoint was not of the correct type to create on the cloud.  It was not a " + "CloudLineBreakpoint");
        return;
    }
    final CloudLineBreakpointType.CloudLineBreakpoint cloudIdeLineBreakpoint = (CloudLineBreakpointType.CloudLineBreakpoint) cloudIdeBreakpoint;
    if (ideBreakpoint.getProperties().isCreatedByServer()) {
        // gets called during construction.
        return;
    }
    if (ideBreakpoint.getProperties().isAddedOnServer() && !ideBreakpoint.getProperties().isDisabledByServer()) {
        // disabled breakpoint
        return;
    }
    PsiFile javaFile = psiManager.findFile(ideBreakpoint.getSourcePosition().getFile());
    if (!(javaFile instanceof PsiJavaFile)) {
        return;
    }
    SourceLocation location = new SourceLocation();
    // Sending the file as com/package/example/Class.java to Cloud Debugger because it plays nice
    // with the CDB plugin. See ServerToIdeFileResolver.
    location.setPath(ServerToIdeFileResolver.getCloudPathFromJavaFile((PsiJavaFile) javaFile));
    location.setLine(ideBreakpoint.getSourcePosition().getLine() + 1);
    Breakpoint serverNewBreakpoint = new Breakpoint();
    serverNewBreakpoint.setLocation(location);
    if (ideBreakpoint.getConditionExpression() != null) {
        serverNewBreakpoint.setCondition(ideBreakpoint.getConditionExpression().getExpression());
    }
    List<String> watches = cloudIdeLineBreakpoint.getWatchExpressions();
    if (watches != null) {
        serverNewBreakpoint.setExpressions(watches);
    }
    // The breakpoint will enter error state asynchronously.  For now, we state that its verified.
    process.getStateController().setBreakpointAsync(serverNewBreakpoint, new SetBreakpointHandler() {

        @Override
        public void onSuccess(@NotNull final String id) {
            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    if (!Strings.isNullOrEmpty(id)) {
                        if (!cloudIdeLineBreakpoint.isEnabled()) {
                            process.getStateController().deleteBreakpointAsync(// race condition
                            id);
                        } else {
                            // Success.
                            // Mark as added so we don't add it again.
                            ideBreakpoint.getProperties().setAddedOnServer(true);
                            cloudIdeLineBreakpoint.setErrorMessage(null);
                            process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
                        }
                    } else {
                        // TODO(joaomartins): Why couldn't the breakpoint be set? Improve this
                        // message.
                        cloudIdeLineBreakpoint.setErrorMessage(GctBundle.getString("clouddebug.errorset"));
                        process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
                    }
                    if (!Strings.isNullOrEmpty(id)) {
                        ideBreakpoint.getProperties().setDisabledByServer(false);
                        String oldId = ideBreakpoint.getUserData(CLOUD_ID);
                        if (!Strings.isNullOrEmpty(oldId)) {
                            ideBreakpoints.remove(oldId);
                        }
                        ideBreakpoint.putUserData(CLOUD_ID, id);
                        ideBreakpoints.put(id, ideBreakpoint);
                    }
                }
            };
            if (ApplicationManager.getApplication().isUnitTestMode()) {
                runnable.run();
            } else {
                SwingUtilities.invokeLater(runnable);
            }
        }

        @Override
        public void onError(String errorMessage) {
            cloudIdeLineBreakpoint.setErrorMessage(errorMessage);
            process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
        }
    });
}
Also used : SourceLocation(com.google.api.services.clouddebugger.v2.model.SourceLocation) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) XBreakpoint(com.intellij.xdebugger.breakpoints.XBreakpoint) CloudLineBreakpoint(com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint) XLineBreakpoint(com.intellij.xdebugger.breakpoints.XLineBreakpoint) CloudLineBreakpoint(com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint) CloudLineBreakpoint(com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint) SetBreakpointHandler(com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler) PsiJavaFile(com.intellij.psi.PsiJavaFile) PsiFile(com.intellij.psi.PsiFile)

Example 2 with SetBreakpointHandler

use of com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudBreakpointHandlerTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    fixture = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getTestName(true)).getFixture();
    fixture.setUp();
    project = new MockProjectEx(getTestRootDisposable());
    psiManager = mock(PsiManager.class);
    project.registerService(PsiManager.class, psiManager);
    XDebugSession session = mock(XDebugSession.class);
    when(session.getProject()).thenReturn(project);
    process = mock(CloudDebugProcess.class);
    when(process.getXDebugSession()).thenReturn(session);
    CloudDebugProcessState processState = mock(CloudDebugProcessState.class);
    existingBreakpoints = new ArrayList<Breakpoint>();
    when(processState.getCurrentServerBreakpointList()).thenReturn(ContainerUtil.immutableList(existingBreakpoints));
    when(process.getProcessState()).thenReturn(processState);
    stateController = mock(CloudDebugProcessStateController.class);
    when(process.getStateController()).thenReturn(stateController);
    registrationShouldSucceed = true;
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            addedBp.set((Breakpoint) invocation.getArguments()[0]);
            SetBreakpointHandler handler = (SetBreakpointHandler) invocation.getArguments()[1];
            if (registrationShouldSucceed) {
                handler.onSuccess(desiredResultId);
            } else {
                handler.onError("Registration failed");
            }
            return null;
        }
    }).when(stateController).setBreakpointAsync(any(Breakpoint.class), any(SetBreakpointHandler.class));
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            removedBp.set((String) invocation.getArguments()[0]);
            return null;
        }
    }).when(stateController).deleteBreakpointAsync(anyString());
    fileResolver = mock(ServerToIdeFileResolver.class);
    handler = new CloudBreakpointHandler(process, fileResolver);
    XDebuggerManager debuggerManager = mock(XDebuggerManager.class);
    project.addComponent(XDebuggerManager.class, debuggerManager);
    breakpointManager = mock(XBreakpointManager.class);
    when(debuggerManager.getBreakpointManager()).thenReturn(breakpointManager);
}
Also used : XDebugSession(com.intellij.xdebugger.XDebugSession) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) XBreakpoint(com.intellij.xdebugger.breakpoints.XBreakpoint) CloudLineBreakpoint(com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint) XLineBreakpoint(com.intellij.xdebugger.breakpoints.XLineBreakpoint) PsiManager(com.intellij.psi.PsiManager) SetBreakpointHandler(com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler) XDebuggerManager(com.intellij.xdebugger.XDebuggerManager) Matchers.anyString(org.mockito.Matchers.anyString) MockProjectEx(com.intellij.mock.MockProjectEx) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) XBreakpointManager(com.intellij.xdebugger.breakpoints.XBreakpointManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Aggregations

Breakpoint (com.google.api.services.clouddebugger.v2.model.Breakpoint)2 SetBreakpointHandler (com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler)2 CloudLineBreakpoint (com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint)2 XBreakpoint (com.intellij.xdebugger.breakpoints.XBreakpoint)2 XLineBreakpoint (com.intellij.xdebugger.breakpoints.XLineBreakpoint)2 SourceLocation (com.google.api.services.clouddebugger.v2.model.SourceLocation)1 MockProjectEx (com.intellij.mock.MockProjectEx)1 PsiFile (com.intellij.psi.PsiFile)1 PsiJavaFile (com.intellij.psi.PsiJavaFile)1 PsiManager (com.intellij.psi.PsiManager)1 XDebugSession (com.intellij.xdebugger.XDebugSession)1 XDebuggerManager (com.intellij.xdebugger.XDebuggerManager)1 XBreakpointManager (com.intellij.xdebugger.breakpoints.XBreakpointManager)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1