// Shared prefs keys for storing dismissed state.
// Index into current dismissed state.
- private static final String DISMISS_INDEX = "_dismiss_index";
+ @VisibleForTesting
+ static final String DISMISS_INDEX = "_dismiss_index";
private static final String SETUP_TIME = "_setup_time";
private static final String IS_DISMISSED = "_is_dismissed";
return elapsedTime > category.exclusiveExpireDaysInMillis;
}
- private boolean isDismissed(Tile suggestion, boolean isSmartSuggestionEnabled) {
+ @VisibleForTesting
+ boolean isDismissed(Tile suggestion, boolean isSmartSuggestionEnabled) {
String dismissControl = getDismissControl(suggestion, isSmartSuggestionEnabled);
if (dismissControl == null) {
return false;
return false;
}
int index = mSharedPrefs.getInt(keyBase + DISMISS_INDEX, 0);
- int currentDismiss = parseDismissString(dismissControl)[index];
+ int[] dismissRules = parseDismissString(dismissControl);
+ if (dismissRules.length <= index) {
+ return true;
+ }
+ int currentDismiss = dismissRules[index];
long time = getEndTime(mSharedPrefs.getLong(keyBase + SETUP_TIME, 0), currentDismiss);
if (System.currentTimeMillis() >= time) {
// Dismiss timeout has passed, undismiss it.
package com.android.settingslib.suggestions;
+import static com.google.common.truth.Truth.assertThat;
+
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import java.util.Arrays;
import java.util.List;
-import static com.google.common.truth.Truth.assertThat;
-
@RunWith(SettingLibRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SuggestionParserTest {
assertThat(sl.getSuggestionForCategory("category2")).hasSize(1);
}
+ @Test
+ public void isSuggestionDismissed_mismatchRule_shouldDismiss() {
+ final Tile suggestion = new Tile();
+ suggestion.metaData = new Bundle();
+ suggestion.metaData.putString(SuggestionParser.META_DATA_DISMISS_CONTROL, "1,2,3");
+ suggestion.intent = new Intent().setComponent(new ComponentName("pkg", "cls"));
+
+ // Dismiss suggestion when smart suggestion is not enabled.
+ mSuggestionParser.dismissSuggestion(suggestion, false /* isSmartSuggestionEnabled */);
+ final String suggestionKey = suggestion.intent.getComponent().flattenToShortString();
+ // And point to last rule in dismiss control
+ mPrefs.edit().putInt(suggestionKey + SuggestionParser.DISMISS_INDEX, 2).apply();
+
+ // Turn on smart suggestion, and check if suggestion is enabled.
+ assertThat(mSuggestionParser.isDismissed(suggestion, true /* isSmartSuggestionEnabled */))
+ .isTrue();
+ }
+
private void readAndDismissSuggestion(boolean isSmartSuggestionEnabled) {
mSuggestionsBeforeDismiss = new ArrayList<>();
mSuggestionsAfterDismiss = new ArrayList<>();