fix: realize power jsmock

Signed-off-by: qiuxiangdong <qiuxiangdong3@huawei.com>
This commit is contained in:
qiuxiangdong
2022-04-06 17:37:25 +08:00
parent 08833b71a7
commit 415e40b1ff
5 changed files with 207 additions and 69 deletions
@@ -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
}
@@ -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":
@@ -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
}
@@ -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
}
@@ -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
}