TooltipLabel extends JBLabel { @VisibleForTesting @Nullable String getTooltip(@NotNull Component component) { if (!isInScope(component)) { return null; } String tooltip = null; TreeWalker treeWalker = new TreeWalker(component); for (Component c : treeWalker.ancestors()) { if (c instanceof JComponent) { JComponent jc = (JComponent)c; tooltip = jc.getToolTipText(); } if (c == myScope || tooltip != null) { break; } } return tooltip; } TooltipLabel(); @Override void setText(String text); void setScope(@Nullable Container scope);  }
@Test public void getTooltipReturnsExpectedValue() throws Exception { assertThat(myTooltipLabel.getTooltip(myRootPanel)).isNull(); assertThat(myTooltipLabel.getTooltip(myNode1)).isEqualTo("node1"); assertThat(myTooltipLabel.getTooltip(myNode1_1)).isEqualTo("node1_1"); assertThat(myTooltipLabel.getTooltip(myNode1_2)).isEqualTo("node1_2"); assertThat(myTooltipLabel.getTooltip(myNode1_1_1)).isEqualTo("node1_1_1"); assertThat(myTooltipLabel.getTooltip(myNode1_2_1_notooltip)).isEqualTo("node1_2"); assertThat(myTooltipLabel.getTooltip(myNode1_2_2_notooltip)).isEqualTo("node1_2"); assertThat(myTooltipLabel.getTooltip(myNode1_2_2_1_notooltip)).isEqualTo("node1_2"); assertThat(myTooltipLabel.getTooltip(myNode2)).isEqualTo("node2"); assertThat(myTooltipLabel.getTooltip(myNode2_1)).isEqualTo("node2_1"); assertThat(myTooltipLabel.getTooltip(myNode2_1_1_notooltip)).isEqualTo("node2_1"); }