Remove some dead code from search

Test: rebuild
Change-Id: If736f58c546fa9eafc405525c11640ca2576d446
This commit is contained in:
Fan Zhang
2018-02-07 16:56:28 -08:00
parent 3d38ab00cb
commit d75baf7e85
6 changed files with 9 additions and 902 deletions

View File

@@ -1,213 +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.android.settings.search.DatabaseResultLoader.BASE_RANKS;
import static com.android.settings.search.SearchResult.TOP_RANK;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.BadParcelableException;
import android.text.TextUtils;
import android.util.Log;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Controller to Build search results from {@link Cursor} Objects.
*
* Each converted {@link Cursor} has the following fields:
* - String Title
* - String Summary
* - int rank
* - {@link Drawable} icon
* - {@link ResultPayload} payload
*/
public class CursorToSearchResultConverter {
private static final String TAG = "CursorConverter";
/**
* These indices are used to match the columns of the this loader's SELECT statement.
* These are not necessarily the same order nor similar coverage as the schema defined in
* IndexDatabaseHelper
*/
public static final int COLUMN_INDEX_ID = 0;
public static final int COLUMN_INDEX_TITLE = 1;
public static final int COLUMN_INDEX_SUMMARY_ON = 2;
public static final int COLUMN_INDEX_SUMMARY_OFF = 3;
public static final int COLUMN_INDEX_CLASS_NAME = 4;
public static final int COLUMN_INDEX_SCREEN_TITLE = 5;
public static final int COLUMN_INDEX_ICON = 6;
public static final int COLUMN_INDEX_INTENT_ACTION = 7;
public static final int COLUMN_INDEX_INTENT_ACTION_TARGET_PACKAGE = 8;
public static final int COLUMN_INDEX_INTENT_ACTION_TARGET_CLASS = 9;
public static final int COLUMN_INDEX_KEY = 10;
public static final int COLUMN_INDEX_PAYLOAD_TYPE = 11;
public static final int COLUMN_INDEX_PAYLOAD = 12;
private final Context mContext;
private final int LONG_TITLE_LENGTH = 20;
private static final String[] whiteList = {
"main_toggle_wifi",
"main_toggle_bluetooth",
"main_toggle_bluetooth_obsolete",
"toggle_airplane",
"tether_settings",
"battery_saver",
"toggle_nfc",
"restrict_background",
"data_usage_enable",
"button_roaming_key",
};
private static final Set<String> prioritySettings = new HashSet(Arrays.asList(whiteList));
public CursorToSearchResultConverter(Context context) {
mContext = context;
}
public Set<SearchResult> convertCursor(Cursor cursorResults, int baseRank) {
if (cursorResults == null) {
return null;
}
final Map<String, Context> contextMap = new HashMap<>();
final Set<SearchResult> results = new HashSet<>();
while (cursorResults.moveToNext()) {
SearchResult result = buildSingleSearchResultFromCursor(contextMap, cursorResults,
baseRank);
if (result != null) {
results.add(result);
}
}
return results;
}
public static ResultPayload getUnmarshalledPayload(byte[] marshalledPayload,
int payloadType) {
try {
switch (payloadType) {
case ResultPayload.PayloadType.INTENT:
return ResultPayloadUtils.unmarshall(marshalledPayload,
ResultPayload.CREATOR);
case ResultPayload.PayloadType.INLINE_SWITCH:
return ResultPayloadUtils.unmarshall(marshalledPayload,
InlineSwitchPayload.CREATOR);
case ResultPayload.PayloadType.INLINE_LIST:
return ResultPayloadUtils.unmarshall(marshalledPayload,
InlineListPayload.CREATOR);
}
} catch (BadParcelableException e) {
Log.w(TAG, "Error creating parcelable: " + e);
}
return null;
}
private SearchResult buildSingleSearchResultFromCursor(Map<String, Context> contextMap,
Cursor cursor, int baseRank) {
final int docId = cursor.getInt(COLUMN_INDEX_ID);
final String pkgName = cursor.getString(COLUMN_INDEX_INTENT_ACTION_TARGET_PACKAGE);
final String title = cursor.getString(COLUMN_INDEX_TITLE);
final String summaryOn = cursor.getString(COLUMN_INDEX_SUMMARY_ON);
final String className = cursor.getString(COLUMN_INDEX_CLASS_NAME);
final String key = cursor.getString(COLUMN_INDEX_KEY);
final String iconResStr = cursor.getString(COLUMN_INDEX_ICON);
final int payloadType = cursor.getInt(COLUMN_INDEX_PAYLOAD_TYPE);
final byte[] marshalledPayload = cursor.getBlob(COLUMN_INDEX_PAYLOAD);
final ResultPayload payload = getUnmarshalledPayload(marshalledPayload, payloadType);
final int rank = getRank(title, baseRank, key);
final SearchResult.Builder builder = new SearchResult.Builder()
.setStableId(docId)
.setTitle(title)
.setSummary(summaryOn)
.setRank(rank)
.setIcon(getIconForPackage(contextMap, pkgName, className, iconResStr))
.setPayload(payload);
return builder.build();
}
private Drawable getIconForPackage(Map<String, Context> contextMap, String pkgName,
String className, String iconResStr) {
final int iconId = TextUtils.isEmpty(iconResStr)
? 0 : Integer.parseInt(iconResStr);
Drawable icon;
Context packageContext;
if (iconId == 0) {
icon = null;
} else {
if (TextUtils.isEmpty(className) && !TextUtils.isEmpty(pkgName)) {
packageContext = contextMap.get(pkgName);
if (packageContext == null) {
try {
packageContext = mContext.createPackageContext(pkgName, 0);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Cannot create Context for package: " + pkgName);
return null;
}
contextMap.put(pkgName, packageContext);
}
} else {
packageContext = mContext;
}
try {
icon = packageContext.getDrawable(iconId);
} catch (Resources.NotFoundException nfe) {
icon = null;
}
}
return icon;
}
/** Uses the breadcrumbs to determine the offset to the base rank.
* There are three checks
* A) If the result is prioritized and the highest base level
* B) If the query matches the highest level menu title
* C) If the query is longer than 20
*
* If the query matches A, set it to TOP_RANK
* If the query matches B, the offset is 0.
* If the query matches C, the offset is 1
* @param title of the result.
* @param baseRank of the result. Lower if it's a better result.
* @return
*/
private int getRank(String title, int baseRank, String key) {
// The result can only be prioritized if it is a top ranked result.
if (prioritySettings.contains(key) && baseRank < BASE_RANKS[1]) {
return TOP_RANK;
}
if (title.length() > LONG_TITLE_LENGTH) {
return baseRank + 1;
}
return baseRank;
}
}

