[CTS]android.telephony.cts.PhoneNumberUtilsTest– testGetMethods
[CTS]com.android.cts.devicepolicy.ManagedProfileTest– testManagedContacts
这个两个CTS错误都是因为系统开启了contacts同步导致的,我们就具体分析第一个问题,Android6.0 QCOM
cts-tf > run cts -c android.telephony.cts.PhoneNumberUtilsTest -m testGetMethods --skip-preconditions
android.telephony.cts.PhoneNumberUtilsTest#testGetMethods FAIL
junit.framework.AssertionFailedError: Expected: but was: +16175551212 //通过这行打印我们可以知道,预期结果是传入null但是我们传入了一串数字
at junit.framework.Assert.fail(Assert.java:50)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertNull(Assert.java:237)
at junit.framework.Assert.assertNull(Assert.java:230)
at android.telephony.cts.PhoneNumberUtilsTest.testGetMethods(PhoneNumberUtilsTest.java:138)
at java.lang.reflect.Method.invoke(Native Method)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at android.support.test.internal.runner.junit3.AndroidTestResult.runProtected(AndroidTestResult.java:77)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:55)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at android.support.test.internal.runner.junit3.AndroidTestSuite$1.run(AndroidTestSuite.java:98)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
通过log信息我们查看PhoneNumberUtilsTest.java:138行代码如下:
public void testGetMethods() throws RemoteException {
// Test getStrippedReversed
assertNull(PhoneNumberUtils.getStrippedReversed(null));
assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141"));
assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141,1234"));
assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141;1234"));
assertEquals("NN145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-41NN"));
assertEquals("", PhoneNumberUtils.getStrippedReversed(""));
assertEquals("#130#*+", PhoneNumberUtils.getStrippedReversed("+*#031#"));
// Test getFormatTypeForLocale
int formatType = PhoneNumberUtils.getFormatTypeForLocale(Locale.CHINA);
assertEquals(PhoneNumberUtils.FORMAT_UNKNOWN, formatType);
formatType = PhoneNumberUtils.getFormatTypeForLocale(Locale.US);
assertEquals(PhoneNumberUtils.FORMAT_NANP, formatType);
formatType = PhoneNumberUtils.getFormatTypeForLocale(Locale.JAPAN);
assertEquals(PhoneNumberUtils.FORMAT_JAPAN, formatType);
// Test getNumberFromIntent, query nothing, return null.
Intent intent = new Intent();
intent.setData(Contacts.People.CONTENT_URI);//CONTENT_URI = Uri.parse("content://contacts/people")
Context context = getContext();
assertNull(PhoneNumberUtils.getNumberFromIntent(intent, context));//错误发生在这一行
intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:+18005555555"));
assertEquals("+18005555555", PhoneNumberUtils.getNumberFromIntent(intent, getContext()));
ContentResolver cr = getContext().getContentResolver();
Uri personRecord = null;
Uri phoneRecord = null;
try {
// insert a contact with phone number
ContentValues values = new ContentValues();
values.put(People.NAME, "CTS test contact");
personRecord = cr.insert(People.CONTENT_URI, values);
Uri phoneUri = Uri.withAppendedPath(personRecord, People.Phones.CONTENT_DIRECTORY);
values.clear();
values.put(People.Phones.TYPE, People.Phones.TYPE_HOME);
values.put(People.Phones.NUMBER, "+18005552871");
phoneRecord = cr.insert(phoneUri, values);
intent = new Intent(Intent.ACTION_DIAL, phoneRecord);
assertEquals("+18005552871",
PhoneNumberUtils.getNumberFromIntent(intent, getContext()));
} finally {
if (personRecord != null) {
cr.delete(personRecord, null, null);
}
if (phoneRecord != null) {
cr.delete(phoneRecord, null, null);
}
}
}
通过查看 assertNull方法我们可以知道,预期是想传入一个null对象,但是实际上跑CTS的时候我们传入了非null对象,所以PhoneNumberUtils.getNumberFromIntent(intent, context)为非null对象,通过下面的代码我们可以发现
/**
* The content:// style URL for this table
* @deprecated see {@link android.provider.ContactsContract}
*/
@Deprecated
public static final Uri CONTENT_URI =
Uri.parse("content://contacts/people");
"ContactsProvider2" //联系人数据库
android:authorities="contacts;com.android.contacts"
android:label="@string/provider_label"
android:multiprocess="false"
android:exported="true"
android:grantUriPermissions="true"
android:readPermission="android.permission.READ_CONTACTS"
android:writePermission="android.permission.WRITE_CONTACTS">
PhoneNumberUtils.getNumberFromIntent(intent, context) 的数据来自于系统联系人数据库,并且最终会调用ContactsProvider2.queryLocal方法查询数据,这个就有点儿奇怪了,跑CTS的时候contacts应用里面应该是没有数据才对,通过查看手机中的联系人我们可以发现,里面确实保存了一个联系人并且号码为:+16175551212,并且它的账户类型为SIM,这下真相大白了,这个联系人信息是从哪SIM导入到数据库的,因为当SIM卡插入手机时,会将里面的联系人数据保存在联系人本地数据库。
我们只需要将自动同步功能关闭就可以解决这个问题,修改如下:
一、android/vendor/qcom/proprietary/qrdplus/Extension/config/default.prop
//将true改为false
//persist.env.contacts.autosync=true
persist.env.contacts.autosync=false
二、android/vendor/qcom/proprietary/qrdplus/Extension/res-overlay/vendor/qcom/proprietary/telephony-apps/SimContacts/res/values/config.xml
//将true改为false
<resources>
//<bool name="sim_contacts_auto_sync">truebool>
<bool name="sim_contacts_auto_sync">falsebool>
resources>
第二个问题的错误信息如下:与contacts是自动同步时成的phone账号有关。
04-21 15:52:47 I/bc17b500: com.android.cts.devicepolicy.ManagedProfileTest#testManagedContacts FAIL
junit.framework.AssertionFailedError
at junit.framework.Assert.fail(Assert.java:48)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertTrue(Assert.java:27)
at com.android.cts.devicepolicy.ManagedProfileTest.testManagedContacts(ManagedProfileTest.java:444)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at com.android.tradefed.testtype.DeviceTestResult$1.protect(DeviceTestResult.java:81)
at com.android.tradefed.testtype.DeviceTestResult.runProtected(DeviceTestResult.java:56)
at com.android.tradefed.testtype.DeviceTestResult.run(DeviceTestResult.java:85)
at junit.framework.TestCase.run(TestCase.java:124)
at com.android.tradefed.testtype.DeviceTestCase.run(DeviceTestCase.java:117)
at com.android.cts.tradefed.testtype.JarHostTest$TestRunnable.run(JarHostTest.java:248)
at com.android.tradefed.util.RunUtil$RunnableNotifier.run(RunUtil.java:378)
04-21 15:51:53 I/BaseDevicePolicyTest: Test com.android.cts.managedprofile.ContactsTest#testManagedProfilePhoneLookup_canNotAccessPrimaryContact: PASSED
04-21 15:51:54 I/BaseDevicePolicyTest: Test com.android.cts.managedprofile.ContactsTest#testManagedProfileEmailLookup_canNotAccessPrimaryContact: PASSED
04-21 15:51:56 I/BaseDevicePolicyTest: Test com.android.cts.managedprofile.ContactsTest#testManagedProfileEnterprisePhoneLookup_canAccessEnterpriseContact: FAILURE
04-21 15:51:56 W/BaseDevicePolicyTest: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.android.cts.managedprofile.ContactsTest$ContactInfo.displayName' on a null object reference
at com.android.cts.managedprofile.ContactsTest.testManagedProfileEnterprisePhoneLookup_canAccessEnterpriseContact(ContactsTest.java:311)
at java.lang.reflect.Method.invoke(Native Method)