index.uts 14.4 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2 3 4 5
import MediaStore from "android.provider.MediaStore";
import Activity from "android.app.Activity";
import Bitmap from "android.graphics.Bitmap";
import FileOutputStream from "java.io.FileOutputStream";
import Intent from 'android.content.Intent';
6 7

/**
杜庆泉's avatar
杜庆泉 已提交
8 9
 * UTSAndroid.getAppContext 测试示例
 * https://uniapp.dcloud.net.cn/plugin/uts-for-android.html#_4-1-application-%E4%B8%8A%E4%B8%8B%E6%96%87%E7%9B%B8%E5%85%B3
10
 */
杜庆泉's avatar
杜庆泉 已提交
11 12
export function getAppContextTest() : boolean {
	if (UTSAndroid.getAppContext() == null) {
13 14 15
		console.log("UTSAndroid.getAppContext() error")
		return false
	}
杜庆泉's avatar
杜庆泉 已提交
16
	if (UTSAndroid.getUniActivity() == null) {
17 18 19
		console.log("UTSAndroid.getUniActivity() error")
		return false
	}
杜庆泉's avatar
杜庆泉 已提交
20
	if (UTSAndroid.getUniActivity()!.getApplicationContext() != UTSAndroid.getAppContext()) {
21 22 23 24 25
		return false
	}
	return true
}

杜庆泉's avatar
杜庆泉 已提交
26 27

export function getJavaClassTest() : boolean {
28 29
	let dispatcherClass = UTSAndroid.getJavaClass(UTSAndroid.getDispatcher())
  if("io.dcloud.uts.task.UTSTaskDispatcher" == dispatcherClass.name){
杜庆泉's avatar
杜庆泉 已提交
30 31
    return true
  }
32 33
  let applicationClass = UTSAndroid.getJavaClass(UTSAndroid.getAppContext()!)
  if("io.dcloud.uniapp.UniApplication" == applicationClass.name){
杜庆泉's avatar
杜庆泉 已提交
34 35 36 37 38 39
    return true
  }
	return false
}


杜庆泉's avatar
杜庆泉 已提交
40 41 42 43 44 45 46 47 48
/**
 * UTSAndroid.getUniActivity 测试示例
 * https://uniapp.dcloud.net.cn/plugin/uts-for-android.html#_4-2-1-getuniactivity
 */
export function getUniActivityTest() : boolean {
	if (UTSAndroid.getUniActivity() == null) {
		return false
	}
	return true
49 50
}

杜庆泉's avatar
杜庆泉 已提交
51 52 53 54 55 56 57 58 59 60 61 62

export function privacyStateTest(callback : (ret : boolean, desc : string) => void) {

	// 先重置用户同意状态
	UTSAndroid.resetPrivacyAgree()
	
	if (UTSAndroid.isPrivacyAgree() == true) {
		// 重置没有生效
		callback(false,"resetPrivacyAgree error")
	}
	
	UTSAndroid.setPrivacyAgree(true)
杜庆泉's avatar
杜庆泉 已提交
63
  UTSAndroid.getDispatcher("io").async(function(_){
杜庆泉's avatar
杜庆泉 已提交
64 65 66 67 68 69 70
		if (UTSAndroid.isPrivacyAgree() == false) {
			// 重置没有生效
			callback(false,"setPrivacyAgree error")
			console.log("setPrivacyAgree error")
		}
		callback(true,"pass")
	},null)
杜庆泉's avatar
杜庆泉 已提交
71 72 73 74 75 76 77 78 79
  
	// UTSAndroid.dispatchAsync("io",function(res){
	// 	if (UTSAndroid.isPrivacyAgree() == false) {
	// 		// 重置没有生效
	// 		callback(false,"setPrivacyAgree error")
	// 		console.log("setPrivacyAgree error")
	// 	}
	// 	callback(true,"pass")
	// },null)
杜庆泉's avatar
杜庆泉 已提交
80 81 82 83
	
}


M
mahaifeng 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
export function privacyStateCallBackTest(callback : (ret : boolean, desc : string) => void) {
  let isAgree : boolean = true
  const cb = (ret : boolean) => {
    console.log('privacyStateCallBackTest->' + ret)
    if (ret == isAgree) {
      callback(true, "pass")
    } else {
      callback(false, "callback error")
    }
  }
  // 先重置用户同意状态
  const callBackId=UTSAndroid.registerPrivacyAgreeListener(cb)
  UTSAndroid.setPrivacyAgree(isAgree)
  UTSAndroid.unRegisterPrivacyAgreeListener(callBackId)
}

