EventUtil { public static List<LocaleChangeObserver> collectLocaleChangeObservers( Element element) { return getImplementingComponents(flattenDescendants(element), LocaleChangeObserver.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 collectLocaleChangeObserverFromElement() throws Exception { Element node = new Element("root"); node.appendChild(new Element("main"), new Element("menu")); Element nested = new Element("nested-locale"); nested.appendChild(new Element("nested-child"), new Element("nested-child-2")); node.appendChild(nested); Component.from(nested, Locale.class); List<LocaleChangeObserver> beforeNavigationObservers = EventUtil .collectLocaleChangeObservers(node); Assert.assertEquals("Wrong amount of listener instances found", 1, beforeNavigationObservers.size()); }
@Test public void collectLocaleChangeObserverFromElement_elementHasVirtualChildren() throws Exception { Element node = new Element("root"); node.appendChild(new Element("main"), new Element("menu")); node.appendVirtualChild(new Element("main-virtual"), new Element("menu-virtual")); Element nested = new Element("nested-locale"); nested.appendVirtualChild(new Element("nested-child"), new Element("nested-child-2")); node.appendVirtualChild(nested); Component.from(nested, Locale.class); List<LocaleChangeObserver> beforeNavigationObservers = EventUtil .collectLocaleChangeObservers(node); Assert.assertEquals("Wrong amount of listener instances found", 1, beforeNavigationObservers.size()); }
@Test public void collectLocaleChangeObserverFromComponentList() throws Exception { Foo foo = new Foo(); foo.getElement().appendChild(new Locale().getElement()); Bar bar = new Bar(); Element nested = new Element("nested-locale"); nested.appendChild(new Element("nested-child"), new Locale().getElement()); bar.getElement().appendChild(new Foo().getElement(), nested); List<LocaleChangeObserver> beforeNavigationObservers = EventUtil .collectLocaleChangeObservers(Arrays.asList(foo, bar)); Assert.assertEquals("Wrong amount of listener instances found", 2, beforeNavigationObservers.size()); }
@Test public void collectLocaleChangeObserverFromComponentList_elementHasVirtualChildren() throws Exception { Foo foo = new Foo(); foo.getElement().appendChild(new Locale().getElement()); Bar bar = new Bar(); Element nested = new Element("nested-locale"); nested.appendChild(new Element("nested-child"), new Locale().getElement()); bar.getElement().appendChild(new Foo().getElement(), nested); List<LocaleChangeObserver> beforeNavigationObservers = EventUtil .collectLocaleChangeObservers(Arrays.asList(foo, bar)); Assert.assertEquals("Wrong amount of listener instances found", 2, beforeNavigationObservers.size()); }