|
3 | 3 | import com.codename1.impl.CodenameOneImplementation; |
4 | 4 | import com.codename1.io.Util; |
5 | 5 | import com.codename1.plugin.PluginSupport; |
| 6 | +import com.codename1.testing.TestCodenameOneImplementation; |
6 | 7 | import com.codename1.ui.Display; |
7 | 8 | import com.codename1.ui.Graphics; |
8 | 9 | import com.codename1.ui.plaf.UIManager; |
@@ -38,41 +39,8 @@ protected void setUpDisplay() throws Exception { |
38 | 39 | display = Display.getInstance(); |
39 | 40 | resetUIManager(); |
40 | 41 |
|
41 | | - implementation = mock(CodenameOneImplementation.class); |
42 | | - final Object defaultFont = new Object(); |
43 | | - when(implementation.getDisplayWidth()).thenReturn(1080); |
44 | | - when(implementation.getDisplayHeight()).thenReturn(1920); |
45 | | - when(implementation.getActualDisplayHeight()).thenReturn(1920); |
46 | | - when(implementation.getDeviceDensity()).thenReturn(Display.DENSITY_MEDIUM); |
47 | | - when(implementation.convertToPixels(anyInt(), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0)); |
48 | | - when(implementation.createFont(anyInt(), anyInt(), anyInt())).thenReturn(defaultFont); |
49 | | - when(implementation.getDefaultFont()).thenReturn(defaultFont); |
50 | | - when(implementation.isTrueTypeSupported()).thenReturn(true); |
51 | | - when(implementation.isLookupFontSupported()).thenReturn(true); |
52 | | - when(implementation.isInitialized()).thenReturn(true); |
53 | | - when(implementation.getCommandBehavior()).thenReturn(Display.COMMAND_BEHAVIOR_DEFAULT); |
54 | | - when(implementation.isNativeFontSchemeSupported()).thenReturn(true); |
55 | | - when(implementation.loadTrueTypeFont(anyString(), anyString())).thenReturn(defaultFont); |
56 | | - when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenReturn(defaultFont); |
57 | | - when(implementation.loadNativeFont(anyString())).thenReturn(defaultFont); |
58 | | - when(implementation.getNativeGraphics()).thenReturn(new Object()); |
59 | | - when(implementation.paintNativePeersBehind()).thenReturn(false); |
60 | | - when(implementation.handleEDTException(any(Throwable.class))).thenReturn(false); |
61 | | - when(implementation.charWidth(any(), anyChar())).thenReturn(8); |
62 | | - when(implementation.stringWidth(any(), anyString())).thenAnswer(invocation -> { |
63 | | - String text = (String) invocation.getArgument(1); |
64 | | - return text == null ? 0 : text.length() * 8; |
65 | | - }); |
66 | | - when(implementation.charsWidth(any(), any(char[].class), anyInt(), anyInt())).thenAnswer(invocation -> { |
67 | | - Integer length = (Integer) invocation.getArgument(3); |
68 | | - return length == null ? 0 : Math.max(0, length) * 8; |
69 | | - }); |
70 | | - when(implementation.getHeight(any())).thenReturn(16); |
71 | | - when(implementation.getPlatformName()).thenReturn("and"); |
72 | | - when(implementation.getProperty(anyString(), anyString())).thenAnswer(invocation -> (String) invocation.getArgument(1)); |
73 | | - when(implementation.loadTrueTypeFont(anyString(), anyString())).thenAnswer(invocation -> new Object()); |
74 | | - when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenAnswer(invocation -> new Object()); |
75 | | - when(implementation.loadNativeFont(anyString())).thenAnswer(invocation -> new Object()); |
| 42 | + implementation = createImplementation(); |
| 43 | + configureImplementation(implementation); |
76 | 44 |
|
77 | 45 | pluginSupport = new PluginSupport(); |
78 | 46 |
|
@@ -113,15 +81,75 @@ private void resetUIManager() throws Exception { |
113 | 81 | } |
114 | 82 |
|
115 | 83 | private Graphics createGraphics() throws Exception { |
| 84 | + Object nativeGraphics = implementation != null ? implementation.getNativeGraphics() : null; |
| 85 | + if (nativeGraphics == null) { |
| 86 | + nativeGraphics = new Object(); |
| 87 | + } |
116 | 88 | java.lang.reflect.Constructor<Graphics> constructor = Graphics.class.getDeclaredConstructor(Object.class); |
117 | 89 | constructor.setAccessible(true); |
118 | | - Graphics graphics = constructor.newInstance(new Object()); |
| 90 | + Graphics graphics = constructor.newInstance(nativeGraphics); |
119 | 91 | Field paintPeersField = Graphics.class.getDeclaredField("paintPeersBehind"); |
120 | 92 | paintPeersField.setAccessible(true); |
121 | 93 | paintPeersField.setBoolean(graphics, false); |
122 | 94 | return graphics; |
123 | 95 | } |
124 | 96 |
|
| 97 | + protected CodenameOneImplementation createImplementation() { |
| 98 | + return mock(CodenameOneImplementation.class); |
| 99 | + } |
| 100 | + |
| 101 | + protected void configureImplementation(CodenameOneImplementation implementation) { |
| 102 | + if (implementation instanceof TestCodenameOneImplementation) { |
| 103 | + configureTestImplementation((TestCodenameOneImplementation) implementation); |
| 104 | + } else { |
| 105 | + configureMockImplementation(implementation); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private void configureMockImplementation(CodenameOneImplementation implementation) { |
| 110 | + final Object defaultFont = new Object(); |
| 111 | + when(implementation.getDisplayWidth()).thenReturn(1080); |
| 112 | + when(implementation.getDisplayHeight()).thenReturn(1920); |
| 113 | + when(implementation.getActualDisplayHeight()).thenReturn(1920); |
| 114 | + when(implementation.getDeviceDensity()).thenReturn(Display.DENSITY_MEDIUM); |
| 115 | + when(implementation.convertToPixels(anyInt(), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0)); |
| 116 | + when(implementation.createFont(anyInt(), anyInt(), anyInt())).thenReturn(defaultFont); |
| 117 | + when(implementation.getDefaultFont()).thenReturn(defaultFont); |
| 118 | + when(implementation.isTrueTypeSupported()).thenReturn(true); |
| 119 | + when(implementation.isLookupFontSupported()).thenReturn(true); |
| 120 | + when(implementation.isInitialized()).thenReturn(true); |
| 121 | + when(implementation.getCommandBehavior()).thenReturn(Display.COMMAND_BEHAVIOR_DEFAULT); |
| 122 | + when(implementation.isNativeFontSchemeSupported()).thenReturn(true); |
| 123 | + when(implementation.loadTrueTypeFont(anyString(), anyString())).thenReturn(defaultFont); |
| 124 | + when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenReturn(defaultFont); |
| 125 | + when(implementation.loadNativeFont(anyString())).thenReturn(defaultFont); |
| 126 | + when(implementation.getNativeGraphics()).thenReturn(new Object()); |
| 127 | + when(implementation.paintNativePeersBehind()).thenReturn(false); |
| 128 | + when(implementation.handleEDTException(any(Throwable.class))).thenReturn(false); |
| 129 | + when(implementation.charWidth(any(), anyChar())).thenReturn(8); |
| 130 | + when(implementation.stringWidth(any(), anyString())).thenAnswer(invocation -> { |
| 131 | + String text = (String) invocation.getArgument(1); |
| 132 | + return text == null ? 0 : text.length() * 8; |
| 133 | + }); |
| 134 | + when(implementation.charsWidth(any(), any(char[].class), anyInt(), anyInt())).thenAnswer(invocation -> { |
| 135 | + Integer length = (Integer) invocation.getArgument(3); |
| 136 | + return length == null ? 0 : Math.max(0, length) * 8; |
| 137 | + }); |
| 138 | + when(implementation.getHeight(any())).thenReturn(16); |
| 139 | + when(implementation.getPlatformName()).thenReturn("and"); |
| 140 | + when(implementation.getProperty(anyString(), anyString())).thenAnswer(invocation -> (String) invocation.getArgument(1)); |
| 141 | + when(implementation.loadTrueTypeFont(anyString(), anyString())).thenAnswer(invocation -> new Object()); |
| 142 | + when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenAnswer(invocation -> new Object()); |
| 143 | + when(implementation.loadNativeFont(anyString())).thenAnswer(invocation -> new Object()); |
| 144 | + } |
| 145 | + |
| 146 | + protected void configureTestImplementation(TestCodenameOneImplementation implementation) { |
| 147 | + implementation.setDisplaySize(1080, 1920); |
| 148 | + implementation.setDeviceDensity(Display.DENSITY_MEDIUM); |
| 149 | + implementation.setTouchDevice(true); |
| 150 | + implementation.setTimeoutSupported(true); |
| 151 | + } |
| 152 | + |
125 | 153 | /** |
126 | 154 | * Processes any pending serial calls that were queued via {@link Display#callSerially(Runnable)}. |
127 | 155 | */ |
|
0 commit comments