|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -/* |
25 | | - * @test |
26 | | - * @bug 8288882 |
27 | | - * @library /java/awt/regtesthelpers |
28 | | - * @build PassFailJFrame |
29 | | - * @requires (os.family == "linux") |
30 | | - * @summary To test if the 1-Empty-File size shows 0.0 KB and other files show correct size. |
31 | | - * @run main/manual FileSizeCheck |
32 | | - */ |
33 | | - |
| 24 | +import java.awt.AWTException; |
| 25 | +import java.awt.Component; |
| 26 | +import java.awt.Container; |
| 27 | +import java.awt.Robot; |
34 | 28 | import java.io.IOException; |
35 | 29 | import java.io.RandomAccessFile; |
36 | 30 | import java.lang.reflect.InvocationTargetException; |
37 | 31 | import java.nio.file.Files; |
38 | 32 | import java.nio.file.Path; |
39 | 33 | import java.nio.file.Paths; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.Locale; |
| 36 | +import java.util.concurrent.atomic.AtomicReference; |
| 37 | +import java.util.function.Predicate; |
40 | 38 |
|
| 39 | +import javax.swing.AbstractButton; |
41 | 40 | import javax.swing.JFileChooser; |
42 | 41 | import javax.swing.JFrame; |
| 42 | +import javax.swing.JLabel; |
| 43 | +import javax.swing.JTable; |
| 44 | +import javax.swing.JToggleButton; |
43 | 45 | import javax.swing.SwingUtilities; |
44 | 46 | import javax.swing.WindowConstants; |
45 | 47 |
|
| 48 | +/* |
| 49 | + * @test |
| 50 | + * @bug 8288882 |
| 51 | + * @key headful |
| 52 | + * @requires (os.family == "linux") |
| 53 | + * @summary Verifies if the size of an empty file is shown as 0.0 KB |
| 54 | + * as well as checks the displayed file sizes are rounded up |
| 55 | + * @run main FileSizeCheck |
| 56 | + */ |
46 | 57 | public class FileSizeCheck { |
47 | | - private static Path[] tempFilePaths; |
48 | | - private static final String INSTRUCTIONS = |
49 | | - "Click on the \"Details\" button in right-top corner.\n\n" + |
50 | | - "Scroll Down if required. \n\n" + |
51 | | - "Test 1: If the size of 1st-Empty-File shows 0.0 KB\n" + |
52 | | - "Test 2: If the size of 2nd-File-1-Byte shows 0.1 KB\n" + |
53 | | - "Test 3: If the size of 3rd-File-160-Byte shows 0.2 KB\n" + |
54 | | - "Test 3: If the size of 4th-File-299-Byte shows 0.3 KB\n" + |
55 | | - "Test 4: If the size of 5th-File-900-Byte shows 0.9 KB\n" + |
56 | | - "Test 6: If the size of 6th-File-901-Byte shows 1.0 KB\n" + |
57 | | - "Test 7: If the size of 7th-File-999-KB shows 999.0 KB\n" + |
58 | | - "Test 8: If the size of 8th-File-1000-KB shows 1.0 MB\n" + |
59 | | - "Test 9: If the size of 9th-File-2.8-MB shows 2.8 MB\n\n" + |
60 | | - "press PASS.\n\n"; |
61 | | - |
62 | | - public static void test() { |
63 | | - JFrame frame = new JFrame("JFileChooser File Size test"); |
64 | | - JFileChooser fc = new JFileChooser(); |
65 | | - fc.setControlButtonsAreShown(false); |
66 | | - Path dir = Paths.get("."); |
67 | | - String[] tempFilesName = {"1st-Empty-File", "2nd-File-1-Byte", "3rd-File-160-Byte", |
68 | | - "4th-File-299-Byte", "5th-File-900-Byte", "6th-File-901-Byte", |
69 | | - "7th-File-999-KB", "8th-File-1000-KB", "9th-File-2.8-MB"}; |
| 58 | + private enum FileSize { |
| 59 | + F0( 0, "0.0 KB"), |
| 60 | + F1( 1, "0.1 KB"), |
70 | 61 |
|
71 | | - int[] tempFilesSize = {0, 1, 160, 299, 900, 901, 999_000, 1_000_000, 2_800_000}; |
| 62 | + F99( 99, "0.1 KB"), |
| 63 | + F100(100, "0.1 KB"), |
| 64 | + F101(101, "0.2 KB"), |
| 65 | + F149(149, "0.2 KB"), |
| 66 | + F150(150, "0.2 KB"), |
| 67 | + F151(151, "0.2 KB"), |
| 68 | + F900(900, "0.9 KB"), |
| 69 | + F901(901, "1.0 KB"), |
72 | 70 |
|
73 | | - tempFilePaths = new Path[tempFilesName.length]; |
74 | | - PassFailJFrame.addTestWindow(frame); |
75 | | - PassFailJFrame.positionTestWindow(frame, PassFailJFrame.Position.HORIZONTAL); |
| 71 | + F999_000(999_000, "999.0 KB"), |
| 72 | + F999_001(999_001, "999.1 KB"), |
| 73 | + F999_900(999_900, "999.9 KB"), |
| 74 | + F999_901(999_901, "1.0 MB"), |
76 | 75 |
|
77 | | - // Create temp files |
78 | | - try { |
79 | | - for (int i = 0; i < tempFilePaths.length; i++) { |
80 | | - tempFilePaths[i] = dir.resolve(tempFilesName[i]); |
81 | | - if (!Files.exists(tempFilePaths[i])){ |
82 | | - RandomAccessFile f = new RandomAccessFile(tempFilePaths[i].toFile(), "rw"); |
83 | | - f.setLength(tempFilesSize[i]); |
84 | | - f.close(); |
| 76 | + F1_000_000(1_000_000, "1.0 MB"), |
| 77 | + F1_000_001(1_000_001, "1.1 MB"), |
| 78 | + F1_000_900(1_000_900, "1.1 MB"), |
| 79 | + F1_001_000(1_001_000, "1.1 MB"), |
| 80 | + F1_100_000(1_100_000, "1.1 MB"), |
| 81 | + F1_100_001(1_100_001, "1.2 MB"), |
| 82 | + |
| 83 | + F2_800_000(2_800_000, "2.8 MB"), |
| 84 | + |
| 85 | +// F1_000_000_000(1_000_000_000, "1.0 GB"), |
| 86 | +// F1_000_000_001(1_000_000_001, "1.1 GB"), |
| 87 | + ; |
| 88 | + |
| 89 | + public final String name; |
| 90 | + public final long size; |
| 91 | + public final String renderedSize; |
| 92 | + |
| 93 | + private Path path; |
| 94 | + |
| 95 | + FileSize(long size, String renderedSize) { |
| 96 | + this.name = String.format("%03d-%010d.test", ordinal(), size); |
| 97 | + this.size = size; |
| 98 | + this.renderedSize = renderedSize; |
| 99 | + } |
| 100 | + |
| 101 | + public void create(final Path parent) { |
| 102 | + path = parent.resolve(name); |
| 103 | + if (!Files.exists(path)) { |
| 104 | + try (var f = new RandomAccessFile(path.toFile(), "rw")) { |
| 105 | + f.setLength(size); |
| 106 | + } catch (IOException e) { |
| 107 | + throw new RuntimeException(e); |
85 | 108 | } |
86 | 109 | } |
87 | | - fc.setCurrentDirectory(dir.toFile()); |
88 | | - } catch (IOException ex) { |
89 | | - throw new RuntimeException(ex); |
90 | 110 | } |
| 111 | + |
| 112 | + public void delete() { |
| 113 | + try { |
| 114 | + Files.deleteIfExists(path); |
| 115 | + } catch (IOException e) { |
| 116 | + e.printStackTrace(); |
| 117 | + // Don't propagate |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static JFrame frame; |
| 123 | + private static JFileChooser fc; |
| 124 | + |
| 125 | + private static final AtomicReference<String> error = new AtomicReference<>(); |
| 126 | + |
| 127 | + private static void createUI() { |
| 128 | + // Create temp files |
| 129 | + Path dir = Paths.get("."); |
| 130 | + Arrays.stream(FileSize.values()) |
| 131 | + .forEach(f -> f.create(dir)); |
| 132 | + |
| 133 | + fc = new JFileChooser(); |
| 134 | + fc.setControlButtonsAreShown(false); |
| 135 | + fc.setCurrentDirectory(dir.toFile()); |
| 136 | + |
| 137 | + frame = new JFrame("JFileChooser File Size test"); |
| 138 | + frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
91 | 139 | frame.add(fc); |
92 | 140 | frame.pack(); |
| 141 | + frame.setLocationRelativeTo(null); |
93 | 142 | frame.setVisible(true); |
94 | | - frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| 143 | + } |
| 144 | + |
| 145 | + private static void disposeUI() { |
| 146 | + if (frame != null) { |
| 147 | + frame.dispose(); |
| 148 | + } |
95 | 149 | } |
96 | 150 |
|
97 | 151 | public static void main(String[] args) throws InterruptedException, |
98 | | - InvocationTargetException { |
99 | | - PassFailJFrame passFailJFrame = new PassFailJFrame("JFileChooser Test Instructions", |
100 | | - INSTRUCTIONS, 5, 19, 35); |
| 152 | + InvocationTargetException, AWTException { |
| 153 | + Locale.setDefault(Locale.US); |
101 | 154 | try { |
102 | | - SwingUtilities.invokeAndWait(FileSizeCheck::test); |
103 | | - passFailJFrame.awaitAndCheck(); |
| 155 | + final Robot robot = new Robot(); |
| 156 | + SwingUtilities.invokeAndWait(FileSizeCheck::createUI); |
| 157 | + |
| 158 | + robot.waitForIdle(); |
| 159 | + robot.delay(500); |
| 160 | + SwingUtilities.invokeAndWait(FileSizeCheck::clickDetails); |
| 161 | + |
| 162 | + robot.waitForIdle(); |
| 163 | + robot.delay(500); |
| 164 | + SwingUtilities.invokeAndWait(FileSizeCheck::checkFileSizes); |
| 165 | + |
| 166 | + if (error.get() != null) { |
| 167 | + throw new Error(error.get()); |
| 168 | + } |
104 | 169 | } finally { |
105 | | - try { |
106 | | - for (int i = 0; i < tempFilePaths.length; ++i) { |
107 | | - Files.deleteIfExists(tempFilePaths[i]); |
| 170 | + Arrays.stream(FileSize.values()) |
| 171 | + .forEach(FileSize::delete); |
| 172 | + |
| 173 | + SwingUtilities.invokeAndWait(FileSizeCheck::disposeUI); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private static void checkFileSizes() { |
| 178 | + final JTable table = findTable(fc); |
| 179 | + if (table == null) { |
| 180 | + throw new Error("Didn't find JTable in JFileChooser"); |
| 181 | + } |
| 182 | + |
| 183 | + String firstError = null; |
| 184 | + int row = findFirstFileRow(table); |
| 185 | + for (FileSize f : FileSize.values()) { |
| 186 | + String fcSize = getCellRenderedText(table, row++, 1); |
| 187 | + if (!f.renderedSize.equals(fcSize)) { |
| 188 | + String errMsg = "Wrong rendered size for " + f + ": " |
| 189 | + + fcSize + " vs. " + f.renderedSize; |
| 190 | + if (firstError == null) { |
| 191 | + firstError = errMsg; |
108 | 192 | } |
109 | | - } catch (IOException ex) { |
110 | | - ex.printStackTrace(); |
| 193 | + System.err.println(errMsg); |
111 | 194 | } |
112 | 195 | } |
| 196 | + if (firstError != null) { |
| 197 | + error.set(firstError); |
| 198 | + } |
113 | 199 | } |
114 | | -} |
115 | 200 |
|
| 201 | + private static int findFirstFileRow(final JTable table) { |
| 202 | + for (int i = 0; i < table.getRowCount(); i++) { |
| 203 | + if (FileSize.F0.name.equals(getCellRenderedText(table, i, 0))) { |
| 204 | + return i; |
| 205 | + } |
| 206 | + } |
| 207 | + throw new Error("Didn't find the first file name in the table"); |
| 208 | + } |
| 209 | + |
| 210 | + private static String getCellRenderedText(final JTable table, |
| 211 | + final int row, |
| 212 | + final int column) { |
| 213 | + Component renderer = |
| 214 | + table.getCellRenderer(row, column) |
| 215 | + .getTableCellRendererComponent(table, |
| 216 | + table.getValueAt(row, column), |
| 217 | + false, false, |
| 218 | + row, column); |
| 219 | + return ((JLabel) renderer).getText(); |
| 220 | + } |
| 221 | + |
| 222 | + private static void clickDetails() { |
| 223 | + AbstractButton details = findDetailsButton(fc); |
| 224 | + if (details == null) { |
| 225 | + throw new Error("Didn't find 'Details' button in JFileChooser"); |
| 226 | + } |
| 227 | + details.doClick(); |
| 228 | + } |
| 229 | + |
| 230 | + private static AbstractButton findDetailsButton(final Container container) { |
| 231 | + Component result = findComponent(container, |
| 232 | + c -> c instanceof JToggleButton button |
| 233 | + && "Details".equals(button.getToolTipText())); |
| 234 | + return (AbstractButton) result; |
| 235 | + } |
| 236 | + |
| 237 | + private static JTable findTable(final Container container) { |
| 238 | + Component result = findComponent(container, |
| 239 | + c -> c instanceof JTable); |
| 240 | + return (JTable) result; |
| 241 | + } |
| 242 | + |
| 243 | + private static Component findComponent(final Container container, |
| 244 | + final Predicate<Component> predicate) { |
| 245 | + for (Component child : container.getComponents()) { |
| 246 | + if (predicate.test(child)) { |
| 247 | + return child; |
| 248 | + } |
| 249 | + if (child instanceof Container cont && cont.getComponentCount() > 0) { |
| 250 | + Component result = findComponent(cont, predicate); |
| 251 | + if (result != null) { |
| 252 | + return result; |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + return null; |
| 257 | + } |
| 258 | +} |
0 commit comments