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 MOCK_JSNAPI_H
17 #define MOCK_JSNAPI_H
18 
19 #include <memory>
20 
21 #include "hilog_tag_wrapper.h"
22 #include "jsnapi.h"
23 
24 using RequestAotCallback =
25     std::function<int32_t(const std::string &bundleName, const std::string &moduleName, int32_t triggerMode)>;
26 
27 namespace panda {
28 class MockJSNApi final {
29 public:
30     MockJSNApi() = default;
31     virtual ~MockJSNApi() = default;
32 
GetInstance()33     static std::shared_ptr<MockJSNApi> GetInstance()
34     {
35         if (instance_ == nullptr) {
36             instance_ = std::make_shared<MockJSNApi>();
37         }
38         return instance_;
39     }
40 
SetRequestAotCallback(const RequestAotCallback & cb)41     void SetRequestAotCallback(const RequestAotCallback& cb)
42     {
43         requestAotCallback_ = cb;
44     }
45 
RequestAot(const std::string & bundleName,const std::string & moduleName,int32_t triggerMode)46     int32_t RequestAot(const std::string &bundleName, const std::string &moduleName, int32_t triggerMode)
47     {
48         if (requestAotCallback_ == nullptr) {
49             TAG_LOGE(AAFwkTag::TEST, "callback is invalid.");
50             return -1;
51         }
52 
53         return requestAotCallback_(bundleName, moduleName, triggerMode);
54     }
55 
56 private:
57     friend JSNApi;
58     static std::shared_ptr<MockJSNApi> instance_;
59     RequestAotCallback requestAotCallback_ = nullptr;
60 };
61 } // namespace panda
62 #endif // MOCK_JSNAPI_H
63