• 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 "napi/native_api.h"
17 #include "hilog/log.h"
18 #include <string>
19 #include <unistd.h>
20 #include "database/pasteboard/oh_pasteboard.h"
21 #include "database/pasteboard/oh_pasteboard_err_code.h"
22 #include "database/udmf/udmf.h"
23 #include "database/udmf/udmf_meta.h"
24 #include "database/udmf/uds.h"
25 #include "database/udmf/utd.h"
26 #include <iostream>
27 #include <map>
28 #include <ctime>
29 #include "common.h"
30 #include <cctype>
31 #include <condition_variable>
32 #include <cstdint>
33 #include <fcntl.h>
34 #include <functional>
35 #include <iostream>
36 #include <list>
37 #include <map>
38 #include <memory>
39 #include <mutex>
40 #include <string>
41 #include <sys/mman.h>
42 #include <thread>
43 #include <vector>
44 #include <string>
45 #include <random>
46 #include <chrono>
47 const char *TAG = "testLog";
48 
OH_Pasteboard_GetMimeTypes0100(napi_env env,napi_callback_info info)49 static napi_value OH_Pasteboard_GetMimeTypes0100(napi_env env, napi_callback_info info)
50 {
51     napi_value result = nullptr;
52     OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
53     NAPI_ASSERT(env, pasteboard != nullptr, "OH_Pasteboard_Create is fail.");
54     OH_Pasteboard_ClearData(pasteboard);
55     unsigned int count = 1000;
56     char** res = OH_Pasteboard_GetMimeTypes(pasteboard, &count);
57     NAPI_ASSERT(env, count == 0, "OH_Pasteboard_GetMimeTypes is fail.");
58     NAPI_ASSERT(env, res == nullptr, "OH_Pasteboard_GetMimeTypes is fail.");
59     napi_create_int32(env, res == nullptr, &result);
60     OH_Pasteboard_Destroy(pasteboard);
61     return result;
62 }
63 
OH_Pasteboard_GetMimeTypes0200(napi_env env,napi_callback_info info)64 static napi_value OH_Pasteboard_GetMimeTypes0200(napi_env env, napi_callback_info info)
65 {
66     napi_value result = nullptr;
67     OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
68     NAPI_ASSERT(env, pasteboard != nullptr, "OH_Pasteboard_Create is fail.");
69     OH_UdmfData* setData = OH_UdmfData_Create();
70     NAPI_ASSERT(env, setData != nullptr, "OH_UdmfData_Create is fail.");
71     OH_UdmfRecord* record = OH_UdmfRecord_Create();
72     NAPI_ASSERT(env, record != nullptr, "OH_UdmfData_Create is fail.");
73     OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
74     NAPI_ASSERT(env, plainText != nullptr, "OH_UdmfData_Create is fail.");
75 
76     char content[] = "hello world";
77     OH_UdsPlainText_SetContent(plainText, content);
78     OH_UdmfRecord_AddPlainText(record, plainText);
79     OH_UdmfData_AddRecord(setData, record);
80     OH_Pasteboard_SetData(pasteboard, setData);
81 
82     unsigned int count = 1000;
83     char** res = OH_Pasteboard_GetMimeTypes(pasteboard, &count);
84     NAPI_ASSERT(env, count == 1, "OH_Pasteboard_GetMimeTypes is fail.");
85     NAPI_ASSERT(env, res != nullptr, "OH_Pasteboard_GetMimeTypes is fail.");
86     NAPI_ASSERT(env, strcmp("text/plain", res[0]) == 0, "OH_Pasteboard_GetMimeTypes is fail.");
87     napi_create_int32(env, strcmp("text/plain", res[0]) == 0, &result);
88     OH_Pasteboard_Destroy(pasteboard);
89     return result;
90 }
91 
OH_Pasteboard_GetMimeTypes0300(napi_env env,napi_callback_info info)92 static napi_value OH_Pasteboard_GetMimeTypes0300(napi_env env, napi_callback_info info)
93 {
94     napi_value result = nullptr;
95     OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
96     NAPI_ASSERT(env, pasteboard != nullptr, "OH_Pasteboard_Create is fail.");
97     OH_UdmfData* setData = OH_UdmfData_Create();
98     NAPI_ASSERT(env, setData != nullptr, "OH_UdmfData_Create is fail.");
99     OH_UdmfRecord* record = OH_UdmfRecord_Create();
100     NAPI_ASSERT(env, record != nullptr, "OH_UdmfData_Create is fail.");
101     OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
102     NAPI_ASSERT(env, plainText != nullptr, "OH_UdmfData_Create is fail.");
103     char content[] = "hello world";
104     OH_UdsPlainText_SetContent(plainText, content);
105     OH_UdmfRecord_AddPlainText(record, plainText);
106     OH_UdmfData_AddRecord(setData, record);
107 
108     OH_UdmfRecord* record2 = OH_UdmfRecord_Create();
109     NAPI_ASSERT(env, record2 != nullptr, "OH_UdmfData_Create is fail.");
110     OH_UdsHtml* htmlText = OH_UdsHtml_Create();
111     NAPI_ASSERT(env, htmlText != nullptr, "OH_UdmfData_Create is fail.");
112     char html[] = "<div class='disabled'>hello</div>";
113     OH_UdsHtml_SetContent(htmlText, html);
114     OH_UdmfRecord_AddHtml(record2, htmlText);
115     OH_UdmfData_AddRecord(setData, record2);
116     OH_Pasteboard_SetData(pasteboard, setData);
117 
118     unsigned int count = 1000;
119     int num = 2;
120     char** res = OH_Pasteboard_GetMimeTypes(pasteboard, &count);
121     NAPI_ASSERT(env, count == num, "OH_Pasteboard_GetMimeTypes is fail.");
122     NAPI_ASSERT(env, res != nullptr, "OH_Pasteboard_GetMimeTypes is fail.");
123     NAPI_ASSERT(env, (strcmp("text/plain", res[0]) == 0 && strcmp("text/html", res[1]) == 0) ||
124         (strcmp("text/plain", res[1]) == 0 && strcmp("text/html", res[0]) == 0), "OH_Pasteboard_GetMimeTypes is fail.");
125     napi_create_int32(env, (strcmp("text/plain", res[0]) == 0 && strcmp("text/html", res[1]) == 0) ||
126         (strcmp("text/plain", res[1]) == 0 && strcmp("text/html", res[0]) == 0), &result);
127     OH_Pasteboard_Destroy(pasteboard);
128     return result;
129 }
130 
131 EXTERN_C_START
Init(napi_env env,napi_value exports)132 static napi_value Init(napi_env env, napi_value exports)
133 {
134     napi_property_descriptor desc[] = {
135         {"OH_Pasteboard_GetMimeTypes0100", nullptr, OH_Pasteboard_GetMimeTypes0100,
136          nullptr, nullptr, nullptr, napi_default, nullptr},
137         {"OH_Pasteboard_GetMimeTypes0200", nullptr, OH_Pasteboard_GetMimeTypes0200,
138          nullptr, nullptr, nullptr, napi_default, nullptr},
139         {"OH_Pasteboard_GetMimeTypes0300", nullptr, OH_Pasteboard_GetMimeTypes0300,
140          nullptr, nullptr, nullptr, napi_default, nullptr}
141     };
142     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
143     return exports;
144 }
145 EXTERN_C_END
146 
147 static napi_module demoModule = {
148     .nm_version = 1,
149     .nm_flags = 0,
150     .nm_filename = nullptr,
151     .nm_register_func = Init,
152     .nm_modname = "entry",
153     .nm_priv = ((void*)0),
154     .reserved = { 0 },
155 };
156 
RegisterEntryModule(void)157 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
158 {
159     napi_module_register(&demoModule);
160 }
161