View File

@@ -18,10 +18,10 @@
package com.android.settings.search;
import static com.android.settings.search.CursorToSearchResultConverter.COLUMN_INDEX_ID;
import static com.android.settings.search.CursorToSearchResultConverter
import static com.android.settings.search.DatabaseResultLoader.COLUMN_INDEX_ID;
import static com.android.settings.search.DatabaseResultLoader
.COLUMN_INDEX_INTENT_ACTION_TARGET_PACKAGE;
import static com.android.settings.search.CursorToSearchResultConverter.COLUMN_INDEX_KEY;
import static com.android.settings.search.DatabaseResultLoader.COLUMN_INDEX_KEY;
import static com.android.settings.search.DatabaseResultLoader.SELECT_COLUMNS;
import static com.android.settings.search.IndexDatabaseHelper.IndexColumns.CLASS_NAME;
import static com.android.settings.search.IndexDatabaseHelper.IndexColumns.DATA_ENTRIES;

View File

@@ -43,14 +43,11 @@ public class DatabaseResultLoader {
};
/**
* Base ranks defines the best possible rank based on what the query matches.
* If the query matches the prefix of the first word in the title, the best rank it can be
* is 1
* If the query matches the prefix of the other words in the title, the best rank it can be
* is 3
* If the query only matches the summary, the best rank it can be is 7
* If the query only matches keywords or entries, the best rank it can be is 9
* These indices are used to match the columns of the this loader's SELECT statement.
* These are not necessarily the same order nor similar coverage as the schema defined in
* IndexDatabaseHelper
*/
public static final int[] BASE_RANKS = {1, 3, 7, 9};
public static final int COLUMN_INDEX_ID = 0;
public static final int COLUMN_INDEX_INTENT_ACTION_TARGET_PACKAGE = 8;
public static final int COLUMN_INDEX_KEY = 10;
}

