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")"))).Times(0);
46
47 // Set up runtime.
48 RuntimeOptions options;
49 options.SetShouldInitializeIntrinsics(false);
50 options.SetShouldLoadBootPandaFiles(false);
51 Runtime::Create(options);
52
53 // Set up connection.
54 client_.Connect(server_, [](std::error_code ec) { ASSERT_TRUE(IsOk(ec)); });
55 (server_ + client_).Poll();
56
57 SetUpSourceFiles();
58
59 if (AttachDebugger()) {
60 client_.Call("Runtime.runIfWaitingForDebugger");
61 debugger_.GetHooks().VmInitialization(PtThread::NONE);
62 }
63 }
64
TearDown()65 void InspectorTestBase::TearDown()
66 {
67 (server_ + client_).Poll();
68 Runtime::Destroy();
69 }
70
LoadSourceFile(const std::string & source)71 Class *InspectorTestBase::LoadSourceFile(const std::string &source)
72 {
73 auto program = pandasm::Parser().Parse(source);
74 if (!program) {
75 return nullptr;
76 }
77
78 auto *classLinker = Runtime::GetCurrent()->GetClassLinker();
79
80 if (auto pf = pandasm::AsmEmitter::Emit(program.Value())) {
81 classLinker->AddPandaFile(std::move(pf));
82 } else {
83 return nullptr;
84 }
85
86 PandaString storage;
87 auto descriptor = ClassHelper::GetDescriptor(utf::CStringAsMutf8("_GLOBAL"), &storage);
88
89 ScopedMutatorLock lock;
90 return classLinker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY)->GetClass(descriptor);
91 }
92 } // namespace panda::tooling::inspector::test
93