From ce517689ff91178489ef45b40196b2e1c9f7caef Mon Sep 17 00:00:00 2001 From: Sidharth Guglani Date: Wed, 10 Jul 2019 08:43:41 -0700 Subject: [PATCH] add tests for layoutPassStart and layoutPassEnd Summary: Add tests for layout pass start and end , same as what we had in InstrumentationTests for marker based approach Reviewed By: davidaurelio Differential Revision: D16073121 fbshipit-source-id: 838f01cb2a41d2d2764ba7ce2f317147f737b287 --- tests/EventsTest.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/tests/EventsTest.cpp b/tests/EventsTest.cpp index ef0c46c9..e10efa07 100644 --- a/tests/EventsTest.cpp +++ b/tests/EventsTest.cpp @@ -134,6 +134,120 @@ TEST_F(EventTest, layout_events) { YGNodeFreeRecursive(root); } +TEST_F(EventTest, layout_events_single_node) { + auto root = YGNodeNew(); + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + ASSERT_EQ(events[1].node, root); + ASSERT_EQ(events[1].type, Event::LayoutPassStart); + + ASSERT_EQ(events[2].node, root); + ASSERT_EQ(events[2].type, Event::NodeLayout); + + ASSERT_EQ(events[3].node, root); + ASSERT_EQ(events[3].type, Event::LayoutPassEnd); + + YGMarkerLayoutData layoutData = + events[3].eventTestData().layoutData; + + ASSERT_EQ(layoutData.layouts, 1); + ASSERT_EQ(layoutData.measures, 0); + ASSERT_EQ(layoutData.maxMeasureCache, 1); +} + +TEST_F(EventTest, layout_events_counts_multi_node_layout) { + auto root = YGNodeNew(); + auto childA = YGNodeNew(); + YGNodeInsertChild(root, childA, 0); + auto childB = YGNodeNew(); + YGNodeInsertChild(root, childB, 1); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + ASSERT_EQ(events[3].node, root); + ASSERT_EQ(events[3].type, Event::LayoutPassStart); + + ASSERT_EQ(events[11].node, root); + ASSERT_EQ(events[11].type, Event::LayoutPassEnd); + + YGMarkerLayoutData layoutData = + events[11].eventTestData().layoutData; + + ASSERT_EQ(layoutData.layouts, 3); + ASSERT_EQ(layoutData.measures, 4); + ASSERT_EQ(layoutData.maxMeasureCache, 3); +} + +TEST_F(EventTest, layout_events_counts_cache_hits_single_node_layout) { + auto root = YGNodeNew(); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + ASSERT_EQ(events[4].node, root); + ASSERT_EQ(events[4].type, Event::LayoutPassStart); + + ASSERT_EQ(events[6].node, root); + ASSERT_EQ(events[6].type, Event::LayoutPassEnd); + + YGMarkerLayoutData layoutData = + events[6].eventTestData().layoutData; + + ASSERT_EQ(layoutData.layouts, 0); + ASSERT_EQ(layoutData.measures, 0); + ASSERT_EQ(layoutData.cachedLayouts, 1); + ASSERT_EQ(layoutData.cachedMeasures, 0); +} + +TEST_F(EventTest, layout_events_counts_cache_hits_multi_node_layout) { + auto root = YGNodeNew(); + auto childA = YGNodeNew(); + YGNodeInsertChild(root, childA, 0); + auto childB = YGNodeNew(); + YGNodeInsertChild(root, childB, 1); + + YGNodeCalculateLayout(root, 987, 654, YGDirectionLTR); + YGNodeCalculateLayout(root, 123, 456, YGDirectionLTR); + + YGNodeCalculateLayout(root, 987, 654, YGDirectionLTR); + + ASSERT_EQ(lastEvent().node, root); + ASSERT_EQ(lastEvent().type, Event::LayoutPassEnd); + + YGMarkerLayoutData layoutData = + lastEvent().eventTestData().layoutData; + + ASSERT_EQ(layoutData.layouts, 3); + ASSERT_EQ(layoutData.measures, 0); + ASSERT_EQ(layoutData.maxMeasureCache, 5); + ASSERT_EQ(layoutData.cachedLayouts, 0); + ASSERT_EQ(layoutData.cachedMeasures, 4); +} + +TEST_F(EventTest, layout_events_has_max_measure_cache) { + auto root = YGNodeNew(); + auto a = YGNodeNew(); + YGNodeInsertChild(root, a, 0); + auto b = YGNodeNew(); + YGNodeInsertChild(root, b, 1); + YGNodeStyleSetFlexBasis(a, 10.0f); + + for (auto s : {20, 30, 40}) { + YGNodeCalculateLayout(root, s, s, YGDirectionLTR); + } + + ASSERT_EQ(lastEvent().node, root); + ASSERT_EQ(lastEvent().type, Event::LayoutPassEnd); + + YGMarkerLayoutData layoutData = + lastEvent().eventTestData().layoutData; + + ASSERT_EQ(layoutData.layouts, 3); + ASSERT_EQ(layoutData.measures, 3); + ASSERT_EQ(layoutData.maxMeasureCache, 7); +} + namespace { template @@ -182,6 +296,7 @@ void EventTest::listen(const YGNode& node, Event::Type type, Event::Data data) { node, data, {eventData.layoutContext, *eventData.layoutData})); break; } + case Event::MeasureCallbackStart: case Event::MeasureCallbackEnd: break;