Repository navigation
Expand file tree
/
Copy pathTestHDFViewImageConversion.java
More file actions
383 lines (313 loc) · 13.3 KB
/
Copy pathTestHDFViewImageConversion.java
File metadata and controls
383 lines (313 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package test.uitest;
import static org.fest.swing.finder.WindowFinder.findFrame;
import static org.fest.swing.launcher.ApplicationLauncher.application;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JViewport;
import org.fest.swing.core.BasicRobot;
import org.fest.swing.core.GenericTypeMatcher;
import org.fest.swing.core.Robot;
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.finder.JFileChooserFinder;
import org.fest.swing.fixture.DialogFixture;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JFileChooserFixture;
import org.fest.swing.fixture.JMenuItemFixture;
import org.fest.swing.fixture.JScrollPaneFixture;
import org.fest.swing.fixture.JTreeFixture;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestHDFViewImageConversion {
private static FrameFixture mainFrameFixture;
// the version of the HDFViewer
private static String VERSION = "2.11";
private static String JPGFILE = "apollo17_earth.jpg";
private static String HDF4IMAGE = JPGFILE+".hdf";
private static String HDF5IMAGE = JPGFILE+".h5";
private File createFile(String name, boolean hdf4_type) {
String file_ext;
String file_type;
if (hdf4_type) {
file_ext = new String(".hdf");
file_type = new String("HDF4");
}
else {
file_ext = new String(".h5");
file_type = new String("HDF5");
}
File hdf_file = new File(name + file_ext);
if (hdf_file.exists())
hdf_file.delete();
try {
JMenuItemFixture fileMenuItem = mainFrameFixture.menuItemWithPath("File", "New", file_type);
mainFrameFixture.robot.waitForIdle();
fileMenuItem.requireVisible();
fileMenuItem.click();
mainFrameFixture.robot.waitForIdle();
JFileChooserFixture fileChooser = JFileChooserFinder.findFileChooser().using(mainFrameFixture.robot);
fileChooser.fileNameTextBox().setText(name + file_ext);
fileChooser.approve();
mainFrameFixture.robot.waitForIdle();
assertTrue("File-" + file_type + " file created", hdf_file.exists());
}
catch (Exception ex) {
ex.printStackTrace();
}
catch (AssertionError ae) {
ae.printStackTrace();
}
return hdf_file;
}
private void closeFile(File hdf_file, boolean delete_file) {
try {
JMenuItemFixture fileMenuItem = mainFrameFixture.menuItemWithPath("File", "Close All");
mainFrameFixture.robot.waitForIdle();
fileMenuItem.requireVisible();
fileMenuItem.click();
mainFrameFixture.robot.waitForIdle();
if (delete_file) {
assertTrue("File file deleted", hdf_file.delete());
assertFalse("File file gone", hdf_file.exists());
}
}
catch (Exception ex) {
ex.printStackTrace();
}
catch (AssertionError ae) {
ae.printStackTrace();
}
}
private void testSamplePixel(int x, int y, String requiredValue) {
JScrollPaneFixture imagePane = mainFrameFixture.scrollPane("imagecontent");
JViewport view = imagePane.component().getViewport();
int increment;
int unitsX;
int unitsY;
int remainderX;
int remainderY;
// Make sure Show Values is selected
if(!mainFrameFixture.menuItemWithPath("Image", "Show Value").component().isSelected()) {
mainFrameFixture.menuItemWithPath("Image", "Show Value").click();
mainFrameFixture.robot.waitForIdle();
}
try {
mainFrameFixture.robot.moveMouse(view, x, y);
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.textBox("valuefield").requireText(requiredValue);
}
catch (AssertionError ae) {
// Sample pixel isn't in the currently visible portion of the image
// Reset View
if(imagePane.component().getHorizontalScrollBar().isVisible()) {
imagePane.horizontalScrollBar().scrollToMinimum();
mainFrameFixture.robot.waitForIdle();
}
if(imagePane.component().getVerticalScrollBar().isVisible()) {
imagePane.verticalScrollBar().scrollToMinimum();
mainFrameFixture.robot.waitForIdle();
}
// Calculate number of units to scroll in order to display this pixel
increment = imagePane.component().getHorizontalScrollBar().getUnitIncrement();
unitsX = x / increment;
unitsY = y / increment;
if(unitsX > 0 && imagePane.component().getHorizontalScrollBar().isVisible()) {
imagePane.horizontalScrollBar().scrollUnitDown(unitsX);
mainFrameFixture.robot.waitForIdle();
}
if(unitsY > 0 && imagePane.component().getVerticalScrollBar().isVisible()) {
imagePane.verticalScrollBar().scrollUnitDown(unitsY);
mainFrameFixture.robot.waitForIdle();
}
// Calculate extra distance to be moved in each direction to find this pixel
remainderX = (x) - (view.getViewPosition().x);
remainderY = (y) - (view.getViewPosition().y);
mainFrameFixture.robot.moveMouse(view, remainderX, remainderY);
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.textBox("valuefield").requireText(requiredValue);
}
}
private static void clearRemovePropertyFile() {
// the local property file name
// look for the property file at the use home directory
String fn = ".hdfview" + VERSION;
String uh = System.getProperty("user.home") + File.separator + fn;
String ud = System.getProperty("user.dir") + File.separator + fn;
File prop_file = new File(uh);
if (prop_file.exists()) {
prop_file.delete();
}
else {
prop_file = new File(ud);
if (prop_file.exists()) {
prop_file.delete();
}
}
}
@BeforeClass
public static void setUpOnce() {
clearRemovePropertyFile();
FailOnThreadViolationRepaintManager.install();
Robot robot = BasicRobot.robotWithNewAwtHierarchy();
String envvalue = System.getProperty("hdfview.root");
application("ncsa.hdf.view.HDFView").withArgs("-root", envvalue).start();
mainFrameFixture = findFrame(new GenericTypeMatcher<JFrame>(JFrame.class) {
protected boolean isMatching(JFrame frame) {
return frame.getTitle().equals("HDFView " + VERSION) && frame.isShowing();
}
}).withTimeout(10000).using(robot);
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.requireVisible();
}
@AfterClass
public static void finishApplication() {
// cleanup leftover files
File hdf_file = new File(HDF4IMAGE);
if (hdf_file.exists())
hdf_file.delete();
hdf_file = new File(HDF5IMAGE);
if (hdf_file.exists())
hdf_file.delete();
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.cleanUp();
}
@Test
public void convertImageToHDF4() {
File hdf_file = createFile(JPGFILE, true);
closeFile(hdf_file, true);
try {
JMenuItemFixture fileMenuItem = mainFrameFixture.menuItemWithPath("Tools", "Convert Image To", "HDF4");
mainFrameFixture.robot.waitForIdle();
fileMenuItem.requireVisible();
fileMenuItem.click();
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.dialog().button("sourcefilebutton").click();
mainFrameFixture.robot.waitForIdle();
JFileChooserFixture fileChooser = JFileChooserFinder.findFileChooser().using(mainFrameFixture.robot);
fileChooser.fileNameTextBox().setText(JPGFILE);
fileChooser.approve();
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.dialog().button("okbutton").click();
mainFrameFixture.robot.waitForIdle();
JTreeFixture filetree = mainFrameFixture.tree().focus();
filetree.requireVisible();
assertTrue("convertImageToHDF4 filetree has file",
(filetree.valueAt(1)).compareTo(JPGFILE) == 0);
JMenuItemFixture imageMenuItem = filetree.showPopupMenuAt(1).menuItemWithPath("Open As");
mainFrameFixture.robot.waitForIdle();
imageMenuItem.requireVisible();
imageMenuItem.click();
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.dialog().radioButton("imagebutton").click();
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.dialog().button("OK").click();
mainFrameFixture.robot.waitForIdle();
// Test a few sample pixel values
testSamplePixel(325, 53, "x=325, y=53, value=(152, 106, 91)");
testSamplePixel(430, 357, "x=430, y=357, value=(83, 80, 107)");
testSamplePixel(197, 239, "x=197, y=239, value=(206, 177, 159)");
// Test metadata
JMenuItemFixture metadataMenuItem = filetree.showPopupMenuAt(1).menuItemWithPath("Show Properties");
mainFrameFixture.robot.waitForIdle();
metadataMenuItem.requireVisible();
metadataMenuItem.click();
mainFrameFixture.robot.waitForIdle();
DialogFixture dlg = mainFrameFixture.dialog();
dlg.requireVisible();
// Test Name
dlg.label("namefield").requireText(JPGFILE);
// Test # of dimensions
dlg.textBox("dimensions").requireText("2");
// Test Dimension Size
dlg.textBox("dimensionsize").requireText("533 x 533");
dlg.button("Close").click();
mainFrameFixture.robot.waitForIdle();
// Done
mainFrameFixture.menuItemWithPath("Image", "Close").click();
mainFrameFixture.robot.waitForIdle();
}
catch (Exception ex) {
ex.printStackTrace();
}
catch (AssertionError ae) {
ae.printStackTrace();
}
finally {
try {
closeFile(hdf_file, true);
}
catch (Exception ex) {
}
}
}
@Test
public void convertImageToHDF5() {
File hdf_file = new File(HDF5IMAGE);
try {
JMenuItemFixture fileMenuItem = mainFrameFixture.menuItemWithPath("Tools", "Convert Image To", "HDF5");
mainFrameFixture.robot.waitForIdle();
fileMenuItem.requireVisible();
fileMenuItem.click();
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.dialog().button("sourcefilebutton").click();
mainFrameFixture.robot.waitForIdle();
JFileChooserFixture fileChooser = JFileChooserFinder.findFileChooser().using(mainFrameFixture.robot);
fileChooser.fileNameTextBox().setText(JPGFILE);
fileChooser.approve();
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.dialog().button("okbutton").click();
mainFrameFixture.robot.waitForIdle();
JTreeFixture filetree = mainFrameFixture.tree().focus();
filetree.requireVisible();
assertTrue("convertImageToHDF5 filetree has file",
(filetree.valueAt(1)).compareTo(JPGFILE) == 0);
JMenuItemFixture imageMenuItem = filetree.showPopupMenuAt(1).menuItemWithPath("Open As");
mainFrameFixture.robot.waitForIdle();
imageMenuItem.requireVisible();
imageMenuItem.click();
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.dialog().radioButton("imagebutton").click();
mainFrameFixture.robot.waitForIdle();
mainFrameFixture.dialog().button("OK").click();
mainFrameFixture.robot.waitForIdle();
// Test a few sample pixel values
testSamplePixel(325, 53, "x=325, y=53, value=(152, 106, 91)");
testSamplePixel(430, 357, "x=430, y=357, value=(83, 80, 107)");
testSamplePixel(197, 239, "x=197, y=239, value=(206, 177, 159)");
// Test metadata
JMenuItemFixture metadataMenuItem = filetree.showPopupMenuAt(1).menuItemWithPath("Show Properties");
mainFrameFixture.robot.waitForIdle();
metadataMenuItem.requireVisible();
metadataMenuItem.click();
mainFrameFixture.robot.waitForIdle();
DialogFixture dlg = mainFrameFixture.dialog();
dlg.requireVisible();
// Test Name
dlg.label("namefield").requireText(JPGFILE);
// Test # of dimensions
dlg.textBox("dimensions").requireText("3");
// Test Dimension Size
dlg.textBox("dimensionsize").requireText("533 x 533 x 3");
dlg.button("Close").click();
mainFrameFixture.robot.waitForIdle();
// Done
mainFrameFixture.menuItemWithPath("Image", "Close").click();
mainFrameFixture.robot.waitForIdle();
}
catch (Exception ex) {
ex.printStackTrace();
}
catch (AssertionError ae) {
ae.printStackTrace();
}
finally {
try {
closeFile(hdf_file, true);
}
catch (Exception ex) {
}
}
}
}