提交 dcd1963b 编写于 作者: C chenyuyan

Signed-off-by: chenyuyan <chenyuyan3@huawei.com>

Change-Id: Icbd645af1016803cb13d6fe32c3a48cb0ea7ac5d
add ipc_napi_test and napiRemoteObjectTest for TDD

Change-Id: Iec40840ea09599ab5bdafff30c482ccc0c225e37
Signed-off-by: Nchenyuyan <chenyuyan3@huawei.com>
上级 832d0b4f
......@@ -98,6 +98,33 @@ ohos_unittest("IPCHiTraceUnitTest") {
"//foundation/communication/ipc/test/resource/ipc/ohos_test.xml"
}
if (support_jsapi) {
ohos_unittest("IPCNapiUnitTest") {
module_out_path = MODULE_OUTPUT_PATH
include_dirs = [
"//foundation/communication/ipc/interfaces/innerkits/ipc_core/include",
"//foundation/arkui/napi/interfaces/kits",
]
sources = [ "ipc_napi_unittest.cpp" ]
configs = []
deps = [
"//foundation/arkui/napi:ace_napi",
"//third_party/googletest:gtest_main",
]
external_deps = [
"hiviewdfx_hilog_native:libhilog",
"ipc_js:rpc",
]
resource_config_file =
"//foundation/communication/ipc/test/resource/ipc/ohos_test.xml"
}
}
###############################################################################
group("unittest") {
testonly = true
......@@ -106,6 +133,9 @@ group("unittest") {
":IPCHiTraceUnitTest",
":IPCNativeUnitTest",
]
if (support_jsapi) {
deps += [ ":IPCNapiUnitTest" ]
}
}
###############################################################################
......
/*
* Copyright (C) 2022 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 <gtest/gtest.h>
#include "napi_remote_object.h"
#include <unistd.h>
#include <securec.h>
using namespace testing::ext;
class IPCNapiTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void IPCNapiTest::SetUpTestCase(void)
{
// input testsuit setup step,setup invoked before all testcases
}
void IPCNapiTest::TearDownTestCase(void)
{
// input testsuit teardown step,teardown invoked after all testcases
}
void IPCNapiTest::SetUp(void)
{
// input testcase setup step,setup invoked before each testcases
}
void IPCNapiTest::TearDown(void)
{
// input testcase teardown step,teardown invoked after each testcases
}
/**
* @tc.name: ipc_napi_001
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: issueNumber
*/
HWTEST_F(IPCNapiTest, IPC_NAPI_TEST_001, TestSize.Level1)
{
napi_env env_ = nullptr;
OHOS::NAPI_CallingInfo oldCallingInfo;
OHOS::CallingInfo callingInfo;
(void)memset_s(&oldCallingInfo, sizeof(oldCallingInfo), 0, sizeof(oldCallingInfo));
(void)memset_s(&callingInfo, sizeof(callingInfo), 0, sizeof(callingInfo));
OHOS::NAPI_RemoteObject_getCallingInfo(callingInfo);
OHOS::NAPI_RemoteObject_saveOldCallingInfo(env_, oldCallingInfo);
OHOS::NAPI_RemoteObject_setNewCallingInfo(env_, callingInfo);
OHOS::NAPI_RemoteObject_resetOldCallingInfo(env_, oldCallingInfo);
EXPECT_EQ(callingInfo.callingPid, getpid());
EXPECT_EQ(callingInfo.callingUid, 0);
}
\ No newline at end of file
# Copyright (C) 2022 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.
import("//build/test.gni")
module_output_path = "ipc/"
ohos_js_unittest("IPCjsTest") {
module_out_path = module_output_path
hap_profile = "./config.json"
certificate_profile = "//test/developertest/signature/openharmony_sx.p7b"
}
group("unittest") {
testonly = true
deps = [ ":IPCjsTest" ]
}
/*
* Copyright (C) 2022 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.
*/
import rpc from '@ohos.rpc';
import featureAbility from '@ohos.ability.featureAbility';
import process from '@ohos.process';
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index';
describe("NapiRemoteObjectTest", function () {
beforeAll(function() {
// input testsuit setup step,setup invoked before all testcases
console.info('beforeAll called')
})
afterAll(function() {
// input testsuit teardown step,teardown invoked after all testcases
console.info('afterAll called')
})
beforeEach(function() {
// input testcase setup step,setup invoked before each testcases
console.info('beforeEach called')
})
afterEach(function() {
// input testcase teardown step,teardown invoked after each testcases
console.info('afterEach called')
})
class Proxy extends rpc.RemoteProxy {
}
let proxy = null
let temp1 = 0
let temp2 = 0
function myTestSendRequest(){
console.info("myTestSendRequest in chen")
let option = new rpc.MessageOption()
let data = rpc.MessageParcel.create()
let reply = rpc.MessageParcel.create()
temp1 = 0
temp2 = 0
data.writeByte(-128)
proxy.sendRequest(1, data, reply, option)
.then(function(result) {
console.info("start to send request")
if (result.errCode != 0) {
console.error("send request failed, errCode: " + result.errCode)
return
}
temp1 = result.reply.readByte()
temp2 = result.reply.readByte()
console.info("test reply1: " + temp1)
console.info("test reply2: " + temp2)
})
.catch(function(e) {
console.error("send request got exception: " + e)
})
.finally(() => {
data.reclaim()
reply.reclaim()
})
}
/*
* @tc.name:napiRemoteObjectTest001
* @tc.desc:The case where only onRemoteRequest is called when the callback function is synchronous.
* @tc.type: FUNC
* @tc.require: issueNumber
*/
it("napiRemoteObjectTest001", 0, async function (){
console.info("chen 123")
let connectId = null
let want = {
"bundleName": "com.example.syncre",
"abilityName": "com.example.entry.ServiceAbility_sync_re",
}
if (!proxy) {
proxy = await new Promise((resolve) => {
let connect = {
onConnect: function (elementName, remote) {
console.info("connect success")
resolve(remote);
},
onDisconnect: function (elementName) {
console.info("disconnect")
},
onFailed: function () {
proxy = null
console.info("connect failed")
resolve(null)
}
}
console.info("before connectid")
connectId = featureAbility.connectAbility(want, connect)
console.info("after connectid")
})
myTestSendRequest()
console.info("myTestSendRequest out chen")
expect(temp1).assertEqual(-128)
expect(temp2).assertEqual(2)
}
})
/*
* @tc.name:napiRemoteObjectTest002
* @tc.desc:The case where only onRemoteRequestEx is called when the callback function is synchronous.
* @tc.type: FUNC
* @tc.require: issueNumber
*/
it("napiRemoteObjectTest002", 0, async function (){
let connectId = null
let want = {
"bundleName": "com.example.sync_ex",
"abilityName": "com.example.entry.ServiceAbility_sync_ex",
}
if (!proxy) {
proxy = await new Promise((resolve) => {
let connect = {
onConnect: function (elementName, remote) {
console.info("connect success")
resolve(remote);
},
onDisconnect: function (elementName) {
console.info("disconnect")
},
onFailed: function () {
proxy = null
console.info("connect failed")
resolve(null)
}
}
connectId = featureAbility.connectAbility(want, connect)
})
myTestSendRequest()
expect(temp1).assertEqual(-128)
expect(temp2).assertEqual(3)
}
})
/*
* @tc.name:napiRemoteObjectTest003
* @tc.desc:The case where only onRemoteRequestEx is called when the callback function is asynchronous.
* @tc.type: FUNC
* @tc.require: issueNumber
*/
it("napiRemoteObjectTest003", 0, async function (){
let connectId = null
let want = {
"bundleName": "com.example.async_ex",
"abilityName": "com.example.entry.ServiceAbility_async_ex",
}
if (!proxy) {
proxy = await new Promise((resolve) => {
let connect = {
onConnect: function (elementName, remote) {
console.info("connect success")
resolve(remote);
},
onDisconnect: function (elementName) {
console.info("disconnect")
},
onFailed: function () {
proxy = null
console.info("connect failed")
resolve(null)
}
}
connectId = featureAbility.connectAbility(want, connect)
})
myTestSendRequest()
expect(temp1).assertEqual(-128)
expect(temp2).assertEqual(3)
}
})
/*
* @tc.name:napiRemoteObjectTest004
* @tc.desc:The case where both onRemoteRequestEx and onRemoteRequest are called
* when the callbacks are synchronized.
* @tc.type: FUNC
* @tc.require: issueNumber
*/
it("napiRemoteObjectTest004", 0, async function (){
let connectId = null
let want = {
"bundleName": "com.example.sync_re_ex",
"abilityName": "com.example.entry.ServiceAbility_sync_re_ex",
}
if (!proxy) {
proxy = await new Promise((resolve) => {
let connect = {
onConnect: function (elementName, remote) {
console.info("connect success")
resolve(remote);
},
onDisconnect: function (elementName) {
console.info("disconnect")
},
onFailed: function () {
proxy = null
console.info("connect failed")
resolve(null)
}
}
connectId = featureAbility.connectAbility(want, connect)
})
myTestSendRequest()
expect(temp1).assertEqual(-128)
expect(temp2).assertEqual(12)
}
})
/*
* @tc.name:napiRemoteObjectTest005
* @tc.desc:The case in which onRemoteRequest is called when the callback is synchronous and
* onRemoteRequestEx is called asynchronously.
* @tc.type: FUNC
* @tc.require: issueNumber
*/
it("napiRemoteObjectTest005", 0, async function (){
let connectId = null
let want = {
"bundleName": "com.example.sync_re_async_ex",
"abilityName": "com.example.entry.ServiceAbility_sync_re_async_ex",
}
if (!proxy) {
proxy = await new Promise((resolve) => {
let connect = {
onConnect: function (elementName, remote) {
console.info("connect success")
resolve(remote);
},
onDisconnect: function (elementName) {
console.info("disconnect")
},
onFailed: function () {
proxy = null
console.info("connect failed")
resolve(null)
}
}
connectId = featureAbility.connectAbility(want, connect)
})
myTestSendRequest()
expect(temp1).assertEqual(-128)
expect(temp2).assertEqual(14)
}
})
})
\ No newline at end of file
{
"app": {
"bundleName": "com.example.myapplication",
"vendor": "example",
"version": {
"code": 1,
"name": "1.0"
},
"apiVersion": {
"compatible": 8,
"target": 8
}
},
"deviceConfig": {},
"module": {
"package": "com.example.myapplication",
"name": ".MyApplication",
"deviceType": [
"phone"
],
"distro": {
"deliveryWithInstall": true,
"moduleName": "entry",
"moduleType": "entry"
},
"abilities": [
{
"visible": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
],
"name": "com.example.myapplication.MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "MyApplication",
"type": "page",
"launchType": "standard"
}
],
"js": [
{
"pages": [
"pages/index/index"
],
"name": "default",
"window": {
"designWidth": 720,
"autoDesignWidth": false
}
}
]
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册