如何跳过ITS测试

测试步骤

CTS Verifier --> CAMERA --> Camera ITS Test

什么是Camera ITS test?

Android Camera Image Test Suite (ITS) is part of Android Compatibility Test Suite (CTS) Verifier and includes tests that verify image content. As of CTS 7.0_r8, CTS Verifier supports ITS test automation via Camera ITS-in-a-box; support for manual tests continues to ensure coverage for all Android device form factors.

ITS就是几条关于camera的测试项,属于CTS-v里的有项。
ITS里总共包括几条测试项,这些测试项测试的难度较大,花费时间较长,过的概率低,所以一般都会要求跳过。

进入ITS的关键代码

xref/cts/apps/CtsVerifier/src/com/android/cts/verifier/camera/its/ItsTestActivity.java

 protected void onCreate(Bundle savedInstanceState) {
332          // Hide the test if all camera devices are legacy
333          CameraManager manager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
334          try {
335              String[] cameraIds = manager.getCameraIdList();
336              mToBeTestedCameraIds = new ArrayList<String>();
337              for (String id : cameraIds) {
338                  CameraCharacteristics characteristics = manager.getCameraCharacteristics(id);
339                  int hwLevel = characteristics.get(
340                          CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
341                  if (hwLevel
342                          != CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY &&
343                          hwLevel
344                          != CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL) {
345                      mToBeTestedCameraIds.add(id);
346                  }
347              }
348              if (mToBeTestedCameraIds.size() == 0) {
349                  showToast(R.string.all_legacy_devices);
350                  ItsTestActivity.this.getReportLog().setSummary(
351                          "PASS: all cameras on this device are LEGACY or EXTERNAL"
352                          , 1.0, ResultType.NEUTRAL, ResultUnit.NONE);
353                  setTestResultAndFinish(true);
354              }
355          } catch (CameraAccessException e) {
356              Toast.makeText(ItsTestActivity.this,
357                      "Received error from camera service while checking device capabilities: "
358                              + e, Toast.LENGTH_SHORT).show();
359          }
360  
361          super.onCreate(savedInstanceState);
362          getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
363      }  

看351行的描述,当设备上所有的camera是LEGACY or EXTERNAL时,PASS。
判断条件是mToBeTestedCameraIds.size() == 0,
看337~346行的代码得知,关键方法是getCameraCharacteristics()。我们需要从该方法得到不一样的返回值,即camera都是LEGACY or EXTERNAL的。

接下来看该方法的源码:
xref/frameworks/base/core/java/android/hardware/camera2/CameraManager.java

3      @NonNull
264      public CameraCharacteristics getCameraCharacteristics(@NonNull String cameraId)
265              throws CameraAccessException {
266          CameraCharacteristics characteristics = null;
267          if (CameraManagerGlobal.sCameraServiceDisabled) {
268              throw new IllegalArgumentException("No cameras available on device");
269          }
270          synchronized (mLock) {
271              /*
272               * Get the camera characteristics from the camera service directly if it supports it,
273               * otherwise get them from the legacy shim instead.
274               */
275              ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
276              if (cameraService == null) {
277                  throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED,
278                          "Camera service is currently unavailable");
279              }
280              try {
281                  if (!supportsCamera2ApiLocked(cameraId)) {
282                      // Legacy backwards compatibility path; build static info from the camera
283                      // parameters
284                      int id = Integer.parseInt(cameraId);
285  
286                      String parameters = cameraService.getLegacyParameters(id);
287  
288                      CameraInfo info = cameraService.getCameraInfo(id);
289  
290                      characteristics = LegacyMetadataMapper.createCharacteristics(parameters, info);
291                  } else {
292                      // Normal path: Get the camera characteristics directly from the camera service
293                      CameraMetadataNative info = cameraService.getCameraCharacteristics(cameraId);
294  
295                      characteristics = new CameraCharacteristics(info);
296                  }
297              } catch (ServiceSpecificException e) {
298                  throwAsPublicException(e);
299              } catch (RemoteException e) {
300                  // Camera service died - act as if the camera was disconnected
301                  throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED,
302                          "Camera service is currently unavailable", e);
303              }
304          }
305          return characteristics;
306      }

关键代码在281行的supportsCamera2ApiLocked():

     private boolean supportsCameraApiLocked(String cameraId, int apiVersion) {
780          /*
781           * Possible return values:
782           * - NO_ERROR => CameraX API is supported
783           * - CAMERA_DEPRECATED_HAL => CameraX API is *not* supported (thrown as an exception)
784           * - Remote exception => If the camera service died
785           *
786           * Anything else is an unexpected error we don't want to recover from.
787           */
788          try {
789              ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
790              // If no camera service, no support
791              if (cameraService == null) return false;
792  
793              return cameraService.supportsCameraApi(cameraId, apiVersion);
794          } catch (RemoteException e) {
795              // Camera service is now down, no support for any API level
796          }
797          return false;
798      }
748      private boolean supportsCamera2ApiLocked(String cameraId) {
749          return supportsCameraApiLocked(cameraId, API_VERSION_2);
750      }
52      /**
753       * Queries the camera service if it supports a camera api directly, or needs a shim.
754       *
755       * @param cameraId a non-{@code null} camera identifier
756       * @param apiVersion the version, i.e. {@code API_VERSION_1} or {@code API_VERSION_2}
757       * @return {@code true} if connecting will work for that device version.
758       */
759      private boolean supportsCameraApiLocked(String cameraId, int apiVersion) {
760          /*
761           * Possible return values:
762           * - NO_ERROR => CameraX API is supported
763           * - CAMERA_DEPRECATED_HAL => CameraX API is *not* supported (thrown as an exception)
764           * - Remote exception => If the camera service died
765           *
766           * Anything else is an unexpected error we don't want to recover from.
767           */
768          try {
769              ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
770              // If no camera service, no support
771              if (cameraService == null) return false;
772  
773              return cameraService.supportsCameraApi(cameraId, apiVersion);
774          } catch (RemoteException e) {
775              // Camera service is now down, no support for any API level
776          }
777          return false;
778      }

代码看到这儿就不用继续往下跟了。

跳过ITS 的代码修改

在前面这些代码中的任意一个方法中加上条件判断都行,只是看哪里更简单方便。建议加在supportsCamera2ApiLocked()中,如下所示:

      private boolean supportsCamera2ApiLocked(String cameraId) {
          String className = getTopAppClassName();
          if(className != null && className.equals("com.android.cts.verifier.camera.its.ItsTestActivity"))
              return false;
          else
              return supportsCameraApiLocked(cameraId, API_VERSION_2);
      }
     private String getTopAppClassName() {
          String className = "";
          try {
              final ActivityManager mActivityManager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
              List<ActivityManager.RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
              className = rti.get(0).topActivity.getClassName();
          }catch (Exception ignored){
          }
          return className;
      }

说明

这样改很明显就是一种规避手段。
但是这样改简单可行,测试报告与正常测过相同,且不影响其它测试。
目前很多项目都是这样做的。

ref

https://source.android.com/compatibility/cts/camera-its-box

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。