Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for A11y headers in Litho. #573

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions litho-core/src/main/java/com/facebook/litho/CommonProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public interface CommonProps extends CommonPropsCopyable, LayoutProps {

void selected(boolean isSelected);

void accessibilityHeading(boolean isHeading);

void visibleHeightRatio(float visibleHeightRatio);

void visibleWidthRatio(float visibleWidthRatio);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ public void selected(boolean isSelected) {
getOrCreateNodeInfo().setSelected(isSelected);
}

@Override
public void accessibilityHeading(boolean isHeading) {
getOrCreateNodeInfo().setAccessibilityHeading(isHeading);
}

@Override
public void visibleHeightRatio(float visibleHeightRatio) {
getOrCreateOtherProps().visibleHeightRatio(visibleHeightRatio);
Expand Down
5 changes: 5 additions & 0 deletions litho-core/src/main/java/com/facebook/litho/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,11 @@ public T importantForAccessibility(int importantForAccessibility) {
return getThis();
}

public T accessibilityHeading(boolean isHeading) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add javadocs here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

mComponent.getOrCreateCommonProps().accessibilityHeading(isHeading);
return getThis();
}

/**
* If true, component duplicates its drawable state (focused, pressed, etc.) from the direct
* parent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ public void onInitializeAccessibilityNodeInfo(
node.setClassName(AccessibilityRole.NONE);
}
}

if (mNodeInfo != null
&& mNodeInfo.getAccessibilityHeadingState() != NodeInfo.ACCESSIBILITY_HEADING_UNSET) {
node.setHeading(
mNodeInfo.getAccessibilityHeadingState() == NodeInfo.ACCESSIBILITY_HEADING_SET_TRUE);
}
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions litho-core/src/main/java/com/facebook/litho/DefaultNodeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class DefaultNodeInfo implements NodeInfo {
private @ClickableState int mClickableState = CLICKABLE_UNSET;
private @EnabledState int mEnabledState = ENABLED_UNSET;
private @SelectedState int mSelectedState = SELECTED_UNSET;
private @AccessibilityHeadingState int mAccessibilityHeadingState = ACCESSIBILITY_HEADING_UNSET;

private int mPrivateFlags;

Expand Down Expand Up @@ -476,6 +477,20 @@ public void setSelected(boolean isSelected) {
return mSelectedState;
}

@Override
public void setAccessibilityHeading(boolean isHeading) {
if (isHeading) {
mAccessibilityHeadingState = ACCESSIBILITY_HEADING_SET_TRUE;
} else {
mAccessibilityHeadingState = ACCESSIBILITY_HEADING_SET_FALSE;
}
}

@Override
public int getAccessibilityHeadingState() {
return mAccessibilityHeadingState;
}

@Override
public float getScale() {
return mScale;
Expand Down Expand Up @@ -648,6 +663,10 @@ public void copyInto(NodeInfo target) {
if (getSelectedState() != SELECTED_UNSET) {
target.setSelected(getSelectedState() == SELECTED_SET_TRUE);
}
if (getAccessibilityHeadingState() != ACCESSIBILITY_HEADING_UNSET) {
target.setAccessibilityHeading(
getAccessibilityHeadingState() == ACCESSIBILITY_HEADING_SET_TRUE);
}
if ((mPrivateFlags & PFLAG_SCALE_IS_SET) != 0) {
target.setScale(mScale);
}
Expand Down
17 changes: 17 additions & 0 deletions litho-core/src/main/java/com/facebook/litho/NodeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ public interface NodeInfo {
@Retention(RetentionPolicy.SOURCE)
@interface SelectedState {}

static final int ACCESSIBILITY_HEADING_UNSET = 0;
static final int ACCESSIBILITY_HEADING_SET_TRUE = 1;
static final int ACCESSIBILITY_HEADING_SET_FALSE = 2;

@IntDef({
ACCESSIBILITY_HEADING_UNSET,
ACCESSIBILITY_HEADING_SET_TRUE,
ACCESSIBILITY_HEADING_SET_FALSE
})
@Retention(RetentionPolicy.SOURCE)
@interface AccessibilityHeadingState {}

void setContentDescription(@Nullable CharSequence contentDescription);

@Nullable
Expand Down Expand Up @@ -220,6 +232,11 @@ void setSendAccessibilityEventUncheckedHandler(
@SelectedState
int getSelectedState();

void setAccessibilityHeading(boolean isHeading);

@AccessibilityHeadingState
int getAccessibilityHeadingState();

float getScale();

void setScale(float scale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public void testSetPropsAndBuild() {
mCommonProps.enabled(false);
mCommonProps.visibleHeightRatio(55);
mCommonProps.visibleWidthRatio(56);
mCommonProps.accessibilityHeading(false);

final EventHandler<VisibleEvent> visibleHandler = mock(EventHandler.class);
final EventHandler<FocusedVisibleEvent> focusedHandler = mock(EventHandler.class);
Expand Down Expand Up @@ -285,6 +286,7 @@ public void testSetPropsAndBuild() {
verify(mNodeInfo).setEnabled(false);
verify(mNode).visibleHeightRatio(55);
verify(mNode).visibleWidthRatio(56);
verify(mNodeInfo).setAccessibilityHeading(false);

verify(mNode).visibleHandler(visibleHandler);
verify(mNode).focusedHandler(focusedHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ protected Component onCreateLayout(ComponentContext c) {
.focusable(true)
.selected(false)
.enabled(false)
.accessibilityHeading(false)
.visibleHeightRatio(55)
.visibleWidthRatio(56)
.visibleHandler(visibleHandler)
Expand Down Expand Up @@ -561,6 +562,7 @@ protected Component onCreateLayout(ComponentContext c) {
verify(nodeInfo).setEnabled(false);
verify(node).visibleHeightRatio(55);
verify(node).visibleWidthRatio(56);
verify(nodeInfo).setAccessibilityHeading(false);

verify(node).visibleHandler(visibleHandler);
verify(node).focusedHandler(focusedHandler);
Expand Down
27 changes: 27 additions & 0 deletions litho-it/src/test/java/com/facebook/litho/NodeInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.facebook.litho;

import static com.facebook.litho.NodeInfo.ACCESSIBILITY_HEADING_SET_FALSE;
import static com.facebook.litho.NodeInfo.ACCESSIBILITY_HEADING_SET_TRUE;
import static com.facebook.litho.NodeInfo.ACCESSIBILITY_HEADING_UNSET;
import static com.facebook.litho.NodeInfo.ENABLED_SET_FALSE;
import static com.facebook.litho.NodeInfo.ENABLED_SET_TRUE;
import static com.facebook.litho.NodeInfo.ENABLED_UNSET;
Expand Down Expand Up @@ -91,6 +94,30 @@ public void testInterceptTouchHandler() {
assertThat(interceptTouchHandler).isSameAs(mUpdatedNodeInfo.getInterceptTouchHandler());
}

@Test
public void testAccessibilityHeadingTrue() {
assertThat(mNodeInfo.getAccessibilityHeadingState()).isEqualTo(ACCESSIBILITY_HEADING_UNSET);
mNodeInfo.setAccessibilityHeading(true);

assertThat(mNodeInfo.getAccessibilityHeadingState()).isEqualTo(ACCESSIBILITY_HEADING_SET_TRUE);

mNodeInfo.copyInto(mUpdatedNodeInfo);
assertThat(mUpdatedNodeInfo.getAccessibilityHeadingState())
.isEqualTo(ACCESSIBILITY_HEADING_SET_TRUE);
}

@Test
public void testAccessibilityHeadingFalse() {
assertThat(mNodeInfo.getAccessibilityHeadingState()).isEqualTo(ACCESSIBILITY_HEADING_UNSET);
mNodeInfo.setAccessibilityHeading(false);

assertThat(mNodeInfo.getAccessibilityHeadingState()).isEqualTo(ACCESSIBILITY_HEADING_SET_FALSE);

mNodeInfo.copyInto(mUpdatedNodeInfo);
assertThat(mUpdatedNodeInfo.getAccessibilityHeadingState())
.isEqualTo(ACCESSIBILITY_HEADING_SET_FALSE);
}

@Test
public void testAccessibilityRole() {
@AccessibilityRole.AccessibilityRoleType final String role = AccessibilityRole.BUTTON;
Expand Down