View File

@@ -1,186 +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 android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import java.util.List;
/**
* Data class as an interface for all Search Results.
*/
public class SearchResult implements Comparable<SearchResult> {
private static final String TAG = "SearchResult";
/**
* Defines the lowest rank for a search result to be considered as ranked. Results with ranks
* higher than this have no guarantee for sorting order.
*/
public static final int BOTTOM_RANK = 10;
/**
* Defines the highest rank for a search result. Used for special search results only.
*/
public static final int TOP_RANK = 0;
/**
* The title of the result and main text displayed.
* Intent Results: Displays as the primary
*/
public final CharSequence title;
/**
* Summary / subtitle text
* Intent Results: Displays the text underneath the title
*/
final public CharSequence summary;
/**
* An ordered list of the information hierarchy.
* Intent Results: Displayed a hierarchy of selections to reach the setting from the home screen
*/
public final List<String> breadcrumbs;
/**
* A suggestion for the ranking of the result.
* Based on Settings Rank:
* 1 is a near perfect match
* 9 is the weakest match
* TODO subject to change
*/
public final int rank;
/**
* Identifier for the recycler view adapter.
*/
@ResultPayload.PayloadType
public final int viewType;
/**
* Metadata for the specific result types.
*/
public final ResultPayload payload;
/**
* Result's icon.
*/
public final Drawable icon;
/**
* Stable id for this object.
*/
public final int stableId;
protected SearchResult(Builder builder) {
stableId = builder.mStableId;
title = builder.mTitle;
summary = builder.mSummary;
breadcrumbs = builder.mBreadcrumbs;
rank = builder.mRank;
icon = builder.mIcon;
payload = builder.mResultPayload;
viewType = payload.getType();
}
@Override
public int compareTo(SearchResult searchResult) {
if (searchResult == null) {
return -1;
}
return this.rank - searchResult.rank;
}
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (!(that instanceof SearchResult)) {
return false;
}
return this.stableId == ((SearchResult) that).stableId;
}
@Override
public int hashCode() {
return stableId;
}
public static class Builder {
protected CharSequence mTitle;
protected CharSequence mSummary;
protected List<String> mBreadcrumbs;
protected int mRank = 42;
protected ResultPayload mResultPayload;
protected Drawable mIcon;
protected int mStableId;
public Builder setTitle(CharSequence title) {
mTitle = title;
return this;
}
public Builder setSummary(CharSequence summary) {
mSummary = summary;
return this;
}
public Builder addBreadcrumbs(List<String> breadcrumbs) {
mBreadcrumbs = breadcrumbs;
return this;
}
public Builder setRank(int rank) {
if (rank >= 0 && rank <= 9) {
mRank = rank;
}
return this;
}
public Builder setIcon(Drawable icon) {
mIcon = icon;
return this;
}
public Builder setPayload(ResultPayload payload) {
mResultPayload = payload;
return this;
}
public Builder setStableId(int stableId) {
mStableId = stableId;
return this;
}
public SearchResult build() {
// Check that all of the mandatory fields are set.
if (TextUtils.isEmpty(mTitle)) {
throw new IllegalStateException("SearchResult missing title argument");
} else if (mStableId == 0) {
Log.v(TAG, "No stable ID on SearchResult with title: " + mTitle);
throw new IllegalStateException("SearchResult missing stableId argument");
} else if (mResultPayload == null) {
throw new IllegalStateException("SearchResult missing Payload argument");
}
return new SearchResult(this);
}
}
}

View File

