|
25 | 25 | import java.util.concurrent.atomic.AtomicInteger; |
26 | 26 | import java.util.stream.Collectors; |
27 | 27 |
|
| 28 | +import net.bytebuddy.ByteBuddy; |
| 29 | +import net.bytebuddy.description.modifier.SyntheticState; |
| 30 | +import net.bytebuddy.description.modifier.Visibility; |
| 31 | +import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; |
28 | 32 | import net.jcip.annotations.NotThreadSafe; |
29 | 33 | import org.junit.Assert; |
30 | 34 | import org.junit.Before; |
|
43 | 47 | import com.vaadin.flow.component.page.Page; |
44 | 48 | import com.vaadin.flow.component.page.PendingJavaScriptResult; |
45 | 49 | import com.vaadin.flow.dom.Element; |
| 50 | +import com.vaadin.flow.internal.ReflectTools; |
46 | 51 | import com.vaadin.flow.router.AfterNavigationEvent; |
47 | 52 | import com.vaadin.flow.router.AfterNavigationObserver; |
48 | 53 | import com.vaadin.flow.router.BeforeEnterEvent; |
@@ -131,6 +136,13 @@ private static class PreservedNestedView extends Text { |
131 | 136 | } |
132 | 137 | } |
133 | 138 |
|
| 139 | + @Route(value = "proxyable") |
| 140 | + public static class ProxyableView extends Text { |
| 141 | + public ProxyableView() { |
| 142 | + super(""); |
| 143 | + } |
| 144 | + } |
| 145 | + |
134 | 146 | private Router router; |
135 | 147 |
|
136 | 148 | @Rule |
@@ -217,6 +229,51 @@ public <T extends HasElement> T createRouteTarget( |
217 | 229 | UI.setCurrent(null); |
218 | 230 | } |
219 | 231 |
|
| 232 | + @Test |
| 233 | + public void getRouteTarget_supportsProxyClasses() { |
| 234 | + try { |
| 235 | + Class<? extends ProxyableView> routeProxyClass = new ByteBuddy() |
| 236 | + .subclass(ProxyableView.class) |
| 237 | + .modifiers(Visibility.PUBLIC, SyntheticState.SYNTHETIC) |
| 238 | + .make().load(ProxyableView.class.getClassLoader(), |
| 239 | + ClassLoadingStrategy.Default.WRAPPER) |
| 240 | + .getLoaded(); |
| 241 | + |
| 242 | + AtomicInteger routeCreationCounter = new AtomicInteger(0); |
| 243 | + MockVaadinServletService service = new MockVaadinServletService(); |
| 244 | + service.init(new MockInstantiator() { |
| 245 | + @Override |
| 246 | + public <T extends HasElement> T createRouteTarget( |
| 247 | + Class<T> routeTargetType, NavigationEvent event) { |
| 248 | + Assert.assertEquals(ProxyableView.class, routeTargetType); |
| 249 | + routeCreationCounter.incrementAndGet(); |
| 250 | + return (T) ReflectTools.createInstance(routeProxyClass); |
| 251 | + } |
| 252 | + }); |
| 253 | + MockUI ui = new MockUI(new AlwaysLockedVaadinSession(service)); |
| 254 | + |
| 255 | + NavigationEvent event = new NavigationEvent( |
| 256 | + new Router(new TestRouteRegistry()), new Location("child"), |
| 257 | + ui, NavigationTrigger.UI_NAVIGATE); |
| 258 | + NavigationStateRenderer renderer = new NavigationStateRenderer( |
| 259 | + navigationStateFromTarget(ProxyableView.class)); |
| 260 | + renderer.handle(event); |
| 261 | + HasElement view = ui.getInternals().getActiveRouterTargetsChain() |
| 262 | + .get(0); |
| 263 | + |
| 264 | + Component routeTarget = renderer.getRouteTarget(ProxyableView.class, |
| 265 | + event); |
| 266 | + |
| 267 | + // Getting route target should not create a new instance |
| 268 | + Assert.assertEquals( |
| 269 | + "Only one view instance should have been created", 1, |
| 270 | + routeCreationCounter.get()); |
| 271 | + Assert.assertSame(view, routeTarget); |
| 272 | + } finally { |
| 273 | + UI.setCurrent(null); |
| 274 | + } |
| 275 | + } |
| 276 | + |
220 | 277 | @Route("parent") |
221 | 278 | private static class RouteParentLayout extends Component |
222 | 279 | implements RouterLayout { |
|
0 commit comments