mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Add unit tests for LayoutableShadowNode::getRelativeLayoutMetrics
Summary: Adds a bare minimum test that verifies correct behaviour of `getRelativeLayoutMetrics`. Changelog: [internal] Reviewed By: shergin Differential Revision: D19449128 fbshipit-source-id: afde997a770921d580575eb0cdd04fce6252cb5a
This commit is contained in:
committed by
Facebook Github Bot
parent
b3af384a13
commit
291b45b686
@@ -85,6 +85,7 @@ fb_xplat_cxx_test(
|
||||
deps = [
|
||||
"fbsource//xplat/folly:molly",
|
||||
"fbsource//xplat/third-party/gmock:gtest",
|
||||
react_native_xplat_target("fabric/components/view:view"),
|
||||
":core",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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 "TestComponent.h"
|
||||
|
||||
using namespace facebook::react;
|
||||
|
||||
class LayoutableShadowNodeTest : public ::testing::Test {
|
||||
protected:
|
||||
LayoutableShadowNodeTest()
|
||||
: eventDispatcher_(std::shared_ptr<EventDispatcher const>()),
|
||||
componentDescriptor_(TestComponentDescriptor({eventDispatcher_})) {
|
||||
auto familyA = std::make_shared<ShadowNodeFamily>(
|
||||
ShadowNodeFamilyFragment{
|
||||
/* .tag = */ 9,
|
||||
/* .surfaceId = */ 1,
|
||||
/* .eventEmitter = */ nullptr,
|
||||
},
|
||||
componentDescriptor_);
|
||||
|
||||
nodeA_ = std::make_shared<TestShadowNode>(
|
||||
ShadowNodeFragment{
|
||||
/* .props = */ std::make_shared<const TestProps>(),
|
||||
/* .children = */ ShadowNode::emptySharedShadowNodeSharedList(),
|
||||
},
|
||||
familyA,
|
||||
ShadowNodeTraits{});
|
||||
|
||||
auto familyB = std::make_shared<ShadowNodeFamily>(
|
||||
ShadowNodeFamilyFragment{
|
||||
/* .tag = */ 10,
|
||||
/* .surfaceId = */ 1,
|
||||
/* .eventEmitter = */ nullptr,
|
||||
},
|
||||
componentDescriptor_);
|
||||
|
||||
nodeB_ = std::make_shared<TestShadowNode>(
|
||||
ShadowNodeFragment{
|
||||
/* .props = */ std::make_shared<const TestProps>(),
|
||||
/* .children = */ ShadowNode::emptySharedShadowNodeSharedList(),
|
||||
},
|
||||
familyB,
|
||||
ShadowNodeTraits{});
|
||||
|
||||
nodeA_->appendChild(nodeB_);
|
||||
}
|
||||
|
||||
std::shared_ptr<EventDispatcher const> eventDispatcher_;
|
||||
std::shared_ptr<TestShadowNode> nodeA_;
|
||||
std::shared_ptr<TestShadowNode> nodeB_;
|
||||
TestComponentDescriptor componentDescriptor_;
|
||||
};
|
||||
|
||||
TEST_F(LayoutableShadowNodeTest, relativeLayourMetrics) {
|
||||
auto layoutMetrics = EmptyLayoutMetrics;
|
||||
layoutMetrics.frame.origin = {10, 20};
|
||||
layoutMetrics.frame.size = {100, 200};
|
||||
nodeA_->setLayoutMetrics(layoutMetrics);
|
||||
nodeB_->setLayoutMetrics(layoutMetrics);
|
||||
|
||||
auto relativeLayoutMetrics = nodeB_->getRelativeLayoutMetrics(*nodeA_, {});
|
||||
|
||||
// A is a parent to B, A has origin {10, 10}, B has origin {10, 10}.
|
||||
// B's relative origin to A should be {10, 10}.
|
||||
// D19447900 has more about the issue.
|
||||
EXPECT_EQ(relativeLayoutMetrics.frame.origin.x, 10);
|
||||
EXPECT_EQ(relativeLayoutMetrics.frame.origin.y, 20);
|
||||
}
|
||||
|
||||
TEST_F(LayoutableShadowNodeTest, relativeLayourMetricsOnClonedNode) {
|
||||
// B is cloned and mutated.
|
||||
auto nodeBRevision2 = std::static_pointer_cast<TestShadowNode>(
|
||||
componentDescriptor_.cloneShadowNode(*nodeB_, ShadowNodeFragment{}));
|
||||
auto layoutMetrics = EmptyLayoutMetrics;
|
||||
layoutMetrics.frame.size = {500, 600};
|
||||
nodeBRevision2->setLayoutMetrics(layoutMetrics);
|
||||
nodeA_->replaceChild(*nodeB_, nodeBRevision2);
|
||||
|
||||
// Even if we ask old ShadowNode for its relative layoutMetrics, it needs to
|
||||
// return correct, new layoutMetrics. D19433873 has more about the issue.
|
||||
auto newRelativeLayoutMetrics = nodeB_->getRelativeLayoutMetrics(*nodeA_, {});
|
||||
EXPECT_EQ(newRelativeLayoutMetrics.frame.size.width, 500);
|
||||
EXPECT_EQ(newRelativeLayoutMetrics.frame.size.height, 600);
|
||||
}
|
||||
@@ -10,8 +10,9 @@
|
||||
#include <memory>
|
||||
|
||||
#include <folly/dynamic.h>
|
||||
#include <react/components/view/ConcreteViewShadowNode.h>
|
||||
#include <react/components/view/ViewProps.h>
|
||||
#include <react/core/ConcreteComponentDescriptor.h>
|
||||
#include <react/core/ConcreteShadowNode.h>
|
||||
#include <react/core/LocalData.h>
|
||||
#include <react/core/RawProps.h>
|
||||
#include <react/core/ShadowNode.h>
|
||||
@@ -39,9 +40,12 @@ class TestLocalData : public LocalData {
|
||||
|
||||
static const char TestComponentName[] = "Test";
|
||||
|
||||
class TestProps : public Props {
|
||||
class TestProps : public ViewProps {
|
||||
public:
|
||||
using Props::Props;
|
||||
using ViewProps::ViewProps;
|
||||
|
||||
TestProps(const TestProps &sourceProps, const RawProps &rawProps)
|
||||
: ViewProps(sourceProps, rawProps) {}
|
||||
};
|
||||
|
||||
using SharedTestProps = std::shared_ptr<const TestProps>;
|
||||
@@ -50,9 +54,14 @@ class TestShadowNode;
|
||||
|
||||
using SharedTestShadowNode = std::shared_ptr<const TestShadowNode>;
|
||||
|
||||
class TestShadowNode : public ConcreteShadowNode<TestComponentName, TestProps> {
|
||||
class TestShadowNode
|
||||
: public ConcreteViewShadowNode<TestComponentName, TestProps> {
|
||||
public:
|
||||
using ConcreteShadowNode::ConcreteShadowNode;
|
||||
using ConcreteViewShadowNode::ConcreteViewShadowNode;
|
||||
|
||||
bool setLayoutMetrics(LayoutMetrics layoutMetrics) {
|
||||
return YogaLayoutableShadowNode::setLayoutMetrics(layoutMetrics);
|
||||
}
|
||||
};
|
||||
|
||||
class TestComponentDescriptor
|
||||
|
||||
Reference in New Issue
Block a user