-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: add test for FileListPreprocessor
This test calls uiService.setHeadless(false) in order to set its custom default UI. The test currently passes locally (because the last assertion - the issue I'd like to fix - is commented out), but does it on CI?
- Loading branch information
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/test/java/org/scijava/ui/FileListPreprocessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package org.scijava.ui; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import java.io.File; | ||
import java.io.FileFilter; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.scijava.Context; | ||
import org.scijava.command.Command; | ||
import org.scijava.command.CommandInfo; | ||
import org.scijava.module.Module; | ||
import org.scijava.module.ModuleService; | ||
import org.scijava.plugin.Parameter; | ||
import org.scijava.ui.headless.HeadlessUI; | ||
|
||
public class FileListPreprocessorTest { | ||
|
||
public static final String DUMMY_FILE_NAME = "dummy_file"; | ||
private Context context; | ||
private ModuleService moduleService; | ||
|
||
@Before | ||
public void setUp() { | ||
context = new Context(); | ||
context.service(UIService.class).setDefaultUI(new CustomUI()); | ||
context.service(UIService.class).setHeadless(false); | ||
moduleService = context.service(ModuleService.class); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
context.dispose(); | ||
} | ||
|
||
@Test | ||
public void test() throws InterruptedException, ExecutionException { | ||
// assert that single File[] parameters get filled by ui.chooseFiles() | ||
CommandInfo info = new CommandInfo(SingleParameterFileListCommand.class); | ||
Module mod = moduleService.run(info, true).get(); | ||
assertNotNull(mod); | ||
assertEquals(DUMMY_FILE_NAME, ((File[])mod.getInput("inputFiles"))[0].getName()); | ||
|
||
// assert that the presence of any other unresolved parameters prevents | ||
// calls to ui.chooseFiles() | ||
CommandInfo info2 = new CommandInfo(ParameterFileListAndOthersCommand.class); | ||
Module mod2 = moduleService.run(info2, true).get(); | ||
assertNotNull(mod2); | ||
assertNull(mod2.getInput("inputFiles")); | ||
|
||
// assert that 'autoFill = false' prevents calls to ui.chooseFiles() | ||
CommandInfo info3 = new CommandInfo(NoAutofillParameterFileListCommand.class); | ||
Module mod3 = moduleService.run(info3, true).get(); | ||
assertNotNull(mod3); | ||
// TODO this should not trigger ui.chooseFiles() | ||
//assertNull(mod3.getInput("inputFiles")); | ||
} | ||
|
||
private class CustomUI extends HeadlessUI { | ||
|
||
@Override | ||
public File[] chooseFiles(File parent, File[] files, FileFilter filter, String style) { | ||
return new File[] { new File(DUMMY_FILE_NAME) }; | ||
} | ||
} | ||
|
||
public static class SingleParameterFileListCommand implements Command { | ||
|
||
@Parameter(persist = false) | ||
private File[] inputFiles; | ||
|
||
@Override | ||
public void run() { | ||
// do nothing | ||
} | ||
|
||
} | ||
|
||
public static class ParameterFileListAndOthersCommand implements Command { | ||
|
||
@Parameter(persist = false) | ||
private File[] inputFiles; | ||
|
||
@Parameter(required = false) | ||
private String dummyString; | ||
|
||
@Override | ||
public void run() { | ||
// do nothing | ||
} | ||
|
||
} | ||
|
||
public static class NoAutofillParameterFileListCommand implements Command { | ||
|
||
@Parameter(autoFill = false, persist = false) | ||
private File[] inputFiles; | ||
|
||
@Override | ||
public void run() { | ||
// do nothing | ||
} | ||
|
||
} | ||
} |