Skip to content

Commit

Permalink
Fixed code review finding improved server route method
Browse files Browse the repository at this point in the history
  • Loading branch information
tltv committed Oct 11, 2024
1 parent 650f5f4 commit 0e9111f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,25 @@ public static boolean hasClientRouteWithAutoLayout(
public static boolean hasServerRouteWithAutoLayout(
AbstractRouteRegistry registry) {
Collection<?> layouts = registry.getLayouts();
return registry.getRegisteredRoutes().stream()
.anyMatch(routeData -> !routeData.getParentLayouts().isEmpty()
&& routeData.getParentLayouts().stream()
.anyMatch(layouts::contains));
return registry.getRegisteredRoutes().stream().anyMatch(routeData -> {
String path;
if (routeData.getNavigationTarget()
.getAnnotation(Route.class) != null) {
path = getRoutePath(registry.getContext(),
routeData.getNavigationTarget());
} else {
path = resolve(registry.getContext(),
routeData.getNavigationTarget());
List<String> parentRoutePrefixes = getRoutePrefixes(
routeData.getNavigationTarget(), null, path);
path = String.join("/", parentRoutePrefixes);
}
return RouteUtil
.isAutolayoutEnabled(routeData.getNavigationTarget(), path)
&& registry.hasLayout(path)
&& collectRouteParentLayouts(registry.getLayout(path))
.stream().anyMatch(layouts::contains);
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.vaadin.flow.internal.UsageStatistics;
import com.vaadin.flow.internal.menu.MenuRegistry;
import com.vaadin.flow.router.Layout;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteConfiguration;
import com.vaadin.flow.router.RouteData;
import com.vaadin.flow.router.Router;
Expand Down Expand Up @@ -76,6 +77,18 @@ public static class TestView extends Component {

}

@Route("test")
@Tag("div")
public static class AnnotatedTestView extends Component {

}

@Route(value = "flow", autoLayout = false)
@Tag("div")
public static class OptOutAutoLayoutTestView extends Component {

}

private class TestSessionDestroyListener implements SessionDestroyListener {

int callCount = 0;
Expand Down Expand Up @@ -160,8 +173,8 @@ class AutoLayout extends Component implements RouterLayout {
VaadinServiceInitListener initListener = event -> {
ApplicationRouteRegistry.getInstance(event.getSource().getContext())
.setLayout(AutoLayout.class);
RouteConfiguration.forApplicationScope().setRoute("test",
TestView.class, AutoLayout.class);
RouteConfiguration.forApplicationScope()
.setAnnotatedRoute(AnnotatedTestView.class);
};
MockVaadinServletService service = new MockVaadinServletService();
runWithClientRoute("test", false, service, () -> {
Expand All @@ -188,8 +201,6 @@ class AutoLayout extends Component implements RouterLayout {
VaadinServiceInitListener initListener = event -> {
ApplicationRouteRegistry.getInstance(event.getSource().getContext())
.setLayout(AutoLayout.class);
RouteConfiguration.forApplicationScope().setRoute("flow",
TestView.class);
};
MockVaadinServletService service = new MockVaadinServletService();
runWithClientRoute("test", true, service, () -> {
Expand All @@ -207,6 +218,40 @@ class AutoLayout extends Component implements RouterLayout {
});
}

@Test
public void should_reported_auto_layout_routes_not_used() {
UsageStatistics.resetEntries();
@Route(value = "not-in-auto-layout")
@Tag("div")
class LayoutTestView extends Component {
}
@Layout("layout")
class AutoLayout extends Component implements RouterLayout {
}
VaadinServiceInitListener initListener = event -> {
ApplicationRouteRegistry.getInstance(event.getSource().getContext())
.setLayout(AutoLayout.class);
RouteConfiguration.forApplicationScope()
.setAnnotatedRoute(OptOutAutoLayoutTestView.class);
RouteConfiguration.forApplicationScope()
.setAnnotatedRoute(LayoutTestView.class);
};
MockVaadinServletService service = new MockVaadinServletService();
runWithClientRoute("test", false, service, () -> {
service.init(new MockInstantiator(initListener));

Assert.assertTrue(UsageStatistics.getEntries()
.anyMatch(e -> Constants.STATISTIC_HAS_AUTO_LAYOUT
.equals(e.getName())));
Assert.assertFalse(UsageStatistics.getEntries().anyMatch(
e -> Constants.STATISTIC_HAS_SERVER_ROUTE_WITH_AUTO_LAYOUT
.equals(e.getName())));
Assert.assertFalse(UsageStatistics.getEntries().anyMatch(
e -> Constants.STATISTIC_HAS_CLIENT_ROUTE_WITH_AUTO_LAYOUT
.equals(e.getName())));
});
}

private void runWithClientRoute(String route, boolean flowLayout,
MockVaadinServletService service, Runnable runnable) {
try (MockedStatic<MenuRegistry> menuRegistry = Mockito
Expand Down

0 comments on commit 0e9111f

Please sign in to comment.