9851b09c694aaf8fa7c5ce0d18b7f615ce0fe4ae
[jalview.git] / test / jalview / bin / HiDPISettingTest2.java
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.bin;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertNotNull;
25
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.util.concurrent.TimeUnit;
31
32 import org.testng.Assert;
33 import org.testng.annotations.BeforeTest;
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36
37 import io.github.classgraph.ClassGraph;
38 import io.github.classgraph.ScanResult;
39 import jalview.gui.Desktop;
40
41 public class HiDPISettingTest2
42 {
43   private static final int TIMEOUT = 10;
44
45   private static class Worker extends Thread
46   {
47     private final Process process;
48
49     private BufferedReader outputReader;
50
51     private BufferedReader errorReader;
52
53     private boolean exited;
54
55     private Worker(Process process)
56     {
57       this.process = process;
58     }
59
60     @Override
61     public void run()
62     {
63       try
64       {
65         exited = process.waitFor(TIMEOUT, TimeUnit.SECONDS);
66       } catch (InterruptedException ignore)
67       {
68         return;
69       }
70       this.interrupt();
71       this.process.destroy();
72     }
73
74     public BufferedReader getOutputReader()
75     {
76       return outputReader;
77     }
78
79     public void setOutputReader(BufferedReader outputReader)
80     {
81       this.outputReader = outputReader;
82     }
83
84     public BufferedReader getErrorReader()
85     {
86       return errorReader;
87     }
88
89     public void setErrorReader(BufferedReader errorReader)
90     {
91       this.errorReader = errorReader;
92     }
93   }
94
95   private static ClassGraph scanner = null;
96
97   private static String classpath = null;
98
99   private static String java_exe = null;
100
101   public synchronized static String getClassPath()
102   {
103     if (scanner == null)
104     {
105       scanner = new ClassGraph();
106       ScanResult scan = scanner.scan();
107       classpath = scan.getClasspath();
108       java_exe = System.getProperty("java.home") + File.separator + "bin"
109               + File.separator + "java";
110
111     }
112     while (classpath == null)
113     {
114       try
115       {
116         Thread.sleep(10);
117       } catch (InterruptedException x)
118       {
119
120       }
121     }
122     return classpath;
123   }
124
125   private Worker getJalviewDesktopRunner(String jvmArgs, String appArgs)
126   {
127     String classpath = getClassPath();
128     String cmd = java_exe + " " + " -classpath " + classpath + " " + jvmArgs
129             + " jalview.bin.Jalview " + " "
130             + "-props test/jalview/bin/hidpiTestProps.jvprops " + appArgs;
131     Process proc = null;
132     Worker worker = null;
133     try
134     {
135       proc = Runtime.getRuntime().exec(cmd);
136     } catch (Throwable e)
137     {
138       e.printStackTrace();
139     }
140     if (proc != null)
141     {
142       BufferedReader outputReader = new BufferedReader(
143               new InputStreamReader(proc.getInputStream()));
144       BufferedReader errorReader = new BufferedReader(
145               new InputStreamReader(proc.getErrorStream()));
146       worker = new Worker(proc);
147       worker.start();
148       worker.setOutputReader(outputReader);
149       worker.setErrorReader(errorReader);
150     }
151     return worker;
152   }
153
154   @BeforeTest(alwaysRun = true)
155   public void initialize()
156   {
157     new HiDPISettingTest2();
158   }
159
160   @Test(groups = { "Functional" }, dataProvider = "hidpiScaleArguments")
161   public void testHiDPISettings(int scale)
162   {
163     String jvmArgs = HiDPISetting.getScalePropertyArg(scale);
164
165     String appArgs = " -open examples/uniref50.fa -nosplash -nonews -noquestionnaire -nousagestats -nowebservicediscovery";
166
167     Worker worker = getJalviewDesktopRunner(jvmArgs, appArgs);
168     assertNotNull(worker, "worker is null");
169
170     String ln = null;
171     int count = 0;
172     boolean scaleFound = false;
173     try
174     {
175       while ((ln = worker.getErrorReader().readLine()) != null)
176       {
177         if (++count > 100)
178         {
179           break;
180         }
181         if (ln.contains(Desktop.debugScaleMessage))
182         {
183           String number = ln.substring(ln.indexOf(Desktop.debugScaleMessage)
184                   + Desktop.debugScaleMessage.length());
185           number = number.substring(0, number.indexOf(' '));
186           try
187           {
188             double d = Double.valueOf(number);
189
190             assertEquals(d, scale * 1.0);
191             scaleFound = true;
192           } catch (NumberFormatException e)
193           {
194             e.printStackTrace();
195             Assert.fail(
196                     "Debug scale message line '" + ln + "' gives number '"
197                             + number + "' which could not be parsed");
198           }
199           break;
200         }
201       }
202     } catch (IOException e)
203     {
204       e.printStackTrace();
205     }
206     if (worker != null && worker.exited == false)
207     {
208       worker.interrupt();
209       worker.process.destroy();
210     }
211     if (!scaleFound)
212     {
213       Assert.fail("Did not find Debug scale message line '"
214               + Desktop.debugScaleMessage + "'");
215     }
216   }
217
218   @DataProvider(name = "hidpiScaleArguments")
219   public static Object[][] getHiDPIScaleArguments()
220   {
221     return new Object[][] { { 1 }, { 2 }, { 4 }, { 10 } };
222   }
223 }