diff options
author | Hui Wang <huiwang@google.com> | 2021-11-17 16:00:28 +0000 |
---|---|---|
committer | Hui Wang <huiwang@google.com> | 2021-11-18 00:01:12 +0000 |
commit | d4a0d9007ec906cf727283e37040aa88e753a153 (patch) | |
tree | 39c5685d34ecd3908ced4d9ddd9fae6b1e5aa88d | |
parent | 27aad754d53f2f58b2558dcff03ebd26c40d1661 (diff) | |
download | CellBroadcastReceiver-d4a0d9007ec906cf727283e37040aa88e753a153.tar.gz |
Fix the flaky test for CellBroadcastSettings
Add a method to clear the cache of resources, and update the methos
to thread safe. As the methods use some static fields to cache the
resources, the behavior may be unexpected if any data is cached. So
the cache should be cleared before the test.
Bug: 206791909
Test: atest CellBroadcastReceiverUnitTests
Change-Id: Ia48a4b4935b1f2bda177e53ebc6fb3fb9d8f3094
-rw-r--r-- | src/com/android/cellbroadcastreceiver/CellBroadcastSettings.java | 72 | ||||
-rw-r--r-- | tests/unit/src/com/android/cellbroadcastreceiver/unit/CellBroadcastSettingsTest.java | 1 |
2 files changed, 46 insertions, 27 deletions
diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastSettings.java b/src/com/android/cellbroadcastreceiver/CellBroadcastSettings.java index a7b71a6c9..e7422c66b 100644 --- a/src/com/android/cellbroadcastreceiver/CellBroadcastSettings.java +++ b/src/com/android/cellbroadcastreceiver/CellBroadcastSettings.java @@ -47,6 +47,7 @@ import androidx.preference.PreferenceManager; import androidx.preference.PreferenceScreen; import androidx.preference.TwoStatePreference; +import com.android.internal.annotations.VisibleForTesting; import com.android.modules.utils.build.SdkLevel; import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity; import com.android.settingslib.widget.MainSwitchPreference; @@ -164,6 +165,7 @@ public class CellBroadcastSettings extends CollapsingToolbarBaseActivity { // Resource cache per operator private static final Map<String, Resources> sResourcesCacheByOperator = new HashMap<>(); + private static final Object sCacheLock = new Object(); // Intent sent from cellbroadcastreceiver to notify cellbroadcastservice that area info update // is disabled/enabled. @@ -883,14 +885,17 @@ public class CellBroadcastSettings extends CollapsingToolbarBaseActivity { return context.getResources(); } - if (sResourcesCache.containsKey(subId)) { - return sResourcesCache.get(subId); - } + synchronized (sCacheLock) { + if (sResourcesCache.containsKey(subId)) { + return sResourcesCache.get(subId); + } + + Resources res = SubscriptionManager.getResourcesForSubId(context, subId); - Resources res = SubscriptionManager.getResourcesForSubId(context, subId); - sResourcesCache.put(subId, res); + sResourcesCache.put(subId, res); - return res; + return res; + } } /** @@ -915,30 +920,32 @@ public class CellBroadcastSettings extends CollapsingToolbarBaseActivity { return getResources(context, subId); } - Resources res = sResourcesCacheByOperator.get(operator); - if (res != null) { - return res; - } + synchronized (sCacheLock) { + Resources res = sResourcesCacheByOperator.get(operator); + if (res != null) { + return res; + } - Configuration overrideConfig = new Configuration(); - try { - int mcc = Integer.parseInt(operator.substring(0, 3)); - int mnc = operator.length() > 3 ? Integer.parseInt(operator.substring(3)) - : Configuration.MNC_ZERO; - - overrideConfig.mcc = mcc; - overrideConfig.mnc = mnc; - } catch (NumberFormatException e) { - // should not happen - Log.e(TAG, "invalid operator: " + operator); - return context.getResources(); - } + Configuration overrideConfig = new Configuration(); + try { + int mcc = Integer.parseInt(operator.substring(0, 3)); + int mnc = operator.length() > 3 ? Integer.parseInt(operator.substring(3)) + : Configuration.MNC_ZERO; - Context newContext = context.createConfigurationContext(overrideConfig); - res = newContext.getResources(); + overrideConfig.mcc = mcc; + overrideConfig.mnc = mnc; + } catch (NumberFormatException e) { + // should not happen + Log.e(TAG, "invalid operator: " + operator); + return context.getResources(); + } + + Context newContext = context.createConfigurationContext(overrideConfig); + res = newContext.getResources(); - sResourcesCacheByOperator.put(operator, res); - return res; + sResourcesCacheByOperator.put(operator, res); + return res; + } } /** @@ -981,4 +988,15 @@ public class CellBroadcastSettings extends CollapsingToolbarBaseActivity { return 0; } } + + /** + * Reset the resources cache. + */ + @VisibleForTesting + public static void resetResourcesCache() { + synchronized (sCacheLock) { + sResourcesCacheByOperator.clear(); + sResourcesCache.clear(); + } + } } diff --git a/tests/unit/src/com/android/cellbroadcastreceiver/unit/CellBroadcastSettingsTest.java b/tests/unit/src/com/android/cellbroadcastreceiver/unit/CellBroadcastSettingsTest.java index f893f8c17..106f54218 100644 --- a/tests/unit/src/com/android/cellbroadcastreceiver/unit/CellBroadcastSettingsTest.java +++ b/tests/unit/src/com/android/cellbroadcastreceiver/unit/CellBroadcastSettingsTest.java @@ -72,6 +72,7 @@ public class CellBroadcastSettingsTest extends mInstrumentation = InstrumentationRegistry.getInstrumentation(); mContext = mInstrumentation.getTargetContext(); mDevice = UiDevice.getInstance(mInstrumentation); + CellBroadcastSettings.resetResourcesCache(); } @InstrumentationTest |