blob: 4a39d3e284749391313c07875b2e34c1dd418803 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2016, 2018 Rogue Wave Software Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Michał Niewrzał (Rogue Wave Software Inc.) - initial implementation
* Martin Lippert (Pivotal Inc.) - fixed instability
*******************************************************************************/
package org.eclipse.lsp4e.test.edit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.nio.file.Files;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.LanguageServiceAccessor;
import org.eclipse.lsp4e.test.AllCleanRule;
import org.eclipse.lsp4e.test.LSDisplayHelper;
import org.eclipse.lsp4e.test.TestUtils;
import org.eclipse.lsp4e.tests.mock.MockLanguageServer;
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
public class DocumentDidSaveTest {
@Rule public AllCleanRule clear = new AllCleanRule();
private IProject project;
@Before
public void setUp() throws CoreException {
project = TestUtils.createProject(getClass().getName() + System.currentTimeMillis());
}
@Test
public void testSave() throws Exception {
IFile testFile = TestUtils.createUniqueTestFile(project, "");
IEditorPart editor = TestUtils.openEditor(testFile);
ITextViewer viewer = TestUtils.getTextViewer(editor);
// make sure that timestamp after save will differ from creation time (no better idea at the moment)
testFile.setLocalTimeStamp(0);
// Force LS to initialize and open file
LanguageServiceAccessor.getLanguageServers(LSPEclipseUtils.getDocument(testFile), capabilites -> Boolean.TRUE);
CompletableFuture<DidSaveTextDocumentParams> didSaveExpectation = new CompletableFuture<DidSaveTextDocumentParams>();
MockLanguageServer.INSTANCE.setDidSaveCallback(didSaveExpectation);
// simulate change in file
viewer.getDocument().replace(0, 0, "Hello");
editor.doSave(new NullProgressMonitor());
new LSDisplayHelper(() -> {
try {
DidSaveTextDocumentParams lastChange = didSaveExpectation.get(10, TimeUnit.MILLISECONDS);
assertNotNull(lastChange.getTextDocument());
assertEquals(LSPEclipseUtils.toUri(testFile).toString(), lastChange.getTextDocument().getUri());
return true;
} catch (TimeoutException | InterruptedException | ExecutionException e) {
return false;
}
}).waitForCondition(Display.getCurrent(), 2000);
}
@Test
public void testSaveExternalFile() throws Exception {
File file = File.createTempFile("testSaveExternalFile", ".lspt");
try {
IEditorPart editor = IDE.openEditorOnFileStore(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), EFS.getStore(file.toURI()));
ITextViewer viewer = TestUtils.getTextViewer(editor);
// make sure that timestamp after save will differ from creation time (no better idea at the moment)
// testFile.setLocalTimeStamp(0);
// Force LS to initialize and open file
LanguageServiceAccessor.getLanguageServers(LSPEclipseUtils.getDocument(editor.getEditorInput()), capabilites -> Boolean.TRUE);
CompletableFuture<DidSaveTextDocumentParams> didSaveExpectation = new CompletableFuture<DidSaveTextDocumentParams>();
MockLanguageServer.INSTANCE.setDidSaveCallback(didSaveExpectation);
// simulate change in file
viewer.getDocument().replace(0, 0, "Hello");
editor.doSave(new NullProgressMonitor());
new LSDisplayHelper(() -> {
try {
DidSaveTextDocumentParams lastChange = didSaveExpectation.get(10, TimeUnit.MILLISECONDS);
assertNotNull(lastChange.getTextDocument());
assertEquals(LSPEclipseUtils.toUri(file).toString(), lastChange.getTextDocument().getUri());
return true;
} catch (TimeoutException | InterruptedException | ExecutionException e) {
return false;
}
}).waitForCondition(Display.getCurrent(), 2000);
} finally {
Files.deleteIfExists(file.toPath());
}
}
}