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 #ifndef OHOS_ABILITY_RUNTIME_JS_TEST_RUNNER_H
17 #define OHOS_ABILITY_RUNTIME_JS_TEST_RUNNER_H
18 
19 #include "bundle_info.h"
20 #include "js_runtime.h"
21 #include "test_runner.h"
22 
23 class NativeReference;
24 
25 namespace OHOS {
26 namespace RunnerRuntime {
27 using namespace AppExecFwk;
28 using namespace AbilityRuntime;
29 
30 class JsTestRunner : public TestRunner {
31 public:
32     /**
33      * Creates a TestRunner instance with the input parameter passed.
34      *
35      * @param runtime Indicates the ability runtime.
36      * @param args Indicates the AbilityDelegatorArgs object.
37      * @param bundleInfo Indicates the bundle info.
38      * @return the TestRunner object if JsTestRunner object is created successfully; returns null otherwise.
39      */
40     static std::unique_ptr<TestRunner> Create(const std::unique_ptr<Runtime> &runtime,
41         const std::shared_ptr<AbilityDelegatorArgs> &args, const AppExecFwk::BundleInfo &bundleInfo, bool isFaJsModel);
42 
43     /**
44      * Default deconstructor used to deconstruct.
45      */
46     ~JsTestRunner() override;
47 
48     /**
49      * Prepares the testing environment for running test code.
50      */
51     void Prepare() override;
52 
53     /**
54      * Runs all test code.
55      */
56     void Run() override;
57 
58     bool Initialize() override;
59 
60 private:
61     JsTestRunner(JsRuntime &jsRuntime,
62         const std::shared_ptr<AbilityDelegatorArgs> &args, const AppExecFwk::BundleInfo &bundleInfo, bool isFaJsModel);
63 
64     void CallObjectMethod(const char *name, napi_value const *argv = nullptr, size_t argc = 0);
65     void ReportFinished(const std::string &msg);
66     void ReportStatus(const std::string &msg);
67 
68     JsRuntime &jsRuntime_;
69     std::unique_ptr<NativeReference> jsTestRunnerObj_;
70     std::string srcPath_;
71     std::string hapPath_;
72     bool isFaJsModel_ = false;
73 };
74 }  // namespace RunnerRuntime
75 }  // namespace OHOS
76 #endif  // OHOS_ABILITY_RUNTIME_JS_TEST_RUNNER_H
77