Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow ComponentsLogger implementations to return null for event types…
… they don't care about

Summary: Quality of life improvement for when people implement loggers that don't care about certain events.

Reviewed By: gmoeck

Differential Revision: D15915909

fbshipit-source-id: ce500e3a75f5412aea9a1ea7808c2635cd919a85
  • Loading branch information
astreet authored and facebook-github-bot committed Jun 20, 2019
1 parent 867119b commit 4075eb7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
Expand Up @@ -33,7 +33,11 @@ enum LogLevel {
FATAL
}

/** Create a new performance event with the given event id and start counting the time. */
/**
* Create a new performance event with the given event id and start counting the time. If the
* logger doesn't care about this event id, it may return null.
*/
@Nullable
PerfEvent newPerformanceEvent(ComponentContext c, @FrameworkLogEvents.LogEventId int eventId);

/** Write a {@link PerfEvent} to storage. This also marks the end of the event. */
Expand Down
Expand Up @@ -37,7 +37,11 @@ private LogTreePopulator() {}
@Nullable
@CheckReturnValue
public static PerfEvent populatePerfEventFromLogger(
ComponentContext c, ComponentsLogger logger, PerfEvent perfEvent) {
ComponentContext c, ComponentsLogger logger, @Nullable PerfEvent perfEvent) {
if (perfEvent == null) {
return null;
}

final String logTag = c.getLogTag();
if (logTag == null) {
logger.cancelPerfEvent(perfEvent);
Expand Down
Expand Up @@ -59,6 +59,7 @@
import android.graphics.Rect;
import android.util.SparseArray;
import android.view.accessibility.AccessibilityManager;
import androidx.annotation.Nullable;
import com.facebook.litho.config.ComponentsConfiguration;
import com.facebook.litho.testing.TestComponent;
import com.facebook.litho.testing.TestDrawableComponent;
Expand All @@ -67,6 +68,7 @@
import com.facebook.litho.testing.TestSizeDependentComponent;
import com.facebook.litho.testing.TestViewComponent;
import com.facebook.litho.testing.Whitebox;
import com.facebook.litho.testing.logging.TestComponentsLogger;
import com.facebook.litho.testing.testrunner.ComponentsTestRunner;
import com.facebook.litho.testing.util.InlineLayoutSpec;
import com.facebook.litho.widget.Text;
Expand Down Expand Up @@ -2765,6 +2767,36 @@ protected Component onCreateLayout(final ComponentContext c) {
ComponentsConfiguration.createPhantomLayoutOutputsForTransitions = false;
}

@Test
public void testComponentsLoggerCanReturnNullPerfEventsDuringLayout() {
final Component component =
new InlineLayoutSpec() {
@Override
protected Component onCreateLayout(final ComponentContext c) {
return create(c).child(TestDrawableComponent.create(c)).wrapInView().build();
}
};

final ComponentsLogger logger =
new TestComponentsLogger() {
@Override
public @Nullable PerfEvent newPerformanceEvent(ComponentContext c, int eventId) {
return null;
}
};

final LayoutState layoutState =
LayoutState.calculate(
new ComponentContext(application, "test", logger),
component,
-1,
makeSizeSpec(100, EXACTLY),
makeSizeSpec(100, EXACTLY),
LayoutState.CalculateLayoutSource.TEST);

assertThat(layoutState.getMountableOutputCount()).isEqualTo(2);
}

private void enableAccessibility() {
final ShadowAccessibilityManager manager = Shadows.shadowOf(
(AccessibilityManager)
Expand Down
Expand Up @@ -150,5 +150,19 @@ public Map<String, String> getExtraAnnotations(TreeProps treeProps) {
assertThat(res).isEqualTo("my_key:1337:other_key:value:");
}

@Test
public void testSkipNullPerfEvent() {
final ComponentsLogger logger =
new TestComponentsLogger() {
@Nullable
@Override
public Map<String, String> getExtraAnnotations(TreeProps treeProps) {
return null;
}
};

assertThat(LogTreePopulator.populatePerfEventFromLogger(mContext, logger, null)).isNull();
}

private static class MyKey {}
}

0 comments on commit 4075eb7

Please sign in to comment.