private static LithoView dispatchVisibilityEvent( ComponentContext context, EventHandler eventHandler, Object eventInstance, Component component) { LithoView lithoView = new LithoView(context); FrameLayout parent = new FrameLayout(context.getAndroidContext()); parent.addView(lithoView); mountComponent( context, lithoView, component, true, 100, 100); lithoView.performIncrementalMount(); try { Whitebox.invokeMethod( component.getEventDispatcher(), "dispatchOnEvent", eventHandler, eventInstance); } catch (Exception e) { throw new RuntimeException(e); } return lithoView; }
@Test public void testComponentOnSyntheticEventClick() { final Class activityClassToLaunch = PlaygroundActivity.class; final Component component = DemoListItemComponent.create(mComponentsRule.getContext()) .model(new DemoListActivity.DemoListDataModel("My Component", activityClassToLaunch)) .currentIndices(null) .build(); // Here, we make use of Litho's internal event infrastructure and manually dispatch the event. final ComponentContext componentContext = withComponentScope(mComponentsRule.getContext(), component); component .getEventDispatcher() .dispatchOnEvent(DemoListItemComponent.onClick(componentContext), new ClickEvent()); final Intent nextIntent = shadowOf(mComponentsRule.getContext().getAndroidContext()) .getShadowApplication() .getNextStartedActivity(); assertThat(nextIntent.getComponent().getClassName()).isSameAs(activityClassToLaunch.getName()); } }