1 /*
2 * Copyright (c) 2022 Huawei Device 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
16 #include "base/pt_returns.h"
17 #include "ecmascript/tests/test_helper.h"
18 #include "tooling/protocol_handler.h"
19
20 using namespace panda::ecmascript;
21 using namespace panda::ecmascript::tooling;
22
23
24 namespace panda::test {
25 class PtReturnsTest : public testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 GTEST_LOG_(INFO) << "SetUpTestCase";
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 GTEST_LOG_(INFO) << "TearDownCase";
35 }
36
SetUp()37 void SetUp() override
38 {
39 TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
40 }
41
TearDown()42 void TearDown() override
43 {
44 TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
45 }
46
47 protected:
48 EcmaVM *ecmaVm {nullptr};
49 EcmaHandleScope *scope {nullptr};
50 JSThread *thread {nullptr};
51 };
52
HWTEST_F_L0(PtReturnsTest,EvaluateOnCallFrameReturns)53 HWTEST_F_L0(PtReturnsTest, EvaluateOnCallFrameReturns)
54 {
55 std::unique_ptr<RemoteObject> result = std::make_unique<RemoteObject>();
56 result->SetType("idle");
57 std::unique_ptr<EvaluateOnCallFrameReturns> evaluateOnCallFrameReturns
58 = std::make_unique<EvaluateOnCallFrameReturns>(std::move(result));
59 ASSERT_NE(evaluateOnCallFrameReturns, nullptr);
60
61 std::unique_ptr<PtJson> tmpJson;
62 ASSERT_EQ(evaluateOnCallFrameReturns->ToJson()->GetObject("exceptionDetails", &tmpJson), Result::NOT_EXIST);
63 }
64
HWTEST_F_L0(PtReturnsTest,GetScriptSourceReturns)65 HWTEST_F_L0(PtReturnsTest, GetScriptSourceReturns)
66 {
67 std::unique_ptr<GetScriptSourceReturns> getScriptSourceReturns = std::make_unique
68 <GetScriptSourceReturns>("source_1");
69 ASSERT_NE(getScriptSourceReturns, nullptr);
70
71 std::unique_ptr<PtJson> tmpJson;
72 ASSERT_EQ(getScriptSourceReturns->ToJson()->GetObject("bytecode", &tmpJson), Result::NOT_EXIST);
73 }
74
HWTEST_F_L0(PtReturnsTest,SearchInContentReturns)75 HWTEST_F_L0(PtReturnsTest, SearchInContentReturns)
76 {
77 auto result = std::vector<std::unique_ptr<SearchMatch>>();
78 std::unique_ptr<SearchMatch> tmpResult = std::make_unique<SearchMatch>();
79 tmpResult->SetLine(13);
80 tmpResult->SetLineContent("line-10");
81 result.emplace_back(std::move(tmpResult));
82 std::unique_ptr<SearchInContentReturns> searchInContentReturns
83 = std::make_unique<SearchInContentReturns>(std::move(result));
84 ASSERT_NE(searchInContentReturns, nullptr);
85
86 std::unique_ptr<PtJson> json;
87 ASSERT_EQ(searchInContentReturns->ToJson()->GetArray("result", &json), Result::SUCCESS);
88 ASSERT_NE(json, nullptr);
89 EXPECT_EQ(json->GetSize(), 1);
90 }
91
HWTEST_F_L0(PtReturnsTest,SetScriptSourceReturns)92 HWTEST_F_L0(PtReturnsTest, SetScriptSourceReturns)
93 {
94 auto callFrames = std::vector<std::unique_ptr<CallFrame>>();
95 std::unique_ptr<CallFrame> callFrame = std::make_unique<CallFrame>();
96 std::unique_ptr<Location> location = std::make_unique<Location>();
97 location->SetScriptId(3).SetLine(36);
98 std::unique_ptr<RemoteObject> res = std::make_unique<RemoteObject>();
99 res->SetType("idle5");
100
101 callFrame->SetCallFrameId(33);
102 callFrame->SetLocation(std::move(location));
103 callFrame->SetThis(std::move(res));
104 callFrames.emplace_back(std::move(callFrame));
105
106 std::unique_ptr<ExceptionDetails> exceptionDetails = std::make_unique<ExceptionDetails>();
107 exceptionDetails->SetExceptionId(12);
108 std::unique_ptr<SetScriptSourceReturns> setScriptSourceReturns
109 = std::make_unique <SetScriptSourceReturns>(std::move(callFrames),
110 true, std::move(exceptionDetails));
111 ASSERT_NE(setScriptSourceReturns, nullptr);
112
113 std::unique_ptr<PtJson> callFramesJson;
114 ASSERT_EQ(setScriptSourceReturns->ToJson()->GetArray("callFrames", &callFramesJson), Result::SUCCESS);
115 ASSERT_NE(callFramesJson, nullptr);
116 EXPECT_EQ(callFramesJson->GetSize(), 1);
117 bool tmpStackChanged;
118 ASSERT_EQ(setScriptSourceReturns->ToJson()->GetBool("stackChanged", &tmpStackChanged), Result::SUCCESS);
119 ASSERT_TRUE(tmpStackChanged);
120 std::unique_ptr<PtJson> exceptionDetailJson;
121 ASSERT_EQ(setScriptSourceReturns->ToJson()->GetObject("exceptionDetails", &exceptionDetailJson), Result::SUCCESS);
122
123 std::unique_ptr<SetScriptSourceReturns> setScriptSource = std::make_unique<SetScriptSourceReturns>(std::nullopt);
124 ASSERT_NE(setScriptSource, nullptr);
125
126 std::unique_ptr<PtJson> callJson;
127 ASSERT_EQ(setScriptSource->ToJson()->GetArray("callFrames", &callJson), Result::NOT_EXIST);
128 bool stack;
129 ASSERT_EQ(setScriptSource->ToJson()->GetBool("stackChanged", &stack), Result::NOT_EXIST);
130 std::unique_ptr<PtJson> exceptionJson;
131 ASSERT_EQ(setScriptSource->ToJson()->GetObject("exceptionDetails", &exceptionJson), Result::NOT_EXIST);
132 }
133
HWTEST_F_L0(PtReturnsTest,GetPropertiesReturns)134 HWTEST_F_L0(PtReturnsTest, GetPropertiesReturns)
135 {
136 auto descriptor = std::vector<std::unique_ptr<panda::ecmascript::tooling::PropertyDescriptor>>();
137 std::unique_ptr<panda::ecmascript::tooling::PropertyDescriptor> propertyDescriptor
138 = std::make_unique<panda::ecmascript::tooling::PropertyDescriptor>();
139 propertyDescriptor->SetName("filename1").SetConfigurable(true).SetEnumerable(true);
140 descriptor.emplace_back(std::move(propertyDescriptor));
141
142 auto internalPropertyDescripties = std::vector<std::unique_ptr<InternalPropertyDescriptor>>();
143 std::unique_ptr<InternalPropertyDescriptor> internalPropertyDescriptor
144 = std::make_unique<InternalPropertyDescriptor>();
145 internalPropertyDescriptor->SetName("filename2");
146 internalPropertyDescripties.emplace_back(std::move(internalPropertyDescriptor));
147
148 auto privateProperties = std::vector<std::unique_ptr<PrivatePropertyDescriptor>>();
149 std::unique_ptr<PrivatePropertyDescriptor> privatePropertyDescriptor
150 = std::make_unique<PrivatePropertyDescriptor>();
151 privatePropertyDescriptor->SetName("filename3");
152 privateProperties.emplace_back(std::move(privatePropertyDescriptor));
153
154 std::unique_ptr<ExceptionDetails> exceptionDetails = std::make_unique<ExceptionDetails>();
155 exceptionDetails->SetExceptionId(12);
156 std::unique_ptr<GetPropertiesReturns> getPropertiesReturns
157 = std::make_unique<GetPropertiesReturns>(std::move(descriptor),
158 std::move(internalPropertyDescripties),
159 std::move(privateProperties),
160 std::move(exceptionDetails));
161 ASSERT_NE(getPropertiesReturns, nullptr);
162
163 std::unique_ptr<PtJson> resultJson;
164 ASSERT_EQ(getPropertiesReturns->ToJson()->GetArray("result", &resultJson), Result::SUCCESS);
165 ASSERT_NE(resultJson, nullptr);
166 EXPECT_EQ(resultJson->GetSize(), 1);
167
168 std::unique_ptr<PtJson> internalJson;
169 ASSERT_EQ(getPropertiesReturns->ToJson()->GetArray("internalProperties", &internalJson), Result::SUCCESS);
170 ASSERT_NE(internalJson, nullptr);
171 EXPECT_EQ(internalJson->GetSize(), 1);
172
173 std::unique_ptr<PtJson> privateJson;
174 ASSERT_EQ(getPropertiesReturns->ToJson()->GetArray("privateProperties", &privateJson), Result::SUCCESS);
175 ASSERT_NE(privateJson, nullptr);
176 EXPECT_EQ(privateJson->GetSize(), 1);
177
178 std::unique_ptr<PtJson> tmpJson;
179 ASSERT_EQ(getPropertiesReturns->ToJson()->GetObject("exceptionDetails", &tmpJson), Result::SUCCESS);
180 }
181
HWTEST_F_L0(PtReturnsTest,GetBestEffortCoverageReturns)182 HWTEST_F_L0(PtReturnsTest, GetBestEffortCoverageReturns)
183 {
184 auto coverages = std::vector<std::unique_ptr<Coverage>>();
185 std::unique_ptr<Coverage> coverage = std::make_unique<Coverage>();
186 coverage->SetStartOffset(0);
187 coverage->SetEndOffset(10);
188 coverage->SetCount(5);
189 coverages.emplace_back(std::move(coverage));
190
191 auto functions = std::vector<std::unique_ptr<FunctionCoverage>>();
192 std::unique_ptr<FunctionCoverage> functionCoverage = std::make_unique<FunctionCoverage>();
193 functionCoverage->SetFunctionName("functionName1");
194 functionCoverage->SetisBlockCoverage(true);
195 functionCoverage->SetFunctions(std::move(coverages));
196
197 auto result = std::vector<std::unique_ptr<ScriptCoverage>>();
198 std::unique_ptr<ScriptCoverage> scriptCoverage = std::make_unique<ScriptCoverage>();
199 scriptCoverage->SetScriptId("13");
200 scriptCoverage->SetUrl("url13");
201 scriptCoverage->SetFunctions(std::move(functions));
202 result.emplace_back(std::move(scriptCoverage));
203
204 std::unique_ptr<GetBestEffortCoverageReturns> getBestEffortCoverageReturns
205 = std::make_unique<GetBestEffortCoverageReturns>(std::move(result));
206
207 std::unique_ptr<PtJson> json;
208 ASSERT_EQ(getBestEffortCoverageReturns->ToJson()->GetArray("result", &json), Result::SUCCESS);
209 ASSERT_NE(json, nullptr);
210 EXPECT_EQ(json->GetSize(), 1);
211 }
212
HWTEST_F_L0(PtReturnsTest,TakePreciseCoverageReturns)213 HWTEST_F_L0(PtReturnsTest, TakePreciseCoverageReturns)
214 {
215 auto coverages = std::vector<std::unique_ptr<Coverage>>();
216 std::unique_ptr<Coverage> coverage = std::make_unique<Coverage>();
217 coverage->SetStartOffset(0);
218 coverage->SetEndOffset(10);
219 coverage->SetCount(5);
220 coverages.emplace_back(std::move(coverage));
221
222 auto functions = std::vector<std::unique_ptr<FunctionCoverage>>();
223 std::unique_ptr<FunctionCoverage> functionCoverage = std::make_unique<FunctionCoverage>();
224 functionCoverage->SetFunctionName("functionName1");
225 functionCoverage->SetisBlockCoverage(true);
226 functionCoverage->SetFunctions(std::move(coverages));
227
228 auto result = std::vector<std::unique_ptr<ScriptCoverage>>();
229 std::unique_ptr<ScriptCoverage> scriptCoverage = std::make_unique<ScriptCoverage>();
230 scriptCoverage->SetScriptId("13");
231 scriptCoverage->SetUrl("url13");
232 scriptCoverage->SetFunctions(std::move(functions));
233 result.emplace_back(std::move(scriptCoverage));
234
235 std::unique_ptr<TakePreciseCoverageReturns> takePreciseCoverageReturns
236 = std::make_unique<TakePreciseCoverageReturns>(std::move(result), 1001);
237 ASSERT_NE(takePreciseCoverageReturns, nullptr);
238
239 std::unique_ptr<PtJson> json;
240 ASSERT_EQ(takePreciseCoverageReturns->ToJson()->GetArray("result", &json), Result::SUCCESS);
241 ASSERT_NE(json, nullptr);
242 EXPECT_EQ(json->GetSize(), 1);
243 }
244
HWTEST_F_L0(PtReturnsTest,TakeTypeProfileReturns)245 HWTEST_F_L0(PtReturnsTest, TakeTypeProfileReturns)
246 {
247 auto types = std::vector<std::unique_ptr<TypeObject>>();
248 std::unique_ptr<TypeObject> typeObject = std::make_unique<TypeObject>();
249 typeObject->SetName("object1");
250
251 auto entries = std::vector<std::unique_ptr<TypeProfileEntry>>();
252 std::unique_ptr<TypeProfileEntry> typeProfileEntry = std::make_unique<TypeProfileEntry>();
253 typeProfileEntry->SetOffset(4);
254 typeProfileEntry->SetTypes(std::move(types));
255
256 auto result = std::vector<std::unique_ptr<ScriptTypeProfile>>();
257 std::unique_ptr<ScriptTypeProfile> scriptTypeProfile = std::make_unique<ScriptTypeProfile>();
258 scriptTypeProfile->SetUrl("url5");
259 scriptTypeProfile->SetScriptId("id5");
260 scriptTypeProfile->SetEntries(std::move(entries));
261 result.emplace_back(std::move(scriptTypeProfile));
262
263 std::unique_ptr<TakeTypeProfileReturns> takeTypeProfileturns
264 = std::make_unique<TakeTypeProfileReturns>(std::move(result));
265
266 std::unique_ptr<PtJson> json;
267 ASSERT_EQ(takeTypeProfileturns->ToJson()->GetArray("result", &json), Result::SUCCESS);
268 ASSERT_NE(json, nullptr);
269 EXPECT_EQ(json->GetSize(), 1);
270 }
271
HWTEST_F_L0(PtReturnsTest,GetCategoriesReturns)272 HWTEST_F_L0(PtReturnsTest, GetCategoriesReturns)
273 {
274 auto result = std::vector<std::string>();
275 std::string str = "getCategories";
276 result.emplace_back(str);
277 std::unique_ptr<GetCategoriesReturns> getCategoriesReturns = std::make_unique
278 <GetCategoriesReturns>(std::move(result));
279
280 std::unique_ptr<PtJson> json;
281 ASSERT_EQ(getCategoriesReturns->ToJson()->GetArray("categories", &json), Result::SUCCESS);
282 ASSERT_NE(json, nullptr);
283 EXPECT_EQ(json->GetSize(), 1);
284 }
285 }