Call measure and baseline fns within YGNode

Summary:
@public

Stricter encapsulation of baseline and measure callbacks withing `YGNode`.

Instead of invoking these callbacks directly (`node->getBaseline()(...)`), they are invoked via methods on `YGNode` (`node->baseline(...)`).

This change will allow us to add the concept of a *Layout Context,* where measure and baseline functions will be able to receive an additional `void *` argument if configured accordingly. This API will be used internally for Yoga’s JNI bindings, to avoid storing a weak JNI reference for each node, and avoid reference table overflows.

Changed API:

- `YGNodeGetMeasureFunc()` -> `YGNodeHasMeasureFunc()`
- `YGNodeGetBaselineFunc()` -> `YGNodeHasBaselineFunc()`
- `YGNode::getMeasure()` -> `YGNode::hasMeasureFunc()` + `YGNode::measure()`
- `YGNpde::getBaseline()` -> `YGNode::hasBaselineFunc()` + `YGNode::baseline()`

Reviewed By: SidharthGuglani

Differential Revision: D14099550

fbshipit-source-id: 2653ab36acc252a9747986bc88d21dac22d8c91b
This commit is contained in:
David Aurelio
2019-02-19 09:54:45 -08:00
committed by Facebook Github Bot
parent d5ad51bccc
commit f86c74ce7e
9 changed files with 154 additions and 53 deletions

View File

@@ -0,0 +1,81 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
#include <gtest/gtest.h>
#include <yoga/YGNode.h>
#include <ostream>
inline bool operator==(const YGSize& lhs, const YGSize& rhs) {
return lhs.width == rhs.width && lhs.height == rhs.height;
}
void PrintTo(const YGSize&, std::ostream*);
TEST(YGNode, hasMeasureFunc_initial) {
auto n = YGNode{};
ASSERT_FALSE(n.hasMeasureFunc());
}
TEST(YGNode, hasMeasureFunc_with_measure_fn) {
auto n = YGNode{};
n.setMeasureFunc([](YGNode*, float, YGMeasureMode, float, YGMeasureMode) {
return YGSize{};
});
ASSERT_TRUE(n.hasMeasureFunc());
}
TEST(YGNode, measure_with_measure_fn) {
auto n = YGNode{};
n.setMeasureFunc(
[](YGNode*, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
return YGSize{w * wm, h / hm};
});
ASSERT_EQ(
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost),
(YGSize{23, 12}));
}
TEST(YGNode, hasMeasureFunc_after_unset) {
auto n = YGNode{};
n.setMeasureFunc([](YGNode*, float, YGMeasureMode, float, YGMeasureMode) {
return YGSize{};
});
n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
}
TEST(YGNode, hasBaselineFunc_initial) {
auto n = YGNode{};
ASSERT_FALSE(n.hasBaselineFunc());
}
TEST(YGNode, hasBaselineFunc_with_baseline_fn) {
auto n = YGNode{};
n.setBaseLineFunc([](YGNode*, float, float) { return 0.0f; });
ASSERT_TRUE(n.hasBaselineFunc());
}
TEST(YGNode, baseline_with_baseline_fn) {
auto n = YGNode{};
n.setBaseLineFunc([](YGNode*, float w, float h) { return w + h; });
ASSERT_EQ(n.baseline(1.25f, 2.5f), 3.75f);
}
TEST(YGNode, hasBaselineFunc_after_unset) {
auto n = YGNode{};
n.setBaseLineFunc([](YGNode*, float, float) { return 0.0f; });
n.setBaseLineFunc(nullptr);
ASSERT_FALSE(n.hasBaselineFunc());
}
void PrintTo(const YGSize& size, std::ostream* os) {
*os << "YGSize{" << size.width << ", " << size.height << "}";
}