summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBrad Ebinger <breadley@google.com>2016-05-05 10:40:57 -0700
committerBrad Ebinger <breadley@google.com>2016-06-22 13:31:22 -0700
commit12ec7f2f001560d935cb3399d9e65bf078ee4760 (patch)
treeec47c28c2a3fe254b8c1cb052a06632ffdffc0a2 /tests
parent54cda7fcb58dbd778514cbce9909ba7bee68f8e8 (diff)
downloadandroid_packages_services_Telephony-12ec7f2f001560d935cb3399d9e65bf078ee4760.tar.gz
android_packages_services_Telephony-12ec7f2f001560d935cb3399d9e65bf078ee4760.tar.bz2
android_packages_services_Telephony-12ec7f2f001560d935cb3399d9e65bf078ee4760.zip
Refactor EmergencyCallHelper to better handle DSDS
Refactor EmergencyCallHelper to better handle DSDS radios by disabling airplane mode and waiting for each Phone's ServiceState to change to allow for Emergency Calling. Also, some tests are added using AndroidJUnit4Runner This is the first part in a series of changes to the Telephony code. Bug: 28200728 Change-Id: I642615480090f0b047cea00ab9f3256f321dfaa1
Diffstat (limited to 'tests')
-rw-r--r--tests/Android.mk2
-rw-r--r--tests/AndroidManifest.xml15
-rw-r--r--tests/src/com/android/TelephonyTestBase.java71
-rw-r--r--tests/src/com/android/phone/MockitoHelper.java9
-rw-r--r--tests/src/com/android/phone/common/mail/MailTransportTest.java2
-rw-r--r--tests/src/com/android/services/telephony/EmergencyCallStateListenerTest.java163
6 files changed, 252 insertions, 10 deletions
diff --git a/tests/Android.mk b/tests/Android.mk
index 6cc035552..e1b564f1a 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -25,6 +25,8 @@ LOCAL_CERTIFICATE := platform
LOCAL_MODULE_TAGS := tests
+LOCAL_JAVA_LIBRARIES := telephony-common
+
LOCAL_INSTRUMENTATION_FOR := TeleService
LOCAL_STATIC_JAVA_LIBRARIES := \
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 890056883..cae4c1b52 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -62,17 +62,16 @@
</application>
<!--
- The prefered way is to use 'runtest':
- runtest phone-unit
+ To run all tests:
+ adb shell am instrument -w
+ com.android.phone.tests/android.support.test.runner.AndroidJUnitRunner
- runtest is a wrapper around 'adb shell'. The low level shell command is:
- adb shell am instrument -w com.android.phone.tests/android.test.InstrumentationTestRunner
+ To run a single class test:
+ adb shell am instrument -e class com.android.phone.unit.FooUnitTest
+ -w com.android.phone.tests/android.support.test.runner.AndroidJUnitRunner
- To run a single test case:
- adb shell am instrument -w com.android.phone.tests/android.test.InstrumentationTestRunner
- -e com.android.phone.unit.FooUnitTest
-->
- <instrumentation android:name="android.test.InstrumentationTestRunner"
+ <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
android:targetPackage="com.android.phone"
android:label="Phone application tests." />
</manifest>
diff --git a/tests/src/com/android/TelephonyTestBase.java b/tests/src/com/android/TelephonyTestBase.java
new file mode 100644
index 000000000..6dee12be3
--- /dev/null
+++ b/tests/src/com/android/TelephonyTestBase.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2016 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;
+
+import android.content.Context;
+import android.os.Handler;
+import android.support.test.InstrumentationRegistry;
+
+import com.android.phone.MockitoHelper;
+
+import org.mockito.MockitoAnnotations;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Helper class to load Mockito Resources into a test.
+ */
+public class TelephonyTestBase {
+
+ protected Context mContext;
+ MockitoHelper mMockitoHelper = new MockitoHelper();
+
+ public void setUp() throws Exception {
+ mContext = InstrumentationRegistry.getTargetContext();
+ mMockitoHelper.setUp(mContext, getClass());
+ MockitoAnnotations.initMocks(this);
+ }
+
+ public void tearDown() throws Exception {
+ mMockitoHelper.tearDown();
+ }
+
+ protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
+ final CountDownLatch lock = new CountDownLatch(1);
+ h.post(lock::countDown);
+ while (lock.getCount() > 0) {
+ try {
+ lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ // do nothing
+ }
+ }
+ }
+
+ protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
+ final CountDownLatch lock = new CountDownLatch(1);
+ h.postDelayed(lock::countDown, delayMs);
+ while (lock.getCount() > 0) {
+ try {
+ lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ // do nothing
+ }
+ }
+ }
+}
diff --git a/tests/src/com/android/phone/MockitoHelper.java b/tests/src/com/android/phone/MockitoHelper.java
index 3da5d6e9c..799803095 100644
--- a/tests/src/com/android/phone/MockitoHelper.java
+++ b/tests/src/com/android/phone/MockitoHelper.java
@@ -16,6 +16,8 @@
package com.android.phone;
+import android.content.Context;
+
import com.android.services.telephony.Log;
/**
@@ -24,6 +26,7 @@ import com.android.services.telephony.Log;
public final class MockitoHelper {
private static final String TAG = "MockitoHelper";
+ private static final String DEXCACHE = "dexmaker.dexcache";
private ClassLoader mOriginalClassLoader;
private Thread mContextThread;
@@ -34,7 +37,7 @@ public final class MockitoHelper {
*
* @param packageClass test case class
*/
- public void setUp(Class<?> packageClass) throws Exception {
+ public void setUp(Context context, Class<?> packageClass) throws Exception {
// makes a copy of the context classloader
mContextThread = Thread.currentThread();
mOriginalClassLoader = mContextThread.getContextClassLoader();
@@ -42,6 +45,9 @@ public final class MockitoHelper {
Log.v(TAG, "Changing context classloader from " + mOriginalClassLoader
+ " to " + newClassLoader);
mContextThread.setContextClassLoader(newClassLoader);
+ String dexCache = context.getCacheDir().toString();
+ Log.v(this, "Setting property %s to %s", DEXCACHE, dexCache);
+ System.setProperty(DEXCACHE, dexCache);
}
/**
@@ -50,5 +56,6 @@ public final class MockitoHelper {
public void tearDown() throws Exception {
Log.v(TAG, "Restoring context classloader to " + mOriginalClassLoader);
mContextThread.setContextClassLoader(mOriginalClassLoader);
+ System.clearProperty(DEXCACHE);
}
} \ No newline at end of file
diff --git a/tests/src/com/android/phone/common/mail/MailTransportTest.java b/tests/src/com/android/phone/common/mail/MailTransportTest.java
index 6acd51778..9eaef6bee 100644
--- a/tests/src/com/android/phone/common/mail/MailTransportTest.java
+++ b/tests/src/com/android/phone/common/mail/MailTransportTest.java
@@ -61,7 +61,7 @@ public class MailTransportTest extends AndroidTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
- mMokitoHelper.setUp(getClass());
+ mMokitoHelper.setUp(getContext(), getClass());
MockitoAnnotations.initMocks(this);
}
diff --git a/tests/src/com/android/services/telephony/EmergencyCallStateListenerTest.java b/tests/src/com/android/services/telephony/EmergencyCallStateListenerTest.java
new file mode 100644
index 000000000..64cf0528a
--- /dev/null
+++ b/tests/src/com/android/services/telephony/EmergencyCallStateListenerTest.java
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2016 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.services.telephony;
+
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.telephony.ServiceState;
+import android.support.test.runner.AndroidJUnit4;
+
+import com.android.TelephonyTestBase;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneConstants;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+
+import static org.mockito.Matchers.anyBoolean;
+import static org.mockito.Matchers.isNull;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests the EmergencyCallStateListener, which listens to one Phone and waits until its service
+ * state changes to accepting emergency calls or in service. If it can not find a tower to camp onto
+ * for emergency calls, then it will fail after a timeout period.
+ */
+@RunWith(AndroidJUnit4.class)
+public class EmergencyCallStateListenerTest extends TelephonyTestBase {
+
+ private static final long TIMEOUT_MS = 100;
+
+ @Mock Phone mMockPhone;
+ @Mock EmergencyCallStateListener.Callback mCallback;
+ EmergencyCallStateListener mListener;
+
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ mListener = new EmergencyCallStateListener();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ mListener.getHandler().removeCallbacksAndMessages(null);
+ super.tearDown();
+ }
+
+ @Test
+ public void testRegisterForCallback() {
+ mListener.waitForRadioOn(mMockPhone, mCallback);
+
+ waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+
+ verify(mMockPhone).unregisterForServiceStateChanged(any(Handler.class));
+ verify(mMockPhone).registerForServiceStateChanged(any(Handler.class),
+ eq(EmergencyCallStateListener.MSG_SERVICE_STATE_CHANGED), isNull());
+ }
+
+ @Test
+ public void testPhoneChangeState_InService() {
+ ServiceState state = new ServiceState();
+ state.setState(ServiceState.STATE_IN_SERVICE);
+ when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE);
+ mListener.waitForRadioOn(mMockPhone, mCallback);
+ waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+
+ mListener.getHandler().obtainMessage(EmergencyCallStateListener.MSG_SERVICE_STATE_CHANGED,
+ new AsyncResult(null, state, null)).sendToTarget();
+
+ waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+ verify(mCallback).onComplete(eq(mListener), eq(true));
+ }
+
+ @Test
+ public void testPhoneChangeState_EmergencyCalls() {
+ ServiceState state = new ServiceState();
+ state.setState(ServiceState.STATE_OUT_OF_SERVICE);
+ state.setEmergencyOnly(true);
+ when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE);
+ when(mMockPhone.getServiceState()).thenReturn(state);
+ mListener.waitForRadioOn(mMockPhone, mCallback);
+ waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+
+ mListener.getHandler().obtainMessage(EmergencyCallStateListener.MSG_SERVICE_STATE_CHANGED,
+ new AsyncResult(null, state, null)).sendToTarget();
+
+ waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+ verify(mCallback).onComplete(eq(mListener), eq(true));
+ }
+
+ @Test
+ public void testPhoneChangeState_OutOfService() {
+ ServiceState state = new ServiceState();
+ state.setState(ServiceState.STATE_OUT_OF_SERVICE);
+ when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE);
+ when(mMockPhone.getServiceState()).thenReturn(state);
+ mListener.waitForRadioOn(mMockPhone, mCallback);
+ waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+
+ // Don't expect any answer, since it is not the one that we want and the timeout for giving
+ // up hasn't expired yet.
+ mListener.getHandler().obtainMessage(EmergencyCallStateListener.MSG_SERVICE_STATE_CHANGED,
+ new AsyncResult(null, state, null)).sendToTarget();
+
+ waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+ verify(mCallback, never()).onComplete(any(EmergencyCallStateListener.class), anyBoolean());
+ }
+
+ @Test
+ public void testTimeout_EmergencyCalls() {
+ ServiceState state = new ServiceState();
+ state.setState(ServiceState.STATE_OUT_OF_SERVICE);
+ state.setEmergencyOnly(true);
+ when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE);
+ when(mMockPhone.getServiceState()).thenReturn(state);
+ mListener.waitForRadioOn(mMockPhone, mCallback);
+ mListener.setTimeBetweenRetriesMillis(500);
+
+ // Wait for the timer to expire and check state manually in onRetryTimeout
+ waitForHandlerActionDelayed(mListener.getHandler(), TIMEOUT_MS, 600);
+
+ verify(mCallback).onComplete(eq(mListener), eq(true));
+ }
+
+ @Test
+ public void testTimeout_RetryFailure() {
+ ServiceState state = new ServiceState();
+ state.setState(ServiceState.STATE_POWER_OFF);
+ when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE);
+ when(mMockPhone.getServiceState()).thenReturn(state);
+ mListener.waitForRadioOn(mMockPhone, mCallback);
+ mListener.setTimeBetweenRetriesMillis(100);
+ mListener.setMaxNumRetries(2);
+
+ // Wait for the timer to expire and check state manually in onRetryTimeout
+ waitForHandlerActionDelayed(mListener.getHandler(), TIMEOUT_MS, 600);
+
+ verify(mCallback).onComplete(eq(mListener), eq(false));
+ verify(mMockPhone, times(2)).setRadioPower(eq(true));
+ }
+
+}