杜庆泉's avatar
杜庆泉 已提交
100 101 102 103
/**
 * UTSAndroid.getAppTempPath 测试示例
 */
export function getAppTempPathTest() : boolean {
杜庆泉's avatar
杜庆泉 已提交
104
	// 1.0 标准基座的情况
杜庆泉's avatar
杜庆泉 已提交
105
	if (UTSAndroid.getAppCachePath()!.contains("data/io.dcloud.HBuilder/apps/HBuilder/temp")) {
杜庆泉's avatar
杜庆泉 已提交
106 107
		return true
	}
杜庆泉's avatar
杜庆泉 已提交
108 109 110 111
  // 1.0 自定义基座的情况
  if (UTSAndroid.getAppCachePath()!.contains("uni.UNI70BE9D0/apps/__UNI__70BE9D0/temp")) {
  	return true
  }
112
  let cachePath = UTSAndroid.getAppCachePath()!
113
	// 2.0的路径
114
	if (cachePath.contains("io.dcloud.uniappx/cache")) {
115 116
		return true
	}
117 118 119 120
  // 2.0 自定义基座的情况
  if (cachePath.contains("uni.UNI70BE9D0/cache")) {
  	return true
  }
121
	
杜庆泉's avatar
杜庆泉 已提交
122
	return false
123 124
}

杜庆泉's avatar
杜庆泉 已提交
125 126 127 128
/**
 * UTSAndroid.typeof 测试示例
 */
export function typeofClickTest() : boolean {
杜庆泉's avatar
杜庆泉 已提交
129
	
杜庆泉's avatar
杜庆泉 已提交
130

杜庆泉's avatar
杜庆泉 已提交
131
	if ((typeof 'hello wrold') != 'string') {
132 133
		return false
	}
杜庆泉's avatar
杜庆泉 已提交
134 135
	
	if ((typeof 3.1415) != 'Double') {
136 137
		return false
	}
杜庆泉's avatar
杜庆泉 已提交
138 139
	
	if ((typeof false) != 'boolean') {
140 141
		return false
	}
杜庆泉's avatar
杜庆泉 已提交
142
	if ((typeof true) != 'boolean') {
143 144
		return false
	}
杜庆泉's avatar
杜庆泉 已提交
145
	if ((typeof new Date()) != 'object') {
146 147
		return false
	}
杜庆泉's avatar
杜庆泉 已提交
148

149 150 151 152
	return true
}


杜庆泉's avatar
杜庆泉 已提交
153 154 155 156 157 158 159 160
/**
 * 手动跳转用户授权管理界面
 */
export function gotoSystemPermissionActivityTest() {
	/**
	 * 用户手动授权录音权限
	 */
	UTSAndroid.gotoSystemPermissionActivity(UTSAndroid.getUniActivity()!, utsArrayOf('Manifest.permission.RECORD_AUDIO'))
161 162
}

杜庆泉's avatar
杜庆泉 已提交
163 164 165 166 167
/**
 * 跳转系统拍照界面,并触发相关生命周期测试
 */
export function gotoCameraTake(imageDone : (event : string) => void) : boolean {

168
	let permissionNeed = ["android.permission.CAMERA"]
杜庆泉's avatar
杜庆泉 已提交
169

杜庆泉's avatar
杜庆泉 已提交
170
	UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _ : string[]) {
杜庆泉's avatar
杜庆泉 已提交
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
		if (allRight) {
			// 交给目前的location 引擎,真实执行
			UTSAndroid.onAppActivityResult((requestCode : Int, resultCode : Int, data ?: Intent) => {

				if ((requestCode == 1001) && (resultCode == Activity.RESULT_OK)) {
					if (data != null) {
						let bundle = data.getExtras();
						let mImageBitmap = bundle!.get("data") as Bitmap;
						let bitmapPath = UTSAndroid.getUniActivity()!.getExternalCacheDir()!.getPath() + "/photo.png"
						console.log(bitmapPath);
						try {
							mImageBitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(bitmapPath))
						} catch (e) {
						}
						imageDone(bitmapPath);
					}
				}
			});

			let takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			//resolveActivity 返回可处理 Intent 的第一个 Activity 组件
			if (takePictureIntent.resolveActivity(UTSAndroid.getUniActivity()!.getPackageManager()) != null) {
				UTSAndroid.getUniActivity()!.startActivityForResult(takePictureIntent, 1001);
			}
		} else {
			//callback(false,"用户拒绝了部分权限")
		}
