Skip to content

Conversation

@frauzufall
Copy link
Member

I ran into issue #48 and though this PR does not give the user a chance to choose which display to use, it at least makes the DefaultDisplayService choose the one with the highest priority.

I also added a test, but could not yet test it myself, because I get a bunch of unrelated errors with this repo which I have to investigate. But if anyone already has an opinion on this in the meantime or can try it out, let me know.

@frauzufall
Copy link
Member Author

Mh, my test it not great because all tests now have this dummy class CustomTextDisplay in scope. How would a better test look like?

@imagejan
Copy link
Member

imagejan commented Dec 3, 2019

@frauzufall wrote:

all tests now have this dummy class CustomTextDisplay in scope

I had a similar issue in scijava/scijava-table#8 where I added a FakeTableIOPlugin that's on the classpath for all tests (see https://github.com/scijava/scijava-table/pull/8/files). I tried first having this as a nested inner class, but that didn't work. If there's a better solution, I'd be interested as well 🙂

final String name = "Text";
final String value = "Hello";
Display<?> display = displayService.createDisplay(name, value);
assertEquals(CustomTextDisplay.class, display.getClass());
Copy link
Member

@imagejan imagejan Dec 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to asserting that CustomTextDisplay gets chosen, can we also test that we actually do get multiple candidate Displays that are able to handle Strings? Otherwise the name of this method would be misleading, no?

@ctrueden
Copy link
Member

ctrueden commented Dec 3, 2019

How would a better test look like?

I recently made it easier to create PluginInfo objects easily; see f31a468. Here is an example of how this could be used in tests:

Context ctx = new Context(...); // whatever services you want
PluginInfo<Display> info = PluginInfo.create(CustomTextDisplay.class, Display.class);
info.setPriority(Priority.HIGH);
ctx.service(PluginService.class).addPlugin(info);
... now do the actual test ...

And then do not annotate CustomTextDisplay with the @Plugin annotation.

Typically the context creation would be in the @Before method, and context disposal in the @After method, but if you have only a single test that wants the CustomTextDisplay in the plugin index, you could add it manually like that at the beginning of that test. Or if all tests of a particular class want it, then make a @Before method for that class adding it there.

…oose first by priority #48

* before, the first display which was able to display an object was
chosen
* now, all matching displays are collected and the one with the highest
priority is used
@frauzufall
Copy link
Member Author

Thanks for the feedback! I used the strategy @ctrueden described and adjusted the test along @imagejan's note. The tests are running now and my use case is also working with it.

Copy link
Member

@ctrueden ctrueden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I think the display plugin selection code can be simplified, though—can you check?

final List<Display<?>> matchingDisplays = getMatchingDisplays(o);
if(matchingDisplays.size() > 0) {
// use the display with the highest priority
Display<?> display = matchingDisplays.stream().max(Comparator.comparing(Prioritized::getPriority)).get();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this. The displays should already always be returned from the PluginService in high-to-low priority. Did you observe otherwise?

@frauzufall
Copy link
Member Author

Hi @ctrueden, you are right, the PluginService returns the displays in the correct order, which means this PR is not needed at all. I did not properly check if the test I added was actually failing before (reminded me of our discussion about whether the PR related test should be the first commit or the last one 🙃 ).. I also cannot reproduce my use case where it failed. Probably put the priority on the wrong class.... Next time I start with an issue.

Sorry for the trouble! I still learned something from your comments 👍

@frauzufall frauzufall closed this Dec 4, 2019
@imagejan
Copy link
Member

imagejan commented Dec 5, 2019

We could still add the test for multiple displays to test if the highest priority wins. I think the current DisplayTest doesn't test this, right?

@ctrueden ctrueden deleted the display-priority branch December 5, 2019 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants