Search in sources :

Example 21 with VaadinContext

use of com.vaadin.flow.server.VaadinContext in project flow by vaadin.

the class IndexHtmlRequestHandlerTest method should_attachWebpackErrors.

@Test
public void should_attachWebpackErrors() throws Exception {
    deploymentConfiguration.setEnableDevServer(true);
    deploymentConfiguration.setProductionMode(false);
    DevModeHandler devServer = Mockito.mock(DevModeHandler.class);
    Mockito.when(devServer.getFailedOutput()).thenReturn("Failed to compile");
    Mockito.when(devServer.prepareConnection(Mockito.anyString(), Mockito.anyString())).thenReturn(Mockito.mock(HttpURLConnection.class));
    service.setContext(context);
    DevModeHandlerManager devModeHandlerManager = new DevModeHandlerManager() {

        @Override
        public Class<?>[] getHandlesTypes() {
            return new Class[0];
        }

        @Override
        public void initDevModeHandler(Set<Class<?>> classes, VaadinContext context) throws VaadinInitializerException {
        }

        @Override
        public void setDevModeHandler(DevModeHandler devModeHandler) {
        }

        @Override
        public DevModeHandler getDevModeHandler() {
            return devServer;
        }
    };
    ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
    Lookup lookup = Lookup.compose(Lookup.of(devModeHandlerManager, DevModeHandlerManager.class), Lookup.of(resourceProvider, ResourceProvider.class));
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    mockApplicationConfiguration(appConfig);
    URL resource = Mockito.mock(URL.class);
    Mockito.when(resourceProvider.getApplicationResource(VAADIN_WEBAPP_RESOURCES + INDEX_HTML)).thenReturn(resource);
    when(resource.openStream()).thenReturn(new ByteArrayInputStream("<html><head></head></html>".getBytes()));
    // Send the request
    indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response);
    String indexHtml = responseOutput.toString(StandardCharsets.UTF_8.name());
    Assert.assertTrue("Should have a system error dialog", indexHtml.contains("<div class=\"v-system-error\">"));
    Assert.assertTrue("Should show webpack failure error", indexHtml.contains("Failed to compile"));
}
Also used : DevModeHandlerManager(com.vaadin.flow.internal.DevModeHandlerManager) VaadinContext(com.vaadin.flow.server.VaadinContext) HttpURLConnection(java.net.HttpURLConnection) Set(java.util.Set) ByteArrayInputStream(java.io.ByteArrayInputStream) DevModeHandler(com.vaadin.flow.internal.DevModeHandler) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 22 with VaadinContext

use of com.vaadin.flow.server.VaadinContext in project flow by vaadin.

the class WebComponentProviderTest method mockRequest.

private VaadinRequest mockRequest(boolean hasConfig) {
    VaadinContext context = Mockito.mock(VaadinContext.class);
    VaadinService service = Mockito.mock(VaadinService.class);
    VaadinRequest request = Mockito.mock(VaadinRequest.class);
    Mockito.when(request.getService()).thenReturn(service);
    Mockito.when(service.getContext()).thenReturn(context);
    WebComponentConfigurationRegistry registry = Mockito.mock(WebComponentConfigurationRegistry.class);
    Mockito.when(context.getAttribute(Mockito.eq(WebComponentConfigurationRegistry.class), Mockito.any())).thenReturn(registry);
    Mockito.when(registry.hasConfigurations()).thenReturn(hasConfig);
    Mockito.when(request.getPathInfo()).thenReturn("/web-component/a-b.js");
    return request;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) VaadinService(com.vaadin.flow.server.VaadinService) VaadinRequest(com.vaadin.flow.server.VaadinRequest)

Example 23 with VaadinContext

use of com.vaadin.flow.server.VaadinContext in project flow by vaadin.

the class WebComponentBootstrapHandlerTest method getMockResponse.

private VaadinResponse getMockResponse(ByteArrayOutputStream stream) throws IOException {
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    VaadinService service = Mockito.mock(VaadinService.class);
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Mockito.when(response.getOutputStream()).thenReturn(stream);
    Mockito.when(response.getService()).thenReturn(service);
    Mockito.when(service.getContext()).thenReturn(context);
    Mockito.when(context.getAttribute(eq(WebComponentConfigurationRegistry.class), any())).thenReturn(Mockito.mock(WebComponentConfigurationRegistry.class));
    return response;
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinContext(com.vaadin.flow.server.VaadinContext) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) VaadinService(com.vaadin.flow.server.VaadinService)

Example 24 with VaadinContext

use of com.vaadin.flow.server.VaadinContext in project flow by vaadin.

the class PushHandlerTest method onConnect_devMode_websocket_refreshConnection_onConnectIsCalled_callWithUIIsNotCalled.

@Test
public void onConnect_devMode_websocket_refreshConnection_onConnectIsCalled_callWithUIIsNotCalled() throws ServiceException {
    MockVaadinServletService service = Mockito.spy(MockVaadinServletService.class);
    MockDeploymentConfiguration deploymentConfiguration = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
    deploymentConfiguration.setProductionMode(false);
    deploymentConfiguration.setDevModeLiveReloadEnabled(true);
    deploymentConfiguration.setDevModeGizmoEnabled(true);
    ApplicationConfiguration applicationConfiguration = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(applicationConfiguration.isProductionMode()).thenReturn(false);
    VaadinContext context = service.getContext();
    context.setAttribute(ApplicationConfiguration.class, applicationConfiguration);
    BrowserLiveReload liveReload = mockBrowserLiveReloadImpl(context);
    AtomicReference<AtmosphereResource> res = new AtomicReference<>();
    runTest(service, (handler, resource) -> {
        AtmosphereRequest request = resource.getRequest();
        Mockito.when(request.getParameter(ApplicationConstants.DEBUG_WINDOW_CONNECTION)).thenReturn("");
        Mockito.when(resource.transport()).thenReturn(TRANSPORT.WEBSOCKET);
        handler.onConnect(resource);
        res.set(resource);
    });
    Mockito.verify(service, Mockito.times(0)).requestStart(Mockito.any(), Mockito.any());
    Mockito.verify(liveReload).onConnect(res.get());
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) BrowserLiveReload(com.vaadin.flow.internal.BrowserLiveReload) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Test(org.junit.Test)

Example 25 with VaadinContext

use of com.vaadin.flow.server.VaadinContext in project flow by vaadin.

the class PushHandlerTest method debugWindowConnection_productionMode_mustNeverBeConnected.

@Test
public void debugWindowConnection_productionMode_mustNeverBeConnected() throws Exception {
    ApplicationConfiguration applicationConfiguration = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(applicationConfiguration.isProductionMode()).thenReturn(true);
    MockVaadinServletService service = Mockito.spy(MockVaadinServletService.class);
    VaadinContext context = service.getContext();
    context.setAttribute(ApplicationConfiguration.class, applicationConfiguration);
    runTest(service, (handler, resource) -> {
        Mockito.when(resource.transport()).thenReturn(TRANSPORT.WEBSOCKET);
        Mockito.when(resource.getRequest().getParameter(ApplicationConstants.DEBUG_WINDOW_CONNECTION)).thenReturn("");
        Mockito.doNothing().when(handler).callWithService(Mockito.any(), Mockito.any());
        handler.onConnect(resource);
        Mockito.verify(handler, Mockito.never()).callWithService(Mockito.any(), Mockito.any());
        Mockito.verify(handler, Mockito.never()).callWithUi(Mockito.any(), Mockito.any());
    });
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Test(org.junit.Test)

Aggregations

VaadinContext (com.vaadin.flow.server.VaadinContext)42 Test (org.junit.Test)16 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)15 VaadinService (com.vaadin.flow.server.VaadinService)11 Lookup (com.vaadin.flow.di.Lookup)8 ResourceProvider (com.vaadin.flow.di.ResourceProvider)8 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)5 BrowserLiveReload (com.vaadin.flow.internal.BrowserLiveReload)5 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)5 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)5 Properties (java.util.Properties)5 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4 VaadinConfig (com.vaadin.flow.server.VaadinConfig)4 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)4 File (java.io.File)4 Before (org.junit.Before)4 VaadinSession (com.vaadin.flow.server.VaadinSession)3 WebComponentConfigurationRegistry (com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry)3 AppShellRegistry (com.vaadin.flow.server.AppShellRegistry)2 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)2