From 750b691b5d44a981abcdfd2753016e1c1dd0b368 Mon Sep 17 00:00:00 2001 From: mayaolll Date: Fri, 26 Jul 2024 09:35:49 +0800 Subject: [PATCH] add tdd for remove_by_id Signed-off-by: mayaolll --- .../unittest/core/pattern/navigation/BUILD.gn | 1 + .../navigation/mock_navigation_stack.h | 42 ++++++-- .../navigation/navigation_pattern_test_ng.cpp | 31 +++--- .../navigation_sync_stack_test_ng.cpp | 99 +++++++++++++++++++ 4 files changed, 150 insertions(+), 23 deletions(-) create mode 100644 test/unittest/core/pattern/navigation/navigation_sync_stack_test_ng.cpp diff --git a/test/unittest/core/pattern/navigation/BUILD.gn b/test/unittest/core/pattern/navigation/BUILD.gn index caec616f508..8cd4e32d15c 100755 --- a/test/unittest/core/pattern/navigation/BUILD.gn +++ b/test/unittest/core/pattern/navigation/BUILD.gn @@ -26,6 +26,7 @@ ace_unittest("navigation_test_ng") { "navigation_model_test_ng.cpp", "navigation_pattern_test_ng.cpp", "navigation_pattern_testtwo_ng.cpp", + "navigation_sync_stack_test_ng.cpp", "navigation_system_bar_style_test_ng.cpp", "navigation_test_ng.cpp", "title_bar_test_ng.cpp", diff --git a/test/unittest/core/pattern/navigation/mock_navigation_stack.h b/test/unittest/core/pattern/navigation/mock_navigation_stack.h index 6cc88b0eb64..a507b5afd07 100644 --- a/test/unittest/core/pattern/navigation/mock_navigation_stack.h +++ b/test/unittest/core/pattern/navigation/mock_navigation_stack.h @@ -28,6 +28,13 @@ struct MockReplace { int32_t isReplace_ = 0; }; +struct MockNavPathInfo { + std::string name = ""; + std::string navDestinationId = "undefined"; + + explicit MockNavPathInfo(std::string name) : name(std::move(name)) {} +}; + using NavigationInterceptionEvent = std::function, const OHOS::Ace::RefPtr, OHOS::Ace::NG::NavigationOperation, bool)>; class MockNavigationStack : public OHOS::Ace::NG::NavigationStack { @@ -109,7 +116,7 @@ public: return OHOS::Ace::AceType::MakeRefPtr(); }); EXPECT_NE(frameNode, nullptr); - auto name = names_[index]; + auto name = mockPathArray_[index].name; auto container = OHOS::Ace::MockContainer::Current(); auto navigationRoute = container->GetNavigationRoute(); if (!navigationRoute) { @@ -129,26 +136,30 @@ public: void Push(const std::string& name, int32_t index) override { - names_.push_back(name); + mockPathArray_.push_back(MockNavPathInfo(name)); } void Push(const std::string& name, const OHOS::Ace::RefPtr& routeInfo = nullptr) override { - names_.push_back(name); + mockPathArray_.push_back(MockNavPathInfo(name)); } std::vector GetAllPathName() override { - return names_; + std::vector pathNames; + for (int32_t i = 0; i < static_cast(mockPathArray_.size()); i++) { + pathNames.emplace_back(mockPathArray_[i].name); + } + return pathNames; } std::vector GetAllPathIndex() override { - if (names_.empty()) { + if (mockPathArray_.empty()) { return {}; } std::vector pathIndex; - for (int32_t i = 0; i < static_cast(names_.size()); i++) { + for (int32_t i = 0; i < static_cast(mockPathArray_.size()); i++) { pathIndex.emplace_back(i); } return pathIndex; @@ -157,12 +168,12 @@ public: void Clear() override { OHOS::Ace::NG::NavigationStack::Clear(); - names_.clear(); + mockPathArray_.clear(); } void Pop() override { - names_.pop_back(); + mockPathArray_.pop_back(); } void SetLifecycleIndex(int8_t index) @@ -180,6 +191,19 @@ public: return navPathList_; } + void SetDestinationIdToJsStack(int32_t index, const std::string& navDestinationId) override + { + if (index < 0 || index >= static_cast(mockPathArray_.size())) { + return; + } + mockPathArray_[index].navDestinationId = navDestinationId; + } + + std::string GetNavPathId(int32_t index) + { + return mockPathArray_[index].navDestinationId; + } + private: int8_t lifecycleIndex_ = 0; std::function onStateChangedCallback_; @@ -187,6 +211,6 @@ private: NavigationInterceptionEvent afterCallback_; std::function modeCallback_; MockReplace *mockReplace_ = new MockReplace(); - std::vector names_; + std::vector mockPathArray_; }; #endif \ No newline at end of file diff --git a/test/unittest/core/pattern/navigation/navigation_pattern_test_ng.cpp b/test/unittest/core/pattern/navigation/navigation_pattern_test_ng.cpp index a757ee11ba1..fa6d36c9eb7 100644 --- a/test/unittest/core/pattern/navigation/navigation_pattern_test_ng.cpp +++ b/test/unittest/core/pattern/navigation/navigation_pattern_test_ng.cpp @@ -1016,16 +1016,19 @@ HWTEST_F(NavigationPatternTestNg, NavigationLayoutAlgorithm001, TestSize.Level1) */ HWTEST_F(NavigationPatternTestNg, NavigationModelNG001, TestSize.Level1) { + auto elementRegister = ElementRegister::GetInstance(); + auto navigationUniqueId = elementRegister->MakeUniqueId(); auto navigation = NavigationGroupNode::GetOrCreateGroupNode( - V2::NAVIGATION_VIEW_ETS_TAG, 11, []() { return AceType::MakeRefPtr(); }); + V2::NAVIGATION_VIEW_ETS_TAG, navigationUniqueId, []() { return AceType::MakeRefPtr(); }); auto navigationStack = AceType::MakeRefPtr(); navigation->GetPattern()->SetNavigationStack(std::move(navigationStack)); auto contentNode = NavDestinationGroupNode::GetOrCreateGroupNode( - "NavDestination", 22, []() { return AceType::MakeRefPtr(); }); - auto navBarNode = - NavBarNode::GetOrCreateNavBarNode("navBarNode", 33, []() { return AceType::MakeRefPtr(); }); - auto dividerNode = - FrameNode::GetOrCreateFrameNode("dividerNode", 44, []() { return AceType::MakeRefPtr(); }); + "NavDestination", elementRegister->MakeUniqueId(), + []() { return AceType::MakeRefPtr(); }); + auto navBarNode = NavBarNode::GetOrCreateNavBarNode( + "navBarNode", elementRegister->MakeUniqueId(), []() { return AceType::MakeRefPtr(); }); + auto dividerNode = FrameNode::GetOrCreateFrameNode( + "dividerNode", elementRegister->MakeUniqueId(), []() { return AceType::MakeRefPtr(); }); navigation->navBarNode_ = navBarNode; navigation->contentNode_ = contentNode; navigation->dividerNode_ = dividerNode; @@ -1034,28 +1037,28 @@ HWTEST_F(NavigationPatternTestNg, NavigationModelNG001, TestSize.Level1) navigationLayoutProperty->propNavigationMode_ = NavigationMode::AUTO; auto* stack = ViewStackProcessor::GetInstance(); - stack->reservedNodeId_ = 11; + stack->reservedNodeId_ = navigationUniqueId; NavigationModelNG model; model.Create(); ASSERT_EQ(navigationLayoutProperty->propNavigationMode_.value(), NavigationMode::AUTO); navigation->navBarNode_ = nullptr; navBarNode = NavBarNode::GetOrCreateNavBarNode( - V2::NAVBAR_ETS_TAG, 55, []() { return AceType::MakeRefPtr(); }); + V2::NAVBAR_ETS_TAG, elementRegister->MakeUniqueId(), []() { return AceType::MakeRefPtr(); }); auto titleBarNode = TitleBarNode::GetOrCreateTitleBarNode( - "titleBarNode", 66, []() { return AceType::MakeRefPtr(); }); + "titleBarNode", elementRegister->MakeUniqueId(), []() { return AceType::MakeRefPtr(); }); auto navBarContentNode = FrameNode::GetOrCreateFrameNode( - "navBarContentNode", 77, []() { return AceType::MakeRefPtr(true); }); + "navBarContentNode", elementRegister->MakeUniqueId(), + []() { return AceType::MakeRefPtr(true); }); auto toolBarNode = FrameNode::GetOrCreateFrameNode( - "toolBarNode", 88, []() { return AceType::MakeRefPtr(false); }); + "toolBarNode", elementRegister->MakeUniqueId(), + []() { return AceType::MakeRefPtr(false); }); navBarNode->titleBarNode_ = titleBarNode; navBarNode->navBarContentNode_ = navBarContentNode; navBarNode->toolBarNode_ = toolBarNode; - stack->reservedNodeId_ = 11; - auto tempRegister = ElementRegister::GetInstance(); - tempRegister->nextUniqueElementId_ = 55; + stack->reservedNodeId_ = navigationUniqueId; model.Create(); ASSERT_EQ(navigationLayoutProperty->propNavigationMode_.value(), NavigationMode::AUTO); } diff --git a/test/unittest/core/pattern/navigation/navigation_sync_stack_test_ng.cpp b/test/unittest/core/pattern/navigation/navigation_sync_stack_test_ng.cpp new file mode 100644 index 00000000000..4560815257b --- /dev/null +++ b/test/unittest/core/pattern/navigation/navigation_sync_stack_test_ng.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "gtest/gtest.h" +#include "mock_navigation_route.h" +#include "mock_navigation_stack.h" + +#define protected public +#define private public +#include "core/components_ng/pattern/navigation/navigation_model_ng.h" +#include "core/components_ng/pattern/navigation/navigation_pattern.h" +#include "test/mock/core/pipeline/mock_pipeline_context.h" +#include "test/mock/core/common/mock_theme_manager.h" +#include "test/mock/core/common/mock_container.h" +#include "test/mock/base/mock_task_executor.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS::Ace::NG { + +class NavigationSyncStackTestNg : public testing::Test { +public: + static void SetUpTestSuite(); + static void TearDownTestSuite(); + void MockPipelineContextGetTheme(); +}; + +void NavigationSyncStackTestNg::SetUpTestSuite() +{ + MockPipelineContext::SetUp(); + MockContainer::SetUp(); +} + +void NavigationSyncStackTestNg::TearDownTestSuite() +{ + MockPipelineContext::TearDown(); + MockContainer::TearDown(); +} + +void NavigationSyncStackTestNg::MockPipelineContextGetTheme() +{ + auto themeManager = AceType::MakeRefPtr(); + MockPipelineContext::GetCurrent()->SetThemeManager(themeManager); + EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr())); +} + +/** + * @tc.name: NavigationSyncStackTestNg001 + * @tc.desc: Test whether the id of navPathInfo is correctly updated after each stack sync + * @tc.type: FUNC + */ +HWTEST_F(NavigationSyncStackTestNg, NavigationSyncStackTestNg001, TestSize.Level1) +{ + /** + * @tc.steps: step1. init model, frameNode and pattern. + */ + MockPipelineContextGetTheme(); + NavigationModelNG navigationModel; + navigationModel.Create(); + auto navigationStack = AceType::MakeRefPtr(); + navigationModel.SetNavigationStack(navigationStack); + RefPtr navigationNode = + AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); + ASSERT_NE(navigationNode, nullptr); + auto navigationPattern = AceType::DynamicCast(navigationNode->GetPattern()); + ASSERT_NE(navigationPattern, nullptr); + /** + * @tc.steps: step2. mock js-stack and call stack sync. + */ + std::string destNameBase = "dest"; + const int32_t testNumber = 10; + for (int32_t index = 0; index < testNumber; ++ index) { + navigationStack->Push(destNameBase + std::to_string(index)); + ASSERT_EQ(navigationStack->GetNavPathId(index), "undefined"); + } + MockContainer::Current()->SetNavigationRoute(AceType::MakeRefPtr("")); + navigationPattern->OnModifyDone(); + navigationPattern->MarkNeedSyncWithJsStack(); + navigationPattern->SyncWithJsStackIfNeeded(); + for (int32_t index = 0; index < testNumber; ++ index) { + ASSERT_NE(navigationStack->GetNavPathId(index), "undefined"); + } +} +} // namespace OHOS::Ace::NG \ No newline at end of file