static void dispatchOnClick(EventHandler<ClickEvent> clickHandler, View view) { assertMainThread(); if (sClickEvent == null) { sClickEvent = new ClickEvent(); } sClickEvent.view = view; final EventDispatcher eventDispatcher = clickHandler.mHasEventDispatcher.getEventDispatcher(); eventDispatcher.dispatchOnEvent(clickHandler, sClickEvent); sClickEvent.view = null; }
@Test public void testClickHandler() throws Exception { Section s = mTester.prepare( VerySimpleGroupSection.create(mTester.getContext()).numberOfDummy(4).build()); SectionsTestHelper.dispatchEvent( s, VerySimpleGroupSection.onImageClick(mTester.getScopedContext(s)), new ClickEvent()); VerySimpleGroupSection.VerySimpleGroupSectionStateContainer stateContainer = mTester.getStateContainer(s); assertThat(stateContainer.extra).isEqualTo(3); }
@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()); } }