diff options
author | Jack Yu <jackyu@google.com> | 2016-10-11 11:50:37 -0700 |
---|---|---|
committer | Jack Yu <jackyu@google.com> | 2016-11-03 15:22:41 -0700 |
commit | 93da96c47c1781c60c367d3b4fc5f5784fc9e194 (patch) | |
tree | f7fca60e5137619eb57e83a34cb843e24c3c202c /tests | |
parent | c3512210fddb10f1324fc653c8a576e218798d0a (diff) | |
download | android_packages_apps_CellBroadcastReceiver-93da96c47c1781c60c367d3b4fc5f5784fc9e194.tar.gz android_packages_apps_CellBroadcastReceiver-93da96c47c1781c60c367d3b4fc5f5784fc9e194.tar.bz2 android_packages_apps_CellBroadcastReceiver-93da96c47c1781c60c367d3b4fc5f5784fc9e194.zip |
Added CMAS/ETWS duplicate expiration support
Now message duplication detection will only look back messages
for 24 hours by default for all countries, except 1 hour in
Japan.
Test: runtest -x packages/apps/CellBroadcastReceiver/tests/unit
bug: 31171149
Change-Id: I90260118bc5e7eef87bc5bb63583fcf83f210f8a
Diffstat (limited to 'tests')
3 files changed, 173 insertions, 18 deletions
diff --git a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialogTest.java b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialogTest.java index e178863e..309371c9 100644 --- a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialogTest.java +++ b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialogTest.java @@ -63,7 +63,7 @@ public class CellBroadcastAlertDialogTest extends protected Intent createActivityIntent() { ArrayList<CellBroadcastMessage> messageList = new ArrayList<>(1); messageList.add(new CellBroadcastMessage( - CellBroadcastAlertServiceTest.createMessage())); + CellBroadcastAlertServiceTest.createMessage(12412))); Intent intent = new Intent(getInstrumentation().getTargetContext(), CellBroadcastAlertDialog.class); @@ -99,7 +99,7 @@ public class CellBroadcastAlertDialogTest extends assertEquals(etremeAlertString, ((TextView) getActivity().findViewById(R.id.alertTitle)).getText()); - assertEquals(CellBroadcastAlertServiceTest.createMessage().getMessageBody(), + assertEquals(CellBroadcastAlertServiceTest.createMessage(34596).getMessageBody(), (String) ((TextView) getActivity().findViewById(R.id.message)).getText()); stopActivity(); @@ -117,7 +117,7 @@ public class CellBroadcastAlertDialogTest extends assertEquals(1, (int) mInt.getValue()); assertEquals(getActivity().getTitle(), b.getCharSequence(Notification.EXTRA_TITLE)); - assertEquals(CellBroadcastAlertServiceTest.createMessage().getMessageBody(), + assertEquals(CellBroadcastAlertServiceTest.createMessage(98235).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 4eaf7c1c..c49eb271 100644 --- a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertServiceTest.java +++ b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastAlertServiceTest.java @@ -17,11 +17,15 @@ package com.android.cellbroadcastreceiver; import android.content.Intent; +import android.os.PersistableBundle; import android.provider.Telephony; +import android.telephony.CarrierConfigManager; import android.telephony.CellBroadcastMessage; import android.telephony.SmsCbCmasInfo; +import android.telephony.SmsCbEtwsInfo; import android.telephony.SmsCbLocation; import android.telephony.SmsCbMessage; +import android.util.Log; import com.android.internal.telephony.gsm.SmsCbConstants; @@ -32,6 +36,9 @@ import java.util.ArrayList; import static com.android.cellbroadcastreceiver.CellBroadcastAlertAudio.ALERT_AUDIO_TONE_TYPE; import static com.android.cellbroadcastreceiver.CellBroadcastAlertService.SHOW_NEW_ALERT_ACTION; +import static org.junit.Assert.assertArrayEquals; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.doReturn; public class CellBroadcastAlertServiceTest extends CellBroadcastServiceTestCase<CellBroadcastAlertService> { @@ -40,8 +47,8 @@ public class CellBroadcastAlertServiceTest extends super(CellBroadcastAlertService.class); } - static SmsCbMessage createMessage() { - return new SmsCbMessage(1, 2, 3, new SmsCbLocation(), + static SmsCbMessage createMessage(int serialNumber) { + return new SmsCbMessage(1, 2, serialNumber, new SmsCbLocation(), SmsCbConstants.MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL, "language", "body", SmsCbMessage.MESSAGE_PRIORITY_EMERGENCY, null, new SmsCbCmasInfo(1, 2, 3, 4, 5, 6)); } @@ -56,11 +63,30 @@ public class CellBroadcastAlertServiceTest extends super.tearDown(); } + private static void compareEtwsWarningInfo(SmsCbEtwsInfo info1, SmsCbEtwsInfo info2) { + if (info1 == info2) return; + assertEquals(info1.toString(), info2.toString()); + assertArrayEquals(info1.getPrimaryNotificationSignature(), + info2.getPrimaryNotificationSignature()); + assertEquals(info1.isPrimary(), info2.isPrimary()); + } + + private static void compareCmasWarningInfo(SmsCbCmasInfo info1, SmsCbCmasInfo info2) { + if (info1 == info2) return; + assertEquals(info1.getCategory(), info2.getCategory()); + assertEquals(info1.getCertainty(), info2.getCertainty()); + assertEquals(info1.getMessageClass(), info2.getMessageClass()); + assertEquals(info1.getResponseType(), info2.getResponseType()); + assertEquals(info1.getSeverity(), info2.getSeverity()); + assertEquals(info1.getUrgency(), info2.getUrgency()); + } + private static void compareCellBroadCastMessage(CellBroadcastMessage cbm1, CellBroadcastMessage cbm2) { + if (cbm1 == cbm2) return; assertEquals(cbm1.getCmasMessageClass(), cbm2.getCmasMessageClass()); - assertEquals(cbm1.getCmasWarningInfo(), cbm2.getCmasWarningInfo()); - assertEquals(cbm1.getEtwsWarningInfo(), cbm2.getEtwsWarningInfo()); + compareCmasWarningInfo(cbm1.getCmasWarningInfo(), cbm2.getCmasWarningInfo()); + compareEtwsWarningInfo(cbm1.getEtwsWarningInfo(), cbm2.getEtwsWarningInfo()); assertEquals(cbm1.getLanguageCode(), cbm2.getLanguageCode()); assertEquals(cbm1.getMessageBody(), cbm2.getMessageBody()); assertEquals(cbm1.getSerialNumber(), cbm2.getSerialNumber()); @@ -69,37 +95,39 @@ public class CellBroadcastAlertServiceTest extends assertEquals(cbm1.getSerialNumber(), cbm2.getSerialNumber()); } - // Test handleCellBroadcastIntent method - public void testHandleCellBroadcastIntent() throws Exception { + private void sendMessage(int serialNumber) { Intent intent = new Intent(mContext, CellBroadcastAlertService.class); intent.setAction(Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION); - SmsCbMessage m = createMessage(); - intent.putExtra("message", m); + SmsCbMessage m = createMessage(serialNumber); + sendMessage(m, intent); + } + private void sendMessage(SmsCbMessage m, Intent intent) { + intent.putExtra("message", m); startService(intent); + } + + // Test handleCellBroadcastIntent method + public void testHandleCellBroadcastIntent() throws Exception { + sendMessage(987654321); waitForMs(200); assertEquals(SHOW_NEW_ALERT_ACTION, mServiceIntentToVerify.getAction()); - assertEquals(CellBroadcastAlertService.class.getName(), - intent.getComponent().getClassName()); CellBroadcastMessage cbmTest = (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message"); - CellBroadcastMessage cbm = new CellBroadcastMessage(m); + CellBroadcastMessage cbm = new CellBroadcastMessage(createMessage(987654321)); compareCellBroadCastMessage(cbm, cbmTest); } // Test showNewAlert method public void testShowNewAlert() throws Exception { - Intent intent = new Intent(mContext, CellBroadcastAlertService.class); intent.setAction(SHOW_NEW_ALERT_ACTION); - - SmsCbMessage message = createMessage(); + SmsCbMessage message = createMessage(34788612); intent.putExtra("message", new CellBroadcastMessage(message)); - startService(intent); waitForMs(200); @@ -120,4 +148,107 @@ public class CellBroadcastAlertServiceTest extends (mActivityIntentToVerify.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK)); compareCellBroadCastMessage(new CellBroadcastMessage(message), newMessageList.get(0)); } + + // Test if we ignore the duplicate message + public void testDuplicateMessage() throws Exception { + sendMessage(4321); + waitForMs(200); + + assertEquals(SHOW_NEW_ALERT_ACTION, mServiceIntentToVerify.getAction()); + + CellBroadcastMessage cbmTest = + (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message"); + CellBroadcastMessage cbm = new CellBroadcastMessage(createMessage(4321)); + + compareCellBroadCastMessage(cbm, cbmTest); + + mServiceIntentToVerify = null; + + Intent intent = new Intent(mContext, CellBroadcastAlertService.class); + intent.setAction(Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION); + + SmsCbMessage m = createMessage(4321); + intent.putExtra("message", m); + + startService(intent); + waitForMs(200); + + // If the duplicate detection is working, the service should not pop-up the dialog and + // play the alert tones. + assertNull(mServiceIntentToVerify); + } + + // Test if we allow non-duplicate message + public void testNonDuplicateMessage() throws Exception { + sendMessage(187286123); + + mServiceIntentToVerify = null; + + Intent intent = new Intent(mContext, CellBroadcastAlertService.class); + intent.setAction(Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION); + + SmsCbMessage m = createMessage(129487394); + intent.putExtra("message", m); + + startService(intent); + waitForMs(200); + + assertEquals(SHOW_NEW_ALERT_ACTION, mServiceIntentToVerify.getAction()); + assertEquals(CellBroadcastAlertService.class.getName(), + intent.getComponent().getClassName()); + + CellBroadcastMessage cbmTest = + (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message"); + CellBroadcastMessage cbm = new CellBroadcastMessage(m); + + compareCellBroadCastMessage(cbm, cbmTest); + } + + // Test when we reach the maximum messages, the oldest one should be evicted. + public void testMaximumMessages() throws Exception { + for (int i = 0; i < 1024 + 1; i++) { + sendMessage(i); + waitForMs(50); + } + + sendMessage(0); + waitForMs(40); + // Check if the oldest one has been already evicted. + CellBroadcastMessage cbmTest = + (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message"); + CellBroadcastMessage cbm = new CellBroadcastMessage(createMessage(0)); + + compareCellBroadCastMessage(cbm, cbmTest); + mActivityIntentToVerify = null; + } + + public void testExpiration() throws Exception { + PersistableBundle b = new PersistableBundle(); + b.putLong(CarrierConfigManager.KEY_MESSAGE_EXPIRATION_TIME_LONG, 1000); + doReturn(b).when(mMockedCarrierConfigManager).getConfigForSubId(anyInt()); + + sendMessage(91924); + waitForMs(100); + + CellBroadcastMessage cbmTest = + (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message"); + assertEquals(91924, cbmTest.getSerialNumber()); + mServiceIntentToVerify = null; + + // Wait until it expires. + waitForMs(1500); + sendMessage(91924); + waitForMs(100); + + // Since the previous one has already expired, this one should not be treated as a duplicate + cbmTest = (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message"); + assertEquals(91924, cbmTest.getSerialNumber()); + + waitForMs(500); + mServiceIntentToVerify = null; + // This one should be treated as a duplicate since it's not expired yet. + sendMessage(91924); + assertNull(mServiceIntentToVerify); + + } }
\ No newline at end of file diff --git a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastServiceTestCase.java b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastServiceTestCase.java index 233728fa..b5b95ba4 100644 --- a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastServiceTestCase.java +++ b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastServiceTestCase.java @@ -21,13 +21,19 @@ import android.content.ComponentName; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; +import android.telephony.CarrierConfigManager; import android.test.ServiceTestCase; +import android.util.Log; import org.junit.Before; +import org.mockito.Mock; import org.mockito.MockitoAnnotations; public abstract class CellBroadcastServiceTestCase<T extends Service> extends ServiceTestCase<T> { + @Mock + protected CarrierConfigManager mMockedCarrierConfigManager; + Intent mServiceIntentToVerify; Intent mActivityIntentToVerify; @@ -44,6 +50,9 @@ public abstract class CellBroadcastServiceTestCase<T extends Service> extends Se } private class TestContextWrapper extends ContextWrapper { + + private final String TAG = TestContextWrapper.class.getSimpleName(); + public TestContextWrapper(Context base) { super(base); } @@ -58,6 +67,21 @@ public abstract class CellBroadcastServiceTestCase<T extends Service> extends Se public void startActivity(Intent intent) { mActivityIntentToVerify = intent; } + + @Override + public Context getApplicationContext() { + return this; + } + + @Override + public Object getSystemService(String name) { + if (name.equals(Context.CARRIER_CONFIG_SERVICE)) { + Log.d(TAG, "return mocked svc for " + name + ", " + mMockedCarrierConfigManager); + return mMockedCarrierConfigManager; + } + Log.d(TAG, "return real service " + name); + return super.getSystemService(name); + } } @Before |