代码如下,下面代码 6.0 以上系统抛出权限错误,需要动态权限申请,此时应该怎么在这个单元测试中申请权限?
package com.reizx.ares.rbl;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Instrumentation;
import android.support.test.rule.ActivityTestRule;
import android.support.test.rule.GrantPermissionRule;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.ShellUtils;
import com.reizx.ares.rbl.util.RblLogUtil;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import io.reactivex.functions.Consumer;
/**
* 一个单元测试的模版用例,用于直接复制改名生成单元测试
*/
@RunWith(AndroidJUnit4.class)
public class PermissionTest extends BaseTest{
//无效
// @Rule
// public ActivityTestRule<RblActivity> mActivityTestRule = new ActivityTestRule<>(RblActivity.class);
// @Rule
// public GrantPermissionRule readPermissionRule = GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE);
// @Rule
// public GrantPermissionRule writePermissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
// @Rule
// public GrantPermissionRule statusPermissionRule = GrantPermissionRule.grant(Manifest.permission.READ_PHONE_STATE);
@Test
public void templateTest(){
//todo 在下面写测试用例
try {
Log.d("PermissionTest", "package name : " + app.getPackageName());
String cmds = "pm grant " + app.getPackageName() + " android.permission.READ_PHONE_STATE";
ShellUtils.execCmd(cmds, false);//无效
GrantPermissionRule.grant("android.permission.READ_PHONE_STATE");// 无效
String deviceId = PhoneUtils.getDeviceId(); //此处抛出没有权限ID
Log.d("PermissionTest", "get deviceId : " + deviceId);
}catch (Exception e){
Log.d("PermissionTest", "get deviceId faield, error : " + e);
}
}