### Question:
Browser { public void addCreatedHandler(Consumer<Browser> createdHandler) { this.createdHandlers.add(createdHandler); if(nativeBrowserCreated) { createdHandler.accept(this); } } Browser(String name, Configuration config, CefClient client); Component getBrowserUi(); T assumeUiApi(String apiId, Class<T> api); void close(Runnable onAfterClose); CefBrowser getNativeBrowser(); @Override String toString(); void addCreatedHandler(Consumer<Browser> createdHandler); String getName(); void addClosedHandler(Consumer<Browser> browserClosed);  }### Answer:
@Test void callsCreatedHandlerOnAfterCreatedWhenHandlerIsAvailable() { Configuration config = mock(Configuration.class); CefClient client = mock(CefClient.class); CefBrowser cefBrowser = mock(CefBrowser.class); when(client.createBrowser(any(), anyBoolean(), anyBoolean())).thenReturn(cefBrowser); Browser browser = new Browser("-dummy name-", config, client); Consumer<Browser> createdHandler = mock(Consumer.class); browser.addCreatedHandler(createdHandler); ArgumentCaptor<CefLifeSpanHandler> lifeSpanCaptor = ArgumentCaptor.forClass(CefLifeSpanHandler.class); verify(client).addLifeSpanHandler(lifeSpanCaptor.capture()); lifeSpanCaptor.getValue().onAfterCreated(cefBrowser); verify(createdHandler).accept(browser); }
@Test void callsCreatedHandlerImmediatelyAtRegisterIfBrowserAlreadyCreated() { Configuration config = mock(Configuration.class); CefClient client = mock(CefClient.class); CefBrowser cefBrowser = mock(CefBrowser.class); when(client.createBrowser(any(), anyBoolean(), anyBoolean())).thenReturn(cefBrowser); Browser browser = new Browser("-dummy name-", config, client); Consumer<Browser> createdHandler = mock(Consumer.class); ArgumentCaptor<CefLifeSpanHandler> lifeSpanCaptor = ArgumentCaptor.forClass(CefLifeSpanHandler.class); verify(client).addLifeSpanHandler(lifeSpanCaptor.capture()); lifeSpanCaptor.getValue().onAfterCreated(cefBrowser); browser.addCreatedHandler(createdHandler); verify(createdHandler).accept(browser); }
@Test void doesNotCallCreatedHandlerWhenEventIsForDifferentNativeBrowser() { Configuration config = mock(Configuration.class); CefClient client = mock(CefClient.class); CefBrowser cefBrowser = mock(CefBrowser.class); when(client.createBrowser(any(), anyBoolean(), anyBoolean())).thenReturn(cefBrowser); Browser browser = new Browser("-dummy name-", config, client); Consumer<Browser> createdHandler = mock(Consumer.class); browser.addCreatedHandler(createdHandler); ArgumentCaptor<CefLifeSpanHandler> lifeSpanCaptor = ArgumentCaptor.forClass(CefLifeSpanHandler.class); verify(client).addLifeSpanHandler(lifeSpanCaptor.capture()); lifeSpanCaptor.getValue().onAfterCreated(mock(CefBrowser.class)); verify(createdHandler, never()).accept(browser); }