杜庆泉's avatar
杜庆泉 已提交
198
	}, function (_ : boolean, _ : string[]) {
杜庆泉's avatar
杜庆泉 已提交
199 200 201 202
		//callback(false,"用户拒绝了部分权限")
	})

	return true;
203 204 205

}

杜庆泉's avatar
杜庆泉 已提交
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
/**
 * 初始化生命周期相关
 */
export function initAppLifecycle(onLifecycleChange : (event : string) => void) {

	/**
	 * application 内存不足的回调函数
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onapptrimmemory
	 */
	UTSAndroid.onAppTrimMemory((level : Number) => {
		let eventName = "onAppTrimMemory - " + level;
		onLifecycleChange(eventName);
		console.log(eventName);
	});

	/**
	 * application 状态改变的回调函数
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onAppConfigChange
	 */
	UTSAndroid.onAppConfigChange((ret : UTSJSONObject) => {
		let eventName = "onAppConfigChange - " + JSON.stringify(ret);
		onLifecycleChange(eventName);
		console.log(eventName);
	});


	/**
	 * activity 销毁生命周期回调
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitydestroy
	 */
	UTSAndroid.onAppActivityDestroy(() => {
		let eventName = "onAppActivityDestroy";
		onLifecycleChange(eventName);
		console.log(eventName);
	});


	/**
	 * activity 失去焦点生命周期回调
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitypause
	 */
	UTSAndroid.onAppActivityPause(() => {
		let eventName = "onAppActivityPause";
		onLifecycleChange(eventName);
		console.log(eventName);
	});
	/**
	 * activity 得到焦点的周期回调
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume
	 */
	UTSAndroid.onAppActivityResume(() => {
		let eventName = "onAppActivityResume";
		onLifecycleChange(eventName);
		console.log(eventName);
	});
	/**
	 * activity 回退物理按键事件回调
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityback
	 */
	UTSAndroid.onAppActivityBack(() => {
		let eventName = "onAppActivityBack";
		onLifecycleChange(eventName);
		console.log(eventName);
	});

271 272
}

杜庆泉's avatar
杜庆泉 已提交
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

/**
 * 取消注册生命周期函数
 */
export function unRegLifecycle() {

	/**
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onapptrimmemory
	 */
	UTSAndroid.offAppTrimMemory();

	/**
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onAppConfigChange
	 */
	UTSAndroid.offAppConfigChange();

	/**
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitydestroy
	 */
	UTSAndroid.offAppActivityDestroy();

	/**
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitypause
	 */
	UTSAndroid.offAppActivityPause();

	/**
	 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume
	 */
	UTSAndroid.offAppActivityResume();
	/**
	 * activity 回退物理按键事件回调
	 */
	UTSAndroid.offAppActivityBack();

308 309
}

杜庆泉's avatar
杜庆泉 已提交
310 311


312 313 314
/**
 * 一组权限,申请流程测试
 */
杜庆泉's avatar
杜庆泉 已提交
315 316
export function arrayPermissionFlowTest(callback : (ret : boolean, desc : string) => void) {

317 318 319 320 321 322 323 324 325 326 327

	if("io.dcloud.uniappx" != UTSAndroid.getAppContext()?.packageName && "io.dcloud.HBuilder" != UTSAndroid.getAppContext()?.packageName){
		/**
		 * 说明是自定义基座,不支持权限测试
		 */
		uni.showToast({
			title:'自定义基座不支持当前测试'
		})
		return 
	}
	
杜庆泉's avatar
杜庆泉 已提交
328 329 330
	/**
	 * 同时申请日历和联系人权限
	 */
331
	let permissionNeed = ["android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.ACCESS_FINE_LOCATION", "android.permission.READ_PHONE_STATE"]
杜庆泉's avatar
杜庆泉 已提交
332 333
	
	if (UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) {
334
		callback(false, "已具备请求权限")
杜庆泉's avatar
杜庆泉 已提交
335 336 337
		return;
	}

杜庆泉's avatar
杜庆泉 已提交
338
	UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _ : string[]) {
杜庆泉's avatar
杜庆泉 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		if (allRight) {
			// 交给目前的location 引擎,真实执行
			// 测试权限api 返回值
			if (!UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) {
				// 预期当前基座具备了读写日历的权限
				callback(false, "权限请求完成,getSystemPermissionDenied 失败")
				return;
			}
			if (!UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!, permissionNeed)) {
				callback(false, "权限请求完成,checkSystemPermissionGranted 失败")
				return;
			}
			callback(true, "")
		} else {
			callback(false, "用户拒绝了部分权限")
		}
