1. 程式人生 > >一則CTS測試錯誤分析

一則CTS測試錯誤分析

CTS測試感測器發現失敗,具體錯誤是PackageManager找不到,SensorManager確能找到

cts_host > start --plan CTS -t android.app.cts.SystemFeaturesTest#testSensorFeatures

start test android.app.cts.SystemFeaturesTest#testSensorFeatures

==============================================================

Test package: android.app

android.app.cts.SystemFeaturesTest#testSensorFeatures...(fail)

junit.framework.AssertionFailedError: PackageManager#hasSystemFeature(android.hardware.sensor.accelerometer) returns false but SensorManager#getSensorList(1) shows sensors [3-axis Accelerometer] expected:<false> but was:<true> at android.app.cts.SystemFeaturesTest.assertFeatureForSensor(SystemFeaturesTest.java:295)

at android.app.cts.SystemFeaturesTest.assertFeatureForSensor(SystemFeaturesTest.java:295)

at android.app.cts.SystemFeaturesTest.testSensorFeatures(SystemFeaturesTest.java:213)

at java.lang.reflect.Method.invokeNative(Native Method)

at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)

at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)

at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)

at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)

at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)

at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

at android.app.cts.SystemFeaturesTest.assertFeatureForSensor(SystemFeaturesTest.java:295)

at android.app.cts.SystemFeaturesTest.testSensorFeatures(SystemFeaturesTest.java:213)

at java.lang.reflect.Method.invokeNative(Native Method)

at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)

at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)

at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)

at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)

at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)

at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

==============================================================

Time: 5177129.236s

cts_host > 

PackageManager找不到感測器,說明錯誤出在PM身上。

檢視CTS測試程式碼,如紅色所示,當expectedFeature為FEATURE_SENSOR_ACCELEROMETER時,返回錯誤。

    private void assertFeatureForSensor(Set<String> featuresLeft, String expectedFeature,

            int expectedSensorType) {

        assertTrue("Features left " + featuresLeft + " to check did not include "

                + expectedFeature, featuresLeft.remove(expectedFeature));

        boolean hasSensorFeature = mPackageManager.hasSystemFeature(expectedFeature);

        List<Sensor> sensors = mSensorManager.getSensorList(expectedSensorType);

        List<String> sensorNames = new ArrayList<String>(sensors.size());

        for (Sensor sensor : sensors) {

            sensorNames.add(sensor.getName());

        }

        boolean hasSensorType = !sensors.isEmpty();

        String message = "PackageManager#hasSystemFeature(" + expectedFeature + ") returns "

                + hasSensorFeature

                + " but SensorManager#getSensorList(" + expectedSensorType + ") shows sensors "

                + sensorNames;

        assertEquals(message, hasSensorFeature, hasSensorType);

    }

這個字串系統中是有的,但是hasSystemFeature需要檢查許可權,需要將frameworks/base/data/etc/android.hardware.sensor.accelerometer.xml檔案新增進目標板。

frameworks/base/core/java/android/content/pm/PackageManager.java:727:    

public static final String FEATURE_SENSOR_ACCELEROMETER = "android.hardware.sensor.accelerometer";

最終在mk中新增以下內容解決問題。

frameworks/base/data/etc/android.hardware.sensor.accelerometer.xml:system/etc/permissions/android.hardware.sensor.accelerometer.xml \