【Android 11 GtsAssistantHostTestCases 失败】

项目场景:

要求相机只使用Camera2

测试原理:

通过命令启动相机,拍照,查询图片数量

@GmsTest(requirement = "GMS-7.5-002")
    @Test
    public void testAssistantOpenRearCameraWithoutVoiceInteraction() throws DeviceNotAvailableException, InterruptedException {
        Assume.assumeTrue("Device does not support rear camera.",
                FeatureUtil.hasSystemFeature(getDevice(), "android.hardware.camera"));
        StringBuilder cmdBuilder = new StringBuilder();
        cmdBuilder
                .append(String.format("am start -a %s ", new Object[]{"android.media.action.STILL_IMAGE_CAMERA"})).append(String.format("--ez %s %s ", new Object[]{"com.google.assistant.extra.CAMERA_OPEN_ONLY", "true"})).append(String.format("--ez %s %s ", new Object[]{"android.intent.extra.CAMERA_OPEN_ONLY", "true"})).append(String.format("--ez %s %s ", new Object[]{"isVoiceQuery", "true"})).append(String.format("--ez %s %s ", new Object[]{"NoUiQuery", "true"})).append(String.format("--es %s %s ", new Object[]{"android.intent.extra.REFERRER_NAME", "android-app://com.google.android.googlequicksearchbox/https/www.google.com"})).append("--activity-clear-task");
        getDevice().executeShellCommand(cmdBuilder.toString());
        sleep();
        MetricsReportLog reportLog = new MetricsReportLog(getBuild(), getAbi().toString(), String.format("%s#testAssistantOpenRearCameraWithoutVoiceInteraction", new Object[]{getClass().getCanonicalName()}), "GtsAssistantTestCases", "report_for_assistant_test");
        reportLog.addValue("test_name", "testAssistantOpenRearCameraWithoutVoiceInteraction", ResultType.NEUTRAL, ResultUnit.NONE);
        reportLog.addValue("status", (
                getActiveCameraFace().equals("back") &&
                        getCameraDirectoryJpegAmount() == this.mImgCountBeforeTesting), ResultType.NEUTRAL, ResultUnit.NONE);
        reportLog.submit();
        if (ApiLevelUtil.isAfter(getDevice(), 28)) {
            Assert.assertTrue(getActiveCameraFace().equals("back"));
            Assert.assertTrue((getCameraDirectoryJpegAmount() == this.mImgCountBeforeTesting));
        }
    }

private int getCameraDirectoryJpegAmount() throws DeviceNotAvailableException {
    try {
      List<String> results = Arrays.asList(getDevice().executeShellCommand("find $EXTERNAL_STORAGE/ -size +100k | grep -i '\\.jpg' | wc -l")
          
          .trim().split("\n"));
      return Integer.parseInt(results.get(results.size() - 1));
    } catch (NumberFormatException e) {
      return 0;
    } 
  }

解决方案:

  1. 修改AndroidManifest
<category android:name="android.intent.category.VOICE" />
  1. 修改CaptureModule.java
//在init()调用
private void initModeByIntent() {
        String action = mActivity.getIntent().getAction();
        Log.d(TAG, "initModeByIntent: " + action);
        Bundle bundle = mActivity.getIntent().getExtras();
        if (bundle != null) {
            Log.v(TAG, "initModeByIntent bundle :" + bundle.toString());
        }
        if (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(action)) {
            mIntentMode = INTENT_MODE_STILL_IMAGE_CAMERA;
            Set<String> categories = mActivity.getIntent().getCategories();
            if (categories != null) {
                for(String categorie: categories) {
                    if(categorie.equals("android.intent.category.VOICE")) {
                        mIsVoiceTakePhoto = true;
                    }
                }
            }
            boolean isOpenOnly = mActivity.getIntent().getBooleanExtra(
                    "com.google.assistant.extra.CAMERA_OPEN_ONLY", false);
            Log.v(TAG, "initModeByIntent isOpenOnly = " + isOpenOnly);
            if (isOpenOnly) {
                mIsVoiceTakePhoto = false;
            }
        }
    }

    private class MainHandler extends Handler {
        public MainHandler() {
            super(Looper.getMainLooper());
        }
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case VOICE_INTERACTION_CAPTURE: {
                    takePictureNow();
                    mIsVoiceTakePhoto = false;
                    break;
                }
            }
        }
    }

在OpenCallback的onReadyForCapture()添加拍照处理:

if (mIntentMode == INTENT_MODE_STILL_IMAGE_CAMERA && mIsVoiceTakePhoto) { 
	mHandler.sendEmptyMessageDelayed(VOICE_INTERACTION_CAPTURE, 500);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值