From fda56b600e27b081db9719d22c809018304dedef Mon Sep 17 00:00:00 2001 From: Xia Wang Date: Thu, 29 Apr 2010 19:12:59 -0700 Subject: Fix test in PhoneNumberUtilsUnitTest.java, combine it with PhoneNumberUtilsTest.java Rename CallerInfoUnitTest.java to CallerInfoTest.java and move to the corresponding directory bug: 2644009 Change-Id: I1ecc09312f404e00bbf30befffb1811efe578cc3 --- .../android/telephony/PhoneNumberUtilsTest.java | 21 +- .../android/internal/telephony/CallerInfoTest.java | 241 +++++++++++++++++++++ .../telephonytest/unit/CallerInfoUnitTest.java | 240 -------------------- .../unit/PhoneNumberUtilsUnitTest.java | 63 ------ 4 files changed, 261 insertions(+), 304 deletions(-) create mode 100644 telephony/tests/telephonytests/src/com/android/internal/telephony/CallerInfoTest.java delete mode 100644 telephony/tests/telephonytests/src/com/android/telephonytest/unit/CallerInfoUnitTest.java delete mode 100644 telephony/tests/telephonytests/src/com/android/telephonytest/unit/PhoneNumberUtilsUnitTest.java diff --git a/telephony/tests/telephonytests/src/android/telephony/PhoneNumberUtilsTest.java b/telephony/tests/telephonytests/src/android/telephony/PhoneNumberUtilsTest.java index 02590d3..b456a03 100644 --- a/telephony/tests/telephonytests/src/android/telephony/PhoneNumberUtilsTest.java +++ b/telephony/tests/telephonytests/src/android/telephony/PhoneNumberUtilsTest.java @@ -16,13 +16,16 @@ package com.android.internal.telephony; +import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; import android.text.SpannableStringBuilder; import android.telephony.PhoneNumberUtils; +import android.telephony.TelephonyManager; +import android.content.Context; import junit.framework.TestCase; -public class PhoneNumberUtilsTest extends TestCase { +public class PhoneNumberUtilsTest extends AndroidTestCase { @SmallTest public void testExtractNetworkPortion() throws Exception { @@ -472,4 +475,20 @@ public class PhoneNumberUtilsTest extends TestCase { PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000", PhoneNumberUtils.FORMAT_UNKNOWN,PhoneNumberUtils.FORMAT_UNKNOWN)); } + + /** + * Basic checks for the VoiceMail number. + */ + @SmallTest + public void testWithNumberNotEqualToVoiceMail() throws Exception { + assertFalse(PhoneNumberUtils.isVoiceMailNumber("911")); + assertFalse(PhoneNumberUtils.isVoiceMailNumber("tel:911")); + assertFalse(PhoneNumberUtils.isVoiceMailNumber("+18001234567")); + assertFalse(PhoneNumberUtils.isVoiceMailNumber("")); + assertFalse(PhoneNumberUtils.isVoiceMailNumber(null)); + TelephonyManager mTelephonyManager = + (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE); + String mVoiceMailNumber = mTelephonyManager.getDefault().getVoiceMailNumber(); + assertTrue(PhoneNumberUtils.isVoiceMailNumber(mVoiceMailNumber)); + } } diff --git a/telephony/tests/telephonytests/src/com/android/internal/telephony/CallerInfoTest.java b/telephony/tests/telephonytests/src/com/android/internal/telephony/CallerInfoTest.java new file mode 100644 index 0000000..1e5dafb --- /dev/null +++ b/telephony/tests/telephonytests/src/com/android/internal/telephony/CallerInfoTest.java @@ -0,0 +1,241 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.internal.telephony; + +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +import android.content.ContentResolver; +import android.content.Context; +import android.content.res.Resources; +import com.android.internal.telephony.CallerInfo; +import com.android.internal.telephony.CallerInfoAsyncQuery; +import android.util.Log; +import android.os.Looper; +import android.test.ActivityInstrumentationTestCase; +import android.util.StringBuilderPrinter; + +/* + * Check the CallerInfo utility class works as expected. + * + */ + +public class CallerInfoTest extends AndroidTestCase { + private CallerInfo mInfo; + private Context mContext; + + private static final String kEmergencyNumber = "Emergency Number"; + private static final int kToken = 0xdeadbeef; + private static final String TAG = "CallerInfoUnitTest"; + + @Override + protected void setUp() throws Exception { + super.setUp(); + mContext = new MockContext(); + mInfo = new CallerInfo(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + /** + * Checks the caller info instance is flagged as an emergency if + * the number is an emergency one. There is no test for the + * contact based constructors because emergency number are not in + * the contact DB. + */ + @SmallTest + public void testEmergencyIsProperlySet() throws Exception { + assertFalse(mInfo.isEmergencyNumber()); + + mInfo = CallerInfo.getCallerInfo(mContext, "911"); + assertIsValidEmergencyCallerInfo(); + + mInfo = CallerInfo.getCallerInfo(mContext, "tel:911"); + assertIsValidEmergencyCallerInfo(); + + + // This one hits the content resolver. + mInfo = CallerInfo.getCallerInfo(mContext, "18001234567"); + assertFalse(mInfo.isEmergencyNumber()); + } + + /** + * Same as testEmergencyIsProperlySet but uses the async query api. + */ + @SmallTest + public void testEmergencyIsProperlySetUsingAsyncQuery() throws Exception { + QueryRunner query; + + query = new QueryRunner("911"); + query.runAndCheckCompletion(); + assertIsValidEmergencyCallerInfo(); + + query = new QueryRunner("tel:911"); + query.runAndCheckCompletion(); + assertIsValidEmergencyCallerInfo(); + + query = new QueryRunner("18001234567"); + query.runAndCheckCompletion(); + assertFalse(mInfo.isEmergencyNumber()); + } + + /** + * For emergency caller info, phoneNumber should be set to the + * string emergency_call_dialog_number_for_display and the + * photoResource should be set to the picture_emergency drawable. + */ + @SmallTest + public void testEmergencyNumberAndPhotoAreSet() throws Exception { + mInfo = CallerInfo.getCallerInfo(mContext, "911"); + + assertIsValidEmergencyCallerInfo(); + } + + // TODO: Add more tests: + /** + * Check if the voice mail number cannot be retrieved that the + * original phone number is preserved. + */ + /** + * Check the markAs* methods work. + */ + + + // + // Helpers + // + + // Partial implementation of MockResources. + public class MockResources extends android.test.mock.MockResources + { + @Override + public String getString(int resId) throws Resources.NotFoundException { + switch (resId) { + case com.android.internal.R.string.emergency_call_dialog_number_for_display: + return kEmergencyNumber; + default: + throw new UnsupportedOperationException("Missing handling for resid " + resId); + } + } + } + + // Partial implementation of MockContext. + public class MockContext extends android.test.mock.MockContext { + private ContentResolver mResolver; + private Resources mResources; + + public MockContext() { + mResolver = new android.test.mock.MockContentResolver(); + mResources = new MockResources(); + } + + @Override + public ContentResolver getContentResolver() { + return mResolver; + } + + @Override + public Resources getResources() { + return mResources; + } + } + + /** + * Class to run a CallerInfoAsyncQuery in a separate thread, with + * its own Looper. We cannot use the main Looper because on the + * 1st quit the thread is maked dead, ie no further test can use + * it. Also there is not way to inject a Looper instance in the + * query, so we have to use a thread with its own looper. + */ + private class QueryRunner extends Thread + implements CallerInfoAsyncQuery.OnQueryCompleteListener { + private Looper mLooper; + private String mNumber; + private boolean mAsyncCompleted; + + public QueryRunner(String number) { + super(); + mNumber = number; + } + + // Run the query in the thread, wait for completion. + public void runAndCheckCompletion() throws InterruptedException { + start(); + join(); + assertTrue(mAsyncCompleted); + } + + @Override + public void run() { + Looper.prepare(); + mLooper = Looper.myLooper(); + mAsyncCompleted = false; + // The query will pick the thread local looper we've just prepared. + CallerInfoAsyncQuery.startQuery(kToken, mContext, mNumber, this, null); + mLooper.loop(); + } + + // Quit the Looper on the 1st callback + // (EVENT_EMERGENCY_NUMBER). There is another message + // (EVENT_END_OF_QUEUE) that will never be delivered because + // the test has exited. The corresponding stack trace + // "Handler{xxxxx} sending message to a Handler on a dead + // thread" can be ignored. + public void onQueryComplete(int token, Object cookie, CallerInfo info) { + mAsyncCompleted = true; + mInfo = info; + mLooper.quit(); + } + } + + /** + * Fail if mInfo does not contain a valid emergency CallerInfo instance. + */ + private void assertIsValidEmergencyCallerInfo() throws Exception { + assertTrue(mInfo.isEmergencyNumber()); + + // For emergency caller info, phoneNumber should be set to the + // string emergency_call_dialog_number_for_display and the + // photoResource should be set to the picture_emergency drawable. + assertEquals(kEmergencyNumber, mInfo.phoneNumber); + assertEquals(com.android.internal.R.drawable.picture_emergency, mInfo.photoResource); + + // The name should be null + assertNull(mInfo.name); + assertEquals(0, mInfo.namePresentation); + assertNull(mInfo.cnapName); + assertEquals(0, mInfo.numberPresentation); + + assertFalse(mInfo.contactExists); + assertEquals(0, mInfo.person_id); + assertFalse(mInfo.needUpdate); + assertNull(mInfo.contactRefUri); + + assertNull(mInfo.phoneLabel); + assertEquals(0, mInfo.numberType); + assertNull(mInfo.numberLabel); + + assertNull(mInfo.contactRingtoneUri); + assertFalse(mInfo.shouldSendToVoicemail); + + assertNull(mInfo.cachedPhoto); + assertFalse(mInfo.isCachedPhotoCurrent); + } +} diff --git a/telephony/tests/telephonytests/src/com/android/telephonytest/unit/CallerInfoUnitTest.java b/telephony/tests/telephonytests/src/com/android/telephonytest/unit/CallerInfoUnitTest.java deleted file mode 100644 index 0f24f15..0000000 --- a/telephony/tests/telephonytests/src/com/android/telephonytest/unit/CallerInfoUnitTest.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.telephonytest.unit; -import android.test.AndroidTestCase; -import android.test.suitebuilder.annotation.SmallTest; - -import android.content.ContentResolver; -import android.content.Context; -import android.content.res.Resources; -import com.android.internal.telephony.CallerInfo; -import com.android.internal.telephony.CallerInfoAsyncQuery; -import android.util.Log; -import android.os.Looper; -import android.test.ActivityInstrumentationTestCase; -import android.util.StringBuilderPrinter; - -/* - * Check the CallerInfo utility class works as expected. - * - */ - -public class CallerInfoUnitTest extends AndroidTestCase { - private CallerInfo mInfo; - private Context mContext; - - private static final String kEmergencyNumber = "Emergency Number"; - private static final int kToken = 0xdeadbeef; - private static final String TAG = "CallerInfoUnitTest"; - - @Override - protected void setUp() throws Exception { - super.setUp(); - mContext = new MockContext(); - mInfo = new CallerInfo(); - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } - - /** - * Checks the caller info instance is flagged as an emergency if - * the number is an emergency one. There is no test for the - * contact based constructors because emergency number are not in - * the contact DB. - */ - @SmallTest - public void testEmergencyIsProperlySet() throws Exception { - assertFalse(mInfo.isEmergencyNumber()); - - mInfo = CallerInfo.getCallerInfo(mContext, "911"); - assertIsValidEmergencyCallerInfo(); - - mInfo = CallerInfo.getCallerInfo(mContext, "tel:911"); - assertIsValidEmergencyCallerInfo(); - - - // This one hits the content resolver. - mInfo = CallerInfo.getCallerInfo(mContext, "18001234567"); - assertFalse(mInfo.isEmergencyNumber()); - } - - /** - * Same as testEmergencyIsProperlySet but uses the async query api. - */ - @SmallTest - public void testEmergencyIsProperlySetUsingAsyncQuery() throws Exception { - QueryRunner query; - - query = new QueryRunner("911"); - query.runAndCheckCompletion(); - assertIsValidEmergencyCallerInfo(); - - query = new QueryRunner("tel:911"); - query.runAndCheckCompletion(); - assertIsValidEmergencyCallerInfo(); - - query = new QueryRunner("18001234567"); - query.runAndCheckCompletion(); - assertFalse(mInfo.isEmergencyNumber()); - } - - /** - * For emergency caller info, phoneNumber should be set to the - * string emergency_call_dialog_number_for_display and the - * photoResource should be set to the picture_emergency drawable. - */ - @SmallTest - public void testEmergencyNumberAndPhotoAreSet() throws Exception { - mInfo = CallerInfo.getCallerInfo(mContext, "911"); - - assertIsValidEmergencyCallerInfo(); - } - - // TODO: Add more tests: - /** - * Check if the voice mail number cannot be retrieved that the - * original phone number is preserved. - */ - /** - * Check the markAs* methods work. - */ - - - // - // Helpers - // - - // Partial implementation of MockResources. - public class MockResources extends android.test.mock.MockResources - { - @Override - public String getString(int resId) throws Resources.NotFoundException { - switch (resId) { - case com.android.internal.R.string.emergency_call_dialog_number_for_display: - return kEmergencyNumber; - default: - throw new UnsupportedOperationException("Missing handling for resid " + resId); - } - } - } - - // Partial implementation of MockContext. - public class MockContext extends android.test.mock.MockContext { - private ContentResolver mResolver; - private Resources mResources; - - public MockContext() { - mResolver = new android.test.mock.MockContentResolver(); - mResources = new MockResources(); - } - - @Override - public ContentResolver getContentResolver() { - return mResolver; - } - - @Override - public Resources getResources() { - return mResources; - } - } - - /** - * Class to run a CallerInfoAsyncQuery in a separate thread, with - * its own Looper. We cannot use the main Looper because on the - * 1st quit the thread is maked dead, ie no further test can use - * it. Also there is not way to inject a Looper instance in the - * query, so we have to use a thread with its own looper. - */ - private class QueryRunner extends Thread - implements CallerInfoAsyncQuery.OnQueryCompleteListener { - private Looper mLooper; - private String mNumber; - private boolean mAsyncCompleted; - - public QueryRunner(String number) { - super(); - mNumber = number; - } - - // Run the query in the thread, wait for completion. - public void runAndCheckCompletion() throws InterruptedException { - start(); - join(); - assertTrue(mAsyncCompleted); - } - - @Override - public void run() { - Looper.prepare(); - mLooper = Looper.myLooper(); - mAsyncCompleted = false; - // The query will pick the thread local looper we've just prepared. - CallerInfoAsyncQuery.startQuery(kToken, mContext, mNumber, this, null); - mLooper.loop(); - } - - // Quit the Looper on the 1st callback - // (EVENT_EMERGENCY_NUMBER). There is another message - // (EVENT_END_OF_QUEUE) that will never be delivered because - // the test has exited. The corresponding stack trace - // "Handler{xxxxx} sending message to a Handler on a dead - // thread" can be ignored. - public void onQueryComplete(int token, Object cookie, CallerInfo info) { - mAsyncCompleted = true; - mInfo = info; - mLooper.quit(); - } - } - - /** - * Fail if mInfo does not contain a valid emergency CallerInfo instance. - */ - private void assertIsValidEmergencyCallerInfo() throws Exception { - assertTrue(mInfo.isEmergencyNumber()); - - // For emergency caller info, phoneNumber should be set to the - // string emergency_call_dialog_number_for_display and the - // photoResource should be set to the picture_emergency drawable. - assertEquals(kEmergencyNumber, mInfo.phoneNumber); - assertEquals(com.android.internal.R.drawable.picture_emergency, mInfo.photoResource); - - // The name should be null - assertNull(mInfo.name); - assertEquals(0, mInfo.namePresentation); - assertNull(mInfo.cnapName); - assertEquals(0, mInfo.numberPresentation); - - assertFalse(mInfo.contactExists); - assertEquals(0, mInfo.person_id); - assertFalse(mInfo.needUpdate); - assertNull(mInfo.contactRefUri); - - assertNull(mInfo.phoneLabel); - assertEquals(0, mInfo.numberType); - assertNull(mInfo.numberLabel); - - assertNull(mInfo.contactRingtoneUri); - assertFalse(mInfo.shouldSendToVoicemail); - - assertNull(mInfo.cachedPhoto); - assertFalse(mInfo.isCachedPhotoCurrent); - } -} diff --git a/telephony/tests/telephonytests/src/com/android/telephonytest/unit/PhoneNumberUtilsUnitTest.java b/telephony/tests/telephonytests/src/com/android/telephonytest/unit/PhoneNumberUtilsUnitTest.java deleted file mode 100644 index 2d3c548..0000000 --- a/telephony/tests/telephonytests/src/com/android/telephonytest/unit/PhoneNumberUtilsUnitTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.telephonytest.unit; -import android.test.AndroidTestCase; -import android.test.suitebuilder.annotation.SmallTest; -import android.util.Log; - -import android.telephony.PhoneNumberUtils; -import android.telephony.TelephonyManager; - -/* - * Check the PhoneNumberUtils utility class works as expected. - * - */ - -public class PhoneNumberUtilsUnitTest extends AndroidTestCase { - private String mVoiceMailNumber; - private static final String TAG = "PhoneNumberUtilsUnitTest"; - - @Override - protected void setUp() throws Exception { - super.setUp(); - // FIXME: Why are we getting a security exception here? The - // permission is declared in the manifest.... - // mVoiceMailNumber = TelephonyManager.getDefault().getVoiceMailNumber(); - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } - - /** - * Basic checks for the VoiceMail number. - * Assumes READ_PHONE_STATE permission and we don't have it. - */ - // TODO: Figure out why we don't have the permission declared in the manifest. - @SmallTest - public void testWithNumberNotEqualToVoiceMail() throws Exception { - assertFalse(PhoneNumberUtils.isVoiceMailNumber("911")); - assertFalse(PhoneNumberUtils.isVoiceMailNumber("tel:911")); - assertFalse(PhoneNumberUtils.isVoiceMailNumber("+18001234567")); - assertFalse(PhoneNumberUtils.isVoiceMailNumber("")); - assertFalse(PhoneNumberUtils.isVoiceMailNumber(null)); - // FIXME: - // assertTrue(PhoneNumberUtils.isVoiceMailNumber(mVoiceMailNumber)); - } - -} -- cgit v1.1