• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 <gtest/gtest.h>
17 #include "cj_runtime.h"
18 #include "runtime.h"
19 #include "cj_mock_runtime.h"
20 
21 #include "event_runner.h"
22 #include "hilog_wrapper.h"
23 #include "cj_runtime.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace AbilityRuntime {
30 namespace {
31 const std::string TEST_BUNDLE_NAME = "com.ohos.contactsdataability";
32 const std::string TEST_MODULE_NAME = ".ContactsDataAbility";
33 const std::string TEST_ABILITY_NAME = "ContactsDataAbility";
34 const std::string TEST_CODE_PATH = "/data/storage/el1/bundle";
35 const std::string TEST_HAP_PATH = "/system/app/com.ohos.contactsdataability/Contacts_DataAbility.hap";
36 const std::string TEST_LIB_PATH = "/data/storage/el1/bundle/lib/";
37 const std::string TEST_MODULE_PATH = "/data/storage/el1/bundle/curCJModulePath";
38 }  // namespace
39 class CjRuntimeTest : public testing::Test {
40 public:
CjRuntimeTest()41     CjRuntimeTest()
42     {}
~CjRuntimeTest()43     ~CjRuntimeTest()
44     {}
45     static void SetUpTestCase(void);
46     static void TearDownTestCase(void);
47     void SetUp() override;
48     void TearDown() override;
49     Runtime::Options options_;
50 };
51 
SetUpTestCase(void)52 void CjRuntimeTest::SetUpTestCase(void)
53 {}
54 
TearDownTestCase(void)55 void CjRuntimeTest::TearDownTestCase(void)
56 {}
57 
SetUp(void)58 void CjRuntimeTest::SetUp(void)
59 {
60     options_.bundleName = TEST_BUNDLE_NAME;
61     options_.codePath = TEST_CODE_PATH;
62     options_.loadAce = false;
63     options_.isBundle = true;
64     options_.preload = false;
65     std::shared_ptr<AppExecFwk::EventRunner> eventRunner = AppExecFwk::EventRunner::Create(TEST_ABILITY_NAME);
66     options_.eventRunner = eventRunner;
67 }
68 
TearDown(void)69 void CjRuntimeTest::TearDown(void)
70 {}
71 
72 /**
73  * @tc.name: CjRuntimeCreate_001
74  * @tc.desc: Interface Create Test
75  * @tc.type: FUNC
76  */
77 HWTEST_F(CjRuntimeTest, CjRuntimeCreate_001, TestSize.Level1)
78 {
79     options_.preload = true;
80     options_.lang = CJRuntime::Language::JS;
81     std::unique_ptr<CJRuntime> runtime = std::make_unique<cjMockRuntime>();
82     auto cjRuntime = runtime->Create(options_);
83     EXPECT_EQ(cjRuntime, nullptr);
84 }
85 
86 /**
87  * @tc.name: CjRuntimeCreate_002
88  * @tc.desc: Interface Create Test for Fail Situation
89  * @tc.type: FUNC
90  */
91 HWTEST_F(CjRuntimeTest, CjRuntimeCreate_002, TestSize.Level1)
92 {
93     options_.preload = true;
94     options_.lang = CJRuntime::Language::JS;
95     std::unique_ptr<CJRuntime> runtime = std::make_unique<cjMockRuntime>();
96     auto cjRuntime = runtime->Create(options_);
97     EXPECT_TRUE(cjRuntime == nullptr);
98 }
99 
100 /**
101  * @tc.name: CjRuntimeGetLanguageTest_001
102  * @tc.desc: CjRuntime Test for GetLanguage
103  * @tc.type: FUNC
104  */
105 HWTEST_F(CjRuntimeTest, CjRuntimeGetLanguageTest_001, TestSize.Level0)
106 {
107     auto instance = std::make_unique<CJRuntime>();
108 
109     CJRuntime::Language language = instance->GetLanguage();
110     EXPECT_TRUE(language == CJRuntime::Language::CJ);
111     instance->UnLoadCJAppLibrary();
112 }
113 
114 /**
115  * @tc.name: CjRuntimeStartDebuggerMode_001
116  * @tc.desc: CjRuntime test for StartDebuggerMode.
117  * @tc.type: FUNC
118  */
119 HWTEST_F(CjRuntimeTest, CjRuntimeStartDebuggerMode_001, TestSize.Level0)
120 {
121     auto instance = std::make_unique<CJRuntime>();
122 
123     bool needBreakPoint = true;
124     bool debugApp = true;
125     const std::string processName = "test";
126 
127     CJRuntime::DebugOption debugOption;
128     debugOption.isStartWithDebug = needBreakPoint;
129     debugOption.isDebugApp = debugApp;
130     debugOption.processName = processName;
131 
132     instance->StartDebugMode(debugOption);
133     EXPECT_TRUE(debugOption.isStartWithDebug);
134     instance->StartDebugMode(debugOption);
135     EXPECT_TRUE(debugOption.isDebugApp);
136 }
137 
138 }  // namespace Runtime
139 }  // namespace OHOS
140