Skip to content

Commit

Permalink
Add PDF Exporter unit test to help with #2093
Browse files Browse the repository at this point in the history
  • Loading branch information
mbastian committed Sep 3, 2022
1 parent 28ba2cd commit 11c73f1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
11 changes: 11 additions & 0 deletions modules/GraphAPI/src/test/java/org/gephi/graph/GraphGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ public GraphGenerator generateTinyMultiGraph() {
return this;
}

public GraphGenerator addRandomPositions() {
Random random = new Random();
double size = 100.0;
for (Node node : graphModel.getGraph().getNodes()) {
node.setX((float) (-size / 2 + size * random.nextDouble()));
node.setY((float) (-size / 2 + size * random.nextDouble()));
node.setSize(random.nextFloat() * (float)size / 25f);
}
return this;
}

public GraphGenerator addIntNodeColumn() {
graphModel.getNodeTable().addColumn(INT_COLUMN, Integer.class);
int age = INT_COLUMN_MIN_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ public synchronized void refreshPreview(Workspace workspace) {

Renderer[] renderers;
if (!mousePressed) {
renderers = model.getManagedEnabledRenderers();
renderers = previewModel.getManagedEnabledRenderers();
} else {
ArrayList<Renderer> renderersList = new ArrayList<>();
for (Renderer renderer : model.getManagedEnabledRenderers()) {
for (Renderer renderer : previewModel.getManagedEnabledRenderers()) {
//Only mouse responsive renderers will be called while mouse is pressed
if (renderer instanceof MouseResponsiveRenderer) {
renderersList.add(renderer);
Expand Down Expand Up @@ -198,7 +198,7 @@ public synchronized void refreshPreview(Workspace workspace) {

//Pre process renderers
for (Renderer r : renderers) {
r.preProcess(model);
r.preProcess(previewModel);
}
}

Expand Down
12 changes: 12 additions & 0 deletions modules/PreviewExport/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-util-lookup</artifactId>
</dependency>
<!-- Test only -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>preview-plugin</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>graph-api</artifactId>
<scope>test</scope>
<type>test-jar</type>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.gephi.io.exporter.preview;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.gephi.graph.GraphGenerator;
import org.gephi.preview.api.PreviewController;
import org.gephi.preview.api.PreviewProperties;
import org.gephi.preview.api.PreviewProperty;
import org.gephi.preview.spi.Renderer;
import org.gephi.project.api.Workspace;
import org.junit.Assert;
import org.junit.Test;
import org.openide.util.Lookup;

public class PDFExporterTest {

@Test
public void testPDFExporter() throws IOException {
Workspace workspace = GraphGenerator.build().withWorkspace()
.generateSmallRandomGraph().addRandomPositions().getWorkspace();

PreviewController previewController = Lookup.getDefault().lookup(PreviewController.class);
PreviewProperties props = previewController.getModel(workspace).getProperties();
props.putValue(PreviewProperty.BACKGROUND_COLOR, Color.CYAN);

PDFExporter pdfExporter = new PDFExporter();
pdfExporter.setWorkspace(workspace);
File tempFile = new File("testPDFExporter.pdf");
tempFile.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempFile);
pdfExporter.setOutputStream(fos);
pdfExporter.execute();
fos.close();

Assert.assertTrue(tempFile.length() > 0);
}
}

0 comments on commit 11c73f1

Please sign in to comment.