1/*
2 * Copyright (c) 2023 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#ifndef ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_TRACING_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_TRACING_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsTracingTest : public TestActions {
23public:
24    JsTracingTest()
25    {
26        testAction = {
27            {SocketAction::SEND, "enable"},
28            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
29            {SocketAction::SEND, "runtime-enable"},
30            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
31            {SocketAction::SEND, "run"},
32            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
33            // load sample.js
34            {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35            // break on start
36            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37            {SocketAction::SEND, "cpuprofile-enable"},
38            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
39            {SocketAction::SEND, "cpuprofile"},
40            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
41            {SocketAction::SEND, "cpuprofile-stop"},
42            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [] (auto recv, auto, auto) -> bool {
43                std::unique_ptr<PtJson> json = PtJson::Parse(recv);
44                Result ret;
45                int id;
46                ret = json->GetInt("id", &id);
47                if (ret != Result::SUCCESS) {
48                    return false;
49                }
50
51                std::unique_ptr<PtJson> result;
52                ret = json->GetObject("result", &result);
53                if (ret != Result::SUCCESS) {
54                    return false;
55                }
56
57                std::unique_ptr<PtJson> profile;
58                ret = result->GetObject("profile", &profile);
59                if (ret != Result::SUCCESS) {
60                    return false;
61                }
62                return true;
63            }},
64            {SocketAction::SEND, "cpuprofile-disable"},
65            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
66            // reply success and run
67            {SocketAction::SEND, "success"},
68            {SocketAction::SEND, "resume"},
69            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
70            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
71        };
72    }
73
74    std::pair<std::string, std::string> GetEntryPoint() override
75    {
76        return {pandaFile_, entryPoint_};
77    }
78    ~JsTracingTest() = default;
79
80private:
81    std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc";
82    std::string sourceFile_ = DEBUGGER_JS_DIR "sample.js";
83    std::string entryPoint_ = "_GLOBAL::func_main_0";
84};
85
86std::unique_ptr<TestActions> GetJsTracingTest()
87{
88    return std::make_unique<JsTracingTest>();
89}
90}  // namespace panda::ecmascript::tooling::test
91
92#endif  // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_TRACING_TEST_H
93