回退 'Pull Request !42038 : 修复rebuild的时候上下文不明确导致的挂载失败'

This commit is contained in:
wangchensu 2024-10-12 10:19:42 +00:00 committed by Gitee
parent a420d53b25
commit e4d76d20d9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 18 additions and 55 deletions

View File

@ -163,9 +163,10 @@ void JSNodeContainer::SetNodeController(const JSRef<JSObject>& object, JsiExecut
CHECK_NULL_VOID(nodeContainerModelInstance);
auto jsFunc = JSRef<JSFunc>::Cast(jsMakeNodeFunc);
auto containerId = Container::CurrentId();
RefPtr<JsFunction> jsMake = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(object), jsFunc);
nodeContainerModelInstance->SetMakeFunction(
[func = std::move(jsMake), execCtx](int32_t containerId) -> RefPtr<NG::UINode> {
[func = std::move(jsMake), containerId, execCtx]() -> RefPtr<NG::UINode> {
JAVASCRIPT_EXECUTION_SCOPE(execCtx);
ContainerScope scope(containerId);
panda::Local<panda::JSValueRef> uiContext = NG::UIContextHelper::GetUIContext(execCtx.vm_, containerId);

View File

@ -26,7 +26,7 @@ public:
virtual ~NodeContainerModel() = default;
virtual void Create() = 0;
virtual void SetMakeFunction(std::function<RefPtr<NG::UINode>(int32_t)>&& makeFunc) = 0;
virtual void SetMakeFunction(std::function<RefPtr<NG::UINode>()>&& makeFunc) = 0;
virtual void SetOnResize(std::function<void(const NG::SizeF& size)>&& resizeFunc) = 0;
virtual void SetOnAppear(std::function<void()>&& onAppearCallback) = 0;
virtual void SetOnDisAppear(std::function<void()>&& onDisAppearCallback) = 0;

View File

@ -28,7 +28,7 @@ void NodeContainerModelNG::Create()
ACE_UPDATE_LAYOUT_PROPERTY(LayoutProperty, Alignment, Alignment::TOP_LEFT);
}
void NodeContainerModelNG::SetMakeFunction(std::function<RefPtr<UINode>(int32_t)>&& makeFunc)
void NodeContainerModelNG::SetMakeFunction(std::function<RefPtr<UINode>()>&& makeFunc)
{
auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
CHECK_NULL_VOID(frameNode);

View File

@ -22,7 +22,7 @@ namespace OHOS::Ace::NG {
class ACE_EXPORT NodeContainerModelNG : public NodeContainerModel {
public:
void Create() override;
void SetMakeFunction(std::function<RefPtr<UINode>(int32_t)>&& makeFunc) override;
void SetMakeFunction(std::function<RefPtr<UINode>()>&& makeFunc) override;
void SetOnResize(std::function<void(const SizeF& size)>&& resizeFunc) override;
void SetOnAppear(std::function<void()>&& onAppearCallback) override;
void SetOnDisAppear(std::function<void()>&& onDisAppearCallback) override;

View File

@ -43,11 +43,7 @@ void NodeContainerPattern::RemakeNode()
{
auto host = GetHost();
CHECK_NULL_VOID(host);
auto context = host->GetContext();
CHECK_NULL_VOID(context);
auto instanceId = context->GetInstanceId();
ContainerScope scope(instanceId);
auto newNode = FireMakeFunction(instanceId);
auto newNode = FireMakeFunction();
auto oldChild = host->GetChildAtIndex(0);
if ((!oldChild && !newNode) || (oldChild && oldChild == newNode)) {
return;

View File

@ -62,15 +62,15 @@ public:
resetFunc_();
}
void SetMakeFunction(std::function<RefPtr<UINode>(int32_t)>&& makeFunc)
void SetMakeFunction(std::function<RefPtr<UINode>()>&& makeFunc)
{
makeFunc_ = std::move(makeFunc);
}
RefPtr<UINode> FireMakeFunction(int32_t instanceId) const
RefPtr<UINode> FireMakeFunction() const
{
CHECK_NULL_RETURN(makeFunc_, nullptr);
return makeFunc_(instanceId);
return makeFunc_();
}
void SetOnResize(std::function<void(const SizeF& size)>&& resizeFunc)
@ -113,7 +113,7 @@ private:
void SetExportTextureInfoIfNeeded();
bool HandleTextureExport(bool isStop);
std::function<void()> resetFunc_;
std::function<RefPtr<UINode>(int32_t)> makeFunc_;
std::function<RefPtr<UINode>()> makeFunc_;
std::function<void(const SizeF& size)> resizeFunc_;
WeakPtr<UINode> exportTextureNode_;
uint64_t surfaceId_ = 0U;

View File

@ -41,7 +41,6 @@ using namespace testing::ext;
namespace OHOS::Ace::NG {
namespace {
const std::string CHILD_NODE = "ChildNode";
constexpr int32_t DEFAULT_INSTANCE_ID = 1000000;
} // namespace
class NodeContainerTestNg : public testing::Test {
@ -56,9 +55,6 @@ protected:
void NodeContainerTestNg::SetUpTestSuite()
{
MockPipelineContext::SetUp();
auto context = MockPipelineContext::GetCurrent();
EXPECT_NE(context, nullptr);
context->instanceId_ = DEFAULT_INSTANCE_ID;
}
void NodeContainerTestNg::TearDownTestSuite()
@ -109,10 +105,7 @@ HWTEST_F(NodeContainerTestNg, NodeContainerRemakeNode001, TestSize.Level1)
* @tc.steps: step2.Fire RemakeNode return with a normal node.
* @tc.expected: process success without crash, and does not change the child of NodeContainer.
*/
pattern->SetMakeFunction([childNode](int32_t instanceId) -> RefPtr<UINode> {
EXPECT_EQ(instanceId, DEFAULT_INSTANCE_ID);
return childNode;
});
pattern->SetMakeFunction([childNode]() -> RefPtr<UINode> { return childNode; });
pattern->RemakeNode();
ASSERT_EQ(nodeContainerNode->GetChildAtIndex(0), nullptr);
@ -139,10 +132,7 @@ HWTEST_F(NodeContainerTestNg, NodeContainerRemakeNode001, TestSize.Level1)
auto childNode_one = FrameNode::CreateFrameNode(CHILD_NODE, 1, AceType::MakeRefPtr<Pattern>());
childNode_one->SetIsRootBuilderNode(false);
childNode_one->SetIsArkTsFrameNode(true);
pattern->SetMakeFunction([childNode_one](int32_t instanceId) -> RefPtr<UINode> {
EXPECT_EQ(instanceId, DEFAULT_INSTANCE_ID);
return childNode_one;
});
pattern->SetMakeFunction([childNode_one]() -> RefPtr<UINode> { return childNode_one; });
pattern->RemakeNode();
ASSERT_EQ(nodeContainerNode->GetChildAtIndex(0)->GetId(), childNode_one->GetId());
@ -153,44 +143,21 @@ HWTEST_F(NodeContainerTestNg, NodeContainerRemakeNode001, TestSize.Level1)
auto childNode_two = FrameNode::CreateFrameNode(V2::CUSTOM_FRAME_NODE_ETS_TAG, 2, AceType::MakeRefPtr<Pattern>());
childNode_two->SetIsRootBuilderNode(false);
childNode_two->SetIsArkTsFrameNode(true);
pattern->SetMakeFunction([childNode_two](int32_t instanceId) -> RefPtr<UINode> {
EXPECT_EQ(instanceId, DEFAULT_INSTANCE_ID);
return childNode_two;
});
pattern->SetMakeFunction([childNode_two]() -> RefPtr<UINode> { return childNode_two; });
pattern->RemakeNode();
ASSERT_EQ(nodeContainerNode->GetChildAtIndex(0)->GetId(), childNode_two->GetId());
}
/**
* @tc.name: NodeContainerRemakeNode002
* @tc.desc: Test the RemakeNode function of NodeContainer.
* @tc.type: FUNC
*/
HWTEST_F(NodeContainerTestNg, NodeContainerRemakeNode002, TestSize.Level1)
{
RefPtr<FrameNode> nodeContainerNode = CreateNode();
ASSERT_NE(nodeContainerNode, nullptr);
RefPtr<FrameNode> childNode = FrameNode::CreateFrameNode(CHILD_NODE, 0, AceType::MakeRefPtr<Pattern>());
auto pattern = nodeContainerNode->GetPattern<NodeContainerPattern>();
ASSERT_NE(pattern, nullptr);
/**
* @tc.steps: step1.Fire RemakeNode return with nullptr. When oldNode is not null.
* @tc.steps: step7.Fire RemakeNode return with nullptr. When oldNode is not null.
* @tc.expected: process success without crash.
*/
auto childNode_three = FrameNode::CreateFrameNode(CHILD_NODE, 3, AceType::MakeRefPtr<Pattern>());
childNode_three->SetIsRootBuilderNode(false);
childNode_three->SetIsArkTsFrameNode(true);
pattern->SetMakeFunction([childNode_three](int32_t instanceId) -> RefPtr<UINode> {
EXPECT_EQ(instanceId, DEFAULT_INSTANCE_ID);
return childNode_three;
});
pattern->SetMakeFunction([childNode_three]() -> RefPtr<UINode> { return childNode_three; });
pattern->RemakeNode();
ASSERT_EQ(nodeContainerNode->GetChildAtIndex(0)->GetId(), childNode_three->GetId());
pattern->SetMakeFunction([childNode](int32_t instanceId) -> RefPtr<UINode> {
EXPECT_EQ(instanceId, DEFAULT_INSTANCE_ID);
return nullptr;
});
pattern->SetMakeFunction([childNode]() -> RefPtr<UINode> { return nullptr; });
pattern->RemakeNode();
ASSERT_EQ(nodeContainerNode->GetChildAtIndex(0), nullptr);
}
@ -248,8 +215,7 @@ HWTEST_F(NodeContainerTestNg, NodeContainerModelNGSetMakeFunction001, TestSize.L
*/
NodeContainerModelNG modelNg;
modelNg.Create();
auto builderFunc = [](int32_t instanceId) -> RefPtr<UINode> {
EXPECT_EQ(instanceId, DEFAULT_INSTANCE_ID);
auto builderFunc = []() -> RefPtr<UINode> {
auto node = FrameNode::CreateFrameNode("node", 0, AceType::MakeRefPtr<Pattern>(), true);
auto childNode = FrameNode::CreateFrameNode("child", 1, AceType::MakeRefPtr<Pattern>(), true);
node->AddChild(childNode);
@ -264,7 +230,7 @@ HWTEST_F(NodeContainerTestNg, NodeContainerModelNGSetMakeFunction001, TestSize.L
CHECK_NULL_VOID(frameNode);
auto pattern = AceType::DynamicCast<NodeContainerPattern>(frameNode->GetPattern());
CHECK_NULL_VOID(pattern);
auto node = pattern->FireMakeFunction(DEFAULT_INSTANCE_ID);
auto node = pattern->FireMakeFunction();
EXPECT_EQ(node->GetChildren().size(), 1);
}