EventUtil { public static List<BeforeLeaveObserver> collectBeforeLeaveObservers(UI ui) { return getImplementingComponents(flattenDescendants(ui.getElement()), BeforeLeaveObserver.class).collect(Collectors.toList()); } private  EventUtil(); static List<BeforeEnterObserver> collectBeforeEnterObservers(
            Collection<? extends HasElement> oldChain,
            Collection<? extends HasElement> newChain); static List<BeforeEnterObserver> collectBeforeEnterObserversFromChain(
            Collection<? extends HasElement> chain,
            Collection<? extends HasElement> childrenExclusions); static List<BeforeEnterObserver> collectBeforeEnterObserversFromChainElement(
            HasElement element,
            Collection<? extends HasElement> childrenExclusions); static List<BeforeLeaveObserver> collectBeforeLeaveObservers(UI ui); static List<AfterNavigationObserver> collectAfterNavigationObservers(
            UI ui); static List<LocaleChangeObserver> collectLocaleChangeObservers(
            Element element); static List<LocaleChangeObserver> collectLocaleChangeObservers(
            List<HasElement> components); static void informLocaleChangeObservers(UI ui); static void informLocaleChangeObservers(UI ui,
            List<HasElement> components); static Stream<T> getImplementingComponents(
            Stream<Element> elementStream, Class<T> type); static void inspectHierarchy(Element node,
            Collection<Element> descendants, Predicate<Element> filter);  }
@Test public void collectBeforeNavigationObserversFromUI() throws Exception { UI ui = UI.getCurrent(); Element node = ui.getElement(); node.appendChild(new Element("main"), new Element("menu")); Element nested = new Element("nested"); nested.appendChild(new Element("nested-child"), new Element("nested-child-2")); node.appendChild(nested); Component.from(nested, LeaveObserver.class); List<BeforeLeaveObserver> beforeNavigationObservers = EventUtil .collectBeforeLeaveObservers(ui); Assert.assertEquals("Wrong amount of listener instances found", 1, beforeNavigationObservers.size()); }
@Test public void collectBeforeNavigationObserversFromUI_elementHasVirtualChildren() throws Exception { UI ui = UI.getCurrent(); Element node = ui.getElement(); node.appendChild(new Element("main"), new Element("menu")); Element nested = new Element("nested"); nested.appendVirtualChild(new Element("nested-child"), new Element("nested-child-2")); node.appendChild(nested); node.getStateProvider().appendVirtualChild(node.getNode(), new Element("attached-by-id"), NodeProperties.INJECT_BY_ID, "id"); Component.from(nested, LeaveObserver.class); List<BeforeLeaveObserver> beforeNavigationObservers = EventUtil .collectBeforeLeaveObservers(ui); Assert.assertEquals("Wrong amount of listener instances found", 1, beforeNavigationObservers.size()); }