|
| 1 | +package net.imagej.plot.convert; |
| 2 | + |
| 3 | +import ij.ImagePlus; |
| 4 | +import net.imagej.plot.AbstractPlot; |
| 5 | +import net.imagej.plot.CategoryChart; |
| 6 | +import net.imagej.plot.PlotService; |
| 7 | +import net.imglib2.img.Img; |
| 8 | +import net.imglib2.type.numeric.ARGBType; |
| 9 | +import org.junit.Test; |
| 10 | +import org.scijava.Context; |
| 11 | +import org.scijava.convert.ConvertService; |
| 12 | + |
| 13 | +import java.lang.reflect.Type; |
| 14 | + |
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | +import static org.junit.Assert.assertFalse; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests {@link PlotToImgConverter} and {@link PlotToImagePlusConverter} |
| 21 | + * |
| 22 | + * @author Matthias Arzt |
| 23 | + */ |
| 24 | +public class PlotToImageConverterTest { |
| 25 | + |
| 26 | + private final Context context = new Context(PlotService.class, ConvertService.class); |
| 27 | + |
| 28 | + private final ConvertService convertService = context.service(ConvertService.class); |
| 29 | + |
| 30 | + private final PlotService plotService = context.service(PlotService.class); |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testCanConvertCategoryChartToImagePlus() { |
| 34 | + testCanConvertCategoryChartTo(ImagePlus.class); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testCanConvertCategoryChartToImg() { |
| 39 | + testCanConvertCategoryChartTo(Img.class); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testWontConvertAbstractPlotToImagePlus() { |
| 44 | + testWontConvertAbstractPlotTo(ImagePlus.class); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testWontConvertAbstractPlotToImg() { |
| 49 | + testWontConvertAbstractPlotTo(Img.class); |
| 50 | + } |
| 51 | + |
| 52 | + public void testCanConvertCategoryChartTo(Class<?> destClass) { |
| 53 | + CategoryChart<String> chart = plotService.newCategoryChart(String.class); |
| 54 | + assertTrue(convertService.supports(chart, destClass)); |
| 55 | + assertTrue(convertService.supports(chart, (Type) destClass)); |
| 56 | + } |
| 57 | + |
| 58 | + public void testWontConvertAbstractPlotTo(Class<?> destClass) { |
| 59 | + AbstractPlot ap = new CustomAbstractPlot(); |
| 60 | + assertFalse(convertService.supports(ap, destClass)); |
| 61 | + assertFalse(convertService.supports(ap, (Type) destClass)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testConversionToImagePlus() { |
| 66 | + // setup |
| 67 | + CategoryChart chart = plotService.newCategoryChart(String.class); |
| 68 | + chart.setTitle("Hello World!"); |
| 69 | + chart.setPreferredSize(12,23); |
| 70 | + // process |
| 71 | + ImagePlus imp = convertService.convert(chart, ImagePlus.class); |
| 72 | + // test |
| 73 | + assertEquals(chart.getTitle(), imp.getTitle()); |
| 74 | + assertEquals(chart.getPreferredWidth(), imp.getWidth()); |
| 75 | + assertEquals(chart.getPreferredHeight(), imp.getHeight()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testConversionToImg() { |
| 80 | + // setup |
| 81 | + CategoryChart chart = plotService.newCategoryChart(String.class); |
| 82 | + chart.setTitle("Hello World!"); |
| 83 | + chart.setPreferredSize(12,23); |
| 84 | + // process |
| 85 | + Img<ARGBType> img = convertService.convert(chart, Img.class); |
| 86 | + // test |
| 87 | + assertEquals(chart.getPreferredWidth(), img.max(0) + 1); |
| 88 | + assertEquals(chart.getPreferredHeight(), img.max(1) + 1); |
| 89 | + } |
| 90 | + |
| 91 | + // -- Helper class -- |
| 92 | + |
| 93 | + class CustomAbstractPlot implements AbstractPlot { |
| 94 | + @Override |
| 95 | + public void setTitle(String title) { |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String getTitle() { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void setPreferredSize(int width, int height) { |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public int getPreferredWidth() { |
| 111 | + return 0; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public int getPreferredHeight() { |
| 116 | + return 0; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments