Remove search indexing pipeline from Settings
Index is already handled by SettingsIntelligenec. No longer needed in Settings. Change-Id: Id43fb3100dc2759185744441cff8cb9cd2d2da20 Fixes: 69808376 Test: robotests
This commit is contained in:
@@ -1,388 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.search;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyBoolean;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyList;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Build;
|
||||
import android.provider.SearchIndexableData;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import com.android.settings.search.indexing.PreIndexData;
|
||||
import com.android.settings.testutils.DatabaseTestUtils;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.ShadowRunnableAsyncTask;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = ShadowRunnableAsyncTask.class)
|
||||
public class DatabaseIndexingManagerTest {
|
||||
|
||||
private final String localeStr = "en_US";
|
||||
|
||||
private final int rank = 8;
|
||||
private final String title = "title\u2011title";
|
||||
private final String updatedTitle = "title-title";
|
||||
private final String normalizedTitle = "titletitle";
|
||||
private final String summaryOn = "summary\u2011on";
|
||||
private final String updatedSummaryOn = "summary-on";
|
||||
private final String normalizedSummaryOn = "summaryon";
|
||||
private final String summaryOff = "summary\u2011off";
|
||||
private final String entries = "entries";
|
||||
private final String keywords = "keywords, keywordss, keywordsss";
|
||||
private final String spaceDelimittedKeywords = "keywords keywordss keywordsss";
|
||||
private final String screenTitle = "screen title";
|
||||
private final String className = "class name";
|
||||
private final int iconResId = 0xff;
|
||||
private final String action = "action";
|
||||
private final String targetPackage = "target package";
|
||||
private final String targetClass = "target class";
|
||||
private final String packageName = "package name";
|
||||
private final String key = "key";
|
||||
private final int userId = -1;
|
||||
private final boolean enabled = true;
|
||||
|
||||
private final String TITLE_ONE = "title one";
|
||||
private final String TITLE_TWO = "title two";
|
||||
private final String KEY_ONE = "key one";
|
||||
private final String KEY_TWO = "key two";
|
||||
|
||||
private Context mContext;
|
||||
|
||||
private DatabaseIndexingManager mManager;
|
||||
private SQLiteDatabase mDb;
|
||||
|
||||
private final List<ResolveInfo> FAKE_PROVIDER_LIST = new ArrayList<>();
|
||||
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mManager = spy(new DatabaseIndexingManager(mContext));
|
||||
mDb = IndexDatabaseHelper.getInstance(mContext).getWritableDatabase();
|
||||
|
||||
doReturn(mPackageManager).when(mContext).getPackageManager();
|
||||
doReturn(FAKE_PROVIDER_LIST).when(mPackageManager)
|
||||
.queryIntentContentProviders(any(Intent.class), anyInt());
|
||||
FakeFeatureFactory.setupForTest();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
DatabaseTestUtils.clearDb(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDatabaseSchema() {
|
||||
Cursor dbCursor = mDb.query("prefs_index", null, null, null, null, null, null);
|
||||
List<String> columnNames = new ArrayList<>(Arrays.asList(dbCursor.getColumnNames()));
|
||||
// Note that docid is not included.
|
||||
List<String> expColumnNames = Arrays.asList(
|
||||
"locale",
|
||||
"data_rank",
|
||||
"data_title",
|
||||
"data_title_normalized",
|
||||
"data_summary_on",
|
||||
"data_summary_on_normalized",
|
||||
"data_summary_off",
|
||||
"data_summary_off_normalized",
|
||||
"data_entries",
|
||||
"data_keywords",
|
||||
"class_name",
|
||||
"screen_title",
|
||||
"intent_action",
|
||||
"intent_target_package",
|
||||
"intent_target_class",
|
||||
"icon",
|
||||
"enabled",
|
||||
"data_key_reference",
|
||||
"user_id",
|
||||
"payload_type",
|
||||
"payload"
|
||||
);
|
||||
// Prevent database schema regressions
|
||||
assertThat(columnNames).containsAllIn(expColumnNames);
|
||||
}
|
||||
|
||||
// Test new public indexing flow
|
||||
|
||||
@Test
|
||||
public void testPerformIndexing_fullIndex_getsDataFromProviders() {
|
||||
SearchIndexableRaw rawData = getFakeRaw();
|
||||
PreIndexData data = getPreIndexData(rawData);
|
||||
doReturn(data).when(mManager).getIndexDataFromProviders(anyList(), anyBoolean());
|
||||
doReturn(true).when(mManager)
|
||||
.isFullIndex(any(Context.class), anyString(), anyString(), anyString());
|
||||
|
||||
mManager.performIndexing();
|
||||
|
||||
verify(mManager).updateDatabase(data, true /* isFullIndex */);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformIndexing_fullIndex_databaseDropped() {
|
||||
// Initialize the Manager and force rebuild
|
||||
DatabaseIndexingManager manager =
|
||||
spy(new DatabaseIndexingManager(mContext));
|
||||
doReturn(false).when(mManager)
|
||||
.isFullIndex(any(Context.class), anyString(), anyString(), anyString());
|
||||
|
||||
// Insert data point which will be dropped
|
||||
insertSpecialCase("Ceci n'est pas un pipe", true, "oui oui mon ami");
|
||||
|
||||
manager.performIndexing();
|
||||
|
||||
// Assert that the Old Title is no longer in the database, since it was dropped
|
||||
final Cursor oldCursor = mDb.rawQuery("SELECT * FROM prefs_index", null);
|
||||
|
||||
assertThat(oldCursor.getCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformIndexing_isfullIndex() {
|
||||
SearchIndexableRaw rawData = getFakeRaw();
|
||||
PreIndexData data = getPreIndexData(rawData);
|
||||
doReturn(data).when(mManager).getIndexDataFromProviders(anyList(), anyBoolean());
|
||||
doReturn(true).when(mManager)
|
||||
.isFullIndex(any(Context.class), anyString(), anyString(), anyString());
|
||||
|
||||
mManager.performIndexing();
|
||||
|
||||
verify(mManager).updateDatabase(data, true /* isFullIndex */);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformIndexing_onOta_buildNumberIsCached() {
|
||||
mManager.performIndexing();
|
||||
|
||||
assertThat(IndexDatabaseHelper.isBuildIndexed(mContext, Build.FINGERPRINT)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocaleUpdated_afterIndexing_localeNotAdded() {
|
||||
PreIndexData emptydata = new PreIndexData();
|
||||
mManager.updateDatabase(emptydata, true /* isFullIndex */);
|
||||
|
||||
assertThat(IndexDatabaseHelper.isLocaleAlreadyIndexed(mContext, localeStr)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocaleUpdated_afterFullIndexing_localeAdded() {
|
||||
mManager.performIndexing();
|
||||
|
||||
assertThat(IndexDatabaseHelper.isLocaleAlreadyIndexed(mContext, localeStr)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateDatabase_newEligibleData_addedToDatabase() {
|
||||
// Test that addDataToDatabase is called when dataToUpdate is non-empty
|
||||
PreIndexData indexData = new PreIndexData();
|
||||
indexData.dataToUpdate.add(getFakeRaw());
|
||||
mManager.updateDatabase(indexData, true /* isFullIndex */);
|
||||
|
||||
Cursor cursor = mDb.rawQuery("SELECT * FROM prefs_index", null);
|
||||
cursor.moveToPosition(0);
|
||||
|
||||
// Locale
|
||||
assertThat(cursor.getString(0)).isEqualTo(localeStr);
|
||||
// Data Title
|
||||
assertThat(cursor.getString(2)).isEqualTo(updatedTitle);
|
||||
// Normalized Title
|
||||
assertThat(cursor.getString(3)).isEqualTo(normalizedTitle);
|
||||
// Summary On
|
||||
assertThat(cursor.getString(4)).isEqualTo(updatedSummaryOn);
|
||||
// Summary On Normalized
|
||||
assertThat(cursor.getString(5)).isEqualTo(normalizedSummaryOn);
|
||||
// Entries
|
||||
assertThat(cursor.getString(8)).isEqualTo(entries);
|
||||
// Keywords
|
||||
assertThat(cursor.getString(9)).isEqualTo(spaceDelimittedKeywords);
|
||||
// Screen Title
|
||||
assertThat(cursor.getString(10)).isEqualTo(screenTitle);
|
||||
// Class Name
|
||||
assertThat(cursor.getString(11)).isEqualTo(className);
|
||||
// Icon
|
||||
assertThat(cursor.getInt(12)).isEqualTo(iconResId);
|
||||
// Intent Action
|
||||
assertThat(cursor.getString(13)).isEqualTo(action);
|
||||
// Target Package
|
||||
assertThat(cursor.getString(14)).isEqualTo(targetPackage);
|
||||
// Target Class
|
||||
assertThat(cursor.getString(15)).isEqualTo(targetClass);
|
||||
// Enabled
|
||||
assertThat(cursor.getInt(16) == 1).isEqualTo(enabled);
|
||||
// Data ref key
|
||||
assertThat(cursor.getString(17)).isNotNull();
|
||||
// User Id
|
||||
assertThat(cursor.getInt(18)).isEqualTo(userId);
|
||||
// Payload Type - default is 0
|
||||
assertThat(cursor.getInt(19)).isEqualTo(0);
|
||||
// Payload
|
||||
byte[] payload = cursor.getBlob(20);
|
||||
ResultPayload unmarshalledPayload = ResultPayloadUtils.unmarshall(payload,
|
||||
ResultPayload.CREATOR);
|
||||
assertThat(unmarshalledPayload).isInstanceOf(ResultPayload.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateDataInDatabase_enabledResultsAreNonIndexable_becomeDisabled() {
|
||||
// Both results are enabled, and then TITLE_ONE gets disabled.
|
||||
final boolean enabled = true;
|
||||
insertSpecialCase(TITLE_ONE, enabled, KEY_ONE);
|
||||
insertSpecialCase(TITLE_TWO, enabled, KEY_TWO);
|
||||
Map<String, Set<String>> niks = new ArrayMap<>();
|
||||
Set<String> keys = new HashSet<>();
|
||||
keys.add(KEY_ONE);
|
||||
niks.put(targetPackage, keys);
|
||||
|
||||
mManager.updateDataInDatabase(mDb, niks);
|
||||
|
||||
Cursor cursor = mDb.rawQuery("SELECT * FROM prefs_index WHERE enabled = 0", null);
|
||||
cursor.moveToPosition(0);
|
||||
|
||||
assertThat(cursor.getString(2)).isEqualTo(TITLE_ONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateDataInDatabase_disabledResultsAreIndexable_becomeEnabled() {
|
||||
// Both results are initially disabled, and then TITLE_TWO gets enabled.
|
||||
final boolean enabled = false;
|
||||
insertSpecialCase(TITLE_ONE, enabled, KEY_ONE);
|
||||
insertSpecialCase(TITLE_TWO, enabled, KEY_TWO);
|
||||
Map<String, Set<String>> niks = new ArrayMap<>();
|
||||
Set<String> keys = new HashSet<>();
|
||||
keys.add(KEY_ONE);
|
||||
niks.put(targetPackage, keys);
|
||||
|
||||
mManager.updateDataInDatabase(mDb, niks);
|
||||
|
||||
Cursor cursor = mDb.rawQuery("SELECT * FROM prefs_index WHERE enabled = 1", null);
|
||||
cursor.moveToPosition(0);
|
||||
|
||||
assertThat(cursor.getString(2)).isEqualTo(TITLE_TWO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyNonIndexableKeys_emptyDataKeyResources_addedToDatabase() {
|
||||
insertSpecialCase(TITLE_ONE, true /* enabled */, null /* dataReferenceKey */);
|
||||
PreIndexData emptydata = new PreIndexData();
|
||||
mManager.updateDatabase(emptydata, false /* needsReindexing */);
|
||||
|
||||
Cursor cursor = mDb.rawQuery("SELECT * FROM prefs_index WHERE enabled = 1", null);
|
||||
cursor.moveToPosition(0);
|
||||
assertThat(cursor.getCount()).isEqualTo(1);
|
||||
assertThat(cursor.getString(2)).isEqualTo(TITLE_ONE);
|
||||
}
|
||||
|
||||
// Util functions
|
||||
|
||||
private SearchIndexableRaw getFakeRaw() {
|
||||
return getFakeRaw(localeStr);
|
||||
}
|
||||
|
||||
private SearchIndexableRaw getFakeRaw(String localeStr) {
|
||||
SearchIndexableRaw data = new SearchIndexableRaw(mContext);
|
||||
data.locale = new Locale(localeStr);
|
||||
data.rank = rank;
|
||||
data.title = title;
|
||||
data.summaryOn = summaryOn;
|
||||
data.summaryOff = summaryOff;
|
||||
data.entries = entries;
|
||||
data.keywords = keywords;
|
||||
data.screenTitle = screenTitle;
|
||||
data.className = className;
|
||||
data.packageName = packageName;
|
||||
data.iconResId = iconResId;
|
||||
data.intentAction = action;
|
||||
data.intentTargetPackage = targetPackage;
|
||||
data.intentTargetClass = targetClass;
|
||||
data.key = key;
|
||||
data.userId = userId;
|
||||
data.enabled = enabled;
|
||||
return data;
|
||||
}
|
||||
|
||||
private void insertSpecialCase(String specialCase, boolean enabled, String key) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DOCID, specialCase.hashCode());
|
||||
values.put(IndexDatabaseHelper.IndexColumns.LOCALE, localeStr);
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DATA_RANK, 1);
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DATA_TITLE, specialCase);
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DATA_TITLE_NORMALIZED, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DATA_SUMMARY_ON, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DATA_SUMMARY_ON_NORMALIZED, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DATA_SUMMARY_OFF, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DATA_SUMMARY_OFF_NORMALIZED, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DATA_ENTRIES, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DATA_KEYWORDS, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.CLASS_NAME, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.SCREEN_TITLE, "Moves");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.INTENT_ACTION, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.INTENT_TARGET_PACKAGE, targetPackage);
|
||||
values.put(IndexDatabaseHelper.IndexColumns.INTENT_TARGET_CLASS, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.ICON, "");
|
||||
values.put(IndexDatabaseHelper.IndexColumns.ENABLED, enabled);
|
||||
values.put(IndexDatabaseHelper.IndexColumns.DATA_KEY_REF, key);
|
||||
values.put(IndexDatabaseHelper.IndexColumns.USER_ID, 0);
|
||||
values.put(IndexDatabaseHelper.IndexColumns.PAYLOAD_TYPE, 0);
|
||||
values.put(IndexDatabaseHelper.IndexColumns.PAYLOAD, (String) null);
|
||||
|
||||
mDb.replaceOrThrow(IndexDatabaseHelper.Tables.TABLE_PREFS_INDEX, null, values);
|
||||
}
|
||||
|
||||
private PreIndexData getPreIndexData(SearchIndexableData fakeData) {
|
||||
PreIndexData data = new PreIndexData();
|
||||
data.dataToUpdate.add(fakeData);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.search;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class DatabaseIndexingUtilsTest {
|
||||
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPreferenceControllerUriMap_BadClassName_ReturnsNull() {
|
||||
Map map = DatabaseIndexingUtils.getPayloadKeyMap("dummy", mContext);
|
||||
assertThat(map).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPreferenceControllerUriMap_NullContext_ReturnsNull() {
|
||||
Map map = DatabaseIndexingUtils.getPayloadKeyMap("dummy", null);
|
||||
assertThat(map).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPayloadFromMap_NullMap_ReturnsNull() {
|
||||
final String className = "com.android.settings.system.SystemDashboardFragment";
|
||||
final Map<String, ResultPayload> map =
|
||||
DatabaseIndexingUtils.getPayloadKeyMap(className, mContext);
|
||||
ResultPayload payload = map.get(null);
|
||||
assertThat(payload).isNull();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -12,18 +12,15 @@
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.search.indexing;
|
||||
package com.android.settings.search;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.SearchIndexableResource;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.search.SearchIndexableRaw;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -1,105 +0,0 @@
|
||||
package com.android.settings.search;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Parcel;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class InlineListPayloadTest {
|
||||
|
||||
private static final String DUMMY_SETTING = "inline_list_key";
|
||||
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_DataRetained() {
|
||||
final String uri = "test.com";
|
||||
final int type = ResultPayload.PayloadType.INLINE_LIST;
|
||||
final int source = ResultPayload.SettingsSource.SYSTEM;
|
||||
final String intentKey = "key";
|
||||
final String intentVal = "value";
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra(intentKey, intentVal);
|
||||
|
||||
InlineListPayload payload = new InlineListPayload(uri, source,
|
||||
intent, true /* isAvailable */, 1 /* numOptions */, 1 /* default */);
|
||||
|
||||
final Intent retainedIntent = payload.getIntent();
|
||||
assertThat(payload.getKey()).isEqualTo(uri);
|
||||
assertThat(payload.getType()).isEqualTo(type);
|
||||
assertThat(payload.mSettingSource).isEqualTo(source);
|
||||
assertThat(payload.getAvailability()).isEqualTo(ResultPayload.Availability.AVAILABLE);
|
||||
assertThat(retainedIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelConstructor_DataRetained() {
|
||||
String uri = "test.com";
|
||||
int type = ResultPayload.PayloadType.INLINE_LIST;
|
||||
int source = ResultPayload.SettingsSource.SYSTEM;
|
||||
final String intentKey = "key";
|
||||
final String intentVal = "value";
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra(intentKey, intentVal);
|
||||
|
||||
Parcel parcel = Parcel.obtain();
|
||||
parcel.writeParcelable(intent, 0);
|
||||
parcel.writeString(uri);
|
||||
parcel.writeInt(source);
|
||||
parcel.writeInt(InlineSwitchPayload.TRUE);
|
||||
parcel.writeInt(InlineSwitchPayload.TRUE);
|
||||
parcel.setDataPosition(0);
|
||||
|
||||
InlineListPayload payload = InlineListPayload.CREATOR.createFromParcel(parcel);
|
||||
|
||||
final Intent builtIntent = payload.getIntent();
|
||||
assertThat(payload.getKey()).isEqualTo(uri);
|
||||
assertThat(payload.getType()).isEqualTo(type);
|
||||
assertThat(payload.mSettingSource).isEqualTo(source);
|
||||
assertThat(payload.getAvailability()).isEqualTo(ResultPayload.Availability.AVAILABLE);
|
||||
assertThat(builtIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputStandardization_inputDoesntChange() {
|
||||
InlineListPayload payload = new InlineListPayload(DUMMY_SETTING,
|
||||
ResultPayload.SettingsSource.SYSTEM, null /* intent */, true /* isDeviceSupport */,
|
||||
3 /* numOptions */, 0 /* default */);
|
||||
int input = 2;
|
||||
|
||||
assertThat(payload.standardizeInput(input)).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSetSystem_negativeValue_throwsError() {
|
||||
InlineListPayload payload = new InlineListPayload(DUMMY_SETTING,
|
||||
ResultPayload.SettingsSource.SYSTEM, null /* intent */, true /* isDeviceSupport */,
|
||||
3 /* numOptions */, 0 /* default */);
|
||||
|
||||
payload.setValue(mContext, -1);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSetSystem_exceedsMaxValue_throwsError() {
|
||||
int maxOptions = 4;
|
||||
InlineListPayload payload = new InlineListPayload(DUMMY_SETTING,
|
||||
ResultPayload.SettingsSource.SYSTEM, null /* intent */, true /* isDeviceSupport */,
|
||||
maxOptions /* numOptions */, 0 /* default */);
|
||||
|
||||
payload.setValue(mContext, maxOptions + 1);
|
||||
}
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package com.android.settings.search;
|
||||
|
||||
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.provider.Settings;
|
||||
|
||||
import com.android.settings.search.ResultPayload.SettingsSource;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class InlinePayloadTest {
|
||||
|
||||
private static final String KEY = "key";
|
||||
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSecure_returnsSecureSetting() {
|
||||
InlinePayload payload = getDummyPayload(SettingsSource.SECURE);
|
||||
int currentValue = 2;
|
||||
Settings.Secure.putInt(mContext.getContentResolver(), KEY, currentValue);
|
||||
|
||||
int newValue = payload.getValue(mContext);
|
||||
|
||||
assertThat(newValue).isEqualTo(currentValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetGlobal_returnsGlobalSetting() {
|
||||
InlinePayload payload = getDummyPayload(SettingsSource.GLOBAL);
|
||||
int currentValue = 2;
|
||||
Settings.Global.putInt(mContext.getContentResolver(), KEY, currentValue);
|
||||
|
||||
int newValue = payload.getValue(mContext);
|
||||
|
||||
assertThat(newValue).isEqualTo(currentValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSystem_returnsSystemSetting() {
|
||||
InlinePayload payload = getDummyPayload(SettingsSource.SYSTEM);
|
||||
int currentValue = 2;
|
||||
Settings.System.putInt(mContext.getContentResolver(), KEY, currentValue);
|
||||
|
||||
int newValue = payload.getValue(mContext);
|
||||
|
||||
assertThat(newValue).isEqualTo(currentValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSecure_updatesSecureSetting() {
|
||||
InlinePayload payload = getDummyPayload(SettingsSource.SECURE);
|
||||
int newValue = 1;
|
||||
ContentResolver resolver = mContext.getContentResolver();
|
||||
Settings.Secure.putInt(resolver, KEY, 0);
|
||||
|
||||
payload.setValue(mContext, newValue);
|
||||
int updatedValue = Settings.System.getInt(resolver, KEY, -1);
|
||||
|
||||
assertThat(updatedValue).isEqualTo(newValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetGlobal_updatesGlobalSetting() {
|
||||
InlinePayload payload = getDummyPayload(SettingsSource.GLOBAL);
|
||||
int newValue = 1;
|
||||
ContentResolver resolver = mContext.getContentResolver();
|
||||
Settings.Global.putInt(resolver, KEY, 0);
|
||||
|
||||
payload.setValue(mContext, newValue);
|
||||
int updatedValue = Settings.Global.getInt(resolver, KEY, -1);
|
||||
|
||||
assertThat(updatedValue).isEqualTo(newValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSystem_updatesSystemSetting() {
|
||||
InlinePayload payload = getDummyPayload(SettingsSource.SECURE);
|
||||
int newValue = 1;
|
||||
ContentResolver resolver = mContext.getContentResolver();
|
||||
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, 0);
|
||||
|
||||
payload.setValue(mContext, newValue);
|
||||
int updatedValue = Settings.System.getInt(resolver, KEY, -1);
|
||||
|
||||
assertThat(updatedValue).isEqualTo(newValue);
|
||||
}
|
||||
|
||||
private InlinePayload getDummyPayload(int source) {
|
||||
return new ConcreteInlinePayload(KEY, source, null /* intent */,
|
||||
true /* isDeviceSupported */);
|
||||
}
|
||||
|
||||
private class ConcreteInlinePayload extends InlinePayload {
|
||||
|
||||
private ConcreteInlinePayload(String key, @SettingsSource int source, Intent intent,
|
||||
boolean isDeviceSupported) {
|
||||
super(key, source, intent, isDeviceSupported, 0 /* defaultValue */);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getType() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int standardizeInput(int input) throws IllegalArgumentException {
|
||||
return input;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.search;
|
||||
|
||||
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Parcel;
|
||||
import android.provider.Settings;
|
||||
|
||||
import com.android.settings.search.ResultPayload.Availability;
|
||||
import com.android.settings.search.ResultPayload.SettingsSource;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class InlineSwitchPayloadTest {
|
||||
|
||||
private static final String DUMMY_SETTING = "inline_test";
|
||||
private static final int STANDARD_ON = 1;
|
||||
private static final int FLIPPED_ON = 0;
|
||||
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_DataRetained() {
|
||||
final String uri = "test.com";
|
||||
final int type = ResultPayload.PayloadType.INLINE_SWITCH;
|
||||
final int source = SettingsSource.SECURE;
|
||||
final String intentKey = "key";
|
||||
final String intentVal = "value";
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra(intentKey, intentVal);
|
||||
|
||||
InlineSwitchPayload payload =
|
||||
new InlineSwitchPayload(uri, source, 1, intent, true, 1 /* default */);
|
||||
final Intent retainedIntent = payload.getIntent();
|
||||
assertThat(payload.getKey()).isEqualTo(uri);
|
||||
assertThat(payload.getType()).isEqualTo(type);
|
||||
assertThat(payload.mSettingSource).isEqualTo(source);
|
||||
assertThat(payload.isStandard()).isTrue();
|
||||
assertThat(payload.getAvailability()).isEqualTo(ResultPayload.Availability.AVAILABLE);
|
||||
assertThat(retainedIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelConstructor_DataRetained() {
|
||||
String uri = "test.com";
|
||||
int type = ResultPayload.PayloadType.INLINE_SWITCH;
|
||||
int source = SettingsSource.SECURE;
|
||||
final String intentKey = "key";
|
||||
final String intentVal = "value";
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra(intentKey, intentVal);
|
||||
Parcel parcel = Parcel.obtain();
|
||||
parcel.writeParcelable(intent, 0);
|
||||
parcel.writeString(uri);
|
||||
parcel.writeInt(source);
|
||||
parcel.writeInt(InlineSwitchPayload.TRUE);
|
||||
parcel.writeInt(InlineSwitchPayload.TRUE);
|
||||
parcel.writeInt(InlineSwitchPayload.TRUE);
|
||||
parcel.setDataPosition(0);
|
||||
|
||||
InlineSwitchPayload payload = InlineSwitchPayload.CREATOR.createFromParcel(parcel);
|
||||
|
||||
final Intent builtIntent = payload.getIntent();
|
||||
assertThat(payload.getKey()).isEqualTo(uri);
|
||||
assertThat(payload.getType()).isEqualTo(type);
|
||||
assertThat(payload.mSettingSource).isEqualTo(source);
|
||||
assertThat(payload.isStandard()).isTrue();
|
||||
assertThat(payload.getAvailability()).isEqualTo(Availability.AVAILABLE);
|
||||
assertThat(builtIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSystem_flippedSetting_returnsFlippedValue() {
|
||||
// Stores 1s as 0s, and vis versa
|
||||
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
|
||||
FLIPPED_ON, null /* intent */, true, 1 /* default */);
|
||||
int currentValue = 1;
|
||||
Settings.System.putInt(mContext.getContentResolver(), DUMMY_SETTING, currentValue);
|
||||
|
||||
int newValue = payload.getValue(mContext);
|
||||
|
||||
assertThat(newValue).isEqualTo(1 - currentValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSystem_flippedSetting_updatesToFlippedValue() {
|
||||
// Stores 1s as 0s, and vis versa
|
||||
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
|
||||
FLIPPED_ON, null /* intent */, true, 1 /* default */);
|
||||
int newValue = 1;
|
||||
ContentResolver resolver = mContext.getContentResolver();
|
||||
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, newValue);
|
||||
|
||||
payload.setValue(mContext, newValue);
|
||||
int updatedValue = Settings.System.getInt(resolver, DUMMY_SETTING, -1);
|
||||
|
||||
assertThat(updatedValue).isEqualTo(1 - newValue);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSetSystem_negativeValue_ThrowsError() {
|
||||
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
|
||||
STANDARD_ON, null /* intent */, true, 1 /* default */);
|
||||
|
||||
payload.setValue(mContext, -1);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSetSystem_highValue_ThrowsError() {
|
||||
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
|
||||
STANDARD_ON, null /* intent */, true, 1 /* default */);
|
||||
|
||||
payload.setValue(mContext, 2);
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.search;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Parcel;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class ResultPayloadTest {
|
||||
|
||||
private static final String EXTRA_KEY = "key";
|
||||
private static final String EXTRA_VALUE = "value";
|
||||
|
||||
@Test
|
||||
public void testParcelOrdering_StaysValid() {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(EXTRA_KEY, EXTRA_VALUE);
|
||||
Parcel parcel = Parcel.obtain();
|
||||
|
||||
final ResultPayload payload = new ResultPayload(intent);
|
||||
payload.writeToParcel(parcel, 0);
|
||||
// Reset parcel for reading
|
||||
parcel.setDataPosition(0);
|
||||
ResultPayload newPayload = ResultPayload.CREATOR.createFromParcel(parcel);
|
||||
|
||||
String originalIntentExtra = payload.getIntent().getStringExtra(EXTRA_KEY);
|
||||
String copiedIntentExtra = newPayload.getIntent().getStringExtra(EXTRA_KEY);
|
||||
assertThat(originalIntentExtra).isEqualTo(copiedIntentExtra);
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.search;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
import android.content.Intent;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class ResultPayloadUtilsTest {
|
||||
|
||||
private static final String EXTRA_KEY = "key";
|
||||
private static final String EXTRA_VALUE = "value";
|
||||
|
||||
private ResultPayload payload;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(EXTRA_KEY, EXTRA_VALUE);
|
||||
payload = new ResultPayload(intent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnmarshallBadData_ExceptionThrown() {
|
||||
byte[] badData = "I'm going to fail :)".getBytes();
|
||||
try {
|
||||
ResultPayloadUtils.unmarshall(badData, ResultPayload.CREATOR);
|
||||
fail("unmarshall should throw exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertThat(e).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarshallResultPayload_NonEmptyArray() {
|
||||
byte[] marshalledPayload = ResultPayloadUtils.marshall(payload);
|
||||
assertThat(marshalledPayload).isNotNull();
|
||||
assertThat(marshalledPayload).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnmarshall_PreservedData() {
|
||||
byte[] marshalledPayload = ResultPayloadUtils.marshall(payload);
|
||||
ResultPayload newPayload =
|
||||
ResultPayloadUtils.unmarshall(marshalledPayload, ResultPayload.CREATOR);
|
||||
|
||||
String originalIntentExtra = payload.getIntent().getStringExtra(EXTRA_KEY);
|
||||
String copiedIntentExtra = newPayload.getIntent().getStringExtra(EXTRA_KEY);
|
||||
assertThat(originalIntentExtra).isEqualTo(copiedIntentExtra);
|
||||
}
|
||||
}
|
||||
@@ -86,12 +86,4 @@ public class SearchFeatureProviderImplTest {
|
||||
final ComponentName cn = new ComponentName(packageName, "class");
|
||||
mProvider.verifyLaunchSearchResultPageCaller(mActivity, cn);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cleanQuery_trimsWhitespace() {
|
||||
final String query = " space ";
|
||||
final String cleanQuery = "space";
|
||||
|
||||
assertThat(mProvider.cleanQuery(query)).isEqualTo(cleanQuery);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import android.net.Uri;
|
||||
import android.provider.SearchIndexablesContract;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.search.indexing.FakeSettingsFragment;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
|
||||
@@ -1,369 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.search.indexing;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.search.ResultPayload;
|
||||
import com.android.settings.search.ResultPayloadUtils;
|
||||
import com.android.settings.search.SearchIndexableRaw;
|
||||
import com.android.settings.testutils.DatabaseTestUtils;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(qualifiers = "mcc999")
|
||||
public class IndexDataConverterTest {
|
||||
|
||||
private static final String localeStr = "en_US";
|
||||
|
||||
private static final String title = "title\u2011title";
|
||||
private static final String updatedTitle = "title-title";
|
||||
private static final String normalizedTitle = "titletitle";
|
||||
private static final String summaryOn = "summary\u2011on";
|
||||
private static final String updatedSummaryOn = "summary-on";
|
||||
private static final String summaryOff = "summary\u2011off";
|
||||
private static final String entries = "entries";
|
||||
private static final String keywords = "keywords, keywordss, keywordsss";
|
||||
private static final String spaceDelimittedKeywords = "keywords keywordss keywordsss";
|
||||
private static final String screenTitle = "screen title";
|
||||
private static final String className = "class name";
|
||||
private static final int iconResId = 0xff;
|
||||
private static final String action = "action";
|
||||
private static final String targetPackage = "target package";
|
||||
private static final String targetClass = "target class";
|
||||
private static final String packageName = "package name";
|
||||
private static final String key = "key";
|
||||
private static final int userId = -1;
|
||||
private static final boolean enabled = true;
|
||||
|
||||
// There are 6 entries in the fake display_settings.xml preference.
|
||||
private static final int NUM_DISPLAY_ENTRIES = 6;
|
||||
private static final String PAGE_TITLE = "page_title";
|
||||
private static final String TITLE_ONE = "pref_title_1";
|
||||
private static final String TITLE_TWO = "pref_title_2";
|
||||
private static final String TITLE_THREE = "pref_title_3";
|
||||
private static final String TITLE_FOUR = "pref_title_4";
|
||||
private static final String TITLE_FIVE = "pref_title_5";
|
||||
private static final String DISPLAY_SPACE_DELIM_KEYWORDS = "keywords1 keywords2 keywords3";
|
||||
|
||||
// There is a title and one preference.
|
||||
private static final int NUM_LEGAL_SETTINGS = 2;
|
||||
|
||||
private Context mContext;
|
||||
|
||||
private IndexDataConverter mConverter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mConverter = spy(new IndexDataConverter(mContext));
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
DatabaseTestUtils.clearDb(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertRawColumn_rowConverted() {
|
||||
final SearchIndexableRaw raw = getFakeRaw();
|
||||
final PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(raw);
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
assertThat(indexData.size()).isEqualTo(1);
|
||||
final IndexData row = indexData.get(0);
|
||||
|
||||
assertThat(row.normalizedTitle).isEqualTo(normalizedTitle);
|
||||
assertThat(row.updatedTitle).isEqualTo(updatedTitle);
|
||||
assertThat(row.locale).isEqualTo(localeStr);
|
||||
assertThat(row.updatedSummaryOn).isEqualTo(updatedSummaryOn);
|
||||
assertThat(row.entries).isEqualTo(entries);
|
||||
assertThat(row.spaceDelimitedKeywords).isEqualTo(spaceDelimittedKeywords);
|
||||
assertThat(row.screenTitle).isEqualTo(screenTitle);
|
||||
assertThat(row.className).isEqualTo(className);
|
||||
assertThat(row.iconResId).isEqualTo(iconResId);
|
||||
assertThat(row.intentAction).isEqualTo(action);
|
||||
assertThat(row.intentTargetPackage).isEqualTo(targetPackage);
|
||||
assertThat(row.intentTargetClass).isEqualTo(targetClass);
|
||||
assertThat(row.enabled).isEqualTo(enabled);
|
||||
assertThat(row.key).isEqualTo(key);
|
||||
assertThat(row.userId).isEqualTo(userId);
|
||||
assertThat(row.payloadType).isEqualTo(0);
|
||||
ResultPayload unmarshalledPayload =
|
||||
ResultPayloadUtils.unmarshall(row.payload, ResultPayload.CREATOR);
|
||||
assertThat(unmarshalledPayload).isInstanceOf(ResultPayload.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertRawColumn_nonIndexableKey_resultIsDisabled() {
|
||||
final SearchIndexableRaw raw = getFakeRaw();
|
||||
// Add non-indexable key for raw row.
|
||||
Set<String> keys = new HashSet<>();
|
||||
keys.add(raw.key);
|
||||
|
||||
final PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(raw);
|
||||
preIndexData.nonIndexableKeys.put(raw.intentTargetPackage, keys);
|
||||
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
assertThat(indexData.size()).isEqualTo(1);
|
||||
assertThat(indexData.get(0).enabled).isFalse();
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO (b/66916397) investigate why locale is attached to IndexData
|
||||
*/
|
||||
@Test
|
||||
public void testInsertRawColumn_mismatchedLocale_rowInserted() {
|
||||
final SearchIndexableRaw raw = getFakeRaw("ca-fr");
|
||||
PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(raw);
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
assertThat(indexData).hasSize(1);
|
||||
}
|
||||
|
||||
// Tests for the flow: IndexOneResource -> IndexFromResource ->
|
||||
// UpdateOneRowWithFilteredData -> UpdateOneRow
|
||||
|
||||
@Test
|
||||
public void testNullResource_NothingInserted() {
|
||||
PreIndexData preIndexData = new PreIndexData();
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
assertThat(indexData).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddResource_RowsInserted() {
|
||||
final SearchIndexableResource resource = getFakeResource(R.xml.display_settings);
|
||||
final PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(resource);
|
||||
|
||||
final List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
int numEnabled = getEnabledResultCount(indexData);
|
||||
|
||||
assertThat(numEnabled).isEqualTo(NUM_DISPLAY_ENTRIES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddResource_withNIKs_rowsInsertedDisabled() {
|
||||
final SearchIndexableResource resource = getFakeResource(R.xml.display_settings);
|
||||
Set<String> keys = new HashSet<>();
|
||||
keys.add("pref_key_1");
|
||||
keys.add("pref_key_3");
|
||||
|
||||
final PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(resource);
|
||||
preIndexData.nonIndexableKeys.put(packageName, keys);
|
||||
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
assertThat(indexData.size()).isEqualTo(NUM_DISPLAY_ENTRIES);
|
||||
assertThat(getEnabledResultCount(indexData)).isEqualTo(NUM_DISPLAY_ENTRIES - 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddResourceHeader_rowsMatch() {
|
||||
final SearchIndexableResource resource = getFakeResource(R.xml.display_settings);
|
||||
final PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(resource);
|
||||
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
final IndexData row = findIndexDataForTitle(indexData, PAGE_TITLE);
|
||||
|
||||
// Header exists
|
||||
assertThat(row).isNotNull();
|
||||
assertThat(row.spaceDelimitedKeywords).isEqualTo("keywords");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddResource_checkboxPreference_rowsMatch() {
|
||||
final SearchIndexableResource resource = getFakeResource(R.xml.display_settings);
|
||||
final PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(resource);
|
||||
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
String checkBoxSummaryOn = "summary_on";
|
||||
String checkBoxKey = "pref_key_5";
|
||||
final IndexData row = findIndexDataForTitle(indexData, TITLE_FIVE);
|
||||
|
||||
assertDisplaySetting(row, TITLE_FIVE, checkBoxSummaryOn, checkBoxKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddResource_listPreference_rowsMatch() {
|
||||
final SearchIndexableResource resource = getFakeResource(R.xml.display_settings);
|
||||
final PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(resource);
|
||||
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
String listSummary = "summary_4";
|
||||
String listKey = "pref_key_4";
|
||||
final IndexData row = findIndexDataForTitle(indexData, TITLE_FOUR);
|
||||
|
||||
assertDisplaySetting(row, TITLE_FOUR, listSummary, listKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddResource_iconAddedFromXml() {
|
||||
final SearchIndexableResource resource = getFakeResource(R.xml.display_settings);
|
||||
final PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(resource);
|
||||
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
final IndexData row = findIndexDataForTitle(indexData, TITLE_THREE);
|
||||
|
||||
assertThat(row).isNotNull();
|
||||
assertThat(row.iconResId).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResource_sameTitleForSettingAndPage_titleNotInserted() {
|
||||
final SearchIndexableResource resource = getFakeResource(R.xml.about_legal);
|
||||
final PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(resource);
|
||||
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
int numEnabled = getEnabledResultCount(indexData);
|
||||
final IndexData nonTitlePref = findIndexDataForKey(indexData, "pref_key_1");
|
||||
|
||||
assertThat(indexData.size()).isEqualTo(NUM_LEGAL_SETTINGS - 1);
|
||||
assertThat(numEnabled).isEqualTo(NUM_LEGAL_SETTINGS - 1);
|
||||
assertThat(nonTitlePref).isNotNull();
|
||||
assertThat(nonTitlePref.enabled).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourceWithoutXml_shouldNotCrash() {
|
||||
final SearchIndexableResource resource = getFakeResource(0);
|
||||
final PreIndexData preIndexData = new PreIndexData();
|
||||
preIndexData.dataToUpdate.add(resource);
|
||||
|
||||
List<IndexData> indexData = mConverter.convertPreIndexDataToIndexData(preIndexData);
|
||||
|
||||
assertThat(indexData).isEmpty();
|
||||
}
|
||||
|
||||
private void assertDisplaySetting(IndexData row, String title, String summaryOn, String key) {
|
||||
assertThat(row.normalizedTitle).isEqualTo(title);
|
||||
assertThat(row.locale).isEqualTo(localeStr);
|
||||
assertThat(row.updatedSummaryOn).isEqualTo(summaryOn);
|
||||
assertThat(row.spaceDelimitedKeywords).isEqualTo(DISPLAY_SPACE_DELIM_KEYWORDS);
|
||||
assertThat(row.screenTitle).isEqualTo(PAGE_TITLE);
|
||||
assertThat(row.className).isEqualTo(className);
|
||||
assertThat(row.enabled).isEqualTo(true);
|
||||
assertThat(row.key).isEqualTo(key);
|
||||
assertThat(row.payloadType).isEqualTo(0);
|
||||
ResultPayload unmarshalledPayload =
|
||||
ResultPayloadUtils.unmarshall(row.payload, ResultPayload.CREATOR);
|
||||
assertThat(unmarshalledPayload).isInstanceOf(ResultPayload.class);
|
||||
}
|
||||
|
||||
private SearchIndexableRaw getFakeRaw() {
|
||||
return getFakeRaw(localeStr);
|
||||
}
|
||||
|
||||
private SearchIndexableRaw getFakeRaw(String localeStr) {
|
||||
SearchIndexableRaw data = new SearchIndexableRaw(mContext);
|
||||
data.locale = new Locale(localeStr);
|
||||
data.title = title;
|
||||
data.summaryOn = summaryOn;
|
||||
data.summaryOff = summaryOff;
|
||||
data.entries = entries;
|
||||
data.keywords = keywords;
|
||||
data.screenTitle = screenTitle;
|
||||
data.className = className;
|
||||
data.packageName = packageName;
|
||||
data.iconResId = iconResId;
|
||||
data.intentAction = action;
|
||||
data.intentTargetPackage = targetPackage;
|
||||
data.intentTargetClass = targetClass;
|
||||
data.key = key;
|
||||
data.userId = userId;
|
||||
data.enabled = enabled;
|
||||
return data;
|
||||
}
|
||||
|
||||
private SearchIndexableResource getFakeResource(int xml) {
|
||||
SearchIndexableResource sir = new SearchIndexableResource(mContext);
|
||||
sir.xmlResId = xml;
|
||||
sir.className = className;
|
||||
sir.packageName = packageName;
|
||||
sir.iconResId = iconResId;
|
||||
sir.intentAction = action;
|
||||
sir.intentTargetPackage = targetPackage;
|
||||
sir.intentTargetClass = targetClass;
|
||||
sir.enabled = enabled;
|
||||
return sir;
|
||||
}
|
||||
|
||||
private static int getEnabledResultCount(List<IndexData> indexData) {
|
||||
int enabledCount = 0;
|
||||
for (IndexData data : indexData) {
|
||||
if (data.enabled) {
|
||||
enabledCount++;
|
||||
}
|
||||
}
|
||||
return enabledCount;
|
||||
}
|
||||
|
||||
private static IndexData findIndexDataForTitle(List<IndexData> indexData,
|
||||
String indexDataTitle) {
|
||||
for (IndexData row : indexData) {
|
||||
if (TextUtils.equals(row.updatedTitle, indexDataTitle)) {
|
||||
return row;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static IndexData findIndexDataForKey(List<IndexData> indexData, String indexDataKey) {
|
||||
for (IndexData row : indexData) {
|
||||
if (TextUtils.equals(row.key, indexDataKey)) {
|
||||
return row;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.search.indexing;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.android.settings.search.InlineSwitchPayload;
|
||||
import com.android.settings.search.ResultPayload;
|
||||
import com.android.settings.search.ResultPayloadUtils;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class IndexDataTest {
|
||||
|
||||
private IndexData.Builder mBuilder;
|
||||
|
||||
private static final String LOCALE = "en_US";
|
||||
private static final String TITLE = "updated-title";
|
||||
private static final String NORM_TITLE = "updatedtitle";
|
||||
private static final String SUMMARY_ON = "updated-summary-on";
|
||||
private static final String NORM_SUMMARY_ON = "updatedsummaryon";
|
||||
private static final String SUMMARY_OFF = "updated-summary-off";
|
||||
private static final String NORM_SUMMARY_OFF = "updatedsummaryoff";
|
||||
private static final String ENTRIES = "entries";
|
||||
private static final String CLASS_NAME = "class name";
|
||||
private static final String SCREEN_TITLE = "screen title";
|
||||
private static final int ICON_RES_ID = 0xff;
|
||||
private static final String SPACE_DELIMITED_KEYWORDS = "keywords";
|
||||
private static final String INTENT_ACTION = "intent action";
|
||||
private static final String INTENT_TARGET_PACKAGE = "target package";
|
||||
private static final String INTENT_TARGET_CLASS = "target class";
|
||||
private static final boolean ENABLED = true;
|
||||
private static final String KEY = "key";
|
||||
private static final int USER_ID = 1;
|
||||
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mBuilder = createBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFullRowBuild_nonNull() {
|
||||
IndexData row = generateRow();
|
||||
assertThat(row).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitivesBuild_noDataLoss() {
|
||||
IndexData row = generateRow();
|
||||
|
||||
assertThat(row.locale).isEqualTo(LOCALE);
|
||||
assertThat(row.updatedTitle).isEqualTo(TITLE);
|
||||
assertThat(row.normalizedTitle).isEqualTo(NORM_TITLE);
|
||||
assertThat(row.updatedSummaryOn).isEqualTo(SUMMARY_ON);
|
||||
assertThat(row.normalizedSummaryOn).isEqualTo(NORM_SUMMARY_ON);
|
||||
assertThat(row.entries).isEqualTo(ENTRIES);
|
||||
assertThat(row.className).isEqualTo(CLASS_NAME);
|
||||
assertThat(row.screenTitle).isEqualTo(SCREEN_TITLE);
|
||||
assertThat(row.iconResId).isEqualTo(ICON_RES_ID);
|
||||
assertThat(row.spaceDelimitedKeywords).isEqualTo(SPACE_DELIMITED_KEYWORDS);
|
||||
assertThat(row.intentAction).isEqualTo(INTENT_ACTION);
|
||||
assertThat(row.intentTargetClass).isEqualTo(INTENT_TARGET_CLASS);
|
||||
assertThat(row.intentTargetPackage).isEqualTo(INTENT_TARGET_PACKAGE);
|
||||
assertThat(row.enabled).isEqualTo(ENABLED);
|
||||
assertThat(row.userId).isEqualTo(USER_ID);
|
||||
assertThat(row.key).isEqualTo(KEY);
|
||||
assertThat(row.payloadType).isEqualTo(ResultPayload.PayloadType.INTENT);
|
||||
assertThat(row.payload).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericIntent_addedToPayload() {
|
||||
final IndexData row = generateRow();
|
||||
final ResultPayload payload =
|
||||
ResultPayloadUtils.unmarshall(row.payload, ResultPayload.CREATOR);
|
||||
final ComponentName name = payload.getIntent().getComponent();
|
||||
assertThat(name.getClassName()).isEqualTo(INTENT_TARGET_CLASS);
|
||||
assertThat(name.getPackageName()).isEqualTo(INTENT_TARGET_PACKAGE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRowWithInlinePayload_genericPayloadNotAdded() {
|
||||
final String URI = "test uri";
|
||||
final InlineSwitchPayload payload = new InlineSwitchPayload(URI, 0 /* mSettingSource */,
|
||||
1 /* onValue */, null /* intent */, true /* isDeviceSupported */, 1 /* default */);
|
||||
mBuilder.setPayload(payload);
|
||||
final IndexData row = generateRow();
|
||||
final InlineSwitchPayload unmarshalledPayload =
|
||||
ResultPayloadUtils.unmarshall(row.payload, InlineSwitchPayload.CREATOR);
|
||||
|
||||
assertThat(row.payloadType).isEqualTo(ResultPayload.PayloadType.INLINE_SWITCH);
|
||||
assertThat(unmarshalledPayload.getKey()).isEqualTo(URI);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRowWithInlinePayload_intentAddedToInlinePayload() {
|
||||
final String URI = "test uri";
|
||||
final ComponentName component =
|
||||
new ComponentName(INTENT_TARGET_PACKAGE, INTENT_TARGET_CLASS);
|
||||
final Intent intent = new Intent();
|
||||
intent.setComponent(component);
|
||||
|
||||
final InlineSwitchPayload payload = new InlineSwitchPayload(URI, 0 /* mSettingSource */,
|
||||
1 /* onValue */, intent, true /* isDeviceSupported */, 1 /* default */);
|
||||
mBuilder.setPayload(payload);
|
||||
final IndexData row = generateRow();
|
||||
final InlineSwitchPayload unmarshalledPayload = ResultPayloadUtils
|
||||
.unmarshall(row.payload, InlineSwitchPayload.CREATOR);
|
||||
final ComponentName name = unmarshalledPayload.getIntent().getComponent();
|
||||
|
||||
assertThat(name.getClassName()).isEqualTo(INTENT_TARGET_CLASS);
|
||||
assertThat(name.getPackageName()).isEqualTo(INTENT_TARGET_PACKAGE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalizeJapaneseString() {
|
||||
final String japaneseString = "\u3042\u3077\u308a";
|
||||
final String normalizedJapaneseString = "\u30a2\u30d5\u309a\u30ea";
|
||||
|
||||
String result = IndexData.normalizeJapaneseString(japaneseString);
|
||||
assertThat(result).isEqualTo(normalizedJapaneseString);
|
||||
}
|
||||
|
||||
private IndexData generateRow() {
|
||||
return mBuilder.build(mContext);
|
||||
}
|
||||
|
||||
private IndexData.Builder createBuilder() {
|
||||
mBuilder = new IndexData.Builder();
|
||||
mBuilder.setTitle(TITLE)
|
||||
.setSummaryOn(SUMMARY_ON)
|
||||
.setEntries(ENTRIES)
|
||||
.setClassName(CLASS_NAME)
|
||||
.setScreenTitle(SCREEN_TITLE)
|
||||
.setIconResId(ICON_RES_ID)
|
||||
.setKeywords(SPACE_DELIMITED_KEYWORDS)
|
||||
.setIntentAction(INTENT_ACTION)
|
||||
.setIntentTargetPackage(INTENT_TARGET_PACKAGE)
|
||||
.setIntentTargetClass(INTENT_TARGET_CLASS)
|
||||
.setEnabled(ENABLED)
|
||||
.setKey(KEY)
|
||||
.setUserId(USER_ID);
|
||||
return mBuilder;
|
||||
}
|
||||
}
|
||||
@@ -1,167 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.search.indexing;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.ProviderInfo;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.net.Uri;
|
||||
import android.provider.SearchIndexableResource;
|
||||
|
||||
import com.android.settings.search.SearchIndexableRaw;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class PreIndexDataCollectorTest {
|
||||
|
||||
private static final String AUTHORITY_ONE = "authority";
|
||||
private static final String PACKAGE_ONE = "com.android.settings";
|
||||
|
||||
@Mock
|
||||
private ContentResolver mResolver;
|
||||
|
||||
private Context mContext;
|
||||
|
||||
private PreIndexDataCollector mDataCollector;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
doReturn(mResolver).when(mContext).getContentResolver();
|
||||
|
||||
mDataCollector = spy(new PreIndexDataCollector(mContext));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectIndexableData_addsResourceData() {
|
||||
final List<ResolveInfo> providerInfo = getDummyResolveInfo();
|
||||
doReturn(true).when(mDataCollector).isWellKnownProvider(any(ResolveInfo.class));
|
||||
|
||||
List<SearchIndexableResource> resources = getFakeResource();
|
||||
doReturn(resources).when(mDataCollector).getIndexablesForXmlResourceUri(
|
||||
any(Context.class), anyString(), any(Uri.class), any(String[].class));
|
||||
|
||||
PreIndexData data =
|
||||
mDataCollector.collectIndexableData(providerInfo, true /* isFullIndex */);
|
||||
|
||||
assertThat(data.dataToUpdate).containsAllIn(resources);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectIndexableData_addsRawData() {
|
||||
final List<ResolveInfo> providerInfo = getDummyResolveInfo();
|
||||
doReturn(true).when(mDataCollector).isWellKnownProvider(any(ResolveInfo.class));
|
||||
|
||||
List<SearchIndexableRaw> rawData = getFakeRaw();
|
||||
doReturn(rawData).when(mDataCollector).getIndexablesForRawDataUri(any(Context.class),
|
||||
anyString(), any(Uri.class), any(String[].class));
|
||||
|
||||
|
||||
PreIndexData data =
|
||||
mDataCollector.collectIndexableData(providerInfo, true /* isFullIndex */);
|
||||
|
||||
assertThat(data.dataToUpdate).containsAllIn(rawData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectIndexableData_addsNonIndexables() {
|
||||
final List<ResolveInfo> providerInfo = getDummyResolveInfo();
|
||||
doReturn(true).when(mDataCollector).isWellKnownProvider(any(ResolveInfo.class));
|
||||
|
||||
List<String> niks = getFakeNonIndexables();
|
||||
|
||||
doReturn(niks).when(mDataCollector)
|
||||
.getNonIndexablesKeysFromRemoteProvider(anyString(), anyString());
|
||||
|
||||
PreIndexData data = mDataCollector.collectIndexableData(providerInfo,
|
||||
true /* isFullIndex */);
|
||||
|
||||
assertThat(data.nonIndexableKeys.get(AUTHORITY_ONE)).containsAllIn(niks);
|
||||
}
|
||||
|
||||
private List<ResolveInfo> getDummyResolveInfo() {
|
||||
List<ResolveInfo> infoList = new ArrayList<>();
|
||||
ResolveInfo info = new ResolveInfo();
|
||||
info.providerInfo = new ProviderInfo();
|
||||
info.providerInfo.exported = true;
|
||||
info.providerInfo.authority = AUTHORITY_ONE;
|
||||
info.providerInfo.packageName = PACKAGE_ONE;
|
||||
info.providerInfo.applicationInfo = new ApplicationInfo();
|
||||
infoList.add(info);
|
||||
|
||||
return infoList;
|
||||
}
|
||||
|
||||
private List<SearchIndexableResource> getFakeResource() {
|
||||
List<SearchIndexableResource> resources = new ArrayList<>();
|
||||
final String BLANK = "";
|
||||
|
||||
SearchIndexableResource sir = new SearchIndexableResource(mContext);
|
||||
sir.rank = 0;
|
||||
sir.xmlResId = 0;
|
||||
sir.className = BLANK;
|
||||
sir.packageName = BLANK;
|
||||
sir.iconResId = 0;
|
||||
sir.intentAction = BLANK;
|
||||
sir.intentTargetPackage = BLANK;
|
||||
sir.intentTargetClass = BLANK;
|
||||
sir.enabled = true;
|
||||
resources.add(sir);
|
||||
|
||||
return resources;
|
||||
}
|
||||
|
||||
private List<SearchIndexableRaw> getFakeRaw() {
|
||||
List<SearchIndexableRaw> rawData = new ArrayList<>();
|
||||
|
||||
SearchIndexableRaw data = new SearchIndexableRaw(mContext);
|
||||
data.title = "bront";
|
||||
data.key = "brint";
|
||||
rawData.add(data);
|
||||
|
||||
return rawData;
|
||||
}
|
||||
|
||||
private List<String> getFakeNonIndexables() {
|
||||
List<String> niks = new ArrayList<>();
|
||||
niks.add("they're");
|
||||
niks.add("good");
|
||||
niks.add("dogs");
|
||||
niks.add("brent");
|
||||
return niks;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user