index.uts 11.7 KB
Newer Older
lizhongyi_'s avatar
lizhongyi_ 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
import { UTSiOS } from "DCloudUTSFoundation";
import { UIColor } from 'UIKit';
import { NSNumber } from 'Foundation';

export type UTSiOSTestResult = {
	key: string,
	value: string | null,
	passed: boolean
}

export type UTSiOSTestCompleteCallback = (res: UTSiOSTestResult[]) => void;

export type UTSiOSTestOptions = {
	complete: UTSiOSTestCompleteCallback
}

/* 获取当前控制器 */
export function getCurrentVCTest(): UTSiOSTestResult {
	let vc = UTSiOS.getCurrentViewController()
	let res: UTSiOSTestResult = {
		key: "",
		value: "",
		passed: false
	}
	if (vc != null) {
		res.passed = true;
	}
	return res;
}

/* 获取keyWindow */
export function getKeyWindowTest(): UTSiOSTestResult {
	let window = UTSiOS.getKeyWindow()
	let res: UTSiOSTestResult = {
		key: "",
		value: "",
		passed: false
	}
	if (window != null) {
		res.passed = true;
	}
	return res;
}

/* 颜色转换 */
export function colorWithStringTest(): string[] {
	let results: string[] = [];
	
	let red0 = UTSiOS.colorWithString("#f00")
	let res0: UTSiOSTestResult = {
		key: "#f00",
		value: "",
		passed: false
	}
	if (red0 != UIColor.black) {
		res0.passed = true
	}
	results.push(JSON.stringify(res0)!)
	
	let red1 = UTSiOS.colorWithString("#ff0000")
	let res1: UTSiOSTestResult = {
		key: "ff0000",
		value: "",
		passed: false
	}
	if (red1 != UIColor.black) {
		res1.passed = true;
	}
	results.push(JSON.stringify(res1)!)
	
	let red2 = UTSiOS.colorWithString("rgb(255, 0, 0)")
	let res2: UTSiOSTestResult = {
		key: "rgb(255, 0, 0)",
		value: "",
		passed: false
	}
	if (red2 != UIColor.black) {
		res2.passed = true
	}
	results.push(JSON.stringify(res2)!)
	
	let red3 = UTSiOS.colorWithString("rgba(255, 0, 0, 0.5)")
	let res3: UTSiOSTestResult = {
		key: "rgba(255, 0, 0, 0.5)",
		value: "",
		passed: false
	}
	if (red3 != UIColor.black) {
		res3.passed = true
	}
	results.push(JSON.stringify(res3)!)
	
	return results;
}

/* 资源路径转换 */
export function getResourcePathTest(value: string): UTSiOSTestResult {
	let src = UTSiOS.getResourcePath(value)
	
	let res: UTSiOSTestResult = {
		key: "转换后路径",
		value: src,
		passed: false
	}
	if (src != null) {
		res.passed = true;
	}
	return res;
}

/* 是否是模拟器 */
function isSimulatorTest(): UTSiOSTestResult {
	let ret = UTSiOS.isSimulator()
	let res: UTSiOSTestResult = {
		key: "当前是",
		value: ret ? "模拟器": "真机",
		passed: true
	}
	return res;
}

/* 获取deviceId */
function getDeviceIdTest(): UTSiOSTestResult {
	let ret = UTSiOS.getDeviceId()
	let res: UTSiOSTestResult = {
		key: "deviceId",
		value: ret,
		passed: true
	}
	return res;
}

/* 获取设备型号 */
function getModelTest(): UTSiOSTestResult {
	let ret = UTSiOS.getModel()
	let res: UTSiOSTestResult = {
		key: "设备型号 model",
		value: ret,
		passed: true
	}
	return res;
}

/* 获取userAgent */
function getUserAgentTest(): UTSiOSTestResult {
	let ret = UTSiOS.getUserAgent()
	let res: UTSiOSTestResult = {
		key: "userAgent",
		value: ret,
		passed: true
	}
	return res;
}


/* 获取设备信息 */
export function getDeviceInfoTest(): string[] {
	let results: string[] = [];
	
	let simaluteRes = isSimulatorTest();
	let deviceIDRes = getDeviceIdTest();
	let modeRes = getModelTest();
	let userSgentRes = getUserAgentTest()

	results.push(JSON.stringify(simaluteRes)!);
	results.push(JSON.stringify(deviceIDRes)!);
	results.push(JSON.stringify(modeRes)!);
	results.push(JSON.stringify(userSgentRes)!);
	return results;
}


/* 获取当前运行的AppId */
function getAppIdTest(): UTSiOSTestResult {
	let ret = UTSiOS.getAppId()
	let res: UTSiOSTestResult = {
		key: "AppID",
		value: ret,
		passed: true
	}
	return res;
}

/* 获取当前运行app的dataPath */
function getDataPathTest(): UTSiOSTestResult {
	let ret = UTSiOS.getDataPath()
	let res: UTSiOSTestResult = {
		key: "dataPath",
		value: ret,
		passed: true
	}
	return res;
}


