Clover icon

Coverage Report

  1. Project Clover database Wed Nov 19 2025 15:00:14 GMT
  2. Package jalview.gui

File ImageExporterTest.java

 
testExportSVG: Exported file size (0) is not as big as it should be (>12...
 

Code metrics

10
49
7
1
215
151
18
0.37
7
7
2.57

Classes

Class Line # Actions
ImageExporterTest 43 49 18
0.1818181918.2%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.gui;
22   
23    import java.awt.Font;
24    import java.io.File;
25   
26    import javax.swing.SwingUtilities;
27   
28    import org.testng.Assert;
29    import org.testng.annotations.AfterMethod;
30    import org.testng.annotations.BeforeClass;
31    import org.testng.annotations.Test;
32   
33    import jalview.bin.Console;
34    import jalview.bin.Jalview;
35    import jalview.datamodel.AlignmentI;
36    import jalview.io.DataSourceType;
37    import jalview.io.FileLoader;
38    import jalview.io.exceptions.ImageOutputException;
39    import jalview.schemes.ColourSchemeI;
40    import jalview.schemes.ColourSchemeProperty;
41    import jalview.structure.StructureSelectionManager;
42   
 
43    public class ImageExporterTest
44    {
45   
46    String exportFile = "test/files/exported.svg";
47   
 
48  1 toggle @BeforeClass(alwaysRun = true)
49    public void setUpJvOptionPane()
50    {
51  1 JvOptionPane.setInteractiveMode(false);
52  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
53    }
54   
55    AlignmentI al;
56   
57    AlignViewport testee;
58   
 
59  1 toggle @BeforeClass(alwaysRun = true)
60    public static void setUpBeforeClass() throws Exception
61    {
62  1 Jalview.main(
63    new String[]
64    { "--nonews", "--nosplash", "--props",
65    "test/jalview/testProps.jvprops" });
66   
67    /*
68    * remove any sequence mappings left lying around by other tests
69    */
70  1 StructureSelectionManager ssm = StructureSelectionManager
71    .getStructureSelectionManager(Desktop.instance);
72  1 ssm.resetAll();
73    }
74   
 
75  1 toggle @AfterMethod(alwaysRun = true)
76    public void tearDown()
77    {
78  1 File exportedFile = new File(exportFile);
79  1 if (exportedFile.exists())
80    {
81  1 exportedFile.delete();
82    }
83    }
84   
 
85  0 toggle @Test(groups = { "Functional" }, timeOut = 30000)
86    public void testExportSVG()
87    {
88    /*
89    * Open a large-ish alignment, increase the font-size, add some colour, set it to wrap mode.
90    */
91  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
92    "examples/PF03760.fa", DataSourceType.FILE);
93  0 ColourSchemeI cs = ColourSchemeProperty.getColourScheme(
94    af.getViewport(), af.getViewport().getAlignment(),
95    "gecos-blossom");
96  0 Font f = af.alignPanel.av.getFont();
97  0 af.alignPanel.av.setFont(f.deriveFont(24f), true);
98   
99  0 try
100    {
101  0 SwingUtilities.invokeAndWait(new Runnable()
102    {
 
103  0 toggle @Override
104    public void run()
105    {
106    // Note that cs == null removes colour scheme from af
107  0 af.changeColour(cs);
108    }
109    });
110    } catch (Exception x)
111    {
112  0 Console.trace("Interrupted whilst waiting for colorAlignFrame action",
113    x);
114   
115    }
116  0 af.setWrapFormat(true, true);
117   
118    // wait for Conservation thread to complete
119  0 waitForCalculations(af.getViewport());
120   
121    /*
122    * Now export as SVG with lineart text and immediately update the AlignFrame.
123    * This used to cause the wrapped output to terminate too soon resulting in a too small export file.
124    * This test is to ensure it is fixed.
125    */
126  0 String filename = exportFile;
127  0 File output = new File(filename);
128  0 Thread exportThread = new Thread(new Runnable()
129    {
 
130  0 toggle @Override
131    public void run()
132    {
133  0 try
134    {
135  0 af.createSVG(output, "lineart");
136    } catch (ImageOutputException e)
137    {
138  0 Assert.fail("An exception occurred when exporting SVG file to "
139    + filename);
140    }
141    }
142    });
143   
144    // start the export
145  0 exportThread.start();
146   
147    // wait a smidge for export to start, then keep prodding the alignment
148  0 final int maxSecondsProdding = 10; // in s
149  0 final int proddingInterval = 20; // in ms
150  0 SeqCanvas sc = af.alignPanel.getSeqPanel().seqCanvas;
151  0 for (int i = 0; i < maxSecondsProdding * 1000 / proddingInterval; i++)
152    {
153  0 if (!exportThread.isAlive())
154    {
155  0 break;
156    }
157  0 try
158    {
159  0 Thread.sleep(proddingInterval);
160    } catch (InterruptedException e)
161    {
162  0 e.printStackTrace();
163    }
164   
165    // provoke a recalculation of the alignFrame wrappedWidths
166    // sc.fastPaintWrapped(0);
167  0 sc.calculateWrappedGeometry(sc.getWidth(), sc.getHeight());
168    }
169   
170    // then allow the svg to finish exporting
171  0 while (exportThread.isAlive())
172    {
173  0 try
174    {
175  0 Thread.sleep(500);
176    } catch (InterruptedException e)
177    {
178  0 e.printStackTrace();
179    }
180    }
181   
182    // brief pause to finish writing
183  0 try
184    {
185  0 Thread.sleep(2000);
186    } catch (InterruptedException e)
187    {
188  0 e.printStackTrace();
189    }
190   
191  0 long fileSize = output.length();
192  0 long expectedMinSize = 120000 * 1024;
193  0 Test failure here Assert.assertTrue(fileSize > expectedMinSize,
194    "Exported file size (" + fileSize
195    + ") is not as big as it should be (>" + expectedMinSize
196    + ")");
197    }
198   
 
199  0 toggle private void waitForCalculations(AlignViewport viewport)
200    {
201  0 synchronized (this)
202    {
203  0 do
204    {
205  0 try
206    {
207  0 wait(50);
208    } catch (InterruptedException e)
209    {
210    }
211  0 } while (viewport.getCalcManager().isWorking());
212    }
213    }
214   
215    }