提交 9afad088 编写于 作者: 杜庆泉's avatar 杜庆泉

整理utsAndroid 测试

上级 fd580c76
<template>
<view>
<button class="width:100%" @click="getAppContextClick" >getAppContext</button>
<button class="width:100%" @click="getUniActivityClick">getUniActivity</button>
<button class="width:100%" @click="getAppTempPathClick">getAppTempPath</button>
<button class="width:100%" @click="typeofClick">typeof</button>
<button class="width:100%" @click="checkSystemPermissionGrantedClick">checkSystemPermissionGranted</button>
<button class="width:100%" @click="getSystemPermissionDeniedClick">getSystemPermissionDenied</button>
<button class="width:100%" @click="gotoSystemPermissionActivityClick">gotoSystemPermissionActivity</button>
<button class="width:100%" @click="arrayPermissionFlowClick">组权限申请流程测试</button>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-hello-text">
逐一点击执行,观察测试反馈
</view>
</view>
<button @click="getAppContextClick">getAppContext</button>
<button @click="getUniActivityClick">getUniActivity</button>
<button @click="getAppTempPathClick">getAppTempPath</button>
<button @click="typeofClick">typeof</button>
<button @click="arrayPermissionFlowClick">组权限申请流程测试</button>
<button @click="singlePermissionFlowClick">单权限申请流程测试</button>
<button @click="dispatchAsyncClick">任务分发测试</button>
<button @click="pathTestClick">路径转换测试</button>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-hello-text">
1. 当前页面已通过initAppLifecycle函数注册了生命周期监听。
</view>
<view class="uni-hello-text">
2. 手动切换其他APP再返回,可在控制台和界面观察事件日志
</view>
</view>
<view class="uni-padding-wrap uni-common-mt">
<view class="text-box" scroll-y="true">
<text>{{text}}</text>
</view>
</view>
<button class="testButton" @click="gotoSystemPermissionActivityClick">手动申请权限测试</button>
<button @tap="testGoOtherActivity">跳转拍照界面</button>
<button @tap="testUnRegLifecycle">取消注册周期函数</button>
<image :src="selectImage" v-if="selectImage"></image>
</view>
</template>
<script>
import {
getAppContextTest,
getUniActivityTest,
getAppTempPathTest,
typeofClickTest,
checkSystemPermissionGrantedTest,
getSystemPermissionDeniedTest,
gotoSystemPermissionActivityTest,
arrayPermissionFlowTest
arrayPermissionFlowTest,
singlePermissionFlowTest,
dispatchAsyncTest,
convert2AbsFullPathTest,
unRegLifecycle,
initAppLifecycle,
gotoCameraTake
} from '@/uni_modules/uts-platform-api'
/**
* 测试在页面生命周期之外,使用api
*/
export default {
data() {
return {}
return {
text: '',
selectImage:''
}
},
onLoad:function(){
let that = this;
initAppLifecycle(function(eventLog){
// 展示捕捉到的声明周期日志
that.text = that.text += eventLog;
that.text = that.text += '\n';
});
},
methods: {
testGoOtherActivity(){
var that = this;
let ret = gotoCameraTake(function(file){
// 展示捕捉到的声明周期日志
console.log(file);
that.selectImage = "file://" + file;
});
if(!ret){
uni.showToast({
icon:'none',
title:'测试失败'
})
}
},
testUnRegLifecycle(){
// 取消注册生命周期
unRegLifecycle();
},
getAppContextClick() {
console.log("getAppContextClick")
if(getAppContextTest()){
if (getAppContextTest()) {
uni.showToast({
title:'测试通过'
title: '测试通过'
})
}else{
} else {
uni.showToast({
icon:'error',
title:'测试失败'
icon: 'error',
title: '测试失败'
})
}
},
getUniActivityClick() {
getUniActivityTest()
},
getAppTempPathClick() {
getAppTempPathTest()
if (getUniActivityTest()) {
uni.showToast({
title: '测试通过'
})
} else {
uni.showToast({
icon: 'error',
title: '测试失败'
})
}
},
typeofClick() {
if(typeofClickTest()){
pathTestClick() {
if (convert2AbsFullPathTest()) {
uni.showToast({
title:'测试通过'
title: '测试通过'
})
}else{
} else {
uni.showToast({
icon:'error',
title:'测试失败'
icon: 'error',
title: '测试失败'
})
}
},
checkSystemPermissionGrantedClick() {
if(checkSystemPermissionGrantedTest()){
getAppTempPathClick() {
if (getAppTempPathTest()) {
uni.showToast({
title:'测试通过'
title: '测试通过'
})
}else{
} else {
uni.showToast({
icon:'error',
title:'测试失败'
icon: 'error',
title: '测试失败'
})
}
},
getSystemPermissionDeniedClick() {
if(getSystemPermissionDeniedTest()){
dispatchAsyncClick() {
dispatchAsyncTest(function(ret,desc){
if (ret) {
uni.showToast({
icon: 'none',
title: '测试通过'
})
} else {
uni.showToast({
icon: 'none',
title: '失败:' + desc
})
}
})
},
typeofClick() {
if (typeofClickTest()) {
uni.showToast({
title:'测试通过'
title: '测试通过'
})
}else{
} else {
uni.showToast({
icon:'error',
title:'测试失败'
icon: 'error',
title: '测试失败'
})
}
},
gotoSystemPermissionActivityClick() {
gotoSystemPermissionActivityTest()
},
arrayPermissionFlowClick() {
arrayPermissionFlowTest()
arrayPermissionFlowTest(function(ret,desc){
if (ret) {
uni.showToast({
icon: 'none',
title: '测试通过'
})
} else {
uni.showToast({
icon: 'none',
title: '失败:' + desc
})
}
})
},
singlePermissionFlowClick() {
singlePermissionFlowTest(function(ret,desc){
if (ret) {
uni.showToast({
icon: 'none',
title: '测试通过'
})
} else {
uni.showToast({
icon: 'none',
title: '失败:' + desc
})
}
})
}
}
}
</script>
<style>
.testButton{
width:100%
}
</style>
\ No newline at end of file
import {
UTSAndroid
} from "io.dcloud.uts";
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';
/**
* UTSAndroid.getAppContext
* 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
*/
export function getAppContextTest():boolean{
if(UTSAndroid.getAppContext() == null){
export function getAppContextTest() : boolean {
if (UTSAndroid.getAppContext() == null) {
console.log("UTSAndroid.getAppContext() error")
return false
}
if(UTSAndroid.getUniActivity() == null){
if (UTSAndroid.getUniActivity() == null) {
console.log("UTSAndroid.getUniActivity() error")
return false
}
if(UTSAndroid.getUniActivity()!.getApplicationContext() == UTSAndroid.getAppContext()){
console.log(UTSAndroid.getUniActivity()!.getApplicationContext())
console.log(UTSAndroid.getAppContext())
if (UTSAndroid.getUniActivity()!.getApplicationContext() != UTSAndroid.getAppContext()) {
return false
}
console.log("getAppContextTest success")
return true
}
export function getUniActivityTest(){
console.log(UTSAndroid.getUniActivity())
/**
* 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
}
export function getAppTempPathTest(){
console.log(UTSAndroid.getAppTempPath())
/**
* UTSAndroid.getAppTempPath 测试示例
*/
export function getAppTempPathTest() : boolean {
if (UTSAndroid.getAppTempPath()!.contains("data/io.dcloud.HBuilder/apps/HBuilder/temp")) {
return true
}
return false
}
export function typeofClickTest():boolean{
if(UTSAndroid.typeof('hello wrold') != 'string'){
/**
* UTSAndroid.typeof 测试示例
*/
export function typeofClickTest() : boolean {
if (UTSAndroid.typeof('hello wrold') != 'string') {
return false
}
if(UTSAndroid.typeof(3.1415926) != 'number'){
if (UTSAndroid.typeof(3.1415926) != 'number') {
return false
}
if(UTSAndroid.typeof(-100) != 'number'){
if (UTSAndroid.typeof(-100) != 'number') {
return false
}
if(UTSAndroid.typeof(false) != 'boolean'){
if (UTSAndroid.typeof(false) != 'boolean') {
return false
}
if(UTSAndroid.typeof(true) != 'boolean'){
if (UTSAndroid.typeof(true) != 'boolean') {
return false
}
if(UTSAndroid.typeof(new Date()) != 'object'){
if (UTSAndroid.typeof(new Date()) != 'object') {
return false
}
return true
}
export function checkSystemPermissionGrantedTest():boolean{
let ret = UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.RECORD_AUDIO'))
console.log(ret)
let ret3 = UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.ACCESS_WIFI_STATE','Manifest.permission.WRITE_CALENDAR','Manifest.permission.BLUETOOTH'))
console.log(ret3)
return true
/**
* 手动跳转用户授权管理界面
*/
export function gotoSystemPermissionActivityTest() {
/**
* 用户手动授权录音权限
*/
UTSAndroid.gotoSystemPermissionActivity(UTSAndroid.getUniActivity()!, utsArrayOf('Manifest.permission.RECORD_AUDIO'))
}
/**
* 跳转系统拍照界面,并触发相关生命周期测试
*/
export function gotoCameraTake(imageDone : (event : string) => void) : boolean {
let permissionNeed = utsArrayOf("android.permission.CAMERA")
UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _grantedList : string[]) {
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,"用户拒绝了部分权限")
}
}, function (_doNotAskAgain : boolean, _grantedList : string[]) {
//callback(false,"用户拒绝了部分权限")
})
return true;
export function getSystemPermissionDeniedTest():boolean{
let ret = UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.RECORD_AUDIO'))
console.log(ret)
let ret3 = UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.ACCESS_WIFI_STATE','Manifest.permission.WRITE_CALENDAR','Manifest.permission.BLUETOOTH'))
console.log(ret3)
return true
}
export function gotoSystemPermissionActivityTest(){
UTSAndroid.gotoSystemPermissionActivity(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.RECORD_AUDIO'))
/**
* 初始化生命周期相关
*/
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);
});
}
export function requestSystemPermission(){
// UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.RECORD_AUDIO'))
// let permissionNeed : string[] = utsArrayOf("android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_FINE_LOCATION");
// UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight:boolean,_grantedList:string[]) {
// if (allRight) {
// // 交给目前的location 引擎,真实执行
// console.log("all right:")
// }
// }, function (_doNotAskAgain:boolean,_grantedList:string[]) {
// console.log("用户拒绝了部分权限:")
// })
/**
* 取消注册生命周期函数
*/
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();
}
/**
* 一组权限,申请流程测试
*/
export function arrayPermissionFlowTest():boolean{
// if(!UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!,utsArrayOf("android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"))){
// // 此时预期
// return false
// }
export function arrayPermissionFlowTest(callback : (ret : boolean, desc : string) => void) {
/**
* 同时申请日历和联系人权限
*/
let permissionNeed = utsArrayOf("android.permission.READ_CALENDAR", "android.permission.WRITE_CALENDAR", "android.permission.READ_CONTACTS", "android.permission.WRITE_CONTACTS", "android.permission.GET_ACCOUNTS")
if (UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) {
callback(false, "预期当前不具备 读写日历/联系人的权限")
return;
}
UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _grantedList : string[]) {
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, "用户拒绝了部分权限")
}
}, function (_doNotAskAgain : boolean, _grantedList : string[]) {
callback(false, "用户拒绝了部分权限")
})
}
/**
* 任务分发测试
*/
export function dispatchAsyncTest(callback : (ret : boolean, desc : string) => void) {
UTSAndroid.dispatchAsync("main",function(res){
if(Thread.currentThread().name != 'main'){
callback(false,"main thread error")
return
}
UTSAndroid.dispatchAsync("dom",function(res){
/**
* dom 参数,只在2.0生效,1.0会自动切换到main线程
*/
if(Thread.currentThread().name != 'main' && Thread.currentThread().name != 'io_dcloud_uniapp_dom'){
callback(false,"dom thread error")
return
}
UTSAndroid.dispatchAsync("io",function(res){
/**
* dom 参数,只在2.0生效,1.0会自动切换到main线程
*/
if(!Thread.currentThread().name.startWith("DefaultDispatcher")){
callback(false,"io thread error")
return
}
callback(true)
},null)
},null)
},null)
}
/**
* 路径转换测试
*/
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;
}
if (!resourcePath.contains("Android/data/io.dcloud")) {
console.log("路径转换结果错误")
return false;
}
let sdcardPath = UTSAndroid.convert2AbsFullPath(resourcePath)
console.log(sdcardPath)
if (resourcePath != sdcardPath) {
console.log("绝对路径转换结果错误")
return false;
}
return true
}
/**
* 单个权限,申请流程测试
*/
export function singlePermissionFlowTest(callback : (ret : boolean, desc : string) => void) {
let permissionNeed = utsArrayOf("android.permission.RECORD_AUDIO")
if (UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) {
callback(false, "预期当前不具备 录音的权限")
return;
}
UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _grantedList : string[]) {
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, "用户拒绝了部分权限")
}
}, function (_doNotAskAgain : boolean, _grantedList : string[]) {
callback(false, "用户拒绝了部分权限")
})
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册