/* 是否是unimp */
function isUniMpTest(): UTSiOSTestResult {
	let ret = UTSiOS.isUniMp()
	let res: UTSiOSTestResult = {
		key: "当前是",
		value: ret ? "小程序" : "app",
		passed: true
	}
	return res;
}

/* manifest.json 中应用名称 */
function getAppNameTest(): UTSiOSTestResult {
	let ret = UTSiOS.getAppName()
	let res: UTSiOSTestResult = {
		key: "appName",
		value: ret,
		passed: true
	}
	return res;
}

/* manifest.json 中应用版本名称 */
function getAppVersionTest(): UTSiOSTestResult {
	let ret = UTSiOS.getAppVersion()
	let res: UTSiOSTestResult = {
		key: "appVersion",
		value: ret,
		passed: true
	}
	return res;
}

/* manifest.json 中应用版本号 */
function getAppVersionCodeTest(): UTSiOSTestResult {
	let ret = UTSiOS.getAppVersionCode()
	let res: UTSiOSTestResult = {
		key: "appVersionCode",
		value: ret,
		passed: true
	}
	return res;
}

/* os language */
function getOsLanguageTest(): UTSiOSTestResult {
	let ret = UTSiOS.getOsLanguage()
	let res: UTSiOSTestResult = {
		key: "osLanguage",
		value: ret,
		passed: true
	}
	return res;
}

/* 应用资源(wgt)的版本名称 */
function getAppWgtVersionTest(): UTSiOSTestResult {
	let ret = UTSiOS.getAppWgtVersion()
	let res: UTSiOSTestResult = {
		key: "wgtVersion",
		value: ret,
		passed: true
	}
	return res;
}

/* 小程序宿主语言 */
function getHostLanguageTest(): UTSiOSTestResult {
	let ret = UTSiOS.getHostLanguage()
	let res: UTSiOSTestResult = {
		key: "hostLanguage",
		value: ret,
		passed: true
	}
	return res;
}

/* App、小程序宿主版本 */
function getHostVersionTest(): UTSiOSTestResult {
	let ret = UTSiOS.getHostVersion()
	let res: UTSiOSTestResult = {
		key: "hostVersion",
		value: ret,
		passed: true
	}
	return res;
}

/* 小程序宿主名称 */
function getHostNameTest(): UTSiOSTestResult {
	let ret = UTSiOS.getHostName()
	let res: UTSiOSTestResult = {
		key: "hostName",
		value: ret,
		passed: true
	}
	return res;
}

/* 小程序宿主包名 */
function getHostPackageNameTest(): UTSiOSTestResult {
	let ret = UTSiOS.getHostPackageName()
	let res: UTSiOSTestResult = {
		key: "hostPackageName",
		value: ret,
		passed: true
	}
	return res;
}

/* 系统当前主题,取值为light或dark。微信小程序全局配置"darkmode":true时才能获取,否则为 undefined (不支持小游戏) */
function getHostThemeTest(): UTSiOSTestResult {
	let ret = UTSiOS.getHostTheme()
	let res: UTSiOSTestResult = {
		key: "hostTheme",
		value: ret,
		passed: true
	}
	return res;
}

/* 引擎版本号 */
function getInnerVersionTest(): UTSiOSTestResult {
	let ret = UTSiOS.getInnerVersion()
	let res: UTSiOSTestResult = {
		key: "innerVersion",
		value: ret,
		passed: true
	}
	return res;
}

/* 获取App相关信息 */
export function getAppInfoTest(): string[] {
	let results: string[] = [];
	
	let appIdRes = getAppIdTest();
	let dataPathRes = getDataPathTest();
	let uniMpRes = isUniMpTest();
	let appNameRes = getAppNameTest();
	let appVersionRes = getAppVersionTest();
	let appVersionCodeRes = getAppVersionCodeTest();
	let osLanguageRes = getOsLanguageTest();
	let wgtVersionRes = getAppWgtVersionTest();
	let hostLanguageRes = getHostLanguageTest();
	let hostVersionRes = getHostVersionTest();
	let hostNameRes = getHostNameTest();
	let hostPackageRes = getHostPackageNameTest();
	let hostThemeRes = getHostThemeTest();
	let innerVersionRes = getInnerVersionTest();
	
	results.push(JSON.stringify(appIdRes)!);
	results.push(JSON.stringify(dataPathRes)!);
	results.push(JSON.stringify(uniMpRes)!);
	results.push(JSON.stringify(appNameRes)!);
	results.push(JSON.stringify(appVersionRes)!);
	results.push(JSON.stringify(appVersionCodeRes)!);
	results.push(JSON.stringify(osLanguageRes)!);
	results.push(JSON.stringify(wgtVersionRes)!);
	results.push(JSON.stringify(hostLanguageRes)!);
	results.push(JSON.stringify(hostVersionRes)!);
	results.push(JSON.stringify(hostNameRes)!);
	results.push(JSON.stringify(hostPackageRes)!);
	results.push(JSON.stringify(hostThemeRes)!);
	results.push(JSON.stringify(innerVersionRes)!);
	
	return results;
}

