diff options
author | Jack Yu <jackyu@google.com> | 2016-10-11 18:34:15 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2016-10-11 18:34:15 +0000 |
commit | 502c5f7c36e125fd4ae1c61a49f5a122691cd5e4 (patch) | |
tree | f530ed14bd5657477074d4558d9bf341308333da /tests | |
parent | 3ad4fac7b8292e2fde23a34b012cc84e497bba7c (diff) | |
parent | 16d5dabfea19e879eafa109877717c8d9e554626 (diff) | |
download | android_packages_apps_CellBroadcastReceiver-502c5f7c36e125fd4ae1c61a49f5a122691cd5e4.tar.gz android_packages_apps_CellBroadcastReceiver-502c5f7c36e125fd4ae1c61a49f5a122691cd5e4.tar.bz2 android_packages_apps_CellBroadcastReceiver-502c5f7c36e125fd4ae1c61a49f5a122691cd5e4.zip |
Added few more CellBroadcastReceiver unit tests. am: 16cb3c7bdd am: 9f57ef95e3
am: 16d5dabfea
Change-Id: If61b0792859aba3f96c76112b681c1a111df5502
Diffstat (limited to 'tests')
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)); |