scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
185 lines • 4.74 kB
JavaScript
import assert from "assert";
import { AbsDevice } from "scriptable-abstract";
function isValidScreenDimension(value) {
return typeof value === "number" && value > 0;
}
function isValidPercentage(value) {
return typeof value === "number" && value >= 0 && value <= 1;
}
function isValidDeviceModel(value) {
return typeof value === "string" && ["iPhone", "iPad"].includes(value);
}
const DEFAULT_STATE = {
name: "iPhone Mock",
systemName: "iOS",
systemVersion: "16.0",
model: "iPhone",
screenSize: {
width: 390,
height: 844
},
screenResolution: {
width: 1170,
height: 2532
},
screenScale: 3,
screenBrightness: 1,
orientation: {
isInPortrait: true,
isInPortraitUpsideDown: false,
isInLandscapeLeft: false,
isInLandscapeRight: false,
isFaceUp: false,
isFaceDown: false
},
battery: {
level: 1,
isDischarging: false,
isCharging: false,
isFullyCharged: true
},
locale: {
preferredLanguages: ["en"],
locale: "en_US",
language: "en"
},
appearance: {
isUsingDarkAppearance: false
},
volume: 0.5
};
class MockDevice extends AbsDevice {
static get instance() {
return super.instance;
}
constructor() {
super(DEFAULT_STATE);
}
// Device information methods
name() {
return this.state.name;
}
systemName() {
return this.state.systemName;
}
systemVersion() {
return this.state.systemVersion;
}
model() {
return this.state.model;
}
isPhone() {
return this.state.model.toLowerCase().includes("phone");
}
isPad() {
return this.state.model.toLowerCase().includes("pad");
}
// Screen methods
screenSize() {
return { ...this.state.screenSize };
}
screenResolution() {
return { ...this.state.screenResolution };
}
screenScale() {
return this.state.screenScale;
}
screenBrightness() {
return this.state.screenBrightness;
}
setScreenBrightness(value) {
assert(isValidPercentage(value), "Brightness must be between 0 and 1");
this.setState({ screenBrightness: value });
}
// Orientation methods
isInPortrait() {
return this.state.orientation.isInPortrait;
}
isInPortraitUpsideDown() {
return this.state.orientation.isInPortraitUpsideDown;
}
isInLandscapeLeft() {
return this.state.orientation.isInLandscapeLeft;
}
isInLandscapeRight() {
return this.state.orientation.isInLandscapeRight;
}
isFaceUp() {
return this.state.orientation.isFaceUp;
}
isFaceDown() {
return this.state.orientation.isFaceDown;
}
// Battery methods
batteryLevel() {
return this.state.battery.level;
}
isDischarging() {
return this.state.battery.isDischarging;
}
isCharging() {
return this.state.battery.isCharging;
}
isFullyCharged() {
return this.state.battery.isFullyCharged;
}
// Locale methods
preferredLanguages() {
return [...this.state.locale.preferredLanguages];
}
locale() {
return this.state.locale.locale;
}
language() {
return this.state.locale.language;
}
// Appearance methods
isUsingDarkAppearance() {
return this.state.appearance.isUsingDarkAppearance;
}
// Volume methods
volume() {
return this.state.volume;
}
setVolume(value) {
assert(isValidPercentage(value), "Volume must be between 0 and 1");
this.setState({ volume: value });
}
updateState(state) {
if (state.screenBrightness !== void 0) {
assert(isValidPercentage(state.screenBrightness), "Brightness must be between 0 and 1");
}
if (state.screenScale !== void 0) {
assert(isValidScreenDimension(state.screenScale), "Screen scale must be greater than 0");
}
if (state.screenResolution) {
const { width, height } = state.screenResolution;
if (width !== void 0) {
assert(isValidScreenDimension(width), "Screen resolution width must be greater than 0");
}
if (height !== void 0) {
assert(isValidScreenDimension(height), "Screen resolution height must be greater than 0");
}
}
if (state.screenSize) {
const { width, height } = state.screenSize;
if (width !== void 0) {
assert(isValidScreenDimension(width), "Screen size width must be greater than 0");
}
if (height !== void 0) {
assert(isValidScreenDimension(height), "Screen size height must be greater than 0");
}
}
if (state.model !== void 0) {
assert(isValidDeviceModel(state.model), "Model must be either iPhone or iPad");
}
if (state.volume !== void 0) {
assert(isValidPercentage(state.volume), "Volume must be between 0 and 1");
}
super.updateState(state);
}
}
export {
MockDevice
};
//# sourceMappingURL=device.js.map