fd27a34c9bd92b0d0a389ef423ab131e91eaf53a
[jalview.git] / test / jalview / bin / HiDPISettingTest1.java
1 package jalview.bin;
2
3 import static org.testng.Assert.assertEquals;
4
5 import org.mockito.Mockito;
6 import org.testng.annotations.AfterClass;
7 import org.testng.annotations.BeforeMethod;
8 import org.testng.annotations.Test;
9
10 import jalview.gui.AlignFrame;
11 import jalview.gui.Desktop;
12 import jalview.gui.JvOptionPane;
13 import jalview.io.DataSourceType;
14 import jalview.io.FileLoader;
15
16 /*
17  * Testing a HiDPI display is difficult without running in a HiDPI display.
18  * The gathering of screen size and resolution performed by jalview.bin.HiDPISetting
19  * is now farmed out into a separate class jalview.bin.ScreenInfo so it can be more
20  * easily Mocked in tests.
21  * Two sets of tests are performed.
22  * 1) testLinuxScalePropertyToActualTransform() sets the property that HiDPISetting
23  * uses (via jalview.bin.Launcher or getdown) to scale up Jalview, and then looks at
24  * the alignment panel graphics2d transform to see if it's been scaled by the same
25  * amount (in this case 4 -- unlikely to happen by accident!).
26  * 2) testHiDPISettingInit() which tests the calculation that HiDPISetting uses to
27  * decide from apparent screen information (mocked using Mockito in the tests) what
28  * scaling factor to set (it doesn't actually set it, just suggests it to
29  * jalview.bin.Launcher)
30  */
31 public class HiDPISettingTest1
32 {
33
34   AlignFrame af = null;
35
36   @BeforeMethod(alwaysRun = true)
37   public void setUp()
38   {
39     JvOptionPane.setInteractiveMode(false);
40     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
41     Cache.loadProperties("test/jalview/bin/hidpiTestProps.jvprops");
42     Jalview.main(
43             new String[]
44             { "-nosplash", "-nonews", "-noquestionnaire",
45                 "-nowebservicediscovery" });
46
47     af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
48             DataSourceType.FILE);
49
50     /*
51      * wait for Consensus thread to complete
52      */
53     do
54     {
55       try
56       {
57         Thread.sleep(50);
58       } catch (InterruptedException x)
59       {
60       }
61     } while (af.getViewport().getCalcManager().isWorking());
62   }
63
64   @AfterClass(alwaysRun = true)
65   public void tearDown()
66   {
67     Desktop.instance.closeAll_actionPerformed(null);
68   }
69
70   @Test(groups = { "Functional" })
71   public void testHiDPISettingInit()
72   {
73     String scalePropertyName = "sun.java2d.uiScale";
74     // ensure scale property is cleared (otherwise it will be re-used)
75     System.clearProperty(HiDPISetting.scalePropertyName);
76
77     { // keep the mock under lock
78       // Ancient monitor -- no property change set
79       setMockScreen(1024, 768, 72);
80       assertEquals(HiDPISetting.getScalePropertyArg(), null);
81
82       // Old monitor -- no property change set
83       setMockScreen(1200, 800, 96);
84       assertEquals(HiDPISetting.getScalePropertyArg(), null);
85
86       // HD screen -- no property change set
87       setMockScreen(1920, 1080, 96);
88       assertEquals(HiDPISetting.getScalePropertyArg(), null);
89
90       // 4K screen -- scale by 2
91       setMockScreen(3180, 2160, 80);
92       assertEquals(HiDPISetting.getScalePropertyArg(),
93               "-D" + scalePropertyName + "=2");
94
95       // 4K screen with high dpi -- scale by 3
96       setMockScreen(3180, 2160, 450);
97       assertEquals(HiDPISetting.getScalePropertyArg(),
98               "-D" + scalePropertyName + "=3");
99
100       // stupidly big screen -- scale by 8
101       setMockScreen(19200, 10800, 72);
102       assertEquals(HiDPISetting.getScalePropertyArg(),
103               "-D" + scalePropertyName + "=8");
104     }
105   }
106
107   private void setMockScreen(int width, int height, int dpi)
108   {
109     HiDPISetting.clear();
110     ScreenInfo mockScreenInfo = Mockito.spy(HiDPISetting.getScreenInfo());
111     Mockito.doReturn(height).when(mockScreenInfo).getScreenHeight();
112     Mockito.doReturn(width).when(mockScreenInfo).getScreenWidth();
113     Mockito.doReturn(dpi).when(mockScreenInfo).getScreenResolution();
114     HiDPISetting.setScreenInfo(mockScreenInfo);
115   }
116 }