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 "inspector_test_base.h"
17 #include "combined_event_loop.h"
18 #include "common.h"
19
20 #include "assembly-emitter.h"
21 #include "assembly-parser.h"
22 #include "class_helper.h"
23 #include "compiler.h"
24 #include "mem/panda_string.h"
25 #include "runtime.h"
26 #include "runtime_options.h"
27 #include "source_lang_enum.h"
28 #include "tooling/pt_thread.h"
29 #include "utils/logger.h"
30 #include "utils/utf.h"
31
32 #include "gtest/gtest.h"
33 #include "gmock/gmock.h"
34
35 #include <system_error>
36 #include <utility>
37
38 using testing::HasSubstr;
39
40 namespace panda::tooling::inspector::test {
SetUp()41 void InspectorTestBase::SetUp()
42 {
43 // Check for unsupported methods on the server, i.e. with id.
44 EXPECT_CALL(logger_, LogLineInternal(Logger::Level::WARNING, Logger::Component::DEBUGGER,
45 HasSubstr(R"(Unsupported method: {"id")")))
46 .Times(0);
47
48 // Set up runtime.
49 RuntimeOptions options;
50 options.SetShouldInitializeIntrinsics(false);
51 options.SetShouldLoadBootPandaFiles(false);
52 Runtime::Create(options);
53
54 // Set up connection.
55 client_.Connect(server_, [](std::error_code ec) { ASSERT_TRUE(IsOk(ec)); });
56 (server_ + client_).Poll();
57
58 SetUpSourceFiles();
59
60 if (AttachDebugger()) {
61 client_.Call("Runtime.runIfWaitingForDebugger");
62 debugger_.GetHooks().VmInitialization(PtThread::NONE);
63 }
64 }
65
TearDown()66 void InspectorTestBase::TearDown()
67 {
68 (server_ + client_).Poll();
69 Runtime::Destroy();
70 }
71
LoadSourceFile(const std::string & source)72 Class *InspectorTestBase::LoadSourceFile(const std::string &source)
73 {
74 auto program = pandasm::Parser().Parse(source);
75 if (!program) {
76 return nullptr;
77 }
78
79 auto *classLinker = Runtime::GetCurrent()->GetClassLinker();
80
81 if (auto pf = pandasm::AsmEmitter::Emit(program.Value())) {
82 classLinker->AddPandaFile(std::move(pf));
83 } else {
84 return nullptr;
85 }
86
87 PandaString storage;
88 auto descriptor = ClassHelper::GetDescriptor(utf::CStringAsMutf8("_GLOBAL"), &storage);
89
90 ScopedMutatorLock lock;
91 return classLinker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY)->GetClass(descriptor);
92 }
93 } // namespace panda::tooling::inspector::test
94