@@ -1,347 +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.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.drawable.Drawable;
import com.android.settings.DisplaySettings;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.gestures.SwipeToNotificationSettings;
import com.android.settings.search.ResultPayload.Availability;
import com.android.settings.search.ResultPayload.PayloadType;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.wifi.WifiSettings;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class CursorToSearchResultConverterTest {
private static final List<String> TITLES = Arrays.asList("title1", "title2", "title3");
private static final String SUMMARY = "summary";
private static final String TARGET_PACKAGE = "a.b.c";
private static final String TARGET_CLASS = "a.b.c.class";
private static final String KEY = "key";
private static final int ICON = R.drawable.ic_search_24dp;
private static final int BASE_RANK = 1;
private static Intent sIntent;
@BeforeClass
public static void beforeClass() {
sIntent = new Intent("com.android.settings");
}
private Drawable mDrawable;
private CursorToSearchResultConverter mConverter;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
Context context = Robolectric.buildActivity(Activity.class).get();
mDrawable = context.getDrawable(ICON);
mConverter = new CursorToSearchResultConverter(context);
}
@Test
public void testParseCursor_MatchesIcon() {
final MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS);
final byte[] payload = ResultPayloadUtils.marshall(new ResultPayload(sIntent));
final String BLANK = "";
cursor.addRow(new Object[] {
KEY.hashCode(), // Doc ID
"Longer than 20 characters", // Title
SUMMARY, // Summary on
SUMMARY, // summary off
DisplaySettings.class.getName(),
BLANK, // screen title
ICON, // icon
BLANK, // action
null, // target package
BLANK, // target class
KEY, // Key
PayloadType.INTENT, // Payload Type
payload // Payload
});
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
Drawable resultDrawable = result.icon;
assertThat(resultDrawable).isNotNull();
assertThat(resultDrawable.toString()).isEqualTo(mDrawable.toString());
}
}
@Test
public void testParseCursor_NoIcon() {
final Set<SearchResult> results = mConverter.convertCursor(
getDummyCursor("noIcon" /* key */, "" /* className */), BASE_RANK);
for (SearchResult result : results) {
assertThat(result.icon).isNull();
}
}
@Test
public void testParseCursor_MatchesPayloadType() {
final Set<SearchResult> results = mConverter.convertCursor(getDummyCursor(), BASE_RANK);
ResultPayload payload;
for (SearchResult result : results) {
payload = result.payload;
assertThat(payload.getType()).isEqualTo(PayloadType.INTENT);
}
}
@Test
public void testLongTitle_PenalizedInRank() {
final MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS);
final byte[] payload = ResultPayloadUtils.marshall(new ResultPayload(sIntent));
final String BLANK = "";
cursor.addRow(new Object[] {
KEY.hashCode(), // Doc ID
"Longer than 20 characters", // Title
SUMMARY, // Summary on
SUMMARY, // summary off
DisplaySettings.class.getName(),
BLANK, // screen title
null, // icon
BLANK, // action
null, // target package
BLANK, // target class
KEY, // Key
PayloadType.INTENT, // Payload Type
payload // Payload
});
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
assertThat(result.rank).isEqualTo(BASE_RANK + 1);
}
}
@Test
public void testParseCursor_MatchesResultPayload() {
final Set<SearchResult> results = mConverter.convertCursor(getDummyCursor(), BASE_RANK);
ResultPayload payload;
for (SearchResult result : results) {
payload = result.payload;
Intent intent = payload.getIntent();
assertThat(intent.getAction()).isEqualTo(sIntent.getAction());
}
}
@Test
public void testParseCursor_MatchesInlineSwitchPayload() {
MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS);
final String BLANK = "";
final String uri = "test.com";
final int type = ResultPayload.PayloadType.INLINE_SWITCH;
final int source = ResultPayload.SettingsSource.SECURE;
final String intentKey = "key";
final String intentVal = "value";
final Intent intent = new Intent();
intent.putExtra(intentKey, intentVal);
final InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, 1 /* onValue */,
intent, true /* isDeviceSupported */, 0 /* defautValue */);
cursor.addRow(new Object[] {
KEY.hashCode(), // Doc ID
TITLES.get(0), // Title
SUMMARY, // Summary on
SUMMARY, // summary off
SwipeToNotificationSettings.class.getName(),
BLANK, // screen title
null, // icon
BLANK, // action
null, // target package
BLANK, // target class
KEY, // Key
type, // Payload Type
ResultPayloadUtils.marshall(payload) // Payload
});
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
final InlineSwitchPayload newPayload = (InlineSwitchPayload) result.payload;
final Intent rebuiltIntent = newPayload.getIntent();
assertThat(newPayload.getKey()).isEqualTo(uri);
assertThat(newPayload.getType()).isEqualTo(type);
assertThat(newPayload.mSettingSource).isEqualTo(source);
assertThat(newPayload.isStandard()).isTrue();
assertThat(newPayload.getAvailability()).isEqualTo(Availability.AVAILABLE);
assertThat(rebuiltIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
}
}
// The following tests are temporary, and should be removed when we replace the Search
// White-list solution for elevating ranking.
@Test
public void testWifiKey_PrioritizedResult() {
final String key = "main_toggle_wifi";
final Cursor cursor = getDummyCursor(key, WifiSettings.class.getName());
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
assertThat(result.rank).isEqualTo(SearchResult.TOP_RANK);
}
}
@Test
public void testBluetoothKey_PrioritizedResult() {
final String key = "main_toggle_bluetooth";
final Cursor cursor = getDummyCursor(key, WifiSettings.class.getName());
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
assertThat(result.rank).isEqualTo(SearchResult.TOP_RANK);
}
}
@Test
public void testAirplaneKey_PrioritizedResult() {
final String key = "toggle_airplane";
final Cursor cursor = getDummyCursor(key, WifiSettings.class.getName());
Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
assertThat(result.rank).isEqualTo(SearchResult.TOP_RANK);
}
}
@Test
public void testHotspotKey_PrioritizedResult() {
final String key = "tether_settings";
final Cursor cursor = getDummyCursor(key, WifiSettings.class.getName());
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
assertThat(result.rank).isEqualTo(SearchResult.TOP_RANK);
}
}
@Test
public void testBatterySaverKey_PrioritizedResult() {
final String key = "battery_saver";
final Cursor cursor = getDummyCursor(key, WifiSettings.class.getName());
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
assertThat(result.rank).isEqualTo(SearchResult.TOP_RANK);
}
}
@Test
public void testNFCKey_PrioritizedResult() {
final String key = "toggle_nfc";
final Cursor cursor = getDummyCursor(key, WifiSettings.class.getName());
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
assertThat(result.rank).isEqualTo(SearchResult.TOP_RANK);
}
}
@Test
public void testDataSaverKey_PrioritizedResult() {
final String key = "restrict_background";
final Cursor cursor = getDummyCursor(key, WifiSettings.class.getName());
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
assertThat(result.rank).isEqualTo(SearchResult.TOP_RANK);
}
}
@Test
public void testDataUsageKey_PrioritizedResult() {
final String key = "data_usage_enable";
final Cursor cursor = getDummyCursor(key, WifiSettings.class.getName());
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
assertThat(result.rank).isEqualTo(SearchResult.TOP_RANK);
}
}
@Test
public void testRoamingKey_PrioritizedResult() {
final String key = "button_roaming_key";
final Cursor cursor = getDummyCursor(key, WifiSettings.class.getName());
final Set<SearchResult> results = mConverter.convertCursor(cursor, BASE_RANK);
for (SearchResult result : results) {
assertThat(result.rank).isEqualTo(SearchResult.TOP_RANK);
}
}
// End of temporary tests
private MatrixCursor getDummyCursor() {
String[] keys = new String[] {KEY + "1", KEY + "2", KEY + "3"};
return getDummyCursor(keys, "" /* className */);
}
private MatrixCursor getDummyCursor(String key, String className) {
String[] keys = new String[] {key};
return getDummyCursor(keys, className);
}
private MatrixCursor getDummyCursor(String[] keys, String className) {
MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS);
final String BLANK = "";
final byte[] payload = ResultPayloadUtils.marshall(new ResultPayload(sIntent));
for (int i = 0; i < keys.length; i++) {
ArrayList<Object> item = new ArrayList<>(DatabaseResultLoader.SELECT_COLUMNS.length);
item.add(keys[i].hashCode()); // Doc ID
item.add(TITLES.get(i)); // Title
item.add(SUMMARY); // Summary on
item.add(BLANK); // summary off
item.add(className); // classname
item.add(BLANK); // screen title
item.add(null); // Icon
item.add(sIntent.getAction()); // Intent action
item.add(TARGET_PACKAGE); // target package
item.add(TARGET_CLASS); // target class
item.add(keys[i]); // Key
item.add(Integer.toString(0)); // Payload Type
item.add(payload); // Payload
cursor.addRow(item);
}
return cursor;
}
}