杜庆泉's avatar
杜庆泉 已提交
355
	}, function (_ : boolean, _ : string[]) {
杜庆泉's avatar
杜庆泉 已提交
356 357 358 359 360 361
		callback(false, "用户拒绝了部分权限")
	})

}


杜庆泉's avatar
杜庆泉 已提交
362 363 364 365 366 367 368
/**
 * 获取设备信息
 */
export function getDeviceInfoTest():string {
	let info = ''
	info += 'isUniMp: '+UTSAndroid.isUniMp() + "\n ";
	info += 'getAppDarkMode: '+UTSAndroid.getAppDarkMode() + "\n ";
杜庆泉's avatar
杜庆泉 已提交
369 370 371
	info += 'getLanguageInfo: '+ JSON.stringify(UTSAndroid.getLanguageInfo(UTSAndroid.getAppContext()!)) + "\n ";
	info += 'getWebViewInfo: '+ JSON.stringify(UTSAndroid.getWebViewInfo(UTSAndroid.getAppContext()!)) + "\n ";
	info += 'getDeviceID: '+UTSAndroid.getDeviceID(UTSAndroid.getAppContext()!) + "\n ";
杜庆泉's avatar
杜庆泉 已提交
372 373 374 375 376
	info += 'getOAID: '+UTSAndroid.getOAID() + "\n ";
	info += 'getInnerVersion: '+UTSAndroid.getInnerVersion() + "\n ";
	info += 'getUniCompileVersion: '+UTSAndroid.getUniCompileVersion() + "\n ";
	info += 'getUniRuntimeVersion: '+UTSAndroid.getUniRuntimeVersion() + "\n ";
	info += 'getAppId: '+UTSAndroid.getAppId() + "\n ";
377
	info += 'getAppVersion: '+JSON.stringify(UTSAndroid.getAppVersion()) + "\n ";
杜庆泉's avatar
杜庆泉 已提交
378 379
	info += 'getAppName: '+UTSAndroid.getAppName() + "\n ";
	info += 'getOsTheme: '+UTSAndroid.getOsTheme() + "\n ";
380
	info += 'getScreenInfo: '+JSON.stringify(UTSAndroid.getScreenInfo()) + "\n ";
杜庆泉's avatar
杜庆泉 已提交
381 382 383 384
	info += 'getStatusBarHeight: '+UTSAndroid.getStatusBarHeight() + "\n ";
	info += 'isTitleNViewShow: '+UTSAndroid.isTitleNViewShow() + "\n ";
	info += 'isTabBarShow: '+UTSAndroid.isTabBarShow() + "\n ";
	info += 'getWindowHeight: '+UTSAndroid.getWindowHeight() + "\n ";
385
	info += 'getSafeAreaInsets: '+JSON.stringify(UTSAndroid.getSafeAreaInsets()) + "\n ";
杜庆泉's avatar
杜庆泉 已提交
386 387 388 389
	info += 'isUniAppX: '+UTSAndroid.isUniAppX() + "\n ";
	
	return info
}
杜庆泉's avatar
杜庆泉 已提交
390 391 392 393
/**
 * 任务分发测试
 */
