From d18e914430cc41a29de3a0699674b18ba330b14a Mon Sep 17 00:00:00 2001 From: qiuxiangdong Date: Wed, 6 Apr 2022 17:37:25 +0800 Subject: [PATCH] fix: realize power jsmock Signed-off-by: qiuxiangdong --- .../systemplugin/napi/batteryStatistics.js | 75 +++++++++++++++++++ .../main/extend/systemplugin/napi/index.js | 12 ++- .../extend/systemplugin/napi/runningLock.js | 66 ---------------- .../extend/systemplugin/napi/runninglock.js | 69 +++++++++++++++++ .../main/extend/systemplugin/napi/thermal.js | 54 +++++++++++++ 5 files changed, 207 insertions(+), 69 deletions(-) create mode 100644 runtime/main/extend/systemplugin/napi/batteryStatistics.js delete mode 100644 runtime/main/extend/systemplugin/napi/runningLock.js create mode 100644 runtime/main/extend/systemplugin/napi/runninglock.js create mode 100644 runtime/main/extend/systemplugin/napi/thermal.js diff --git a/runtime/main/extend/systemplugin/napi/batteryStatistics.js b/runtime/main/extend/systemplugin/napi/batteryStatistics.js new file mode 100644 index 00000000..7e771254 --- /dev/null +++ b/runtime/main/extend/systemplugin/napi/batteryStatistics.js @@ -0,0 +1,75 @@ +/* + * 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 { paramMock } from "../utils" + +export function mockBatteryStatistics() { + const ConsumptionType = { + CONSUMPTION_TYPE_INVALID: -17, + CONSUMPTION_TYPE_APP: '[PC preview] unknow CONSUMPTION_TYPE_APP', + CONSUMPTION_TYPE_BLUETOOTH: '[PC preview] unknow CONSUMPTION_TYPE_BLUETOOTH', + CONSUMPTION_TYPE_IDLE: '[PC preview] unknow CONSUMPTION_TYPE_IDLE', + CONSUMPTION_TYPE_PHONE: '[PC preview] unknow CONSUMPTION_TYPE_PHONE', + CONSUMPTION_TYPE_RADIO: '[PC preview] unknow CONSUMPTION_TYPE_RADIO', + CONSUMPTION_TYPE_SCREEN: '[PC preview] unknow CONSUMPTION_TYPE_SCREEN', + CONSUMPTION_TYPE_USER: '[PC preview] unknow CONSUMPTION_TYPE_USER', + CONSUMPTION_TYPE_WIFI: '[PC preview] unknow CONSUMPTION_TYPE_WIFI' + } + const BatteryStatsInfo = { + uid: '[PC preview] unknow uid', + type: '[PC preview] unknow type', + power: '[PC preview] unknow power', + } + const batteryStatistics = { + ConsumptionType, + BatteryStatsInfo, + getBatteryStats: function (...args) { + console.warn("batteryStatistics.getBatteryStats interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + var batteryStatsInfos = new Array(BatteryStatsInfo) + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, batteryStatsInfos); + }else { + return new Promise((resolve, reject) => { + resolve(batteryStatsInfos); + }) + } + }, + getAppPowerValue: function (...args) { + console.warn("batteryStatistics.getAppPowerValue interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + return paramMock.paramNumberMock; + }, + getAppPowerPercent: function (...args) { + console.warn("batteryStatistics.getAppPowerPercent interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + return paramMock.paramNumberMock; + }, + getHardwareUnitPowerValue: function (...args) { + console.warn("batteryStatistics.getHardwareUnitPowerValue interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + return paramMock.paramNumberMock; + + }, + getHardwareUnitPowerPercent: function (...args) { + console.warn("batteryStatistics.getHardwareUnitPowerPercent interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + return paramMock.paramNumberMock; + } + } + return batteryStatistics + } + \ No newline at end of file diff --git a/runtime/main/extend/systemplugin/napi/index.js b/runtime/main/extend/systemplugin/napi/index.js index da28c720..a3c9e0ff 100644 --- a/runtime/main/extend/systemplugin/napi/index.js +++ b/runtime/main/extend/systemplugin/napi/index.js @@ -31,9 +31,11 @@ import { mockFileio } from './fileio' import { mockWorker } from './worker' import { mockUtil } from './util' import { mockPower } from './power' +import { mockRunninglock } from './runninglock' +import { mockThermal } from './thermal' +import { mockBatteryStatistics } from './batteryStatistics' import { mockIntl } from './intl' import { mockI18N } from './i18n' -import { mockRunningLock } from './runningLock' import { mockRpc } from './rpc' import { mockBytrace } from './bytrace' import { mockwebgl } from './webgl' @@ -141,8 +143,12 @@ export function mockRequireNapiFun() { return mockI18N(); case "power": return mockPower(); - case "runningLock": - return mockRunningLock(); + case "runninglock": + return mockRunninglock(); + case "thermal": + return mockThermal(); + case "batteryStatistics": + return mockBatteryStatistics(); case "rpc": return mockRpc(); case "webgl": diff --git a/runtime/main/extend/systemplugin/napi/runningLock.js b/runtime/main/extend/systemplugin/napi/runningLock.js deleted file mode 100644 index c5bccce1..00000000 --- a/runtime/main/extend/systemplugin/napi/runningLock.js +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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. - */ - -import { paramMock } from "../utils" - -export function mockRunningLock() { - const RunningLockMock = { - lock: function (...args) { - console.warn("RunningLock.lock interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - }, - isUsed: function () { - console.warn("RunningLock.isUsed interface mocked in the Previewer. How this interface works on the" + - " Previewer may be different from that on a real device.") - return paramMock.paramBooleanMock - }, - unlock: function () { - console.warn("RunningLock.unlock interface mocked in the Previewer. How this interface works on the Previewer" + - " may be different from that on a real device.") - } - }; - const RunningLockType = { - BACKGROUND: '[PC Preview] unknow BACKGROUND', - PROXIMITY_SCREEN_CONTROL: '[PC Preview] unknow PROXIMITY_SCREEN_CONTROL' - }; - const runningLock = { - RunningLockType, - isRunningLockTypeSupported: function (...args) { - console.warn("runningLock.isRunningLockTypeSupported interface mocked in the Previewer. How this interface works on the" + - " Previewer may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); - } else { - return new Promise((resolve, reject) => { - resolve(paramMock.paramBooleanMock); - }) - } - }, - createRunningLock: function (...args) { - console.warn("runningLock.createRunningLock interface mocked in the Previewer. How this interface works on the" + - " Previewer may be different from that on a real device.") - const len = args.length - if (typeof args[len - 1] === 'function') { - args[len - 1].call(this, paramMock.businessErrorMock, RunningLockMock); - } else { - return new Promise((resolve, reject) => { - resolve(RunningLockMock); - }) - } - } - } - return runningLock -} diff --git a/runtime/main/extend/systemplugin/napi/runninglock.js b/runtime/main/extend/systemplugin/napi/runninglock.js new file mode 100644 index 00000000..3fd00a27 --- /dev/null +++ b/runtime/main/extend/systemplugin/napi/runninglock.js @@ -0,0 +1,69 @@ +/* + * 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 { paramMock } from "../utils" + +export function mockRunninglock() { + const RunningLockType = { + BACKGROUND: 1, + PROXIMITY_SCREEN_CONTROL: '[PC Preview] unknow PROXIMITY_SCREEN_CONTROL', + } + const RunningLockClass = class RunningLock { + constructor() { + this.lock = function (...args) { + console.warn("RunningLock.lock interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + }; + this.isUsed = function (...args) { + console.warn("RunningLock.isUsed interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + return paramMock.paramBooleanMock + }; + this.unlock = function (...args) { + console.warn("RunningLock.unlock interface mocked in the Previewer. How this interface works on the Previewer" + + " may be different from that on a real device.") + }; + } + } + const runninglock = { + RunningLock:RunningLockClass, + RunningLockType, + isRunningLockTypeSupported: function (...args) { + console.warn("runningLock.isRunningLockTypeSupported interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); + } else { + return new Promise((resolve, reject) => { + resolve(paramMock.paramBooleanMock); + }) + } + }, + createRunningLock: function (...args) { + console.warn("runningLock.createRunningLock interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, new RunningLockClass()); + } else { + return new Promise((resolve, reject) => { + resolve(new RunningLockClass()); + }) + } + } + } + return runninglock +} diff --git a/runtime/main/extend/systemplugin/napi/thermal.js b/runtime/main/extend/systemplugin/napi/thermal.js new file mode 100644 index 00000000..5fd603c3 --- /dev/null +++ b/runtime/main/extend/systemplugin/napi/thermal.js @@ -0,0 +1,54 @@ +/* + * 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 { paramMock } from "../utils" + +export function mockThermal() { + const ThermalLevel = { + COOL: 0, + NORMAL: 1, + WARM: 2, + HOT: 3, + OVERHEATED: 4, + WARNING: 5, + EMERGENCY: 6, + } + const thermal = { + ThermalLevel, + subscribeThermalLevel: function (...args) { + console.warn("thermal.subscribeThermalLevel interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, ThermalLevel); + } + }, + unsubscribeThermalLevel: function (...args) { + console.warn("thermal.unsubscribeThermalLevel interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock); + } + }, + getThermalLevel: function (...args) { + console.warn("thermal.getThermalLevel interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + return ThermalLevel + } + } + return thermal + } + \ No newline at end of file