mirror of
https://gitee.com/openharmony/inputmethod_imf
synced 2024-11-23 22:59:45 +00:00
输入法框架,同步接口新增测试用例
Signed-off-by: guojin31 <guojin31@huawei.com>
This commit is contained in:
parent
2d1a69de5b
commit
eb5ec5a1c0
@ -43,6 +43,7 @@ public:
|
||||
static std::string bundleName;
|
||||
static std::vector<std::string> extName;
|
||||
static std::vector<std::string> language;
|
||||
static std::vector<std::string> locale;
|
||||
};
|
||||
bool InputMethodSwitchTest::imeChangeFlag = false;
|
||||
sptr<InputMethodController> InputMethodSwitchTest::imc_;
|
||||
@ -51,6 +52,7 @@ std::vector<std::string> InputMethodSwitchTest::newImeSubName{ "lowerInput", "up
|
||||
std::string InputMethodSwitchTest::bundleName = "com.example.testIme";
|
||||
std::vector<std::string> InputMethodSwitchTest::extName{ "InputMethodExtAbility", "InputMethodExtAbility2" };
|
||||
std::vector<std::string> InputMethodSwitchTest::language{ "chinese", "english" };
|
||||
std::vector<std::string> InputMethodSwitchTest::locale{ "zh-CN", "en-US" };
|
||||
constexpr uint32_t IME_EXT_NUM = 2;
|
||||
constexpr uint32_t NEW_IME_SUBTYPE_NUM = 3;
|
||||
constexpr uint32_t TOTAL_IME_MIN_NUM = 2;
|
||||
@ -123,7 +125,7 @@ void InputMethodSwitchTest::CheckCurrentSubProps()
|
||||
EXPECT_EQ(subProps[i].id, extName[i]);
|
||||
EXPECT_EQ(subProps[i].name, bundleName);
|
||||
EXPECT_EQ(subProps[i].language, language[i]);
|
||||
EXPECT_EQ(subProps[i].locale, "");
|
||||
EXPECT_EQ(subProps[i].locale, locale[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -181,11 +183,30 @@ HWTEST_F(InputMethodSwitchTest, testSubTypeSwitch_002, TestSize.Level0)
|
||||
int32_t ret = imc_->SwitchInputMethod(bundleName, extName[1]);
|
||||
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
|
||||
EXPECT_TRUE(imeChangeFlag);
|
||||
CheckCurrentProp(extName[1]);
|
||||
CheckCurrentProp(extName[0]);
|
||||
CheckCurrentSubProp(extName[1]);
|
||||
CheckCurrentSubProps();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: testSubTypeSwitch_003
|
||||
* @tc.desc: switch subtype with extName1
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issuesI62BHB
|
||||
* @tc.author: chenyu
|
||||
*/
|
||||
HWTEST_F(InputMethodSwitchTest, testSubTypeSwitch_003, TestSize.Level0)
|
||||
{
|
||||
IMSA_HILOGI("oldIme testSubTypeSwitch_003 Test START");
|
||||
imeChangeFlag = false;
|
||||
int32_t ret = imc_->SwitchInputMethod(bundleName, extName[0]);
|
||||
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
|
||||
EXPECT_TRUE(imeChangeFlag);
|
||||
CheckCurrentProp(extName[0]);
|
||||
CheckCurrentSubProp(extName[0]);
|
||||
CheckCurrentSubProps();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: testSubTypeSwitchWithErrorSubName
|
||||
* @tc.desc: switch subtype with error subName.
|
||||
|
@ -43,6 +43,7 @@ describe('InputMethodTest', function () {
|
||||
let language = ['english', 'english', 'chinese'];
|
||||
let bundleName1 = 'com.example.testIme';
|
||||
let extName1 = ['InputMethodExtAbility', 'InputMethodExtAbility2'];
|
||||
let locale1 = ['zh-CN', 'en-US'];
|
||||
let language1 = ['chinese', 'english'];
|
||||
const LEAST_ALL_IME_NUM = 2;
|
||||
const ENABLE_IME_NUM = 1;
|
||||
@ -99,7 +100,7 @@ describe('InputMethodTest', function () {
|
||||
{
|
||||
expect(subProp.name).assertEqual(bundleName1);
|
||||
expect(subProp.id).assertEqual(extName1[index]);
|
||||
expect(subProp.locale).assertEqual('');
|
||||
expect(subProp.locale).assertEqual(locale1[index]);
|
||||
expect(subProp.language).assertEqual(language1[index]);
|
||||
}
|
||||
|
||||
@ -109,7 +110,7 @@ describe('InputMethodTest', function () {
|
||||
for (let i = 0; i < subProps.length; i++) {
|
||||
expect(subProps[i].name).assertEqual(bundleName1);
|
||||
expect(subProps[i].id).assertEqual(extName1[i]);
|
||||
expect(subProps[i].locale).assertEqual('');
|
||||
expect(subProps[i].locale).assertEqual(locale1[i]);
|
||||
expect(subProps[i].language).assertEqual(language1[i]);
|
||||
}
|
||||
}
|
||||
@ -491,8 +492,8 @@ describe('InputMethodTest', function () {
|
||||
let InputMethodSubtype = {
|
||||
name:bundleName1,
|
||||
id:extName1[0],
|
||||
locale:'en_US.ISO-8859-1',
|
||||
language:'en',
|
||||
locale:locale1[0],
|
||||
language:language1[0],
|
||||
extra:{},
|
||||
};
|
||||
let inputMethodProperty = {
|
||||
|
@ -14,15 +14,49 @@
|
||||
*/
|
||||
|
||||
import inputMethod from '@ohos.inputMethod';
|
||||
import commonEventManager from '@ohos.commonEventManager';
|
||||
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index';
|
||||
|
||||
describe('InputMethodWithAttachTest', function () {
|
||||
beforeAll(function () {
|
||||
console.info('beforeAll called');
|
||||
const WAIT_DEAL_OK = 500;
|
||||
const TEST_RESULT_CODE = 0;
|
||||
const TEST_FUNCTION = {
|
||||
INSERT_TEXT_SYNC: 0,
|
||||
MOVE_CURSOR_SYNC: 1,
|
||||
GET_ATTRIBUTE_SYNC: 2,
|
||||
SELECT_BY_RANGE_SYNC: 3,
|
||||
SELECT_BY_MOVEMENT_SYNC: 4,
|
||||
GET_INDEX_AT_CURSOR_SYNC: 5,
|
||||
DELETE_FORWARD_SYNC: 6,
|
||||
DELETE_BACKWARD_SYNC: 7,
|
||||
GET_FORWARD_SYNC: 8,
|
||||
GET_BACKWARD_SYNC: 9
|
||||
}
|
||||
|
||||
beforeAll(async function (done) {
|
||||
console.info('beforeAll called');
|
||||
let inputMethodProperty = {
|
||||
name:'com.example.testIme',
|
||||
id:'InputMethodExtAbility'
|
||||
};
|
||||
await inputMethod.switchInputMethod(inputMethodProperty);
|
||||
setTimeout(()=>{
|
||||
done();
|
||||
}, WAIT_DEAL_OK);
|
||||
});
|
||||
|
||||
afterAll(function () {
|
||||
console.info('afterAll called');
|
||||
afterAll(async function () {
|
||||
console.info('afterAll called');
|
||||
let inputMethodSetting = inputMethod.getInputMethodSetting();
|
||||
let props = await inputMethodSetting.listInputMethod();
|
||||
let bundleName = 'com.example.newTestIme';
|
||||
let bundleName1 = 'com.example.testIme';
|
||||
for(let i = 0;i< props.length; i++) {
|
||||
let prop = props[i];
|
||||
if(prop.name !== bundleName && prop.name !== bundleName1){
|
||||
await inputMethod.switchInputMethod(prop);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
beforeEach(async function () {
|
||||
@ -44,6 +78,29 @@ describe('InputMethodWithAttachTest', function () {
|
||||
await inputMethodCtrl.detach();
|
||||
});
|
||||
|
||||
function publishCommonEvent(codeNumber) {
|
||||
console.info(`[publishCommonEvent] publish event, codeNumber = ${codeNumber}`);
|
||||
commonEventManager.publish('syncTestFunction', { code: codeNumber }, (err)=>{
|
||||
console.info(`inputMethod publish finish, err = ${JSON.stringify(err)}`);
|
||||
})
|
||||
}
|
||||
|
||||
function subscribe(subscribeInfo, functionCode, done) {
|
||||
commonEventManager.createSubscriber(subscribeInfo).then((data)=>{
|
||||
let subscriber = data;
|
||||
commonEventManager.subscribe(subscriber, (err, eventData)=>{
|
||||
console.info("inputMethod subscribe");
|
||||
if(eventData.code === TEST_RESULT_CODE) {
|
||||
expect(true).assertTrue();
|
||||
}else{
|
||||
expect().assertFail();
|
||||
}
|
||||
commonEventManager.unsubscribe(subscriber);
|
||||
done();
|
||||
})
|
||||
publishCommonEvent(functionCode);
|
||||
})
|
||||
}
|
||||
/*
|
||||
* @tc.number inputmethod_with_attach_test_showTextInput_001
|
||||
* @tc.name Test whether the keyboard is displayed successfully.
|
||||
@ -536,4 +593,236 @@ describe('InputMethodWithAttachTest', function () {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/*
|
||||
* @tc.number inputmethod_test_insertTextSync_001
|
||||
* @tc.name Test Indicates the input method which will replace the current one.
|
||||
* @tc.desc Function test
|
||||
* @tc.level 2
|
||||
*/
|
||||
it('inputmethod_test_insertTextSync_001', 0, async function (done) {
|
||||
console.info('************* inputmethod_test_insertTextSync_001 Test start*************');
|
||||
let inputMethodCtrl = inputMethod.getController();
|
||||
try {
|
||||
inputMethodCtrl.on('insertText', (text) => {
|
||||
console.info(`inputMethod insertText success, text: ${JSON.stringify(text)}`);
|
||||
expect(true).assertTrue();
|
||||
done();
|
||||
});
|
||||
publishCommonEvent(TEST_FUNCTION.INSERT_TEXT_SYNC);
|
||||
} catch(error) {
|
||||
console.info(`inputmethod_test_insertTextSync result: ${JSON.stringify(error)}`);
|
||||
expect().assertFail();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
* @tc.number inputmethod_test_moveCursorSync_001
|
||||
* @tc.name Test Indicates the input method which will replace the current one.
|
||||
* @tc.desc Function test
|
||||
* @tc.level 2
|
||||
*/
|
||||
it('inputmethod_test_moveCursorSync_001', 0, async function (done) {
|
||||
console.info('************* inputmethod_test_moveCursorSync_001 Test start*************');
|
||||
let inputMethodCtrl = inputMethod.getController();
|
||||
try {
|
||||
inputMethodCtrl.on('moveCursor', (direction) => {
|
||||
console.info(`inputMethod moveCursor success, direction: ${direction}`);
|
||||
expect(true).assertTrue();
|
||||
done();
|
||||
});
|
||||
publishCommonEvent(TEST_FUNCTION.MOVE_CURSOR_SYNC);
|
||||
} catch(error) {
|
||||
console.info(`inputmethod_text_moveCursorSync result: ${JSON.stringify(error)}`);
|
||||
expect().assertFail();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
* @tc.number inputmethod_test_getEditorAttributeSync_001
|
||||
* @tc.name Test Indicates the input method which will replace the current one.
|
||||
* @tc.desc Function test
|
||||
* @tc.level 2
|
||||
*/
|
||||
it('inputmethod_test_getEditorAttributeSync_001', 0, async function (done) {
|
||||
console.info('************* inputmethod_test_getEditorAttributeSync_001 Test start*************');
|
||||
try {
|
||||
let subscribeInfo = {
|
||||
events: ['getEditorAttributeSyncResult']
|
||||
};
|
||||
subscribe(subscribeInfo, TEST_FUNCTION.GET_ATTRIBUTE_SYNC, done);
|
||||
} catch(error) {
|
||||
console.info(`inputmethod_test_getEditorAttributeSync_001 result: ${JSON.stringify(error)}`);
|
||||
expect().assertFail();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
* @tc.number inputmethod_test_SelectByRangeSync_001
|
||||
* @tc.name Test Indicates the input method which will replace the current one.
|
||||
* @tc.desc Function test
|
||||
* @tc.level 2
|
||||
*/
|
||||
it('inputmethod_test_selectByRangeSync_001', 0, async function (done) {
|
||||
console.info('************* inputmethod_test_selectByRangeSync_001 Test start*************');
|
||||
let inputMethodCtrl = inputMethod.getController();
|
||||
try {
|
||||
inputMethodCtrl.on('selectByRange', (range) => {
|
||||
console.info(`inputMethod selectByRangeSync success, direction: ${range}`);
|
||||
expect(true).assertTrue();
|
||||
done();
|
||||
});
|
||||
publishCommonEvent(TEST_FUNCTION.SELECT_BY_RANGE_SYNC);
|
||||
} catch(error) {
|
||||
console.info(`inputmethod_text_selectByRangeSync result: ${JSON.stringify(error)}`);
|
||||
expect().assertFail();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
* @tc.number inputmethod_test_selectByMovementSync_001
|
||||
* @tc.name Test Indicates the input method which will replace the current one.
|
||||
* @tc.desc Function test
|
||||
* @tc.level 2
|
||||
*/
|
||||
it('inputmethod_test_selectByMovementSync_001', 0, async function (done) {
|
||||
console.info('************* inputmethod_test_selectByMovementSync_001 Test start*************');
|
||||
let inputMethodCtrl = inputMethod.getController();
|
||||
try {
|
||||
inputMethodCtrl.on('selectByMovement', (movement) => {
|
||||
console.info(`inputMethod selectByMovementSync success, direction: ${movement}`);
|
||||
expect(true).assertTrue();
|
||||
done();
|
||||
});
|
||||
publishCommonEvent(TEST_FUNCTION.SELECT_BY_MOVEMENT_SYNC);
|
||||
} catch(error) {
|
||||
console.info(`inputmethod_text_selectByMovementSync result: ${JSON.stringify(error)}`);
|
||||
expect().assertFail();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
* @tc.number inputmethod_test_selectByMovementSync_001
|
||||
* @tc.name Test Indicates the input method which will replace the current one.
|
||||
* @tc.desc Function test
|
||||
* @tc.level 2
|
||||
*/
|
||||
it('inputmethod_test_getTextIndexAtCursorSync_001', 0, async function (done) {
|
||||
console.info('************* inputmethod_test_getTextIndexAtCursorSync_001 Test start*************');
|
||||
let inputMethodCtrl = inputMethod.getController();
|
||||
try {
|
||||
inputMethodCtrl.on('getTextIndexAtCursor', () => {
|
||||
console.info(`inputMethod getTextIndexAtCursor success`);
|
||||
return 2;
|
||||
});
|
||||
let subscribeInfo = {
|
||||
events: ['getTextIndexAtCursorSyncResult']
|
||||
};
|
||||
subscribe(subscribeInfo, TEST_FUNCTION.GET_INDEX_AT_CURSOR_SYNC, done);
|
||||
} catch(error) {
|
||||
console.info(`inputmethod_test_getTextIndexAtCursorSync_001 result: ${JSON.stringify(error)}`);
|
||||
expect().assertFail();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
* @tc.number inputmethod_test_deleteForwardSync_001
|
||||
* @tc.name Test Indicates the input method which will replace the current one.
|
||||
* @tc.desc Function test
|
||||
* @tc.level 2
|
||||
*/
|
||||
it('inputmethod_test_deleteForwardSync_001', 0, async function (done) {
|
||||
console.info('************* inputmethod_test_deleteForwardSync_001 Test start*************');
|
||||
let inputMethodCtrl = inputMethod.getController();
|
||||
try {
|
||||
inputMethodCtrl.on('deleteLeft', (movement) => {
|
||||
console.info(`inputMethod deleteForwardSync success, direction: ${movement}`);
|
||||
expect(true).assertTrue();
|
||||
done();
|
||||
});
|
||||
publishCommonEvent(TEST_FUNCTION.DELETE_FORWARD_SYNC);
|
||||
} catch(error) {
|
||||
console.info(`inputmethod_text_deleteForwardSync result: ${JSON.stringify(error)}`);
|
||||
expect().assertFail();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
* @tc.number inputmethod_test_deleteBackwardSync_001
|
||||
* @tc.name Test Indicates the input method which will replace the current one.
|
||||
* @tc.desc Function test
|
||||
* @tc.level 2
|
||||
*/
|
||||
it('inputmethod_test_deleteBackwardSync_001', 0, async function (done) {
|
||||
console.info('************* inputmethod_test_deleteBackwardSync_001 Test start*************');
|
||||
let inputMethodCtrl = inputMethod.getController();
|
||||
try {
|
||||
inputMethodCtrl.on('deleteRight', (movement) => {
|
||||
console.info(`inputMethod deleteBackwardSync success, direction: ${movement}`);
|
||||
expect(true).assertTrue();
|
||||
done();
|
||||
});
|
||||
publishCommonEvent(TEST_FUNCTION.DELETE_BACKWARD_SYNC);
|
||||
} catch(error) {
|
||||
console.info(`inputmethod_text_deleteBackwardSync result: ${JSON.stringify(error)}`);
|
||||
expect().assertFail();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
* @tc.number inputmethod_test_getForwardSync_001
|
||||
* @tc.name Test Indicates the input method which will replace the current one.
|
||||
* @tc.desc Function test
|
||||
* @tc.level 2
|
||||
*/
|
||||
it('inputmethod_test_getForwardSync_001', 0, async function (done) {
|
||||
console.info('************* inputmethod_test_getForwardSync_001 Test start*************');
|
||||
let inputMethodCtrl = inputMethod.getController();
|
||||
try {
|
||||
inputMethodCtrl.on('getLeftTextOfCursor', (length) => {
|
||||
console.info(`inputMethod getForwardSync success, length: ${length}`);
|
||||
return 'getLeftTextOfCursor';
|
||||
});
|
||||
let subscribeInfo = {
|
||||
events: ['getForwardSyncResult']
|
||||
};
|
||||
subscribe(subscribeInfo, TEST_FUNCTION.GET_FORWARD_SYNC, done);
|
||||
} catch(error) {
|
||||
console.info(`inputmethod_text_getForwardSync result: ${JSON.stringify(error)}`);
|
||||
expect().assertFail();
|
||||
done();
|
||||
}
|
||||
});
|
||||
/*
|
||||
* @tc.number inputmethod_test_getBackwardSync_001
|
||||
* @tc.name Test Indicates the input method which will replace the current one.
|
||||
* @tc.desc Function test
|
||||
* @tc.level 2
|
||||
*/
|
||||
it('inputmethod_test_getBackwardSync_001', 0, async function (done) {
|
||||
console.info('************* inputmethod_test_getBackwardSync_001 Test start*************');
|
||||
let inputMethodCtrl = inputMethod.getController();
|
||||
try {
|
||||
inputMethodCtrl.on('getRightTextOfCursor', (length) => {
|
||||
console.info(`inputMethod getBackwardSync success, length: ${length}`);
|
||||
return 'getRightTextOfCursor';
|
||||
});
|
||||
let subscribeInfo = {
|
||||
events: ['getBackwardSyncResult']
|
||||
};
|
||||
subscribe(subscribeInfo, TEST_FUNCTION.GET_BACKWARD_SYNC, done);
|
||||
} catch(error) {
|
||||
console.info(`inputmethod_text_getBackwardSync result: ${JSON.stringify(error)}`);
|
||||
expect().assertFail();
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
@ -40,6 +40,10 @@
|
||||
{
|
||||
"name": "ohos.permission.CONNECT_IME_ABILITY",
|
||||
"reason": "ceshi"
|
||||
},
|
||||
{
|
||||
"name": "ohos.permission.COMMONEVENT_STICKY",
|
||||
"reason": "ceshi"
|
||||
}
|
||||
],
|
||||
"abilities": [
|
||||
|
@ -29,6 +29,7 @@ ohos_hap("extImf") {
|
||||
}
|
||||
|
||||
ohos_js_assets("extImf_ets_assets") {
|
||||
ets2abc = true
|
||||
source_dir = "entry/src/main/ets"
|
||||
hap_profile = "entry/src/main/module.json"
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2022-2023 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
|
||||
@ -13,10 +13,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import AbilityStage from "@ohos.application.AbilityStage"
|
||||
import AbilityStage from '@ohos.app.ability.AbilityStage';
|
||||
|
||||
export default class MyAbilityStage extends AbilityStage {
|
||||
onCreate() {
|
||||
console.log("[Demo] MyAbilityStage onCreate")
|
||||
}
|
||||
onCreate() {
|
||||
console.log("[Demo] MyAbilityStage onCreate")
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2022-2023 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
|
||||
@ -14,18 +14,15 @@
|
||||
*/
|
||||
|
||||
import ImeExtension from '@ohos.InputMethodExtensionAbility'
|
||||
import {KeyboardController} from './model/KeyboardController'
|
||||
import keyboardController from './model/KeyboardController'
|
||||
|
||||
export default class InputMethodExtAbility extends ImeExtension {
|
||||
private keyboardController: KeyboardController;
|
||||
onCreate(want) {
|
||||
console.log(`InputMethodExtAbility onCreate, want: ${want.abilityName}`);
|
||||
keyboardController.onCreate(this.context); // Initializes the window and registers event listeners for the input method framework
|
||||
}
|
||||
|
||||
onCreate(want) {
|
||||
console.log(`InputMethodExtAbility onCreate, want: ${want.abilityName}`);
|
||||
this.keyboardController = new KeyboardController(this.context);
|
||||
this.keyboardController.onCreate();
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
console.log(`InputMethodExtAbility onDestroy`);
|
||||
}
|
||||
onDestroy() {
|
||||
console.log(`InputMethodExtAbility onDestroy`);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 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
|
||||
@ -13,55 +13,203 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import inputMethodEngine from '@ohos.inputMethodEngine'
|
||||
import commonEventManager from '@ohos.commonEventManager';
|
||||
import display from '@ohos.display';
|
||||
import inputMethodEngine from '@ohos.inputMethodEngine';
|
||||
import InputMethodExtensionContext from '@ohos.InputMethodExtensionContext';
|
||||
|
||||
globalThis.inputEngine = inputMethodEngine.getInputMethodAbility()
|
||||
// Call the getInputMethodAbility method of the input method framework to get the instance
|
||||
const inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility();
|
||||
const DEFAULT_DIRECTION: number = 1;
|
||||
const DEFAULT_LENGTH: number = 1;
|
||||
const DEFAULT_SELECT_RANGE: number = 10;
|
||||
|
||||
export class KeyboardController {
|
||||
private TAG: string = 'inputDemo: KeyboardController ';
|
||||
|
||||
constructor(context) {
|
||||
this.addLog('constructor');
|
||||
this.mContext = context;
|
||||
}
|
||||
|
||||
public onCreate(): void {
|
||||
this.addLog('onCreate');
|
||||
this.registerListener();
|
||||
}
|
||||
|
||||
private registerListener(): void {
|
||||
this.addLog('registerListener')
|
||||
this.registerInputListener();
|
||||
}
|
||||
|
||||
private registerInputListener() {
|
||||
globalThis.inputEngine.on('inputStart', (kbController, textInputClient) => {
|
||||
globalThis.textInputClient = textInputClient;
|
||||
globalThis.keyboardController = kbController;
|
||||
})
|
||||
globalThis.inputEngine.on('inputStop', (imeId) => {
|
||||
this.addLog("[inputDemo] inputStop:" + imeId);
|
||||
if (imeId == "com.example.kikainput/InputDemoService") {
|
||||
this.onDestroy();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private unRegisterListener(): void {
|
||||
this.addLog("unRegisterListener");
|
||||
globalThis.inputEngine.off('inputStart');
|
||||
globalThis.inputEngine.off('inputStop');
|
||||
}
|
||||
|
||||
public onDestroy(): void {
|
||||
this.addLog('onDestroy');
|
||||
this.unRegisterListener();
|
||||
this.mContext.destroy();
|
||||
}
|
||||
|
||||
private addLog(message): void {
|
||||
console.log(this.TAG + message)
|
||||
}
|
||||
enum TEST_RESULT_CODE {
|
||||
SUCCESS,
|
||||
FAILED
|
||||
}
|
||||
|
||||
enum TEST_FUNCTION {
|
||||
INSERT_TEXT_SYNC,
|
||||
MOVE_CURSOR_SYNC,
|
||||
GET_ATTRIBUTE_SYNC,
|
||||
SELECT_BY_RANGE_SYNC,
|
||||
SELECT_BY_MOVEMENT_SYNC,
|
||||
GET_INDEX_AT_CURSOR_SYNC,
|
||||
DELETE_FORWARD_SYNC,
|
||||
DELETE_BACKWARD_SYNC,
|
||||
GET_FORWARD_SYNC,
|
||||
GET_BACKWARD_SYNC
|
||||
}
|
||||
|
||||
export class KeyboardController {
|
||||
private TAG: string = 'inputDemo: KeyboardController ';
|
||||
private mContext: InputMethodExtensionContext | undefined = undefined; // save the context property of InputMethodExtensionAbility
|
||||
private panel: inputMethodEngine.Panel | undefined = undefined;
|
||||
private subscriber = undefined;
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
public onCreate(context: InputMethodExtensionContext): void {
|
||||
this.mContext = context;
|
||||
this.initWindow(); // init window
|
||||
this.registerListener(); //register input listener
|
||||
}
|
||||
|
||||
public onDestroy(): void {
|
||||
this.unRegisterListener();
|
||||
if (this.panel) {
|
||||
this.panel.hide();
|
||||
inputMethodAbility.destroyPanel(this.panel);
|
||||
}
|
||||
this.mContext.destroy();
|
||||
}
|
||||
|
||||
private initWindow(): void {
|
||||
if (this.mContext === undefined) {
|
||||
return;
|
||||
}
|
||||
let dis = display.getDefaultDisplaySync();
|
||||
let dWidth = dis.width;
|
||||
let dHeight = dis.height;
|
||||
let keyHeightRate = 0.47;
|
||||
let keyHeight = dHeight * keyHeightRate;
|
||||
let nonBarPosition = dHeight - keyHeight;
|
||||
let panelInfo: inputMethodEngine.PanelInfo = {
|
||||
type: inputMethodEngine.PanelType.SOFT_KEYBOARD,
|
||||
flag: inputMethodEngine.PanelFlag.FLG_FLOATING
|
||||
};
|
||||
inputMethodAbility.createPanel(this.mContext, panelInfo).then(async (inputPanel: inputMethodEngine.Panel) => {
|
||||
this.panel = inputPanel;
|
||||
if (this.panel) {
|
||||
await this.panel.resize(dWidth, keyHeight);
|
||||
await this.panel.moveTo(0, nonBarPosition);
|
||||
await this.panel.setUiContent('pages/Index');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
publishCommonEvent(event: string, codeNumber: number): void {
|
||||
this.addLog(`[inputDemo] publish event, event= ${event}, codeNumber= ${codeNumber}`);
|
||||
commonEventManager.publish(event, { code: codeNumber }, (err) => {
|
||||
if (err) {
|
||||
this.addLog(`inputDemo publish ${event} failed, err = ${JSON.stringify(err)}`);
|
||||
} else {
|
||||
this.addLog(`inputDemo publish ${event} success`);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private registerListener(): void {
|
||||
this.registerInputListener();
|
||||
let subscribeInfo = {
|
||||
events: ['syncTestFunction']
|
||||
}
|
||||
commonEventManager.createSubscriber(subscribeInfo).then((data) => {
|
||||
this.subscriber = data;
|
||||
commonEventManager.subscribe(this.subscriber, (err, eventData) => {
|
||||
this.addLog(`[inputDemo] subscribe, eventData.code = ${eventData.code}`);
|
||||
if (globalThis.textInputClient === undefined) {
|
||||
return;
|
||||
}
|
||||
switch (eventData.code) {
|
||||
case TEST_FUNCTION.INSERT_TEXT_SYNC:
|
||||
globalThis.textInputClient.insertTextSync("text");
|
||||
break;
|
||||
case TEST_FUNCTION.MOVE_CURSOR_SYNC:
|
||||
globalThis.textInputClient.moveCursorSync(DEFAULT_DIRECTION);
|
||||
break;
|
||||
case TEST_FUNCTION.GET_ATTRIBUTE_SYNC:
|
||||
this.getAttributeSync();
|
||||
break;
|
||||
case TEST_FUNCTION.SELECT_BY_RANGE_SYNC:
|
||||
globalThis.textInputClient.selectByRangeSync({ start: 0, end: DEFAULT_SELECT_RANGE });
|
||||
break;
|
||||
case TEST_FUNCTION.SELECT_BY_MOVEMENT_SYNC:
|
||||
globalThis.textInputClient.selectByMovementSync({ direction: inputMethodEngine.CURSOR_LEFT });
|
||||
break;
|
||||
case TEST_FUNCTION.GET_INDEX_AT_CURSOR_SYNC:
|
||||
this.getIndexAtCursorSync()
|
||||
break;
|
||||
case TEST_FUNCTION.DELETE_FORWARD_SYNC:
|
||||
globalThis.textInputClient.deleteForwardSync(DEFAULT_LENGTH);
|
||||
break;
|
||||
case TEST_FUNCTION.DELETE_BACKWARD_SYNC:
|
||||
globalThis.textInputClient.deleteBackwardSync(DEFAULT_LENGTH);
|
||||
break;
|
||||
case TEST_FUNCTION.GET_FORWARD_SYNC:
|
||||
this.getForwardSync();
|
||||
break;
|
||||
case TEST_FUNCTION.GET_BACKWARD_SYNC:
|
||||
this.getBackwardSync();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
private registerInputListener(): void { // 注册对输入法框架服务的开启及停止事件监听
|
||||
inputMethodAbility.on('inputStart', (kbController, textInputClient) => {
|
||||
globalThis.textInputClient = textInputClient; // 此为输入法客户端实例,由此调用输入法框架提供给输入法应用的功能接口
|
||||
globalThis.keyboardController = kbController;
|
||||
})
|
||||
inputMethodAbility.on('inputStop', () => {
|
||||
this.onDestroy();
|
||||
});
|
||||
}
|
||||
|
||||
private unRegisterListener(): void {
|
||||
inputMethodAbility.off('inputStart');
|
||||
inputMethodAbility.off('inputStop', () => {
|
||||
});
|
||||
}
|
||||
|
||||
getBackwardSync() {
|
||||
let backward: string = globalThis.textInputClient.getBackwardSync(1);
|
||||
if (backward.length >= 0) {
|
||||
this.publishCommonEvent('getBackwardSyncResult', TEST_RESULT_CODE.SUCCESS);
|
||||
} else {
|
||||
this.publishCommonEvent('getBackwardSyncResult', TEST_RESULT_CODE.FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
getForwardSync() {
|
||||
let forward: string = globalThis.textInputClient.getForwardSync(1);
|
||||
if (forward.length >= 0) {
|
||||
this.publishCommonEvent('getForwardSyncResult', TEST_RESULT_CODE.SUCCESS);
|
||||
} else {
|
||||
this.publishCommonEvent('getForwardSyncResult', TEST_RESULT_CODE.FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
getAttributeSync() {
|
||||
try {
|
||||
let editAttribute = globalThis.textInputClient.getEditorAttributeSync();
|
||||
this.addLog(`[inputDemo] publish getEditorAttributeSync editAttribute= ${JSON.stringify(editAttribute)}`);
|
||||
this.publishCommonEvent('getEditorAttributeSyncResult', TEST_RESULT_CODE.SUCCESS);
|
||||
} catch (err) {
|
||||
this.publishCommonEvent('getEditorAttributeSyncResult', TEST_RESULT_CODE.FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
getIndexAtCursorSync() {
|
||||
try {
|
||||
let index = globalThis.textInputClient.getTextIndexAtCursorSync();
|
||||
this.addLog(`[inputDemo] publish getTextIndexAtCursorSync index= ${index}`);
|
||||
this.publishCommonEvent('getTextIndexAtCursorSyncResult', TEST_RESULT_CODE.SUCCESS);
|
||||
} catch (err) {
|
||||
this.publishCommonEvent('getTextIndexAtCursorSyncResult', TEST_RESULT_CODE.FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
private addLog(message): void {
|
||||
console.log(this.TAG + message)
|
||||
}
|
||||
}
|
||||
|
||||
const keyboardController = new KeyboardController();
|
||||
|
||||
export default keyboardController;
|
@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2023 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 ImeExtension from '@ohos.inputmethodextensionability'
|
||||
import Logger from '../model/Logger'
|
||||
import InputMethodModel from '../model/InputMethodModel'
|
||||
const ERROR_CODE = -1;
|
||||
const SUCCESS_CODE = 1;
|
||||
|
||||
export default class InputMethodExtAbility extends ImeExtension {
|
||||
private inputMethodModel:InputMethodModel;
|
||||
onCreate(want) {
|
||||
Logger.log(`InputMethodExtAbility onCreate, want: ${want.abilityName}`);
|
||||
this.inputMethodModel = new InputMethodModel(this.context);
|
||||
// test startAbility
|
||||
this.inputMethodModel.startAbility(function (code) {
|
||||
if (code == SUCCESS_CODE) {
|
||||
console.log("InputMethodExtAbility startAbility success");
|
||||
} else {
|
||||
console.log("InputMethodExtAbility startAbility failed");
|
||||
}
|
||||
})
|
||||
}
|
||||
onDestroy() {
|
||||
Logger.log(`InputMethodExtAbility onDestroy`);
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2022-2023 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
|
||||
@ -13,43 +13,43 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import Ability from '@ohos.application.Ability'
|
||||
import UIAbility from '@ohos.app.ability.UIAbility';
|
||||
|
||||
export default class MainAbility extends Ability {
|
||||
onCreate(want, launchParam) {
|
||||
console.log("[Demo] MainAbility onCreate")
|
||||
globalThis.abilityWant = want;
|
||||
}
|
||||
export default class MainAbility extends UIAbility {
|
||||
onCreate(want, launchParam) {
|
||||
console.log("[Demo] MainAbility onCreate")
|
||||
globalThis.abilityWant = want;
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
console.log("[Demo] MainAbility onDestroy")
|
||||
}
|
||||
onDestroy() {
|
||||
console.log("[Demo] MainAbility onDestroy")
|
||||
}
|
||||
|
||||
onWindowStageCreate(windowStage) {
|
||||
// Main window is created, set main page for this ability
|
||||
console.log("[Demo] MainAbility onWindowStageCreate")
|
||||
onWindowStageCreate(windowStage) {
|
||||
// Main window is created, set main page for this ability
|
||||
console.log("[Demo] MainAbility onWindowStageCreate")
|
||||
|
||||
windowStage.loadContent("pages/index", (err, data) => {
|
||||
if (err.code) {
|
||||
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
|
||||
return;
|
||||
}
|
||||
console.info('Succeeded in loading the content. Data: ' + JSON.stringify(data))
|
||||
});
|
||||
}
|
||||
windowStage.loadContent("pages/Index", (err, data) => {
|
||||
if (err.code) {
|
||||
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
|
||||
return;
|
||||
}
|
||||
console.info('Succeeded in loading the content. Data: ' + JSON.stringify(data))
|
||||
});
|
||||
}
|
||||
|
||||
onWindowStageDestroy() {
|
||||
// Main window is destroyed, release UI related resources
|
||||
console.log("[Demo] MainAbility onWindowStageDestroy")
|
||||
}
|
||||
onWindowStageDestroy() {
|
||||
// Main window is destroyed, release UI related resources
|
||||
console.log("[Demo] MainAbility onWindowStageDestroy")
|
||||
}
|
||||
|
||||
onForeground() {
|
||||
// Ability has brought to foreground
|
||||
console.log("[Demo] MainAbility onForeground")
|
||||
}
|
||||
onForeground() {
|
||||
// Ability has brought to foreground
|
||||
console.log("[Demo] MainAbility onForeground")
|
||||
}
|
||||
|
||||
onBackground() {
|
||||
// Ability has back to background
|
||||
console.log("[Demo] MainAbility onBackground")
|
||||
}
|
||||
onBackground() {
|
||||
// Ability has back to background
|
||||
console.log("[Demo] MainAbility onBackground")
|
||||
}
|
||||
};
|
||||
|
@ -39,39 +39,22 @@
|
||||
"extensionAbilities": [
|
||||
{
|
||||
"description": "InputMethodExtDemo",
|
||||
"icon": "$media:app_icon",
|
||||
"icon": "$media:icon",
|
||||
"name": "InputMethodExtAbility",
|
||||
"srcEntrance": "./ets/InputMethodExtAbility/InputMethodExtAbility.ts",
|
||||
"type": "inputMethod",
|
||||
"visible": true,
|
||||
"metadata": [
|
||||
{
|
||||
"name": "language",
|
||||
"value": "chinese"
|
||||
},
|
||||
{
|
||||
"name": "mode",
|
||||
"value": "lower"
|
||||
"name": "ohos.extension.input_method",
|
||||
"resource": "$profile:input_method_config"
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
],
|
||||
"requestPermissions": [
|
||||
{
|
||||
"description": "InputMethodExtDemo2",
|
||||
"icon": "$media:app_icon",
|
||||
"name": "InputMethodExtAbility2",
|
||||
"srcEntrance": "./ets/InputMethodExtAbility2/InputMethodExtAbility2.ts",
|
||||
"type": "inputMethod",
|
||||
"visible": true,
|
||||
"metadata": [
|
||||
{
|
||||
"name": "language",
|
||||
"value": "english"
|
||||
},
|
||||
{
|
||||
"name": "mode",
|
||||
"value": "upper"
|
||||
}
|
||||
]
|
||||
"name": "ohos.permission.COMMONEVENT_STICKY"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -11,6 +11,14 @@
|
||||
{
|
||||
"name": "MainAbility_label",
|
||||
"value": "imeTest"
|
||||
},
|
||||
{
|
||||
"name": "english",
|
||||
"value": "english"
|
||||
},
|
||||
{
|
||||
"name": "chinese",
|
||||
"value": "chinese"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
{
|
||||
"subtypes": [
|
||||
{
|
||||
"icon": "$media:icon",
|
||||
"id": "InputMethodExtAbility",
|
||||
"label": "$string:chinese",
|
||||
"locale": "zh-CN",
|
||||
"mode": "lower"
|
||||
},
|
||||
{
|
||||
"icon": "$media:icon",
|
||||
"id": "InputMethodExtAbility2",
|
||||
"label": "$string:english",
|
||||
"locale": "en-US",
|
||||
"mode": "upper"
|
||||
}
|
||||
]
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"src": [
|
||||
"pages/index"
|
||||
"pages/Index"
|
||||
]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user