/* 获取系统设置 */
export function getSystemSettingTest(): UTSiOSTestResult {
	let ret = UTSiOS.getSystemSetting()
	let res: UTSiOSTestResult = {
		key: "systemSetting",
		value: JSON.stringify(ret) ?? "",
		passed: true
	}
	return res;
}

/* 将数据转换为 string */
function convertStringTest(): UTSiOSTestResult {
	let ret = UTSiOS.convertString(123)
	let res: UTSiOSTestResult = {
		key: "123转成字符串",
		value: ret,
		passed: true
	}
	return res;
}

/* 将数据转换为 bool */
function convertBoolTest(): UTSiOSTestResult {
	let ret: boolean = UTSiOS.convertBool(1)
	let res: UTSiOSTestResult = {
		key: "1转成boolean",
		value: `${ret}`,
		passed: true
	}
	return res;
}

/* 将数据转换为 NSNumber */
function convertNumberTest(): UTSiOSTestResult {
	let ret: NSNumber = UTSiOS.convertNumber(1000)
	let res: UTSiOSTestResult = {
		key: "1000 转成number",
		value: `${ret.intValue}`,
		passed: true
	}
	return res;
}

/* 将数据转换为 Double */
function convertDoubleTest(): UTSiOSTestResult {
	let ret = UTSiOS.convertDouble("123.1")
	let res: UTSiOSTestResult = {
		key: "字符串123.1转成double",
		value: `${ret}`,
		passed: true
	}
	return res;
}

/* 将数据转换为 Float */
function convertFloatTest(): UTSiOSTestResult {
	let ret = UTSiOS.convertFloat("123")
	let res: UTSiOSTestResult = {
		key: "字符串123转成float",
		value: `${ret}`,
		passed: true
	}
	return res;
}


/* 将数据转换为 int */
function convertIntTest(): UTSiOSTestResult {
	let ret = UTSiOS.convertInt("123.0")
	let res: UTSiOSTestResult = {
		key: "123.0转成int",
		value: `${ret}`,
		passed: true
	}
	return res;
}


/* 将数据转换为 Array */
function convertArrayTest(): UTSiOSTestResult {
	let ret = UTSiOS.convertArray([1, "2", "3", "abc"])
	let res: UTSiOSTestResult = {
		key: `${JSON.stringify([1, "2", "3", "abc"])}转成数组`,
		value: `${ret}`,
		passed: true
	}
	return res;
}


/* 将数据转换为 Dictionary */
function convertDictionaryTest(): UTSiOSTestResult {
	let map: Map<string, number> = new Map();
	map.set("no", 1)
	map.set('age', 10)
	let ret = UTSiOS.convertDictionary(map)
	let res: UTSiOSTestResult = {
		key: `${JSON.stringify(map)}转成dictionary`,
		value: `${ret}`,
		passed: true
	}
	return res;
}

/* 数据转换测试 */
export function dataConvertTest(): string[] {
	let results: string[] = [];
	
	let strRes = convertStringTest();
	let boolRes = convertBoolTest();
	let numberRes = convertNumberTest();
	let doubleRes = convertDoubleTest();
	let floatRes = convertFloatTest();
	let intRes = convertIntTest();
	let arrayRes = convertArrayTest();
	let dicRes = convertDictionaryTest();
	
	results.push(JSON.stringify(strRes)!);
	results.push(JSON.stringify(boolRes)!);
	results.push(JSON.stringify(numberRes)!);
	results.push(JSON.stringify(doubleRes)!);
	results.push(JSON.stringify(floatRes)!);
	results.push(JSON.stringify(intRes)!);
	results.push(JSON.stringify(arrayRes)!);
	results.push(JSON.stringify(dicRes)!);
	
	return results;
}

export function tepeofTest(): string[] {
	let results: string[] = [];
	
	let str0 = UTSiOS.typeof("uts is cool")
	let res0: UTSiOSTestResult = {
		key: "uts is cool类型是",
		value: str0,
		passed: false
	}
	if (str0 == "string") {
		res0.passed = true
	}
	results.push(JSON.stringify(res0)!)
	
	let str1 = UTSiOS.typeof(true)
	let res1: UTSiOSTestResult = {
		key: "true类型是",
		value: str1,
		passed: false
	}
	if (str1 == "boolean") {
		res1.passed = true
	}
	results.push(JSON.stringify(res1)!)
	
lizhongyi_'s avatar
lizhongyi_ 已提交
520 521
	let num = 2;
	let str2 = UTSiOS.typeof(num)
lizhongyi_'s avatar
lizhongyi_ 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	let res2: UTSiOSTestResult = {
		key: "2类型是",
		value: str2,
		passed: false
	}
	if (str2 == "number") {
		res2.passed = true
	}
	results.push(JSON.stringify(res2)!)
	
	let str3 = UTSiOS.typeof(res2)
	let res3: UTSiOSTestResult = {
		key: `${JSON.stringify(res2)}类型是`,
		value: str3,
		passed: false
	}
	if (str3 == "object") {
		res3.passed = true
	}
	results.push(JSON.stringify(res3)!)
	
	return results;
}