!38880 新增removeByNavDestinationId接口对应tdd

Merge pull request !38880 from MaYaoHW/remove_by_id_tdd
This commit is contained in:
openharmony_ci 2024-08-07 10:56:48 +00:00 committed by Gitee
commit 7520625709
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 150 additions and 23 deletions

View File

@ -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",

View File

@ -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<void(const OHOS::Ace::RefPtr<OHOS::Ace::NG::NavDestinationContext>,
const OHOS::Ace::RefPtr<OHOS::Ace::NG::NavDestinationContext>, OHOS::Ace::NG::NavigationOperation, bool)>;
class MockNavigationStack : public OHOS::Ace::NG::NavigationStack {
@ -109,7 +116,7 @@ public:
return OHOS::Ace::AceType::MakeRefPtr<OHOS::Ace::NG::NavDestinationPattern>();
});
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<OHOS::Ace::NG::RouteInfo>& routeInfo = nullptr) override
{
names_.push_back(name);
mockPathArray_.push_back(MockNavPathInfo(name));
}
std::vector<std::string> GetAllPathName() override
{
return names_;
std::vector<std::string> pathNames;
for (int32_t i = 0; i < static_cast<int32_t>(mockPathArray_.size()); i++) {
pathNames.emplace_back(mockPathArray_[i].name);
}
return pathNames;
}
std::vector<int32_t> GetAllPathIndex() override
{
if (names_.empty()) {
if (mockPathArray_.empty()) {
return {};
}
std::vector<int32_t> pathIndex;
for (int32_t i = 0; i < static_cast<int32_t>(names_.size()); i++) {
for (int32_t i = 0; i < static_cast<int32_t>(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<int32_t>(mockPathArray_.size())) {
return;
}
mockPathArray_[index].navDestinationId = navDestinationId;
}
std::string GetNavPathId(int32_t index)
{
return mockPathArray_[index].navDestinationId;
}
private:
int8_t lifecycleIndex_ = 0;
std::function<void()> onStateChangedCallback_;
@ -187,6 +211,6 @@ private:
NavigationInterceptionEvent afterCallback_;
std::function<void(OHOS::Ace::NG::NavigationMode)> modeCallback_;
MockReplace *mockReplace_ = new MockReplace();
std::vector<std::string> names_;
std::vector<MockNavPathInfo> mockPathArray_;
};
#endif

View File

@ -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<NavigationPattern>(); });
V2::NAVIGATION_VIEW_ETS_TAG, navigationUniqueId, []() { return AceType::MakeRefPtr<NavigationPattern>(); });
auto navigationStack = AceType::MakeRefPtr<NavigationStack>();
navigation->GetPattern<NavigationPattern>()->SetNavigationStack(std::move(navigationStack));
auto contentNode = NavDestinationGroupNode::GetOrCreateGroupNode(
"NavDestination", 22, []() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
auto navBarNode =
NavBarNode::GetOrCreateNavBarNode("navBarNode", 33, []() { return AceType::MakeRefPtr<NavBarPattern>(); });
auto dividerNode =
FrameNode::GetOrCreateFrameNode("dividerNode", 44, []() { return AceType::MakeRefPtr<DividerPattern>(); });
"NavDestination", elementRegister->MakeUniqueId(),
[]() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
auto navBarNode = NavBarNode::GetOrCreateNavBarNode(
"navBarNode", elementRegister->MakeUniqueId(), []() { return AceType::MakeRefPtr<NavBarPattern>(); });
auto dividerNode = FrameNode::GetOrCreateFrameNode(
"dividerNode", elementRegister->MakeUniqueId(), []() { return AceType::MakeRefPtr<DividerPattern>(); });
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<NavBarPattern>(); });
V2::NAVBAR_ETS_TAG, elementRegister->MakeUniqueId(), []() { return AceType::MakeRefPtr<NavBarPattern>(); });
auto titleBarNode = TitleBarNode::GetOrCreateTitleBarNode(
"titleBarNode", 66, []() { return AceType::MakeRefPtr<TitleBarPattern>(); });
"titleBarNode", elementRegister->MakeUniqueId(), []() { return AceType::MakeRefPtr<TitleBarPattern>(); });
auto navBarContentNode = FrameNode::GetOrCreateFrameNode(
"navBarContentNode", 77, []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
"navBarContentNode", elementRegister->MakeUniqueId(),
[]() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
auto toolBarNode = FrameNode::GetOrCreateFrameNode(
"toolBarNode", 88, []() { return AceType::MakeRefPtr<LinearLayoutPattern>(false); });
"toolBarNode", elementRegister->MakeUniqueId(),
[]() { return AceType::MakeRefPtr<LinearLayoutPattern>(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);
}

View File

@ -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 <optional>
#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<MockThemeManager>();
MockPipelineContext::GetCurrent()->SetThemeManager(themeManager);
EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<NavigationBarTheme>()));
}
/**
* @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<MockNavigationStack>();
navigationModel.SetNavigationStack(navigationStack);
RefPtr<NavigationGroupNode> navigationNode =
AceType::DynamicCast<NavigationGroupNode>(ViewStackProcessor::GetInstance()->Finish());
ASSERT_NE(navigationNode, nullptr);
auto navigationPattern = AceType::DynamicCast<NavigationPattern>(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<MockNavigationRoute>(""));
navigationPattern->OnModifyDone();
navigationPattern->MarkNeedSyncWithJsStack();
navigationPattern->SyncWithJsStackIfNeeded();
for (int32_t index = 0; index < testNumber; ++ index) {
ASSERT_NE(navigationStack->GetNavPathId(index), "undefined");
}
}
} // namespace OHOS::Ace::NG