Build slice from indexed data in SliceProvider

Connect the SliceIndexing data to the SliceProvider,
such that a query to SliceProvider can build a Slice
via the indexed data from SlicesIndexingManager.

We take the key from the Uri supplied to the SettingSliceProvider
and find a potential matching row in the indexed data. The
matched data is then used to Build a slice for the caller.

Bug: 67996923
Test: robotests
Change-Id: If51bfd1a05c3f3817ae720554f95a98fc7b002e1
This commit is contained in:
Matthew Fritze
2017-12-15 10:48:40 -08:00
parent 87f7a1be1e
commit 8c96843fe3
15 changed files with 858 additions and 14 deletions

View File

@@ -0,0 +1,52 @@
/*
* 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.
* 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.slices;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.core.TogglePreferenceController;
public class FakeToggleController extends TogglePreferenceController {
private String settingKey = "toggle_key";
private final int ON = 1;
private final int OFF = 0;
public FakeToggleController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public boolean isChecked() {
return Settings.System.getInt(mContext.getContentResolver(),
settingKey, OFF) == ON;
}
@Override
public boolean setChecked(boolean isChecked) {
return Settings.System.putInt(mContext.getContentResolver(), settingKey,
isChecked ? ON : OFF);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -0,0 +1,97 @@
/*
* 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.
* 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.slices;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContentResolver;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import com.android.settings.TestConfig;
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 androidx.app.slice.Slice;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SettingsSliceProviderTest {
private final String fakeTitle = "title";
private final String KEY = "key";
private Context mContext;
private SettingsSliceProvider mProvider;
private SQLiteDatabase mDb;
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.application);
mProvider = spy(new SettingsSliceProvider());
mDb = SlicesDatabaseHelper.getInstance(mContext).getWritableDatabase();
SlicesDatabaseHelper.getInstance(mContext).setIndexedState();
}
@After
public void cleanUp() {
DatabaseTestUtils.clearDb(mContext);
}
@Test
public void testInitialSliceReturned_emmptySlice() {
Uri uri = SettingsSliceProvider.getUri(KEY);
Slice slice = mProvider.onBindSlice(uri);
assertThat(slice.getUri()).isEqualTo(uri);
assertThat(slice.getItems()).isEmpty();
}
@Test
public void testUriBuilder_returnsValidSliceUri() {
Uri uri = SettingsSliceProvider.getUri(KEY);
assertThat(uri.getScheme()).isEqualTo(ContentResolver.SCHEME_CONTENT);
assertThat(uri.getAuthority()).isEqualTo(SettingsSliceProvider.SLICE_AUTHORITY);
assertThat(uri.getLastPathSegment()).isEqualTo(KEY);
}
private void insertSpecialCase(String key) {
ContentValues values = new ContentValues();
values.put(SlicesDatabaseHelper.IndexColumns.KEY, key);
values.put(SlicesDatabaseHelper.IndexColumns.TITLE, fakeTitle);
values.put(SlicesDatabaseHelper.IndexColumns.SUMMARY, "s");
values.put(SlicesDatabaseHelper.IndexColumns.SCREENTITLE, "s");
values.put(SlicesDatabaseHelper.IndexColumns.ICON_RESOURCE, 1234);
values.put(SlicesDatabaseHelper.IndexColumns.FRAGMENT, "test");
values.put(SlicesDatabaseHelper.IndexColumns.CONTROLLER, "test");
mDb.replaceOrThrow(SlicesDatabaseHelper.Tables.TABLE_SLICES_INDEX, null, values);
}
}

View File

@@ -0,0 +1,121 @@
/*
* 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.
* 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.slices;
import static com.google.common.truth.Truth.assertThat;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import com.android.settings.TestConfig;
import com.android.settings.search.FakeIndexProvider;
import com.android.settings.search.SearchIndexableResources;
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.Set;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SliceBroadcastReceiverTest {
private final String fakeTitle = "title";
private final String fakeSummary = "summary";
private final String fakeScreenTitle = "screen_title";
private final int fakeIcon = 1234;
private final String fakeFragmentClassName = FakeIndexProvider.class.getName();
private final String fakeControllerName = FakeToggleController.class.getName();
private Context mContext;
private SQLiteDatabase mDb;
private SliceBroadcastReceiver mReceiver;
private Set<Class> mProviderClassesCopy;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mDb = SlicesDatabaseHelper.getInstance(mContext).getWritableDatabase();
mReceiver = new SliceBroadcastReceiver();
mProviderClassesCopy = new HashSet<>(SearchIndexableResources.providerValues());
SlicesDatabaseHelper helper = SlicesDatabaseHelper.getInstance(mContext);
helper.setIndexedState();
}
@After
public void cleanUp() {
DatabaseTestUtils.clearDb(mContext);
SearchIndexableResources.providerValues().clear();
SearchIndexableResources.providerValues().addAll(mProviderClassesCopy);
}
@Test
public void testOnReceive_toggleChanged() {
String key = "key";
SearchIndexableResources.providerValues().clear();
insertSpecialCase(key);
// Turn on toggle setting
FakeToggleController fakeToggleController = new FakeToggleController(mContext, key);
fakeToggleController.setChecked(true);
Intent intent = new Intent(SettingsSliceProvider.ACTION_TOGGLE_CHANGED);
intent.putExtra(SettingsSliceProvider.EXTRA_SLICE_KEY, key);
assertThat(fakeToggleController.isChecked()).isTrue();
// Toggle setting
mReceiver.onReceive(mContext, intent);
assertThat(fakeToggleController.isChecked()).isFalse();
}
@Test(expected = IllegalStateException.class)
public void testOnReceive_noExtra_illegalSatetException() {
Intent intent = new Intent(SettingsSliceProvider.ACTION_TOGGLE_CHANGED);
mReceiver.onReceive(mContext, intent);
}
@Test(expected = IllegalStateException.class)
public void testOnReceive_emptyKey_throwsIllegalStateException() {
Intent intent = new Intent(SettingsSliceProvider.ACTION_TOGGLE_CHANGED);
intent.putExtra(SettingsSliceProvider.EXTRA_SLICE_KEY, (String) null);
mReceiver.onReceive(mContext, intent);
}
private void insertSpecialCase(String key) {
ContentValues values = new ContentValues();
values.put(SlicesDatabaseHelper.IndexColumns.KEY, key);
values.put(SlicesDatabaseHelper.IndexColumns.TITLE, fakeTitle);
values.put(SlicesDatabaseHelper.IndexColumns.SUMMARY, fakeSummary);
values.put(SlicesDatabaseHelper.IndexColumns.SCREENTITLE, fakeScreenTitle);
values.put(SlicesDatabaseHelper.IndexColumns.ICON_RESOURCE, fakeIcon);
values.put(SlicesDatabaseHelper.IndexColumns.FRAGMENT, fakeFragmentClassName);
values.put(SlicesDatabaseHelper.IndexColumns.CONTROLLER, fakeControllerName);
mDb.replaceOrThrow(SlicesDatabaseHelper.Tables.TABLE_SLICES_INDEX, null, values);
}
}

View File

@@ -0,0 +1,130 @@
/*
* 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.
* 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.slices;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import com.android.settings.TestConfig;
import com.android.settings.search.FakeIndexProvider;
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;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SlicesDatabaseAccessorTest {
private final String fakeTitle = "title";
private final String fakeSummary = "summary";
private final String fakeScreenTitle = "screen_title";
private final int fakeIcon = 1234;
private final String fakeFragmentClassName = FakeIndexProvider.class.getName();
private final String fakeControllerName = FakePreferenceController.class.getName();
private Context mContext;
private SQLiteDatabase mDb;
private SlicesDatabaseAccessor mAccessor;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mAccessor = spy(new SlicesDatabaseAccessor(mContext));
mDb = SlicesDatabaseHelper.getInstance(mContext).getWritableDatabase();
SlicesDatabaseHelper.getInstance(mContext).setIndexedState();
}
@After
public void cleanUp() {
DatabaseTestUtils.clearDb(mContext);
}
@Test
public void testGetSliceDataFromKey_validKey_validSliceReturned() {
String key = "key";
insertSpecialCase(key);
SliceData data = mAccessor.getSliceDataFromKey(key);
assertThat(data.getKey()).isEqualTo(key);
assertThat(data.getTitle()).isEqualTo(fakeTitle);
assertThat(data.getSummary()).isEqualTo(fakeSummary);
assertThat(data.getScreenTitle()).isEqualTo(fakeScreenTitle);
assertThat(data.getIconResource()).isEqualTo(fakeIcon);
assertThat(data.getFragmentClassName()).isEqualTo(fakeFragmentClassName);
assertThat(data.getUri()).isNull();
assertThat(data.getPreferenceController()).isEqualTo(fakeControllerName);
}
@Test(expected = IllegalStateException.class)
public void testGetSliceDataFromKey_invalidKey_errorThrown() {
String key = "key";
mAccessor.getSliceDataFromKey(key);
}
@Test
public void testGetSliceFromUri_validUri_validSliceReturned() {
String key = "key";
insertSpecialCase(key);
Uri uri = SettingsSliceProvider.getUri(key);
SliceData data = mAccessor.getSliceDataFromUri(uri);
assertThat(data.getKey()).isEqualTo(key);
assertThat(data.getTitle()).isEqualTo(fakeTitle);
assertThat(data.getSummary()).isEqualTo(fakeSummary);
assertThat(data.getScreenTitle()).isEqualTo(fakeScreenTitle);
assertThat(data.getIconResource()).isEqualTo(fakeIcon);
assertThat(data.getFragmentClassName()).isEqualTo(fakeFragmentClassName);
assertThat(data.getUri()).isEqualTo(uri);
assertThat(data.getPreferenceController()).isEqualTo(fakeControllerName);
}
@Test(expected = IllegalStateException.class)
public void testGetSliceFromUri_invalidUri_errorThrown() {
Uri uri = SettingsSliceProvider.getUri("durr");
mAccessor.getSliceDataFromUri(uri);
}
private void insertSpecialCase(String key) {
ContentValues values = new ContentValues();
values.put(SlicesDatabaseHelper.IndexColumns.KEY, key);
values.put(SlicesDatabaseHelper.IndexColumns.TITLE, fakeTitle);
values.put(SlicesDatabaseHelper.IndexColumns.SUMMARY, fakeSummary);
values.put(SlicesDatabaseHelper.IndexColumns.SCREENTITLE, fakeScreenTitle);
values.put(SlicesDatabaseHelper.IndexColumns.ICON_RESOURCE, fakeIcon);
values.put(SlicesDatabaseHelper.IndexColumns.FRAGMENT, fakeFragmentClassName);
values.put(SlicesDatabaseHelper.IndexColumns.CONTROLLER, fakeControllerName);
mDb.replaceOrThrow(SlicesDatabaseHelper.Tables.TABLE_SLICES_INDEX, null, values);
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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.
* 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.slices;
import static com.android.settings.TestConfig.SDK_VERSION;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.net.Uri;
import com.android.settings.TestConfig;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import androidx.app.slice.Slice;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = SDK_VERSION)
public class SlicesDatabaseUtilsTest {
private final String KEY = "KEY";
private final String TITLE = "title";
private final String SUMMARY = "summary";
private final String SCREEN_TITLE = "screen title";
private final String FRAGMENT_NAME = "fragment name";
private final int ICON = 1234; // I declare a thumb war
private final Uri URI = Uri.parse("content://com.android.settings.slices/test");
private final String PREF_CONTROLLER = FakeToggleController.class.getName();
;
private Context mContext;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
}
@Test
public void testBuildSlice_returnsMatchingSlice() {
Slice slice = SliceBuilderUtils.buildSlice(mContext, getDummyData());
assertThat(slice).isNotNull(); // TODO improve test for Slice content
}
@Test
public void testGetPreferenceController_buildsMatchingController() {
BasePreferenceController controller = SliceBuilderUtils.getPreferenceController(mContext,
getDummyData());
assertThat(controller).isInstanceOf(FakeToggleController.class);
}
private SliceData getDummyData() {
return new SliceData.Builder()
.setKey(KEY)
.setTitle(TITLE)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setIcon(ICON)
.setFragmentName(FRAGMENT_NAME)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER)
.build();
}
}