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 <cstring>
17 #include <gtest/gtest.h>
18 #include <surface.h>
19 
20 #define private public
21 #include "nweb_adapter_helper.h"
22 #include "nweb_create_window.h"
23 #include "nweb_helper.h"
24 #undef private
25 
26 #include "nweb_init_params.h"
27 #include "ohos_adapter_helper.h"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace OHOS;
32 using namespace OHOS::Rosen;
33 namespace OHOS::NWeb {
34 namespace {
35 sptr<Surface> g_surface = nullptr;
36 const std::string MOCK_NWEB_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.arkwebcore";
37 #if defined(NWEB_PRINT_ENABLE)
38 const std::string PRINT_FILE_DIR = "/data/storage/el2/base/print.png";
39 const std::string PRINT_JOB_NAME = "webPrintTestJob";
40 #endif
41 } // namespace
42 
43 #if defined(NWEB_PRINT_ENABLE)
44 class PrintDocumentAdapterImpl : public OHOS::NWeb::PrintDocumentAdapterAdapter {
45 public:
PrintDocumentAdapterImpl()46     PrintDocumentAdapterImpl() {}
47     ~PrintDocumentAdapterImpl() = default;
48 
OnStartLayoutWrite(const std::string & jobId,const OHOS::NWeb::PrintAttributesAdapter & oldAttrs,const OHOS::NWeb::PrintAttributesAdapter & newAttrs,uint32_t fd,std::shared_ptr<PrintWriteResultCallbackAdapter> callback)49     void OnStartLayoutWrite(const std::string& jobId, const OHOS::NWeb::PrintAttributesAdapter& oldAttrs,
50         const OHOS::NWeb::PrintAttributesAdapter& newAttrs, uint32_t fd,
51         std::shared_ptr<PrintWriteResultCallbackAdapter> callback) override
52     {}
53 
OnJobStateChanged(const std::string & jobId,uint32_t state)54     void OnJobStateChanged(const std::string& jobId, uint32_t state) override {}
55 };
56 #endif
57 
58 class OhosAdapterHelperTest : public testing::Test {
59 public:
60     static void SetUpTestCase(void);
61     static void TearDownTestCase(void);
62     void SetUp();
63     void TearDown();
64 };
65 
SetUpTestCase(void)66 void OhosAdapterHelperTest::SetUpTestCase(void)
67 {
68     RSSurfaceNodeConfig config;
69     config.SurfaceNodeName = "webTestSurfaceName";
70     auto surfaceNode = RSSurfaceNode::Create(config, false);
71     EXPECT_NE(surfaceNode, nullptr);
72     g_surface = surfaceNode->GetSurface();
73     EXPECT_NE(g_surface, nullptr);
74 }
75 
TearDownTestCase(void)76 void OhosAdapterHelperTest::TearDownTestCase(void) {}
77 
SetUp(void)78 void OhosAdapterHelperTest::SetUp(void) {}
79 
TearDown(void)80 void OhosAdapterHelperTest::TearDown(void) {}
81 
82 /**
83  * @tc.name: OhosAdapterHelper_GetCookieManager_001.
84  * @tc.desc: Test the GetCookieManager.
85  * @tc.type: FUNC
86  * @tc.require:
87  */
88 HWTEST_F(OhosAdapterHelperTest, OhosAdapterHelper_GetCookieManager_001, TestSize.Level1)
89 {
90     std::string hapPath = "";
91     if (access(MOCK_NWEB_INSTALLATION_DIR.c_str(), F_OK) == 0) {
92         hapPath = MOCK_NWEB_INSTALLATION_DIR;
93     }
94     int32_t nweb_id = 1;
95     NWebHelper& helper = NWebHelper::Instance();
96     helper.SetBundlePath(hapPath);
97     helper.Init(false);
98     auto cook = helper.GetCookieManager();
99     EXPECT_EQ(cook, nullptr);
100     auto base = helper.GetDataBase();
101     EXPECT_NE(base, nullptr);
102     auto storage = helper.GetWebStorage();
103     EXPECT_NE(storage, nullptr);
104     auto nweb = helper.GetNWeb(nweb_id);
105     EXPECT_EQ(nweb, nullptr);
106     std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
107     helper.SetHttpDns(config);
108 }
109 
110 /**
111  * @tc.name: OhosAdapterHelper_GetInstance_002.
112  * @tc.desc: Test the GetInstance.
113  * @tc.type: FUNC
114  * @tc.require:
115  */
116 HWTEST_F(OhosAdapterHelperTest, OhosAdapterHelper_GetInstance_002, TestSize.Level1)
117 {
118     OhosAdapterHelper& helper = OhosAdapterHelper::GetInstance();
119     std::unique_ptr<AafwkAppMgrClientAdapter> client = helper.CreateAafwkAdapter();
120     EXPECT_NE(client, nullptr);
121     std::unique_ptr<PowerMgrClientAdapter> powerMgr = helper.CreatePowerMgrClientAdapter();
122     EXPECT_NE(powerMgr, nullptr);
123     std::unique_ptr<DisplayManagerAdapter> display = helper.CreateDisplayMgrAdapter();
124     EXPECT_NE(display, nullptr);
125     std::unique_ptr<BatteryMgrClientAdapter> battery = helper.CreateBatteryClientAdapter();
126     EXPECT_NE(battery, nullptr);
127     helper.GetOhosWebDataBaseAdapterInstance();
128     std::unique_ptr<NetConnectAdapter> connect = helper.CreateNetConnectAdapter();
129     EXPECT_NE(connect, nullptr);
130     helper.GetPasteBoard();
131     std::unique_ptr<AudioRendererAdapter> audioRender = helper.CreateAudioRendererAdapter();
132     EXPECT_NE(audioRender, nullptr);
133     std::unique_ptr<AudioCapturerAdapter> audioCapter = helper.CreateAudioCapturerAdapter();
134     EXPECT_NE(audioCapter, nullptr);
135     helper.GetAudioSystemManager();
136     helper.GetWebPermissionDataBaseInstance();
137     std::unique_ptr<MMIAdapter> mmiAdapter = helper.CreateMMIAdapter();
138     EXPECT_NE(mmiAdapter, nullptr);
139     auto socPerfClientAdapterImpl = helper.CreateSocPerfClientAdapter();
140     EXPECT_NE(socPerfClientAdapterImpl, nullptr);
141     auto ohosResourceAdapterImpl = helper.GetResourceAdapter("testPath");
142     EXPECT_NE(ohosResourceAdapterImpl, nullptr);
143 #if defined(NWEB_PRINT_ENABLE)
144     PrintManagerAdapter& printAdapter = helper.GetPrintManagerInstance();
145     std::vector<std::string> fileList = { PRINT_FILE_DIR };
146     std::vector<uint32_t> fdList = { 1 };
147     std::string taskId;
148     int32_t ret = printAdapter.StartPrint(fileList, fdList, taskId);
149     EXPECT_EQ(ret, -1);
150     std::shared_ptr<PrintDocumentAdapterAdapter> printDocumentAdapterImpl;
151     PrintAttributesAdapter printAttributesAdapter;
152     EXPECT_EQ(printAdapter.Print(PRINT_JOB_NAME, printDocumentAdapterImpl, printAttributesAdapter), -1);
153     void* token = nullptr;
154     EXPECT_EQ(printAdapter.Print(PRINT_JOB_NAME, printDocumentAdapterImpl, printAttributesAdapter, token), -1);
155 #endif
156     sptr<Surface> surface = nullptr;
157     std::shared_ptr<NWebEngineInitArgsImpl> initArgs = std::make_shared<NWebEngineInitArgsImpl>();
158     uint32_t width = 1;
159     uint32_t height = 1;
160     auto nweb = NWebAdapterHelper::Instance().CreateNWeb(surface, initArgs, width, height);
161     EXPECT_EQ(nweb, nullptr);
162 }
163 
164 /**
165  * @tc.name: OhosAdapterHelper_GetDataBase_003.
166  * @tc.desc: Test the GetDataBase.
167  * @tc.type: FUNC
168  * @tc.require:
169  */
170 HWTEST_F(OhosAdapterHelperTest, OhosAdapterHelper_GetDataBase_003, TestSize.Level1)
171 {
172     int32_t nweb_id = 1;
173     NWebHelper& helper = NWebHelper::Instance();
174     std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
175     std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
176     NWebHelper::Instance().SetHttpDns(config);
177     auto webview = helper.CreateNWeb(create_info);
178     EXPECT_EQ(webview, nullptr);
179     auto cook = helper.GetCookieManager();
180     EXPECT_EQ(cook, nullptr);
181     auto base = helper.GetDataBase();
182     EXPECT_NE(base, nullptr);
183     auto storage = helper.GetWebStorage();
184     EXPECT_NE(storage, nullptr);
185     auto nweb = helper.GetNWeb(nweb_id);
186     EXPECT_EQ(nweb, nullptr);
187 }
188 
189 /**
190  * @tc.name: OhosAdapterHelper_GetSystemPropertiesInstance_004.
191  * @tc.desc: Test the GetSystemPropertiesInstance.
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(OhosAdapterHelperTest, OhosAdapterHelper_GetSystemPropertiesInstance_004, TestSize.Level1)
196 {
197     OhosAdapterHelper& helper = OhosAdapterHelper::GetInstance();
198     helper.GetSystemPropertiesInstance();
199     helper.GetVSyncAdapter();
200     auto initWebAdapter = helper.GetInitWebAdapter();
201     EXPECT_NE(initWebAdapter, nullptr);
202     helper.GetKeystoreAdapterInstance();
203     helper.GetEnterpriseDeviceManagementInstance();
204     helper.GetDatashareInstance();
205     auto imfAdapter = helper.CreateIMFAdapter();
206     EXPECT_NE(imfAdapter, nullptr);
207     auto managerAdapter = helper.GetRootCertDataAdapter();
208     EXPECT_NE(managerAdapter, nullptr);
209     helper.GetAccessTokenAdapterInstance();
210     auto eventHandler = helper.GetEventHandlerAdapter();
211     EXPECT_NE(eventHandler, nullptr);
212     auto playerAdapter = helper.CreatePlayerAdapter();
213     EXPECT_NE(playerAdapter, nullptr);
214     helper.GetWindowAdapterInstance();
215     helper.GetHiSysEventAdapterInstance();
216     helper.GetHiTraceAdapterInstance();
217     helper.GetNetProxyInstance();
218     helper.GetCameraManagerAdapter();
219     auto screenCapture = helper.CreateScreenCaptureAdapter();
220     EXPECT_NE(screenCapture, nullptr);
221     auto dateTimeFormat = helper.CreateDateTimeFormatAdapter();
222     EXPECT_NE(dateTimeFormat, nullptr);
223     auto nativeImage = helper.CreateNativeImageAdapter();
224     EXPECT_NE(nativeImage, nullptr);
225 }
226 } // namespace OHOS::NWeb
227