diff options
author | Jack Yu <jackyu@google.com> | 2016-10-06 21:44:23 -0700 |
---|---|---|
committer | Jack Yu <jackyu@google.com> | 2016-10-07 14:25:39 -0700 |
commit | 16cb3c7bdd773c237bb42e841c8766f16a7a4972 (patch) | |
tree | 9feca0492853348b71d389c999adc3080b4ae794 | |
parent | 85f8978e580a2172c2a1f26d42fcd09269bc581d (diff) | |
download | android_packages_apps_CellBroadcastReceiver-16cb3c7bdd773c237bb42e841c8766f16a7a4972.tar.gz android_packages_apps_CellBroadcastReceiver-16cb3c7bdd773c237bb42e841c8766f16a7a4972.tar.bz2 android_packages_apps_CellBroadcastReceiver-16cb3c7bdd773c237bb42e841c8766f16a7a4972.zip |
Added few more CellBroadcastReceiver unit tests.
Test: runtest -x packages/apps/CellBroadcastReceiver/tests/unit
bug: 30367782
Change-Id: I15898aded17219cf5ca50edacb8a5fe527302234
3 files changed, 235 insertions, 1 deletions
diff --git a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastActivityTestCase.java b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastActivityTestCase.java new file mode 100644 index 00000000..5d7cef45 --- /dev/null +++ b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastActivityTestCase.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2016 Google Inc. + * + * 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.cellbroadcastreceiver; + +import android.app.Activity; +import android.content.Context; +import android.content.ContextWrapper; +import android.content.Intent; +import android.os.Bundle; +import android.test.ActivityUnitTestCase; +import android.util.Log; + +import java.util.HashMap; + +public class CellBroadcastActivityTestCase<T extends Activity> extends ActivityUnitTestCase<T> { + + protected TestContext mContext; + + private T mActivity; + + CellBroadcastActivityTestCase(Class<T> activityClass) { + super(activityClass); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + mContext = new TestContext(getInstrumentation().getTargetContext()); + setActivityContext(mContext); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + protected T startActivity() throws Throwable { + runTestOnUiThread(new Runnable() { + @Override + public void run() { + mActivity = startActivity(createActivityIntent(), null, null); + } + }); + return mActivity; + } + + protected void stopActivity() throws Exception { + getInstrumentation().callActivityOnStop(mActivity); + } + + public static void waitForMs(long ms) { + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + } + } + + protected Intent createActivityIntent() { + Intent intent = new Intent(); + return intent; + } + + protected <S> void injectSystemService(Class<S> cls, S service) { + mContext.injectSystemService(cls, service); + } + + public static class TestContext extends ContextWrapper { + + private static final String TAG = TestContext.class.getSimpleName(); + + private HashMap<String, Object> mInjectedSystemServices = new HashMap<>(); + + public TestContext(Context base) { + super(base); + } + + public <S> void injectSystemService(Class<S> cls, S service) { + final String name = getSystemServiceName(cls); + mInjectedSystemServices.put(name, service); + } + + @Override + public Context getApplicationContext() { + return this; + } + + @Override + public Object getSystemService(String name) { + if (mInjectedSystemServices.containsKey(name)) { + Log.d(TAG, "return mocked system service for " + name); + return mInjectedSystemServices.get(name); + } + Log.d(TAG, "return real system service for " + name); + return super.getSystemService(name); + } + } +}
\ No newline at end of file diff --git a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialogTest.java b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialogTest.java new file mode 100644 index 00000000..e178863e --- /dev/null +++ b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialogTest.java @@ -0,0 +1,123 @@ +/* + * 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.cellbroadcastreceiver; + +import android.app.Notification; +import android.app.NotificationManager; +import android.content.Intent; +import android.os.Bundle; +import android.os.IPowerManager; +import android.os.PowerManager; +import android.telephony.CellBroadcastMessage; +import android.widget.TextView; + +import org.junit.After; +import org.junit.Before; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +public class CellBroadcastAlertDialogTest extends + CellBroadcastActivityTestCase<CellBroadcastAlertDialog> { + + @Mock + private NotificationManager mMockedNotificationManager; + + @Mock + private IPowerManager.Stub mMockedPowerManagerService; + + @Captor + private ArgumentCaptor<Integer> mInt; + + @Captor + private ArgumentCaptor<Notification> mNotification; + + private PowerManager mPowerManager; + + public CellBroadcastAlertDialogTest() { + super(CellBroadcastAlertDialog.class); + } + + @Override + protected Intent createActivityIntent() { + ArrayList<CellBroadcastMessage> messageList = new ArrayList<>(1); + messageList.add(new CellBroadcastMessage( + CellBroadcastAlertServiceTest.createMessage())); + + Intent intent = new Intent(getInstrumentation().getTargetContext(), + CellBroadcastAlertDialog.class); + intent.putParcelableArrayListExtra(CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA, + messageList); + return intent; + } + + @Before + public void setUp() throws Exception { + super.setUp(); + MockitoAnnotations.initMocks(this); + injectSystemService(NotificationManager.class, mMockedNotificationManager); + // PowerManager is a final class so we can't use Mockito to mock it, but we can mock + // its underlying service. + doReturn(true).when(mMockedPowerManagerService).isInteractive(); + mPowerManager = new PowerManager(mContext, mMockedPowerManagerService, null); + injectSystemService(PowerManager.class, mPowerManager); + } + + @After + public void tearDown() throws Exception { + super.tearDown(); + } + + public void testTitleAndMessageText() throws Throwable { + startActivity(); + waitForMs(100); + + CharSequence etremeAlertString = + getActivity().getResources().getText(R.string.cmas_extreme_alert); + assertEquals(etremeAlertString, getActivity().getTitle()); + assertEquals(etremeAlertString, + ((TextView) getActivity().findViewById(R.id.alertTitle)).getText()); + + assertEquals(CellBroadcastAlertServiceTest.createMessage().getMessageBody(), + (String) ((TextView) getActivity().findViewById(R.id.message)).getText()); + + stopActivity(); + } + + public void testAddToNotification() throws Throwable { + startActivity(); + waitForMs(100); + stopActivity(); + waitForMs(100); + verify(mMockedNotificationManager, times(1)).notify(mInt.capture(), + mNotification.capture()); + Bundle b = mNotification.getValue().extras; + + assertEquals(1, (int) mInt.getValue()); + + assertEquals(getActivity().getTitle(), b.getCharSequence(Notification.EXTRA_TITLE)); + assertEquals(CellBroadcastAlertServiceTest.createMessage().getMessageBody(), + b.getCharSequence(Notification.EXTRA_TEXT)); + } +}
\ No newline at end of file diff --git a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertServiceTest.java b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertServiceTest.java index 3783495e..4eaf7c1c 100644 --- a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertServiceTest.java +++ b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertServiceTest.java @@ -40,7 +40,7 @@ public class CellBroadcastAlertServiceTest extends super(CellBroadcastAlertService.class); } - private SmsCbMessage createMessage() { + static SmsCbMessage createMessage() { return new SmsCbMessage(1, 2, 3, new SmsCbLocation(), SmsCbConstants.MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL, "language", "body", SmsCbMessage.MESSAGE_PRIORITY_EMERGENCY, null, new SmsCbCmasInfo(1, 2, 3, 4, 5, 6)); |