View File

@@ -1,144 +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 android.content.Intent;
import android.graphics.drawable.Drawable;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.search.SearchResult.Builder;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SearchResultBuilderTest {
private static final String TITLE = "title";
private static final String SUMMARY = "summary";
private Builder mBuilder;
private ArrayList<String> mBreadcrumbs;
private int mRank;
private ResultPayload mResultPayload;
private Drawable mIcon;
@Before
public void setUp() {
mBuilder = new Builder();
mBreadcrumbs = new ArrayList<>();
mRank = 3;
mResultPayload = new ResultPayload(new Intent());
final Context context = ShadowApplication.getInstance().getApplicationContext();
mIcon = context.getDrawable(R.drawable.ic_search_24dp);
}
@Test
public void testAllInfo_BuildSearchResult() {
mBuilder.setTitle(TITLE)
.setSummary(SUMMARY)
.setRank(mRank)
.addBreadcrumbs(mBreadcrumbs)
.setIcon(mIcon)
.setPayload(mResultPayload)
.setStableId(1);
SearchResult result = mBuilder.build();
assertThat(result).isNotNull();
assertThat(result.title).isEqualTo(TITLE);
assertThat(result.summary).isEqualTo(SUMMARY);
assertThat(result.rank).isEqualTo(mRank);
assertThat(result.breadcrumbs).isEqualTo(mBreadcrumbs);
assertThat(result.icon).isEqualTo(mIcon);
assertThat(result.payload).isEqualTo(mResultPayload);
}
@Test(expected = IllegalStateException.class)
public void testNoStableId_BuildSearchResultException() {
mBuilder.setTitle(TITLE)
.setSummary(SUMMARY)
.setRank(mRank)
.addBreadcrumbs(mBreadcrumbs)
.setIcon(mIcon)
.setPayload(mResultPayload);
mBuilder.build();
}
@Test(expected = IllegalStateException.class)
public void testNoTitle_BuildSearchResultException() {
mBuilder.setSummary(SUMMARY)
.setRank(mRank)
.addBreadcrumbs(mBreadcrumbs)
.setIcon(mIcon)
.setPayload(mResultPayload)
.setStableId(1);
mBuilder.build();
}
@Test
public void testNoRank_BuildSearchResult_pass() {
mBuilder.setTitle(TITLE)
.setSummary(SUMMARY)
.addBreadcrumbs(mBreadcrumbs)
.setIcon(mIcon)
.setPayload(mResultPayload)
.setStableId(1);
assertThat(mBuilder.build()).isNotNull();
}
@Test
public void testNoIcon_BuildSearchResult_pass() {
mBuilder.setTitle(TITLE)
.setSummary(SUMMARY)
.setRank(mRank)
.addBreadcrumbs(mBreadcrumbs)
.setPayload(mResultPayload)
.setStableId(1);
assertThat(mBuilder.build()).isNotNull();
}
@Test(expected = IllegalStateException.class)
public void testNoPayload_BuildSearchResultException() {
mBuilder.setTitle(TITLE)
.setSummary(SUMMARY)
.setRank(mRank)
.addBreadcrumbs(mBreadcrumbs)
.setIcon(mIcon)
.setStableId(1);
mBuilder.build();
}
}