1 /* 2 * Copyright (c) 2025 iSoftStone Information Technology (Group) Co.,Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include <cstdint> 16 #include <fcntl.h> 17 #include <functional> 18 #include <memory> 19 #include <optional> 20 #include <utility> 21 #include <vector> 22 23 #include "gtest/gtest.h" 24 25 #define protected public 26 #define private public 27 #include "test/mock/base/mock_task_executor.h" 28 #include "test/mock/core/pipeline/mock_pipeline_context.h" 29 30 #include "base/utils/utils.h" 31 #include "core/common/ace_application_info.h" 32 #include "core/components_ng/base/inspector.h" 33 #include "core/components_ng/base/inspector_filter.h" 34 #include "core/components_ng/base/simplified_inspector.h" 35 #include "core/components_ng/base/ui_node.h" 36 #include "core/components_ng/pattern/stage/stage_manager.h" 37 #include "core/components_ng/pattern/stage/stage_pattern.h" 38 #include "core/components_ng/pattern/stage/page_pattern.h" 39 #include "core/components_ng/pattern/text/span_node.h" 40 #include "core/components_v2/inspector/inspector_constants.h" 41 #include "core/pipeline_ng/pipeline_context.h" 42 #include "base/json/json_util.h" 43 #include "core/components_ng/pattern/custom/custom_node.h" 44 #include "test/mock/core/common/mock_container.h" 45 #include "test/mock/core/render/mock_render_context.h" 46 #include "test/mock/core/render/mock_rosen_render_context.h" 47 48 using namespace testing; 49 using namespace testing::ext; 50 namespace OHOS::Ace::NG { 51 namespace { 52 }; // namespace-*// 53 54 55 class SimplifiedInspectorTestNg : public testing::Test { 56 public: SetUpTestSuite()57 static void SetUpTestSuite() 58 { 59 MockPipelineContext::SetUp(); 60 } TearDownTestSuite()61 static void TearDownTestSuite() 62 { 63 MockPipelineContext::TearDown(); 64 } 65 }; 66 67 /** 68 * @tc.name: SimplifiedInspectorTestNg001 69 * @tc.desc: Test the operation of GetSimplifiedInspector 70 * @tc.type: FUNC 71 */ 72 HWTEST_F(SimplifiedInspectorTestNg, SimplifiedInspectorTestNg001, TestSize.Level1) 73 { 74 auto id = ElementRegister::GetInstance()->MakeUniqueId(); 75 RefPtr<FrameNode> ONE = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true); 76 auto context = PipelineContext::GetCurrentContext(); 77 ASSERT_NE(context, nullptr); 78 context->stageManager_ = AceType::MakeRefPtr<StageManager>(ONE); 79 int32_t containerId = 1; 80 TreeParams params { false }; 81 auto inspector = std::make_shared<SimplifiedInspector>(containerId, params); 82 auto collector = std::make_shared<Recorder::InspectorTreeCollector>( __anon3dbfd4090202(const std::shared_ptr<std::string> result) 83 [](const std::shared_ptr<std::string> result) { 84 ASSERT_NE(result, nullptr); 85 EXPECT_NE(result->c_str(), ""); 86 }, 87 false); 88 inspector->GetInspectorAsync(collector); 89 auto result = inspector->GetInspector(); 90 EXPECT_NE(result, ""); 91 context->stageManager_ = nullptr; 92 } 93 94 /** 95 * @tc.name: SimplifiedInspectorTestNg002 96 * @tc.desc: Test the operation of FilterEmptyInitially 97 * @tc.type: FUNC 98 */ 99 HWTEST_F(SimplifiedInspectorTestNg, SimplifiedInspectorTestNg002, TestSize.Level1) 100 { 101 auto id = ElementRegister::GetInstance()->MakeUniqueId(); 102 RefPtr<FrameNode> stageNode = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true); 103 auto context = PipelineContext::GetCurrentContext(); 104 ASSERT_NE(context, nullptr); 105 context->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>(); 106 context->stageManager_ = AceType::MakeRefPtr<StageManager>(stageNode); 107 auto pageId = ElementRegister::GetInstance()->MakeUniqueId(); 108 const RefPtr<FrameNode> pageA = FrameNode::CreateFrameNode("PageA", pageId, 109 AceType::MakeRefPtr<PagePattern>(AceType::MakeRefPtr<PageInfo>(10, "page/Index", "page/Index"))); 110 stageNode->AddChild(pageA); 111 int32_t containerId = 100; 112 TreeParams params { false }; 113 auto inspector = std::make_shared<SimplifiedInspector>(containerId, params); 114 auto collector = std::make_shared<Recorder::InspectorTreeCollector>( __anon3dbfd4090302(const std::shared_ptr<std::string> result) 115 [](const std::shared_ptr<std::string> result) { 116 ASSERT_NE(result, nullptr); 117 auto inspectorJson = JsonUtil::ParseJsonString(*result); 118 EXPECT_EQ(inspectorJson->GetValue("pageUrl")->GetString(), "page/Index"); 119 }, 120 false); 121 inspector->GetInspectorBackgroundAsync(collector); 122 auto result = collector->GetJson()->ToString(); 123 EXPECT_EQ(result, "{}"); 124 context->stageManager_ = nullptr; 125 } 126 127 /** 128 * @tc.name: SimplifiedInspectorTestNg003 129 * @tc.desc: Test the operation of FilterEmptyInitially 130 * @tc.type: FUNC 131 */ 132 HWTEST_F(SimplifiedInspectorTestNg, SimplifiedInspectorTestNg003, TestSize.Level1) 133 { 134 auto id = ElementRegister::GetInstance()->MakeUniqueId(); 135 RefPtr<FrameNode> stageNode = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true); 136 auto context = PipelineContext::GetCurrentContext(); 137 ASSERT_NE(context, nullptr); 138 context->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>(); 139 context->stageManager_ = AceType::MakeRefPtr<StageManager>(stageNode); 140 int32_t containerId = 100; 141 TreeParams params { false }; 142 auto inspector = std::make_shared<SimplifiedInspector>(containerId, params); 143 auto collector = std::make_shared<Recorder::InspectorTreeCollector>( __anon3dbfd4090402(const std::shared_ptr<std::string> result) 144 [](const std::shared_ptr<std::string> result) {}, false); 145 inspector->GetInspectorBackgroundAsync(collector); 146 auto result = collector->GetJson()->ToString(); 147 EXPECT_EQ(result, "{}"); 148 } 149 150 /** 151 * @tc.name: SimplifiedInspectorTestNg004 152 * @tc.desc: Test the operation of FilterEmptyInitially 153 * @tc.type: FUNC 154 */ 155 HWTEST_F(SimplifiedInspectorTestNg, SimplifiedInspectorTestNg004, TestSize.Level1) 156 { 157 auto id = ElementRegister::GetInstance()->MakeUniqueId(); 158 RefPtr<FrameNode> stageNode = FrameNode::CreateFrameNode("one", id, AceType::MakeRefPtr<Pattern>(), true); 159 auto context = PipelineContext::GetCurrentContext(); 160 ASSERT_NE(context, nullptr); 161 context->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>(); 162 context->stageManager_ = AceType::MakeRefPtr<StageManager>(stageNode); 163 int32_t containerId = 100; 164 TreeParams params { true }; 165 auto inspector = std::make_shared<SimplifiedInspector>(containerId, params); 166 auto collector = std::make_shared<Recorder::InspectorTreeCollector>( __anon3dbfd4090502(const std::shared_ptr<std::string> result) 167 [](const std::shared_ptr<std::string> result) {}, false); 168 inspector->GetInspectorBackgroundAsync(collector); 169 EXPECT_FALSE(inspector->isBackground_); 170 EXPECT_TRUE(inspector->isAsync_); 171 } 172 } // namespace OHOS::Ace::NG 173