summaryrefslogtreecommitdiff
path: root/src/com/android
diff options
context:
space:
mode:
authorNazanin <nazaninb@google.com>2020-05-04 17:27:40 -0700
committerNazanin Bakhshi <nazaninb@google.com>2020-05-13 17:35:00 +0000
commitde520ee212941b193f1ac5075b8fdb5a02f1044b (patch)
treeb97699b501aac6bc32c7d2973eef20b84b13e464 /src/com/android
parent4ca8eb9385906815e702dfaf426b0d33694a5d3a (diff)
downloadCellBroadcastReceiver-de520ee212941b193f1ac5075b8fdb5a02f1044b.tar.gz
Add tests for CellBroadcastSearchIndexableProviderTest
and fix CellBroadcastReceiverTests Bug: 156124649 Test: atest Change-Id: I72808823f65ecfa873ced069f7e1db239b637b90
Diffstat (limited to 'src/com/android')
-rw-r--r--src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java70
-rw-r--r--src/com/android/cellbroadcastreceiver/CellBroadcastSearchIndexableProvider.java61
2 files changed, 106 insertions, 25 deletions
diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java b/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java
index e52af12b7..fd0198ab8 100644
--- a/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java
+++ b/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java
@@ -85,24 +85,34 @@ public class CellBroadcastReceiver extends BroadcastReceiver {
});
}
+ /**
+ * this method is to make this class unit-testable, because CellBroadcastSettings.getResources()
+ * is a static method and cannot be stubbed.
+ * @return resources
+ */
+ @VisibleForTesting
+ public Resources getResourcesMethod() {
+ return CellBroadcastSettings.getResources(mContext,
+ SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+ }
+
@Override
public void onReceive(Context context, Intent intent) {
if (DBG) log("onReceive " + intent);
mContext = context.getApplicationContext();
String action = intent.getAction();
- Resources res = CellBroadcastSettings.getResources(mContext,
- SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+ Resources res = getResourcesMethod();
if (ACTION_MARK_AS_READ.equals(action)) {
final long deliveryTime = intent.getLongExtra(EXTRA_DELIVERY_TIME, -1);
getCellBroadcastTask(deliveryTime);
} else if (CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(action)) {
initializeSharedPreference();
- startConfigService(mContext);
+ startConfigService();
} else if (CELLBROADCAST_START_CONFIG_ACTION.equals(action)
|| SubscriptionManager.ACTION_DEFAULT_SMS_SUBSCRIPTION_CHANGED.equals(action)) {
- startConfigService(mContext);
+ startConfigService();
} else if (Telephony.Sms.Intents.ACTION_SMS_EMERGENCY_CB_RECEIVED.equals(action) ||
Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION.equals(action)) {
intent.setClass(mContext, CellBroadcastAlertService.class);
@@ -141,7 +151,8 @@ public class CellBroadcastReceiver extends BroadcastReceiver {
*
* @param on {@code true} if testing mode is on, otherwise off.
*/
- private void setTestingMode(boolean on) {
+ @VisibleForTesting
+ public void setTestingMode(boolean on) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
sp.edit().putBoolean(TESTING_MODE, on).commit();
}
@@ -182,19 +193,35 @@ public class CellBroadcastReceiver extends BroadcastReceiver {
if (DBG) Log.d(TAG, "Default interval " + currentIntervalDefault + " did not change.");
}
}
+ /**
+ * This method's purpose if to enable unit testing
+ * @return sharedePreferences for mContext
+ */
+ @VisibleForTesting
+ public SharedPreferences getDefaultSharedPreferences() {
+ return PreferenceManager.getDefaultSharedPreferences(mContext);
+ }
/**
+ * return if there are default values in shared preferences
+ * @return boolean
+ */
+ @VisibleForTesting
+ public Boolean sharedPrefsHaveDefaultValues() {
+ return mContext.getSharedPreferences(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES,
+ Context.MODE_PRIVATE).getBoolean(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES,
+ false);
+ }
+ /**
* initialize shared preferences before starting services
*/
@VisibleForTesting
public void initializeSharedPreference() {
- if (isSystemUser(mContext)) {
+ if (isSystemUser()) {
Log.d(TAG, "initializeSharedPreference");
- SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
+ SharedPreferences sp = getDefaultSharedPreferences();
- if (!mContext.getSharedPreferences(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES,
- Context.MODE_PRIVATE).getBoolean(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES,
- false)) {
+ if (!sharedPrefsHaveDefaultValues()) {
// Sets the default values of the shared preference if there isn't any.
PreferenceManager.setDefaultValues(mContext, R.xml.preferences, false);
@@ -336,13 +363,30 @@ public class CellBroadcastReceiver extends BroadcastReceiver {
}
/**
+ * This method's purpose if to enable unit testing
+ * @return if the mContext user is a system user
+ */
+ @VisibleForTesting
+ public boolean isSystemUser() {
+ return isSystemUser(mContext);
+ }
+
+ /**
+ * This method's purpose if to enable unit testing
+ */
+ @VisibleForTesting
+ public void startConfigService() {
+ startConfigService(mContext);
+ }
+
+ /**
* Check if user from context is system user
* @param context
* @return whether the user is system user
*/
- @VisibleForTesting
- public static boolean isSystemUser(Context context) {
- return ((UserManager) context.getSystemService(Context.USER_SERVICE)).isSystemUser();
+ private static boolean isSystemUser(Context context) {
+ UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
+ return userManager.isSystemUser();
}
/**
diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastSearchIndexableProvider.java b/src/com/android/cellbroadcastreceiver/CellBroadcastSearchIndexableProvider.java
index d9c0b0287..3d56f7b05 100644
--- a/src/com/android/cellbroadcastreceiver/CellBroadcastSearchIndexableProvider.java
+++ b/src/com/android/cellbroadcastreceiver/CellBroadcastSearchIndexableProvider.java
@@ -35,6 +35,8 @@ import static android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS;
import static android.provider.SearchIndexablesContract.INDEXABLES_XML_RES_COLUMNS;
import static android.provider.SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS;
+import android.annotation.Nullable;
+import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
@@ -44,13 +46,16 @@ import android.provider.SearchIndexablesProvider;
import android.telephony.SubscriptionManager;
import android.text.TextUtils;
+import com.android.internal.annotations.VisibleForTesting;
+
import java.util.ArrayList;
import java.util.List;
public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvider {
+ @VisibleForTesting
// Additional keywords for settings search
- private static final int[] INDEXABLE_KEYWORDS_RESOURCES = {
+ public static final int[] INDEXABLE_KEYWORDS_RESOURCES = {
R.string.etws_earthquake_warning,
R.string.etws_tsunami_warning,
R.string.cmas_presidential_level_alert,
@@ -58,11 +63,44 @@ public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvid
R.string.emergency_alerts_title
};
- private static final SearchIndexableResource[] INDEXABLE_RES = new SearchIndexableResource[] {
+ @VisibleForTesting
+ public static final SearchIndexableResource[] INDEXABLE_RES = new SearchIndexableResource[] {
new SearchIndexableResource(1, R.xml.preferences,
CellBroadcastSettings.class.getName(),
R.mipmap.ic_launcher_cell_broadcast),
};
+
+ /**
+ * this method is to make this class unit-testable, because super.getContext() is a final
+ * method and therefore not mockable
+ * @return context
+ */
+ @VisibleForTesting
+ public @Nullable Context getContextMethod() {
+ return super.getContext();
+ }
+
+ /**
+ * this method is to make this class unit-testable, because CellBroadcastSettings.getResources()
+ * is a static method and cannot be stubbed.
+ * @return resources
+ */
+ @VisibleForTesting
+ public Resources getResourcesMethod() {
+ return CellBroadcastSettings.getResources(getContextMethod(),
+ SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+ }
+
+ /**
+ * this method is to make this class unit-testable, because
+ * CellBroadcastSettings.isTestAlertsToggleVisible is a static method and therefore not mockable
+ * @return true if test alerts toggle is Visible
+ */
+ @VisibleForTesting
+ public boolean isTestAlertsToggleVisible() {
+ return CellBroadcastSettings.isTestAlertsToggleVisible(getContextMethod());
+ }
+
@Override
public boolean onCreate() {
return true;
@@ -79,7 +117,7 @@ public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvid
ref[COLUMN_INDEX_XML_RES_CLASS_NAME] = null;
ref[COLUMN_INDEX_XML_RES_ICON_RESID] = INDEXABLE_RES[n].iconResId;
ref[COLUMN_INDEX_XML_RES_INTENT_ACTION] = Intent.ACTION_MAIN;
- ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE] = getContext().getPackageName();
+ ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE] = getContextMethod().getPackageName();
ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_CLASS] = INDEXABLE_RES[n].className;
cursor.addRow(ref);
}
@@ -89,8 +127,7 @@ public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvid
@Override
public Cursor queryRawData(String[] projection) {
MatrixCursor cursor = new MatrixCursor(INDEXABLES_RAW_COLUMNS);
- final Resources res = CellBroadcastSettings.getResources(getContext(),
- SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+ final Resources res = getResourcesMethod();
Object[] raw = new Object[INDEXABLES_RAW_COLUMNS.length];
raw[COLUMN_INDEX_RAW_TITLE] = res.getString(R.string.sms_cb_settings);
@@ -99,7 +136,8 @@ public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvid
keywordList.add(res.getString(keywordRes));
}
- CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(getContext(),
+ CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(
+ getContextMethod(),
SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
if (!channelManager.getCellBroadcastChannelRanges(
@@ -117,7 +155,7 @@ public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvid
raw[COLUMN_INDEX_RAW_SCREEN_TITLE] = res.getString(R.string.sms_cb_settings);
raw[COLUMN_INDEX_RAW_KEY] = CellBroadcastSettings.class.getSimpleName();
raw[COLUMN_INDEX_RAW_INTENT_ACTION] = Intent.ACTION_MAIN;
- raw[COLUMN_INDEX_RAW_INTENT_TARGET_PACKAGE] = getContext().getPackageName();
+ raw[COLUMN_INDEX_RAW_INTENT_TARGET_PACKAGE] = getContextMethod().getPackageName();
raw[COLUMN_INDEX_RAW_INTENT_TARGET_CLASS] = CellBroadcastSettings.class.getName();
cursor.addRow(raw);
@@ -128,8 +166,7 @@ public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvid
public Cursor queryNonIndexableKeys(String[] projection) {
MatrixCursor cursor = new MatrixCursor(NON_INDEXABLES_KEYS_COLUMNS);
- Resources res = CellBroadcastSettings.getResources(getContext(),
- SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+ Resources res = getResourcesMethod();
Object[] ref;
if (!res.getBoolean(R.bool.show_presidential_alerts_settings)) {
@@ -169,8 +206,8 @@ public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvid
cursor.addRow(ref);
}
- CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(getContext(),
- SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+ CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(
+ getContextMethod(), SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
if (channelManager.getCellBroadcastChannelRanges(
R.array.cmas_amber_alerts_channels_range_strings).isEmpty()) {
ref = new Object[1];
@@ -203,7 +240,7 @@ public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvid
cursor.addRow(ref);
}
- if (!CellBroadcastSettings.isTestAlertsToggleVisible(getContext())) {
+ if (!isTestAlertsToggleVisible()) {
ref = new Object[1];
ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] =
CellBroadcastSettings.KEY_ENABLE_TEST_ALERTS;