blob: f6404a2215fb9fe919ae69fc3374acf07f32288f [file] [log] [blame]
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Fan Zhange138ef12017-05-11 15:29:56 -070017package com.android.settingslib.suggestions;
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080018
Fan Zhang7b77cf82017-07-21 10:21:36 -070019import static com.google.common.truth.Truth.assertThat;
20
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080021import android.content.ComponentName;
Fan Zhang73b161d2017-02-08 11:46:54 -080022import android.content.Context;
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080023import android.content.Intent;
24import android.content.SharedPreferences;
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080025import android.content.pm.ResolveInfo;
Fan Zhang73b161d2017-02-08 11:46:54 -080026import android.os.Bundle;
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080027import android.preference.PreferenceManager;
28
Fan Zhange138ef12017-05-11 15:29:56 -070029import com.android.settingslib.SettingLibRobolectricTestRunner;
30import com.android.settingslib.TestConfig;
Fan Zhang73b161d2017-02-08 11:46:54 -080031import com.android.settingslib.drawer.Tile;
32import com.android.settingslib.drawer.TileUtilsTest;
33
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080034import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
Fan Zhang73b161d2017-02-08 11:46:54 -080037import org.robolectric.RuntimeEnvironment;
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080038import org.robolectric.annotation.Config;
Maurice Lamf74b9e52017-03-23 14:58:47 -070039import org.robolectric.res.ResourceLoader;
40import org.robolectric.res.builder.DefaultPackageManager;
Fan Zhanga0f8d232017-05-08 09:33:04 -070041import org.robolectric.res.builder.RobolectricPackageManager;
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080042
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080043import java.util.ArrayList;
Maurice Lamf74b9e52017-03-23 14:58:47 -070044import java.util.Arrays;
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080045import java.util.List;
46
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080047@RunWith(SettingLibRobolectricTestRunner.class)
48@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
49public class SuggestionParserTest {
50
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080051 private Context mContext;
Fan Zhanga0f8d232017-05-08 09:33:04 -070052 private RobolectricPackageManager mPackageManager;
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080053 private SuggestionParser mSuggestionParser;
Fan Zhange138ef12017-05-11 15:29:56 -070054 private SuggestionCategory mMultipleCategory;
55 private SuggestionCategory mExclusiveCategory;
56 private SuggestionCategory mExpiredExclusiveCategory;
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080057 private List<Tile> mSuggestionsBeforeDismiss;
58 private List<Tile> mSuggestionsAfterDismiss;
59 private SharedPreferences mPrefs;
60 private Tile mSuggestion;
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080061
62 @Before
63 public void setUp() {
Maurice Lamf74b9e52017-03-23 14:58:47 -070064 RuntimeEnvironment.setRobolectricPackageManager(
65 new TestPackageManager(RuntimeEnvironment.getAppResourceLoader()));
66 mContext = RuntimeEnvironment.application;
Fan Zhanga0f8d232017-05-08 09:33:04 -070067 mPackageManager = RuntimeEnvironment.getRobolectricPackageManager();
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080068 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
69 mSuggestion = new Tile();
70 mSuggestion.intent = new Intent("action");
71 mSuggestion.intent.setComponent(new ComponentName("pkg", "cls"));
72 mSuggestion.metaData = new Bundle();
Fan Zhange138ef12017-05-11 15:29:56 -070073 mMultipleCategory = new SuggestionCategory();
Maurice Lamf74b9e52017-03-23 14:58:47 -070074 mMultipleCategory.category = "category1";
75 mMultipleCategory.multiple = true;
Fan Zhange138ef12017-05-11 15:29:56 -070076 mExclusiveCategory = new SuggestionCategory();
Maurice Lamf74b9e52017-03-23 14:58:47 -070077 mExclusiveCategory.category = "category2";
78 mExclusiveCategory.exclusive = true;
Fan Zhange138ef12017-05-11 15:29:56 -070079 mExpiredExclusiveCategory = new SuggestionCategory();
Fan Zhanga0f8d232017-05-08 09:33:04 -070080 mExpiredExclusiveCategory.category = "category3";
81 mExpiredExclusiveCategory.exclusive = true;
82 mExpiredExclusiveCategory.exclusiveExpireDaysInMillis = 0;
83
84 mSuggestionParser = new SuggestionParser(mContext, mPrefs,
85 Arrays.asList(mMultipleCategory, mExclusiveCategory, mExpiredExclusiveCategory),
Fan Zhangc07ae9c2017-08-03 17:33:18 -070086 "0");
Maurice Lamf74b9e52017-03-23 14:58:47 -070087
Fan Zhange138ef12017-05-11 15:29:56 -070088 ResolveInfo info1 = TileUtilsTest.newInfo(true, null);
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080089 info1.activityInfo.packageName = "pkg";
Fan Zhange138ef12017-05-11 15:29:56 -070090 ResolveInfo infoDupe1 = TileUtilsTest.newInfo(true, null);
Fan Zhanga0f8d232017-05-08 09:33:04 -070091 infoDupe1.activityInfo.packageName = "pkg";
92
Fan Zhange138ef12017-05-11 15:29:56 -070093 ResolveInfo info2 = TileUtilsTest.newInfo(true, null);
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -080094 info2.activityInfo.packageName = "pkg2";
Fan Zhange138ef12017-05-11 15:29:56 -070095 ResolveInfo info3 = TileUtilsTest.newInfo(true, null);
Maurice Lamf74b9e52017-03-23 14:58:47 -070096 info3.activityInfo.packageName = "pkg3";
Fan Zhange138ef12017-05-11 15:29:56 -070097 ResolveInfo info4 = TileUtilsTest.newInfo(true, null);
Fan Zhanga0f8d232017-05-08 09:33:04 -070098 info4.activityInfo.packageName = "pkg4";
Maurice Lamf74b9e52017-03-23 14:58:47 -070099
100 Intent intent1 = new Intent(Intent.ACTION_MAIN).addCategory("category1");
101 Intent intent2 = new Intent(Intent.ACTION_MAIN).addCategory("category2");
Fan Zhanga0f8d232017-05-08 09:33:04 -0700102 Intent intent3 = new Intent(Intent.ACTION_MAIN).addCategory("category3");
103
104 mPackageManager.addResolveInfoForIntent(intent1, info1);
105 mPackageManager.addResolveInfoForIntent(intent1, info2);
106 mPackageManager.addResolveInfoForIntent(intent1, infoDupe1);
107 mPackageManager.addResolveInfoForIntent(intent2, info3);
108 mPackageManager.addResolveInfoForIntent(intent3, info4);
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -0800109 }
110
111 @Test
Fan Zhangc07ae9c2017-08-03 17:33:18 -0700112 public void dismissSuggestion_shouldDismiss() {
113 assertThat(mSuggestionParser.dismissSuggestion(mSuggestion)).isTrue();
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -0800114 }
115
116 @Test
Fan Zhangc07ae9c2017-08-03 17:33:18 -0700117 public void testGetSuggestions_withoutSmartSuggestions_shouldDismiss() {
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -0800118 readAndDismissSuggestion(false);
Maurice Lamf74b9e52017-03-23 14:58:47 -0700119 mSuggestionParser.readSuggestions(mMultipleCategory, mSuggestionsAfterDismiss, false);
120 assertThat(mSuggestionsBeforeDismiss).hasSize(2);
121 assertThat(mSuggestionsAfterDismiss).hasSize(1);
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -0800122 assertThat(mSuggestionsBeforeDismiss.get(1)).isEqualTo(mSuggestionsAfterDismiss.get(0));
123 }
124
125 @Test
Fan Zhangc07ae9c2017-08-03 17:33:18 -0700126 public void testGetSuggestions_withSmartSuggestions_shouldDismiss() {
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -0800127 readAndDismissSuggestion(true);
Maurice Lamf74b9e52017-03-23 14:58:47 -0700128 assertThat(mSuggestionsBeforeDismiss).hasSize(2);
Fan Zhangc07ae9c2017-08-03 17:33:18 -0700129 assertThat(mSuggestionsAfterDismiss).hasSize(1);
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -0800130 }
131
Maurice Lamf74b9e52017-03-23 14:58:47 -0700132 @Test
Fan Zhanga0f8d232017-05-08 09:33:04 -0700133 public void testGetSuggestion_exclusiveNotAvailable_onlyRegularCategoryAndNoDupe() {
134 mPackageManager.removeResolveInfosForIntent(
Maurice Lamf74b9e52017-03-23 14:58:47 -0700135 new Intent(Intent.ACTION_MAIN).addCategory("category2"),
136 "pkg3");
Fan Zhanga0f8d232017-05-08 09:33:04 -0700137 mPackageManager.removeResolveInfosForIntent(
138 new Intent(Intent.ACTION_MAIN).addCategory("category3"),
139 "pkg4");
Maurice Lamf74b9e52017-03-23 14:58:47 -0700140
141 // If exclusive item is not available, the other categories should be shown
Fan Zhange138ef12017-05-11 15:29:56 -0700142 final SuggestionList sl =
143 mSuggestionParser.getSuggestions(false /* isSmartSuggestionEnabled */);
144 final List<Tile> suggestions = sl.getSuggestions();
Maurice Lamf74b9e52017-03-23 14:58:47 -0700145 assertThat(suggestions).hasSize(2);
Fan Zhange138ef12017-05-11 15:29:56 -0700146
Fan Zhanga0f8d232017-05-08 09:33:04 -0700147 assertThat(suggestions.get(0).intent.getComponent().getPackageName()).isEqualTo("pkg");
Fan Zhanga0f8d232017-05-08 09:33:04 -0700148 assertThat(suggestions.get(1).intent.getComponent().getPackageName()).isEqualTo("pkg2");
149 }
150
151 @Test
152 public void testGetSuggestion_exclusiveExpiredAvailable_shouldLoadWithRegularCategory() {
153 // First remove permanent exclusive
154 mPackageManager.removeResolveInfosForIntent(
155 new Intent(Intent.ACTION_MAIN).addCategory("category2"),
156 "pkg3");
157 // Set the other exclusive to be expired.
158 mPrefs.edit()
159 .putLong(mExpiredExclusiveCategory.category + "_setup_time",
160 System.currentTimeMillis() - 1000)
161 .commit();
162
163 // If exclusive is expired, they should be shown together with the other categories
Fan Zhange138ef12017-05-11 15:29:56 -0700164 final SuggestionList sl =
165 mSuggestionParser.getSuggestions(true /* isSmartSuggestionEnabled */);
166 final List<Tile> suggestions = sl.getSuggestions();
167
Fan Zhanga0f8d232017-05-08 09:33:04 -0700168 assertThat(suggestions).hasSize(3);
Fan Zhange138ef12017-05-11 15:29:56 -0700169
170 final List<Tile> category1Suggestions = sl.getSuggestionForCategory("category1");
171 final List<Tile> category3Suggestions = sl.getSuggestionForCategory("category3");
172
173 assertThat(category1Suggestions).hasSize(2);
174 assertThat(category3Suggestions).hasSize(1);
Maurice Lamf74b9e52017-03-23 14:58:47 -0700175 }
176
177 @Test
178 public void testGetSuggestions_exclusive() {
Fan Zhange138ef12017-05-11 15:29:56 -0700179 final SuggestionList sl =
180 mSuggestionParser.getSuggestions(false /* isSmartSuggestionEnabled */);
181 final List<Tile> suggestions = sl.getSuggestions();
182
Maurice Lamf74b9e52017-03-23 14:58:47 -0700183 assertThat(suggestions).hasSize(1);
Fan Zhange138ef12017-05-11 15:29:56 -0700184 assertThat(sl.getSuggestionForCategory("category2")).hasSize(1);
Maurice Lamf74b9e52017-03-23 14:58:47 -0700185 }
186
Fan Zhang7b77cf82017-07-21 10:21:36 -0700187 @Test
Fan Zhangc07ae9c2017-08-03 17:33:18 -0700188 public void isSuggestionDismissed_dismissedSuggestion_shouldReturnTrue() {
Fan Zhang7b77cf82017-07-21 10:21:36 -0700189 final Tile suggestion = new Tile();
190 suggestion.metaData = new Bundle();
191 suggestion.metaData.putString(SuggestionParser.META_DATA_DISMISS_CONTROL, "1,2,3");
192 suggestion.intent = new Intent().setComponent(new ComponentName("pkg", "cls"));
193
194 // Dismiss suggestion when smart suggestion is not enabled.
Fan Zhangc07ae9c2017-08-03 17:33:18 -0700195 mSuggestionParser.dismissSuggestion(suggestion);
Fan Zhang7b77cf82017-07-21 10:21:36 -0700196
Fan Zhang7b77cf82017-07-21 10:21:36 -0700197 assertThat(mSuggestionParser.isDismissed(suggestion, true /* isSmartSuggestionEnabled */))
198 .isTrue();
199 }
200
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -0800201 private void readAndDismissSuggestion(boolean isSmartSuggestionEnabled) {
Maurice Lamf74b9e52017-03-23 14:58:47 -0700202 mSuggestionsBeforeDismiss = new ArrayList<>();
203 mSuggestionsAfterDismiss = new ArrayList<>();
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -0800204 mSuggestionParser.readSuggestions(
Maurice Lamf74b9e52017-03-23 14:58:47 -0700205 mMultipleCategory, mSuggestionsBeforeDismiss, isSmartSuggestionEnabled);
Fan Zhange138ef12017-05-11 15:29:56 -0700206
Maurice Lamf74b9e52017-03-23 14:58:47 -0700207 final Tile suggestion = mSuggestionsBeforeDismiss.get(0);
Fan Zhangc07ae9c2017-08-03 17:33:18 -0700208 if (mSuggestionParser.dismissSuggestion(suggestion)) {
Maurice Lamf74b9e52017-03-23 14:58:47 -0700209 RuntimeEnvironment.getRobolectricPackageManager().removeResolveInfosForIntent(
Fan Zhange138ef12017-05-11 15:29:56 -0700210 new Intent(Intent.ACTION_MAIN).addCategory(mMultipleCategory.category),
Maurice Lamf74b9e52017-03-23 14:58:47 -0700211 suggestion.intent.getComponent().getPackageName());
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -0800212 }
213 mSuggestionParser.readSuggestions(
Maurice Lamf74b9e52017-03-23 14:58:47 -0700214 mMultipleCategory, mSuggestionsAfterDismiss, isSmartSuggestionEnabled);
215 }
216
217 private static class TestPackageManager extends DefaultPackageManager {
218
219 TestPackageManager(ResourceLoader appResourceLoader) {
220 super(appResourceLoader);
221 }
222
223 @Override
224 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId) {
225 return super.queryIntentActivities(intent, flags);
226 }
Soroosh Mariooryad56ce7662017-02-06 15:23:00 -0800227 }
228}