提交 31c9e509 编写于 作者: Y y00314596

add by yangqing3@huawei.com

Signed-off-by: Ny00314596 <yangqing3@huawei.com>
上级 d47f951c
......@@ -17,6 +17,7 @@ group("global") {
testonly = true
if (is_standard_system) {
deps = [
"global_napi_test:ActsGlobalNapiTest",
"i18n_standard/intljs:intljs_test",
"perf/perfjs:perfjs_test",
"resmgr_standard/resmgrjs:resmgrjs_test",
......
......@@ -30,7 +30,7 @@ config("public_config") {
}
ohos_shared_library("resmgrndk") {
sources = [ "./napi/test_string.cpp" ]
sources = [ "./napi/global_napi_test.cpp" ]
if (target_cpu == "arm") {
libs = [ "${clang_base_path}/../libcxx-ndk/lib/arm-linux-ohos/c++/libc++_shared.so" ]
} else if (target_cpu == "arm64") {
......@@ -38,7 +38,10 @@ ohos_shared_library("resmgrndk") {
} else {
libs = []
}
include_dirs = [ "//test/xts/acts/arkui/ace_napi_test/entry/src/main/cpp" ]
include_dirs = [
"//base/global/resource_management/interfaces/native/resource/include",
"//test/xts/acts/arkui/ace_napi_test/entry/src/main/cpp",
]
configs = [ ":config" ]
deps = [
......@@ -47,5 +50,7 @@ ohos_shared_library("resmgrndk") {
"//foundation/arkui/napi:ace_napi",
]
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
output_extension = "so"
}
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "napi/native_api.h"
#include "raw_file_manager.h"
#include "raw_file.h"
#include "raw_dir.h"
#include "hilog/log.h"
#include <js_native_api.h>
#include <js_native_api_types.h>
#include <vector>
int GLOBAL_RESMGR = 0xDDD;
static napi_value GetFileList(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value argv[2] = { nullptr };
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
napi_valuetype valueType;
napi_typeof(env, argv[0], &valueType);
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string filename(strBuf, strSize);
RawDir* rawDir = OH_ResourceManager_OpenRawDir(mNativeResMgr, filename.c_str());
int count = OH_ResourceManager_GetRawFileCount(rawDir);
std::vector<std::string> tempArray;
for(int i = 0; i < count; i++) {
std::string filename = OH_ResourceManager_GetRawFileName(rawDir, i);
tempArray.emplace_back(filename);
}
napi_value fileList;
napi_create_array(env, &fileList);
for (size_t i = 0; i < tempArray.size(); i++) {
napi_value jsString;
napi_create_string_utf8(env, tempArray[i].c_str(), NAPI_AUTO_LENGTH, &jsString);
napi_set_element(env, fileList, i, jsString);
}
OH_ResourceManager_CloseRawDir(rawDir);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
return fileList;
}
napi_value CreateJsArrayValue(napi_env env, std::unique_ptr<uint8_t[]> &data, long length)
{
napi_value buffer;
napi_status status = napi_create_external_arraybuffer(env, data.get(), length,
[](napi_env env, void *data, void *hint) {
delete[] static_cast<char*>(data);
}, nullptr, &buffer);
if (status != napi_ok) {
return nullptr;
}
napi_value result = nullptr;
status = napi_create_typedarray(env, napi_uint8_array, length, buffer, 0, &result);
if (status != napi_ok) {
return nullptr;
}
data.release();
return result;
}
static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value argv[2] = { nullptr };
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
napi_valuetype valueType;
napi_typeof(env, argv[0], &valueType);
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string filename(strBuf, strSize);
RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
if (rawFile != nullptr) {
return nullptr;
}
long len = OH_ResourceManager_GetRawFileSize(rawFile);
std::unique_ptr<uint8_t[]> data= std::make_unique<uint8_t[]>(len);
long offset = OH_ResourceManager_GetRawFileOffset(rawFile);
if(offset == 0){
return nullptr;
}
long size = OH_ResourceManager_SeekRawFile(rawFile, 1, 0);
if(size == -1){
return nullptr;
}
int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len);
if(res == 0){
return nullptr;
}
OH_ResourceManager_CloseRawFile(rawFile);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
return CreateJsArrayValue(env, data, len);
}
napi_value createJsFileDescriptor(napi_env env, RawFileDescriptor &descriptor)
{
napi_value result;
napi_status status = napi_create_object(env, &result);
if (status != napi_ok) {
return result;
}
napi_value fd;
status = napi_create_int32(env, descriptor.fd, &fd);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "fd", fd);
if (status != napi_ok) {
return result;
}
napi_value offset;
status = napi_create_int64(env, descriptor.start, &offset);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "offset", offset);
if (status != napi_ok) {
return result;
}
napi_value length;
status = napi_create_int64(env, descriptor.length, &length);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "length", length);
if (status != napi_ok) {
return result;
}
return result;
}
static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value argv[2] = { nullptr };
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
napi_valuetype valueType;
napi_typeof(env, argv[0], &valueType);
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string filename(strBuf, strSize);
RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
if (rawFile != nullptr) {
return nullptr;
}
RawFileDescriptor descriptor;
OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
OH_ResourceManager_ReleaseRawFileDescriptor(descriptor);
OH_ResourceManager_CloseRawFile(rawFile);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
return createJsFileDescriptor(env,descriptor);
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "GetFileList", nullptr, GetFileList, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "GetRawFileContent", nullptr, GetRawFileContent, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "GetRawFileDescriptor", nullptr, GetRawFileDescriptor, nullptr, nullptr, nullptr, napi_default, nullptr }
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
static napi_module demoModule = {
.nm_version =1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "resmgrndk",
.nm_priv = ((void*)0),
.reserved = { 0 },
};
extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
napi_module_register(&demoModule);
}
\ No newline at end of file
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "native_common.h"
#include "rawfile/raw_file_manager.h"
#include <fstream>
#include <memory>
#include <vector>
#include "hilog/log.h"
#include "node_api.h"
namespace OHOS {
namespace Global {
namespace Resource {
#define GET_PARAMS(env, info, num) \
size_t argc = num; \
napi_value argv[num] = {nullptr}; \
napi_value thisVar = nullptr; \
void *data = nullptr; \
napi_get_cb_info(env, info , &argc, argv, &thisVar, &data)
void NdkAsyncContext::SetErrorMsg(const std::string &msg)
{
errMsg_ = msg;
success_ = false;
}
napi_async_execute_callback TestRawFileExecute()
{
return [](napi_env env, void* data) {
NdkAsyncContext *asyncContext = static_cast<NdkAsyncContext*>(data);
// test OH_ResourceManager_OpenRawDir
RawDir* rawDir = OH_ResourceManager_OpenRawDir(asyncContext->ndk_, asyncContext->path_.c_str());
// test OH_ResourceManager_GetRawFileCount
int count = OH_ResourceManager_GetRawFileCount(rawDir);
// test OH_ResourceManager_GetRawFileName
for (int i = 0; i< count; i++) {
std::string tempFileName = OH_ResourceManager_GetRawFileName(rawDir, i);
}
// test OH_ResourceManager_OpenRawFile
std::string fileName = OH_ResourceManager_GetRawFileName(rawDir, 0);
asyncContext->path_ = fileName;
RawFile* rawFile = OH_ResourceManager_OpenRawFile(asyncContext->ndk_, fileName.c_str());
// test OH_ResourceManager_GetRawFileSize
asyncContext->len_ = OH_ResourceManager_GetRawFileSize(rawFile);
// // test OH_ResourceManager_SeekRawFile
// // test OH_ResourceManager_GetRawFileOffset
// test OH_ResourceManager_GetRawFileDescriptor
RawFileDescriptor descriptor;
bool getFd = OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
// test OH_ResourceManager_ReadRawFile
asyncContext->mediaData = std::make_unique<char[]>(asyncContext->len_);
// HiLog::Error(LABEL, "ReadRawFile:%{public}d", OH_ResourceManager_ReadRawFile(rawFile, asyncContext->mediaData.get(), asyncContext->len_));
// test OH_ResourceManager_ReleaseRawFileDescriptor
OH_ResourceManager_ReleaseRawFileDescriptor(descriptor);
// test OH_ResourceManager_CloseRawFile
OH_ResourceManager_CloseRawFile(rawFile);
// test OH_ResourceManager_CloseRawDir
OH_ResourceManager_CloseRawDir(rawDir);
// test OH_ResourceManager_ReleaseNativeResourceManager
OH_ResourceManager_ReleaseNativeResourceManager(asyncContext->ndk_);
asyncContext->createValueFunc_ = [](napi_env env, NdkAsyncContext &context) -> napi_value {
napi_value buffer;
napi_status status = napi_create_external_arraybuffer(env, context.mediaData.get(), context.len_,
[](napi_env env, void *data, void *hint) {
delete[] static_cast<char*>(data);
}, nullptr, &buffer);
if (status != napi_ok) {
context.SetErrorMsg("Failed to create media external array buffer");
return nullptr;
}
napi_value result = nullptr;
status = napi_create_typedarray(env, napi_uint8_array, context.len_, buffer, 0, &result);
if (status != napi_ok) {
context.SetErrorMsg("Failed to create media typed array");
return nullptr;
}
context.mediaData.release();
return result;
};
};
}
auto completeFunc = [](napi_env env, napi_status status, void* data) {
NdkAsyncContext* asyncContext = static_cast<NdkAsyncContext*>(data);
napi_value finalResult = nullptr;
if (asyncContext->createValueFunc_ != nullptr) {
finalResult = asyncContext->createValueFunc_(env, *asyncContext);
}
napi_value result[] = { nullptr, nullptr };
if (asyncContext->success_) {
napi_get_undefined(env, &result[0]);
result[1] = finalResult;
} else {
napi_value message = nullptr;
napi_create_string_utf8(env, asyncContext->errMsg_.c_str(), NAPI_AUTO_LENGTH, &message);
napi_create_error(env, nullptr, message, &result[0]);
napi_get_undefined(env, &result[1]);
}
do {
if (asyncContext->deferred_) {
if (asyncContext->success_) {
if (napi_resolve_deferred(env, asyncContext->deferred_, result[1]) != napi_ok) {
}
} else {
if (napi_reject_deferred(env, asyncContext->deferred_, result[0]) != napi_ok) {
}
}
} else {
napi_value callback = nullptr;
napi_status status = napi_get_reference_value(env, asyncContext->callbackRef_, &callback);
if (status != napi_ok) {
break;
}
napi_value userRet = nullptr;
status = napi_call_function(env, nullptr, callback, sizeof(result) / sizeof(napi_value), result, &userRet);
if (status != napi_ok) {
break;
}
status = napi_delete_reference(env, asyncContext->callbackRef_);
if (status != napi_ok) {
break;
}
}
} while (false);
napi_delete_async_work(env, asyncContext->work_);
delete asyncContext;
};
napi_value GetResourceManager(napi_env env, napi_callback_info info)
{
GET_PARAMS(env, info, 3);
std::unique_ptr<NdkAsyncContext> asyncContext = std::make_unique<NdkAsyncContext>();
for (size_t i = 0; i < argc; i++) {
napi_valuetype valueType;
napi_typeof(env, argv[i], &valueType);
if (i == 0 && valueType == napi_string) {
size_t len = 0;
napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
if (status != napi_ok) {
return nullptr;
}
std::vector<char> buf(len + 1);
status = napi_get_value_string_utf8(env, argv[0], buf.data(), len + 1, &len);
if (status != napi_ok) {
return nullptr;
}
asyncContext->path_ = buf.data();
} else if (i == 1 && valueType == napi_object) {
// test OH_ResourceManager_InitNativeResourceManager
asyncContext->ndk_ = OH_ResourceManager_InitNativeResourceManager(env, argv[i]);
} else if (i == 2 && valueType == napi_function) {
napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef_);
break;
} else {
// self resorucemanager with promise
}
}
napi_value result = nullptr;
if (asyncContext->callbackRef_ == nullptr) {
napi_create_promise(env, &asyncContext->deferred_, &result);
} else {
napi_get_undefined(env, &result);
}
napi_value resource = nullptr;
napi_create_string_utf8(env, "testRawFile", NAPI_AUTO_LENGTH, &resource);
napi_status status = napi_create_async_work(env, nullptr, resource, TestRawFileExecute(), completeFunc,
static_cast<void*>(asyncContext.get()), &asyncContext->work_);
if (status != napi_ok) {
return result;
}
status = napi_queue_async_work(env, asyncContext->work_);
if (status != napi_ok) {
return result;
}
asyncContext.release();
return result;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "testRawFile", nullptr, GetResourceManager, nullptr, nullptr, nullptr, napi_default, nullptr }
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
static napi_module demoModule = {
.nm_version =1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "resmgrndk",
.nm_priv = ((void*)0),
.reserved = { 0 },
};
extern "C" __attribute__((constructor)) void RegisterModule(void)
{
napi_module_register(&demoModule);
}
}
}
}
\ No newline at end of file
......@@ -21,30 +21,62 @@ import ndk from 'libresmgrndk.so'
export default function abilityTest() {
describe('ActsAbilityTest', function () {
it('assertContain',0, function () {
console.info("it begin")
let a = 'abc'
let b = 'b'
expect(a).assertContain(b)
expect(a).assertEqual(a)
})
/* *
* @tc.number SUB_GLOBAL_RESMGR_NDK_0100
* @tc.name test ResourceManager NDK interface
* @tc.desc test ResourceManager NDK interface
*/
it('getResourceManager_ndktest_0100', 0, async function (done) {
console.log("getResourceManager_ndktest_0100 1");
resManager.getResourceManager().then(mgr => {
console.log("getResourceManager_ndktest_0100 2");
expect(mgr !== null).assertTrue();
console.log("getResourceManager_ndktest_0100 22");
let value = ndk.GetFileList(mgr,"test");
console.log("getResourceManager_ndktest_0100 3");
console.log("getResourceManager_ndktest_0100" + value);
console.log("getResourceManager_ndktest_0100" + value.length);
expect(value.length > 0).assertTrue();
})
done();
})
/* *
* @tc.number SUB_GLOBAL_RESMGR_NDK_0200
* @tc.name test ResourceManager NDK interface
* @tc.desc test ResourceManager NDK interface
*/
it('getResourceManager_ndktest_0200', 0, async function (done) {
console.log("getResourceManager_ndktest_0200 1");
resManager.getResourceManager().then(mgr => {
console.log("getResourceManager_ndktest_0200 2");
expect(mgr !== null).assertTrue();
console.log("getResourceManager_ndktest_0200 22");
let value = ndk.GetRawFileContent(mgr,"test/aa.xml");
console.log("getResourceManager_ndktest_0200 3");
console.log("getResourceManager_ndktest_0200" + value);
expect(value !== null).assertTrue();
})
done();
})
/* *
* @tc.number SUB_GLOBAL_RESMGR_NDK_0300
* @tc.name test ResourceManager NDK interface
* @tc.desc test ResourceManager NDK interface
*/
it('getResourceManager_ndktest_0300', 0, async function (done) {
console.log("getResourceManager_ndktest_0300 1");
resManager.getResourceManager().then(mgr => {
console.log("getResourceManager_ndktest_0300 2");
expect(mgr !== null).assertTrue();
ndk.testRawFile("test",mgr,(error,value) => {
if(error != null){
console.log("getResourceManager_ndktest_0100" + error);
} else{
console.log("getResourceManager_ndktest_0100" + value.length);
expect(value.length > 0).assertTrue();
}
});
console.log("getResourceManager_ndktest_0300 22");
let value = ndk.GetRawFileDescriptor(mgr,"test/aa.xml");
console.log("getResourceManager_ndktest_0300 3");
console.log("getResourceManager_ndktest_0300" + value.fd);
console.log("getResourceManager_ndktest_0300" + value.offset);
console.log("getResourceManager_ndktest_0300" + value.length);
expect(value !== null).assertTrue();
})
done();
})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册