export function dispatchAsyncTest(callback : (ret : boolean, desc : string) => void) {
杜庆泉's avatar
杜庆泉 已提交
394
  UTSAndroid.getDispatcher("main").async(function(_){
杜庆泉's avatar
杜庆泉 已提交
395 396 397 398
		if(Thread.currentThread().name != 'main'){
			callback(false,"main thread error")
			return
		}
杜庆泉's avatar
杜庆泉 已提交
399
		UTSAndroid.getDispatcher("dom").async(function(_){
杜庆泉's avatar
杜庆泉 已提交
400 401 402 403 404 405 406
			/**
			 * dom 参数,只在2.0生效,1.0会自动切换到main线程
			 */
			if(Thread.currentThread().name != 'main' && Thread.currentThread().name != 'io_dcloud_uniapp_dom'){
				callback(false,"dom thread error")
				return
			}
杜庆泉's avatar
杜庆泉 已提交
407
			UTSAndroid.getDispatcher("io").async(function(_){
杜庆泉's avatar
杜庆泉 已提交
408 409 410
				/**
				 * dom 参数,只在2.0生效,1.0会自动切换到main线程
				 */
杜庆泉's avatar
杜庆泉 已提交
411
				if(!Thread.currentThread().name.contains("DefaultDispatcher")){
杜庆泉's avatar
杜庆泉 已提交
412 413 414
					callback(false,"io thread error")
					return
				}
杜庆泉's avatar
杜庆泉 已提交
415
				callback(true,"pass")
杜庆泉's avatar
杜庆泉 已提交
416 417 418 419
			},null)
			
		},null)
	},null)
杜庆泉's avatar
杜庆泉 已提交
420
  
杜庆泉's avatar
杜庆泉 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

}

/**
 * 路径转换测试
 */
export function convert2AbsFullPathTest() : boolean {

	let resourcePath = UTSAndroid.convert2AbsFullPath("static/logo.png")
	console.log(resourcePath)
	let resourcePath2 = UTSAndroid.getResourcePath("static/logo.png")
	console.log(resourcePath2)
	if (resourcePath != resourcePath2) {
		console.log("convert2AbsFullPath 与 getResourcePath 结果不一致")
		return false;
	}

438 439 440 441 442 443 444 445 446

	if("io.dcloud.uniappx" == UTSAndroid.getAppContext()?.packageName || "io.dcloud.HBuilder" == UTSAndroid.getAppContext()?.packageName){
		/**
		 * 只有标准基座,校验这个选项
		 */
		if (!resourcePath.contains("Android/data/io.dcloud")) {
			console.log("路径转换结果错误")
			return false;
		}
杜庆泉's avatar
杜庆泉 已提交
447
	}
448 449
	
	
杜庆泉's avatar
杜庆泉 已提交
450 451 452 453 454 455 456 457 458

	let sdcardPath = UTSAndroid.convert2AbsFullPath(resourcePath)
	console.log(sdcardPath)

	if (resourcePath != sdcardPath) {
		console.log("绝对路径转换结果错误")
		return false;
	}

459
	return true
杜庆泉's avatar
杜庆泉 已提交
460 461 462 463 464 465 466 467
}



/**
 * 单个权限,申请流程测试
 */
export function singlePermissionFlowTest(callback : (ret : boolean, desc : string) => void) {
468 469 470 471 472 473 474 475 476 477
	
	if("io.dcloud.uniappx" != UTSAndroid.getAppContext()?.packageName && "io.dcloud.HBuilder" != UTSAndroid.getAppContext()?.packageName){
		/**
		 * 说明是自定义基座,不支持权限测试
		 */
		uni.showToast({
			title:'自定义基座不支持当前测试'
		})
		return 
	}
杜庆泉's avatar
杜庆泉 已提交
478

479
	let permissionNeed = ["android.permission.READ_PHONE_STATE"]
杜庆泉's avatar
杜庆泉 已提交
480
	if (UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) {
481
		callback(false, "已具备权限")
杜庆泉's avatar
杜庆泉 已提交
482 483 484
		return;
	}

杜庆泉's avatar
杜庆泉 已提交
485
	UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _ : string[]) {
杜庆泉's avatar
杜庆泉 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499
		if (allRight) {
			// 测试权限api 返回值
			if (!UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) {
				callback(false, "权限请求完成,getSystemPermissionDenied 失败")
				return;
			}
			if (!UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!, permissionNeed)) {
				callback(false, "权限请求完成,checkSystemPermissionGranted 失败")
				return;
			}
			callback(true, "")
		} else {
			callback(false, "用户拒绝了部分权限")
		}
杜庆泉's avatar
杜庆泉 已提交
500
	}, function (_ : boolean, _ : string[]) {
杜庆泉's avatar
杜庆泉 已提交
501 502 503
		callback(false, "用户拒绝了部分权限")
	})

504
}