### Question:
MessageRouterHandler extends CefMessageRouterHandlerAdapter { @Override public boolean onQuery(CefBrowser browser, CefFrame frame, long query_id, String request, boolean persistent, CefQueryCallback callback) { Map<String, Object> apis = apisSupplier.get(); if(jsApiCache == null || apis != lastApis) { jsApiCache = jsCacheBuilder.apply(apisSupplier.get()); lastApis = apis; } if("--getApis".equals(request)) { callback.success(jsApiCache.getApiDescription()); return true; } if(request.startsWith("--call:")) { Gson gson = new Gson(); FunctionCall functionCall = gson.fromJson(request.substring("--call:".length()), FunctionCall.class); ObjectDescription objectDescription = jsApiCache.call(functionCall); callback.success(objectDescription.describe()); return true; } return false; } MessageRouterHandler(Supplier<Map<String, Object>> apisSupplier, Function<Map<String, Object>, JsApiCache> jsCacheBuilder); @Override boolean onQuery(CefBrowser browser, CefFrame frame, long query_id, String request,
						   boolean persistent, CefQueryCallback callback);  }### Answer:
@Test void rebuildsApisJSCacheWhenApisChanged() { Supplier<Map<String, Object>> supplier = () -> new HashMap<>(); Function<Map<String, Object>, JsApiCache> jsCacheBuilder = mock(Function.class); MessageRouterHandler handler = new MessageRouterHandler(supplier, jsCacheBuilder); handler.onQuery(null,null,0,"request",false, mock(CefQueryCallback.class)); handler.onQuery(null,null,0,"request",false, mock(CefQueryCallback.class)); verify(jsCacheBuilder, times(2)).apply(any()); }
@Test void doesNotRebuildApisJSCacheWhenApisUnchanged() { Map<String, Object> map = new HashMap<>(); Supplier<Map<String, Object>> supplier = () -> map; Function<Map<String, Object>, JsApiCache> jsCacheBuilder = mock(Function.class); when(jsCacheBuilder.apply(any())).thenReturn(mock(JsApiCache.class)); MessageRouterHandler handler = new MessageRouterHandler(supplier, jsCacheBuilder); handler.onQuery(null,null,0,"request",false, mock(CefQueryCallback.class)); handler.onQuery(null,null,0,"request",false, mock(CefQueryCallback.class)); verify(jsCacheBuilder, times(1)).apply(map); }
@Test void returnsApisDescriptionToJsWhenRequestIsGetApis() { Supplier<Map<String, Object>> supplier = () -> new HashMap<>(); Function<Map<String, Object>, JsApiCache> jsCacheBuilder = mock(Function.class); JsApiCache jsCache = mock(JsApiCache.class); when(jsCache.getApiDescription()).thenReturn("the description"); when(jsCacheBuilder.apply(any())).thenReturn(jsCache); MessageRouterHandler handler = new MessageRouterHandler(supplier, jsCacheBuilder); CefQueryCallback callback = mock(CefQueryCallback.class); handler.onQuery(null,null,0,"--getApis",false, callback); verify(callback).success("the description"); }