FocusManagerImpl implements FocusManager { @Override public void focus(String componentId) { FacesContext context = FacesContext.getCurrentInstance(); if (context == null) { throw new IllegalStateException(FocusManager.class.getSimpleName() + " can't be used without FacesContext available"); } if (componentId == null) { setContextAttribute(context, null); } else { UIComponent currentComponent = UIComponent.getCurrentComponent(context); if (currentComponent == null) { currentComponent = context.getViewRoot(); } currentComponent.findComponent(componentId); UIComponent component = RendererUtils.getInstance().findComponentFor(currentComponent, componentId); if (component == null) { String message = FocusManager.class.getSimpleName() + ": Component with ID '" + componentId + "' was not found"; LOG.warn(message); context.addMessage(null, new FacesMessage(message)); } else { String clientId = component.getClientId(context); setContextAttribute(context, clientId); JavaScriptService javaScriptService = ServiceTracker.getService(context, JavaScriptService.class); javaScriptService.addPageReadyScript(context, new JSLiteral(String.format(SCRIPT, clientId))); } } }  @Override void focus(String componentId);  }
@Test public void test() { FocusManager focusManager = new FocusManagerImpl(); focusManager.focus(null); verify(attributes).put(FocusManager.FOCUS_CONTEXT_ATTRIBUTE, null); assertFalse(FocusRendererUtils.isFocusEnforced(facesContext)); verifyZeroInteractions(javaScriptService); }
@Test public void test2() { FocusManager focusManager = new FocusManagerImpl(); String componentId = "someComponentId"; String clientId = "someClientId"; when(component.getClientId(facesContext)).thenReturn(clientId); when(viewRoot.findComponent(componentId)).thenReturn(component); focusManager.focus(componentId); assertEquals(clientId, attributes.get(FocusManager.FOCUS_CONTEXT_ATTRIBUTE)); assertTrue(attributes.containsKey(FocusManager.FOCUS_CONTEXT_ATTRIBUTE)); assertTrue(FocusRendererUtils.isFocusEnforced(facesContext)); verify(javaScriptService).addPageReadyScript(Mockito.any(FacesContext.class), Mockito.any(JSLiteral.class)); }