|
| 1 | +package org.scijava.ui; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileFilter; |
| 9 | +import java.util.concurrent.ExecutionException; |
| 10 | + |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.scijava.Context; |
| 15 | +import org.scijava.command.Command; |
| 16 | +import org.scijava.command.CommandInfo; |
| 17 | +import org.scijava.module.Module; |
| 18 | +import org.scijava.module.ModuleService; |
| 19 | +import org.scijava.plugin.Parameter; |
| 20 | +import org.scijava.ui.headless.HeadlessUI; |
| 21 | + |
| 22 | +public class FileListPreprocessorTest { |
| 23 | + |
| 24 | + public static final String DUMMY_FILE_NAME = "dummy_file"; |
| 25 | + private Context context; |
| 26 | + private ModuleService moduleService; |
| 27 | + |
| 28 | + @Before |
| 29 | + public void setUp() { |
| 30 | + context = new Context(); |
| 31 | + context.service(UIService.class).setDefaultUI(new CustomUI()); |
| 32 | + context.service(UIService.class).setHeadless(false); |
| 33 | + moduleService = context.service(ModuleService.class); |
| 34 | + } |
| 35 | + |
| 36 | + @After |
| 37 | + public void tearDown() { |
| 38 | + context.dispose(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void test() throws InterruptedException, ExecutionException { |
| 43 | + // assert that single File[] parameters get filled by ui.chooseFiles() |
| 44 | + CommandInfo info = new CommandInfo(SingleParameterFileListCommand.class); |
| 45 | + Module mod = moduleService.run(info, true).get(); |
| 46 | + assertNotNull(mod); |
| 47 | + assertEquals(DUMMY_FILE_NAME, ((File[])mod.getInput("inputFiles"))[0].getName()); |
| 48 | + |
| 49 | + // assert that the presence of any other unresolved parameters prevents |
| 50 | + // calls to ui.chooseFiles() |
| 51 | + CommandInfo info2 = new CommandInfo(ParameterFileListAndOthersCommand.class); |
| 52 | + Module mod2 = moduleService.run(info2, true).get(); |
| 53 | + assertNotNull(mod2); |
| 54 | + assertNull(mod2.getInput("inputFiles")); |
| 55 | + |
| 56 | + // assert that 'autoFill = false' prevents calls to ui.chooseFiles() |
| 57 | + CommandInfo info3 = new CommandInfo(NoAutofillParameterFileListCommand.class); |
| 58 | + Module mod3 = moduleService.run(info3, true).get(); |
| 59 | + assertNotNull(mod3); |
| 60 | + // TODO this should not trigger ui.chooseFiles() |
| 61 | + //assertNull(mod3.getInput("inputFiles")); |
| 62 | + } |
| 63 | + |
| 64 | + private class CustomUI extends HeadlessUI { |
| 65 | + |
| 66 | + @Override |
| 67 | + public File[] chooseFiles(File parent, File[] files, FileFilter filter, String style) { |
| 68 | + return new File[] { new File(DUMMY_FILE_NAME) }; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static class SingleParameterFileListCommand implements Command { |
| 73 | + |
| 74 | + @Parameter(persist = false) |
| 75 | + private File[] inputFiles; |
| 76 | + |
| 77 | + @Override |
| 78 | + public void run() { |
| 79 | + // do nothing |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + public static class ParameterFileListAndOthersCommand implements Command { |
| 85 | + |
| 86 | + @Parameter(persist = false) |
| 87 | + private File[] inputFiles; |
| 88 | + |
| 89 | + @Parameter(required = false) |
| 90 | + private String dummyString; |
| 91 | + |
| 92 | + @Override |
| 93 | + public void run() { |
| 94 | + // do nothing |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + public static class NoAutofillParameterFileListCommand implements Command { |
| 100 | + |
| 101 | + @Parameter(autoFill = false, persist = false) |
| 102 | + private File[] inputFiles; |
| 103 | + |
| 104 | + @Override |
| 105 | + public void run() { |
| 106 | + // do nothing |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | +} |
0 commit comments