提交 3d10c634 编写于 作者: Z zhijianwen

add+

Signed-off-by: Nzhijianwen <zhijianwen@huawei.com>
上级 422b7f8e
......@@ -24,53 +24,10 @@
"moduleName": "entry",
"moduleType": "entry"
},
"defPermissions": [
"reqPermissions": [
{
"availableScope": [],
"grantMode": "system_grant",
"name": "publish_event0800_permission"
},
{
"availableScope": [],
"grantMode": "system_grant",
"name": "publish_event0900_subscriberPermissions"
},
{
"availableScope": [],
"grantMode": "system_grant",
"name": "publish_event0900_publisherPermission"
},
{
"availableScope": [],
"grantMode": "system_grant",
"name": "publish_event1000_permission"
},
{
"availableScope": [],
"grantMode": "system_grant",
"name": "permission.COMMONEVENT_STICKY"
}
],
"reqPermissions": [
{
"name": "publish_event0800_permission",
"reason": "ceshi"
},
{
"name": "publish_event0900_subscriberPermissions",
"reason": "ceshi"
},
{
"name": "publish_event0900_publisherPermission",
"reason": "ceshi"
},
{
"name": "publish_event1000_permission",
"reason": "ceshi"
},
{
"name": "permission.COMMONEVENT_STICKY",
"reason": "ceshi"
"name": "ohos.permission.NOTIFICATION_CONTROLLER",
"reason": "use ohos.permission.NOTIFICATION_CONTROLLER"
}
],
"abilities": [
......
......@@ -12,98 +12,346 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import emitter from '@ohos.events.emitter'
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
import events_emitter from '@ohos.events.emitter';
import notification from '@ohos.notification';
import commonEvent from '@ohos.commonEvent';
import wantAgent from '@ohos.wantAgent';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import bundle from '@ohos.bundle';
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, Assert } from 'deccjsunit/index';
describe('EmitterTest', function () {
console.info("===========EmitterTest start====================>");
var eventData = {
data: {
"id": 1,
"content": "message",
const TAG = 'EmitterTest ===> '
async function applyPermission() {
let appInfo = await bundle.getApplicationInfo('com.example.emittertest', 0, 100);
let atManager = abilityAccessCtrl.createAtManager();
if (atManager != null) {
let tokenID = appInfo.accessTokenId;
console.info('[permission]case accessTokenId is' + tokenID);
let permissionName1 = 'ohos.permission.NOTIFICATION_CONTROLLER';
await atManager.grantUserGrantedPermission(tokenID, permissionName1).then((result) => {
console.info('[permission]case grantUserGrantedPermission success:' + result);
}).catch((err) => {
console.info('[permission]case grantUserGrantedPermission failed:' + err);
});
} else {
console.info('[permission]case apply permission failed,createAtManager failed');
}
}
var InnerEventImmediate = {
eventId: 1,
priority: emitter.EventPriority.IMMEDIATE
const bundlel = {
bundle: 'com.example.emittertest'
}
var InnerEventLow = {
eventId: 2,
priority: emitter.EventPriority.LOW
let innerEvent = {
eventId: undefined,
priority: undefined
}
function callback(data){
console.info('callback success');
expect(data.eventId).assertEqual("message");
let eventData = {
data: {
'id': undefined,
'content': undefined
}
}
function callback01(data){
console.info('callback success');
expect(data.data.id).assertEqual(1);
expect(data.data.content).assertEqual("message");
let wantAgentInfo = {
wants: [
{
bundleName: 'com.example.emittertest',
abilityName: 'com.example.emittertest.TestAbility'
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG],
extraInfo: {
key_1: 'value_1',
key_2: 'value_2',
}
}
let subscriber = undefined
beforeAll(async function() {
await applyPermission();
await notification.enableNotification(bundlel, true, (err) => {
if (err.code) {
console.info(TAG + ': enableNotification failed! Err.code ===> ' + JSON.stringify(err.code))
console.info(TAG + ': enableNotification failed! Err.message ===> ' + JSON.stringify(err.message))
expect(false).assertTrue()
} else {
console.info(TAG + ': enableNotification successd!')
expect(true).assertTrue()
}
});
console.info(TAG + 'beforeAll case');
})
afterEach(async function() {
await notification.cancelAll((err) => {
if (err.code) {
console.info(TAG + ': cancelAll failed! Err.code ===> ' + err.code)
console.info(TAG + ': cancelAll failed! Err.message ===> ' + err.message)
expect(false).assertTrue()
} else {
console.info(TAG + ': cancelAll successd!')
expect(true).assertTrue()
}
})
})
function EmitterCallback(eventData) {
console.info(TAG + 'eventData.id: ' + JSON.stringify(eventData.data.id));
console.info(TAG + 'eventData.content: ' + JSON.stringify(eventData.data.content));
if (eventData.data.id == 0) {
expect(eventData.data.content).assertEqual('message_0');
} else if (eventData.data.id == 1) {
expect(eventData.data.content).assertEqual('message_1');
} else if (eventData.data.id == 2) {
expect(eventData.data.content).assertEqual('message_2');
} else if (eventData.data.id == 3) {
expect(eventData.data.content).assertEqual('message_3');
} else if (eventData.data.id == 4) {
expect(eventData.data.content).assertEqual('message_4');
} else if (eventData.data.id == 5) {
expect(eventData.data.content).assertEqual('message_5');
}
}
/*
* @tc.number : EmitterOnTest
* @tc.name : verify on : Check subscribe same on common ordered event
* @tc.desc : Check the subscriber can receive event "EmitterOnTest" type of the interface (by Promise)
*/
it('EmitterOnTest', 0, async function (done) {
console.info("===========EmitterOnTest====================>");
emitter.on(InnerEventImmediate, callback);
done();
it('EmitterTest_001', 0, async function (done) {
console.info(TAG + 'EmitterTest_001 start.')
innerEvent.eventId = 1
innerEvent.priority = events_emitter.EventPriority.IDLE
eventData.data.id = 0
eventData.data.content = 'message_0'
events_emitter.on(innerEvent, EmitterCallback)
events_emitter.emit(innerEvent, eventData)
eventData.data.id = 1
eventData.data.content = 'message_1'
events_emitter.emit(innerEvent, eventData)
console.info(TAG + 'EmitterTest_001 end.')
done()
})
/*
* @tc.number : EmitterOnceTest
* @tc.name : verify once : Check subscribe same once common ordered event
* @tc.desc : Check the subscriber can receive event "EmitterOnceTest" type of the interface (by Promise)
*/
it('EmitterOnceTest', 0, async function (done){
console.info("===========EmitterOnceTest====================>");
emitter.once(InnerEventImmediate, callback);
done();
it('EmitterTest_002', 0, async function (done) {
console.info(TAG + 'EmitterTest_002 start.')
innerEvent.eventId = 2
innerEvent.priority = events_emitter.EventPriority.LOW
eventData.data.id = 2
eventData.data.content = 'message_2'
events_emitter.once(innerEvent, EmitterCallback)
events_emitter.emit(innerEvent, eventData)
console.info(TAG + 'EmitterTest_002 end.')
done()
})
/*
* @tc.number : EmitterEmitTest
* @tc.name : verify emit : Check subscribe same emit common ordered event
* @tc.desc : Check the subscriber can receive event "EmitterEmitTest" type of the interface (by Promise)
*/
it('EmitterEmitTest', 0, async function (done){
console.info("===========EmitterEmitTest====================>");
emitter.once(InnerEventImmediate, callback01);
emitter.emit(InnerEventImmediate, eventData);
done();
it('EmitterTest_003', 0, async function (done) {
console.info(TAG + 'EmitterTest_003 start.')
innerEvent.eventId = 3
innerEvent.priority = events_emitter.EventPriority.HIGH
eventData.data.id = 3
eventData.data.content = 'message_3'
events_emitter.once(innerEvent, EmitterCallback)
events_emitter.emit(innerEvent, eventData)
console.info(TAG + 'EmitterTest_003 end.')
done()
})
/*
* @tc.number : EmitterLowTest
* @tc.name : verify low : Check subscribe same low common ordered event
* @tc.desc : Check the subscriber can receive event "EmitterLowTest" type of the interface (by Promise)
*/
it('EmitterLowTest', 0, async function (done){
console.info("===========EmitterLowTest====================>");
emitter.once(InnerEventLow, callback01);
emitter.emit(InnerEventLow,eventData);
done();
it('EmitterTest_004', 0, async function (done) {
console.info(TAG + 'EmitterTest_004 start.')
innerEvent.eventId = 4
innerEvent.priority = events_emitter.EventPriority.IMMEDIATE
eventData.data.id = 4
eventData.data.content = 'message_4'
events_emitter.once(innerEvent, EmitterCallback)
events_emitter.emit(innerEvent, eventData)
console.info(TAG + 'EmitterTest_004 end.')
done()
})
/*
* @tc.number : EmitterOffTest
* @tc.name : verify off : Check subscribe same off common ordered event
* @tc.desc : Check the subscriber can receive event "EmitterOffTest" type of the interface (by Promise)
*/
it('EmitterOffTest', 0, async function (done){
console.info("===========EmitterOffTest111====================>");
emitter.once(InnerEventLow, callback01);
emitter.emit(InnerEventImmediate, eventData);
emitter.off(1);
done();
it('EmitterTest_005', 0, async function (done) {
console.info(TAG + 'EmitterTest_005 start.')
innerEvent.eventId = 5
innerEvent.priority = events_emitter.EventPriority.IMMEDIATE
eventData.data.id = 5
eventData.data.content = 'message_5'
events_emitter.once(innerEvent, EmitterCallback)
events_emitter.emit(innerEvent, eventData)
events_emitter.off(5)
console.info(TAG + 'EmitterTest_005 end.')
done()
})
it('wantAgent_trigger_001', 0, async function (done){
await wantAgent.getWantAgent(wantAgentInfo, (err, data) => {
if (err.code) {
console.info(TAG + ': getWant failed! Err.code ===> ' + JSON.stringify(err.code))
console.info(TAG + ': getWant failed! Err.message ===> ' + JSON.stringify(err.message))
expect(false).assertTrue()
done()
} else {
console.info(TAG + ': getWant successd!')
console.info(TAG + ': wantAgentInfo ===> ' + JSON.stringify(data))
expect(typeof(data)).assertEqual('object')
let triggerInfo = {
code: 100,
want: null,
permission: null,
extraInfo: {
triggerInfo_key_1: 'triggerInfo_value_1',
triggerInfo_key_2: 'triggerInfo_value_2',
}
}
wantAgent.trigger(data, triggerInfo, (err, data) => {
console.info(TAG + ': trigger successd! CompleteData ===> ' + JSON.stringify(data))
console.info(TAG + ': data.want.bundleName ===> ' + JSON.stringify(data.want.abilityName))
console.info(TAG + ': data.want.abilityName ===> ' + JSON.stringify(data.want.abilityName))
console.info(TAG + ': data.want.parameters.key_1 ===> ' + JSON.stringify(data.want.parameters.key_1))
console.info(TAG + ': data.want.parameters.key_2 ===> ' + JSON.stringify(data.want.parameters.key_2))
expect(data.want.bundleName).assertEqual('com.example.emittertest')
expect(data.want.abilityName).assertEqual('com.example.emittertest.TestAbility')
expect(data.want.parameters.key_1).assertEqual('value_1')
expect(data.want.parameters.key_2).assertEqual('value_2')
done()
})
}
})
})
it('notification_publish_001', 0, async function (done){
await notification.isNotificationEnabled(bundlel, (err, data) => {
if (err.code) {
console.info(TAG + ': isNotificationEnabled failed! Err.code ===> ' + JSON.stringify(err.code))
console.info(TAG + ': isNotificationEnabled failed! Err.message ===> ' + JSON.stringify(err.message))
expect(false).assertTrue()
} else {
console.info(TAG + ': isNotificationEnabled successd! Result ===> ' + JSON.stringify(data))
expect(true).assertTrue()
}
})
async function SubscribeCallbackData(data) {
console.info(TAG + ': data ===> ' + JSON.stringify(data))
console.info(TAG + ': data.request.content.normal.title ===> ' + JSON.stringify(data.request.content.normal.title))
console.info(TAG + ': data.request.content.normal.text ===> ' + JSON.stringify(data.request.content.normal.text))
console.info(TAG + ': data.request.content.normal.additionalText ===> ' + JSON.stringify(data.request.content.normal.additionalText))
console.info(TAG + ': data.request.actionButtons[0].title ===> ' + JSON.stringify(data.request.actionButtons[0].title))
expect(data.request.content.normal.title).assertEqual('title');
expect(data.request.content.normal.text).assertEqual('text');
expect(data.request.content.normal.additionalText).assertEqual('additionalText');
expect(data.request.content.normal.creatorBundleName).assertEqual(bundlel.bundle);
expect(data.request.content.normal.creatorPid != undefined).assertTrue()
expect(data.request.content.normal.creatorUid != undefined).assertTrue()
expect(data.request.actionButtons[0].title).assertEqual('additionalText');
expect(data.request.actionButtons[0].extras.wantAgentInfo_key_1).assertEqual('wantAgentInfo_key_1')
expect(data.request.actionButtons[0].extras.wantAgentInfo_key_2).assertEqual('wantAgentInfo_key_2')
expect(data.request.actionButtons[0].userInput.inputKey).assertEqual('Please input at this')
expect(true).assertTrue()
await notification.unsubscribe(NotificationSubscriber, (err) => {
if (err.code) {
console.info(TAG + ': unsubscribe failed! Err.code ===> ' + JSON.stringify(err.code))
console.info(TAG + ': unsubscribe failed! Err.message ===> ' + JSON.stringify(err.message))
expect(false).assertTrue()
done()
} else {
console.info(TAG + ': unsubscribe successd!')
expect(true).assertTrue()
}
})
}
let NotificationSubscriber = {
onConsume: SubscribeCallbackData
}
await notification.subscribe(NotificationSubscriber, (err)=>{
if (err.code) {
console.info(TAG + ': subscribe failed! Err.code ===> ' + JSON.stringify(err.code))
console.info(TAG + ': subscribe failed! Err.message ===> ' + JSON.stringify(err.message))
expect(false).assertTrue()
} else {
console.info(TAG + ': subscribe successd!')
expect(true).assertTrue()
done()
}
})
let wantAgentInstance = await wantAgent.getWantAgent(wantAgentInfo)
const NotificationRequest = {
content : {
contentType : notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal : {
title : 'title',
text : 'text',
additionalText : 'additionalText'
},
},
actionButtons: [
{
title : 'activeButton_title',
wantAgent : wantAgentInstance,
extras : {
wantAgentInfo_key_1: 'wantAgentInfo_key_1',
wantAgentInfo_key_2: 'wantAgentInfo_key_2',
},
userInput : {
inputKey : 'Please input at this'
}
}
],
}
// 发布通知
await notification.publish(NotificationRequest, (err) => {
if (err.code) {
console.info(TAG + ': publish failed! Err.code ===> ' + JSON.stringify(err.code))
console.info(TAG + ': publish failed! Err.message ===> ' + JSON.stringify(err.message))
expect(false).assertTrue()
} else {
console.info(TAG + ': publish successd!')
expect(true).assertTrue()
}
})
})
it('commonEvent_subscriberInfo_001', 0, async function (done){
let CommonEventSubscribeInfo = {
events: ['event'],
publisherPermission: '',
publisherDeviceId: '',
userId: 100,
priority: 1000
}
let CommonEventSubscriber = await commonEvent.createSubscriber(CommonEventSubscribeInfo)
if (CommonEventSubscriber == undefined) {
console.info(TAG + ': createSubscriber failed! Err.message ===> ' + JSON.stringify(CommonEventSubscriber))
expect(false).assertTrue()
}
await CommonEventSubscriber.getSubscribeInfo((err, CommonEventSubscribeInfo)=>{
if (err.code) {
console.info(TAG + ': getSubscribeInfo failed! Err.code ===> ' + JSON.stringify(err.code))
console.info(TAG + ': getSubscribeInfo failed! Err.message ===> ' + JSON.stringify(err.message))
expect(false).assertTrue()
done()
} else {
console.info(TAG + ': getSubscribeInfo successd! CommonEventSubscribeInfo ===> ' + JSON.stringify(CommonEventSubscribeInfo))
done()
}
})
})
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册