/* * Copyright (c) 2022 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 "base/pt_returns.h" #include "ecmascript/tests/test_helper.h" #include "tooling/protocol_handler.h" using namespace panda::ecmascript; using namespace panda::ecmascript::tooling; namespace panda::test { class PtReturnsTest : public testing::Test { public: static void SetUpTestCase() { GTEST_LOG_(INFO) << "SetUpTestCase"; } static void TearDownTestCase() { GTEST_LOG_(INFO) << "TearDownCase"; } void SetUp() override { TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope); } void TearDown() override { TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope); } protected: EcmaVM *ecmaVm {nullptr}; EcmaHandleScope *scope {nullptr}; JSThread *thread {nullptr}; }; HWTEST_F_L0(PtReturnsTest, EvaluateOnCallFrameReturns) { std::unique_ptr result = std::make_unique(); result->SetType("idle"); std::unique_ptr evaluateOnCallFrameReturns = std::make_unique(std::move(result)); ASSERT_NE(evaluateOnCallFrameReturns, nullptr); std::unique_ptr tmpJson; ASSERT_EQ(evaluateOnCallFrameReturns->ToJson()->GetObject("exceptionDetails", &tmpJson), Result::NOT_EXIST); } HWTEST_F_L0(PtReturnsTest, GetScriptSourceReturns) { std::unique_ptr getScriptSourceReturns = std::make_unique ("source_1"); ASSERT_NE(getScriptSourceReturns, nullptr); std::unique_ptr tmpJson; ASSERT_EQ(getScriptSourceReturns->ToJson()->GetObject("bytecode", &tmpJson), Result::NOT_EXIST); } HWTEST_F_L0(PtReturnsTest, SearchInContentReturns) { auto result = std::vector>(); std::unique_ptr tmpResult = std::make_unique(); tmpResult->SetLine(13); tmpResult->SetLineContent("line-10"); result.emplace_back(std::move(tmpResult)); std::unique_ptr searchInContentReturns = std::make_unique(std::move(result)); ASSERT_NE(searchInContentReturns, nullptr); std::unique_ptr json; ASSERT_EQ(searchInContentReturns->ToJson()->GetArray("result", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 1); } HWTEST_F_L0(PtReturnsTest, SetScriptSourceReturns) { auto callFrames = std::vector>(); std::unique_ptr callFrame = std::make_unique(); std::unique_ptr location = std::make_unique(); location->SetScriptId(3).SetLine(36); std::unique_ptr res = std::make_unique(); res->SetType("idle5"); callFrame->SetCallFrameId(33); callFrame->SetLocation(std::move(location)); callFrame->SetThis(std::move(res)); callFrames.emplace_back(std::move(callFrame)); std::unique_ptr exceptionDetails = std::make_unique(); exceptionDetails->SetExceptionId(12); std::unique_ptr setScriptSourceReturns = std::make_unique (std::move(callFrames), true, std::move(exceptionDetails)); ASSERT_NE(setScriptSourceReturns, nullptr); std::unique_ptr callFramesJson; ASSERT_EQ(setScriptSourceReturns->ToJson()->GetArray("callFrames", &callFramesJson), Result::SUCCESS); ASSERT_NE(callFramesJson, nullptr); EXPECT_EQ(callFramesJson->GetSize(), 1); bool tmpStackChanged; ASSERT_EQ(setScriptSourceReturns->ToJson()->GetBool("stackChanged", &tmpStackChanged), Result::SUCCESS); ASSERT_TRUE(tmpStackChanged); std::unique_ptr exceptionDetailJson; ASSERT_EQ(setScriptSourceReturns->ToJson()->GetObject("exceptionDetails", &exceptionDetailJson), Result::SUCCESS); std::unique_ptr setScriptSource = std::make_unique(std::nullopt); ASSERT_NE(setScriptSource, nullptr); std::unique_ptr callJson; ASSERT_EQ(setScriptSource->ToJson()->GetArray("callFrames", &callJson), Result::NOT_EXIST); bool stack; ASSERT_EQ(setScriptSource->ToJson()->GetBool("stackChanged", &stack), Result::NOT_EXIST); std::unique_ptr exceptionJson; ASSERT_EQ(setScriptSource->ToJson()->GetObject("exceptionDetails", &exceptionJson), Result::NOT_EXIST); } HWTEST_F_L0(PtReturnsTest, GetPropertiesReturns) { auto descriptor = std::vector>(); std::unique_ptr propertyDescriptor = std::make_unique(); propertyDescriptor->SetName("filename1").SetConfigurable(true).SetEnumerable(true); descriptor.emplace_back(std::move(propertyDescriptor)); auto internalPropertyDescripties = std::vector>(); std::unique_ptr internalPropertyDescriptor = std::make_unique(); internalPropertyDescriptor->SetName("filename2"); internalPropertyDescripties.emplace_back(std::move(internalPropertyDescriptor)); auto privateProperties = std::vector>(); std::unique_ptr privatePropertyDescriptor = std::make_unique(); privatePropertyDescriptor->SetName("filename3"); privateProperties.emplace_back(std::move(privatePropertyDescriptor)); std::unique_ptr exceptionDetails = std::make_unique(); exceptionDetails->SetExceptionId(12); std::unique_ptr getPropertiesReturns = std::make_unique(std::move(descriptor), std::move(internalPropertyDescripties), std::move(privateProperties), std::move(exceptionDetails)); ASSERT_NE(getPropertiesReturns, nullptr); std::unique_ptr resultJson; ASSERT_EQ(getPropertiesReturns->ToJson()->GetArray("result", &resultJson), Result::SUCCESS); ASSERT_NE(resultJson, nullptr); EXPECT_EQ(resultJson->GetSize(), 1); std::unique_ptr internalJson; ASSERT_EQ(getPropertiesReturns->ToJson()->GetArray("internalProperties", &internalJson), Result::SUCCESS); ASSERT_NE(internalJson, nullptr); EXPECT_EQ(internalJson->GetSize(), 1); std::unique_ptr privateJson; ASSERT_EQ(getPropertiesReturns->ToJson()->GetArray("privateProperties", &privateJson), Result::SUCCESS); ASSERT_NE(privateJson, nullptr); EXPECT_EQ(privateJson->GetSize(), 1); std::unique_ptr tmpJson; ASSERT_EQ(getPropertiesReturns->ToJson()->GetObject("exceptionDetails", &tmpJson), Result::SUCCESS); } HWTEST_F_L0(PtReturnsTest, GetBestEffortCoverageReturns) { auto coverages = std::vector>(); std::unique_ptr coverage = std::make_unique(); coverage->SetStartOffset(0); coverage->SetEndOffset(10); coverage->SetCount(5); coverages.emplace_back(std::move(coverage)); auto functions = std::vector>(); std::unique_ptr functionCoverage = std::make_unique(); functionCoverage->SetFunctionName("functionName1"); functionCoverage->SetisBlockCoverage(true); functionCoverage->SetFunctions(std::move(coverages)); auto result = std::vector>(); std::unique_ptr scriptCoverage = std::make_unique(); scriptCoverage->SetScriptId("13"); scriptCoverage->SetUrl("url13"); scriptCoverage->SetFunctions(std::move(functions)); result.emplace_back(std::move(scriptCoverage)); std::unique_ptr getBestEffortCoverageReturns = std::make_unique(std::move(result)); std::unique_ptr json; ASSERT_EQ(getBestEffortCoverageReturns->ToJson()->GetArray("result", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 1); } HWTEST_F_L0(PtReturnsTest, TakePreciseCoverageReturns) { auto coverages = std::vector>(); std::unique_ptr coverage = std::make_unique(); coverage->SetStartOffset(0); coverage->SetEndOffset(10); coverage->SetCount(5); coverages.emplace_back(std::move(coverage)); auto functions = std::vector>(); std::unique_ptr functionCoverage = std::make_unique(); functionCoverage->SetFunctionName("functionName1"); functionCoverage->SetisBlockCoverage(true); functionCoverage->SetFunctions(std::move(coverages)); auto result = std::vector>(); std::unique_ptr scriptCoverage = std::make_unique(); scriptCoverage->SetScriptId("13"); scriptCoverage->SetUrl("url13"); scriptCoverage->SetFunctions(std::move(functions)); result.emplace_back(std::move(scriptCoverage)); std::unique_ptr takePreciseCoverageReturns = std::make_unique(std::move(result), 1001); ASSERT_NE(takePreciseCoverageReturns, nullptr); std::unique_ptr json; ASSERT_EQ(takePreciseCoverageReturns->ToJson()->GetArray("result", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 1); } HWTEST_F_L0(PtReturnsTest, TakeTypeProfileReturns) { auto types = std::vector>(); std::unique_ptr typeObject = std::make_unique(); typeObject->SetName("object1"); auto entries = std::vector>(); std::unique_ptr typeProfileEntry = std::make_unique(); typeProfileEntry->SetOffset(4); typeProfileEntry->SetTypes(std::move(types)); auto result = std::vector>(); std::unique_ptr scriptTypeProfile = std::make_unique(); scriptTypeProfile->SetUrl("url5"); scriptTypeProfile->SetScriptId("id5"); scriptTypeProfile->SetEntries(std::move(entries)); result.emplace_back(std::move(scriptTypeProfile)); std::unique_ptr takeTypeProfileturns = std::make_unique(std::move(result)); std::unique_ptr json; ASSERT_EQ(takeTypeProfileturns->ToJson()->GetArray("result", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 1); } HWTEST_F_L0(PtReturnsTest, GetCategoriesReturns) { auto result = std::vector(); std::string str = "getCategories"; result.emplace_back(str); std::unique_ptr getCategoriesReturns = std::make_unique (std::move(result)); std::unique_ptr json; ASSERT_EQ(getCategoriesReturns->ToJson()->GetArray("categories", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 1); } }