Remove DeviceIndex feature/service.

Change-Id: I8444101669aef013183842c2d13cf3960c756398
Fixes: 112587202
Test: rerun all tests
This commit is contained in:
Fan Zhang
2018-10-03 14:27:53 -07:00
parent 77f7bf675c
commit 6632529ba1
12 changed files with 11 additions and 691 deletions

View File

@@ -1,148 +0,0 @@
/*
* 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.search;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.app.job.JobScheduler;
import android.os.Binder;
import android.provider.Settings;
import com.android.settings.testutils.FakeFeatureFactory;
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.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.shadows.ShadowBinder;
@RunWith(SettingsRobolectricTestRunner.class)
public class DeviceIndexFeatureProviderTest {
@Mock
private JobScheduler mJobScheduler;
private DeviceIndexFeatureProvider mProvider;
private Activity mActivity;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowBinder.reset();
FakeFeatureFactory.setupForTest();
mActivity = spy(Robolectric.buildActivity(Activity.class).create().visible().get());
mProvider = spy(new DeviceIndexFeatureProviderImpl());
when(mActivity.getSystemService(JobScheduler.class)).thenReturn(mJobScheduler);
}
@After
public void tearDown() {
ShadowBinder.reset();
}
@Test
public void updateIndex_disabled_shouldDoNothing() {
when(mProvider.isIndexingEnabled()).thenReturn(false);
mProvider.updateIndex(mActivity, false);
verify(mJobScheduler, never()).schedule(any());
}
@Test
public void updateIndex_enabled_unprovisioned_shouldDoNothing() {
when(mProvider.isIndexingEnabled()).thenReturn(true);
Settings.Global.putInt(mActivity.getContentResolver(),
Settings.Global.DEVICE_PROVISIONED, 0);
mProvider.updateIndex(mActivity, false);
verify(mJobScheduler, never()).schedule(any());
}
@Test
public void updateIndex_enabled_provisioned_shouldIndex() {
Settings.Global.putInt(mActivity.getContentResolver(),
Settings.Global.DEVICE_PROVISIONED, 1);
when(mProvider.isIndexingEnabled()).thenReturn(true);
mProvider.updateIndex(mActivity, false);
verify(mJobScheduler).schedule(any());
}
@Test
public void updateIndex_enabled_provisioned_newBuild_shouldIndex() {
Settings.Global.putInt(mActivity.getContentResolver(),
Settings.Global.DEVICE_PROVISIONED, 1);
DeviceIndexFeatureProvider.setIndexState(mActivity);
Settings.Global.putString(mActivity.getContentResolver(),
DeviceIndexFeatureProvider.INDEX_VERSION, "new version");
Settings.Global.putString(mActivity.getContentResolver(),
DeviceIndexFeatureProvider.LANGUAGE.toString(),
DeviceIndexFeatureProvider.INDEX_LANGUAGE);
when(mProvider.isIndexingEnabled()).thenReturn(true);
mProvider.updateIndex(mActivity, false);
verify(mJobScheduler).schedule(any());
}
@Test
public void updateIndex_enabled_provisioned_differentUid_shouldNotIndex() {
Settings.Global.putInt(mActivity.getContentResolver(),
Settings.Global.DEVICE_PROVISIONED, 1);
when(mProvider.isIndexingEnabled()).thenReturn(true);
ShadowBinder.setCallingUid(Binder.getCallingUid() + 2000);
mProvider.updateIndex(mActivity, false);
verify(mJobScheduler, never()).schedule(any());
}
@Test
public void updateIndex_enabled_provisioned_newIndex_shouldIndex() {
Settings.Global.putInt(mActivity.getContentResolver(),
Settings.Global.DEVICE_PROVISIONED, 1);
DeviceIndexFeatureProvider.setIndexState(mActivity);
Settings.Global.putString(mActivity.getContentResolver(),
DeviceIndexFeatureProvider.INDEX_LANGUAGE, "new language");
when(mProvider.isIndexingEnabled()).thenReturn(true);
mProvider.updateIndex(mActivity, false);
verify(mJobScheduler).schedule(any());
}
@Test
public void updateIndex_enabled_provisioned_sameBuild_sameLang_shouldNotIndex() {
// Enabled
when(mProvider.isIndexingEnabled()).thenReturn(true);
// Provisioned
Settings.Global.putInt(mActivity.getContentResolver(),
Settings.Global.DEVICE_PROVISIONED, 1);
// Same build and same language
DeviceIndexFeatureProvider.setIndexState(mActivity);
mProvider.updateIndex(mActivity, false);
verify(mJobScheduler, never()).schedule(any());
}
}

View File

@@ -1,144 +0,0 @@
/*
* 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.search;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.ContentResolver;
import android.net.Uri;
import androidx.slice.Slice;
import androidx.slice.SliceMetadata;
import androidx.slice.SliceViewManager;
import com.android.settings.slices.SettingsSliceProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
public class DeviceIndexUpdateJobServiceTest {
private static final Uri BASE_URI = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
.build();
private Activity mActivity;
private DeviceIndexUpdateJobService mJob;
private SliceViewManager mSliceManager;
@Before
public void setup() {
FakeFeatureFactory.setupForTest();
mActivity = spy(Robolectric.buildActivity(Activity.class).create().visible().get());
mJob = spy(new DeviceIndexUpdateJobService());
mSliceManager = mock(SliceViewManager.class);
doReturn(mActivity.getPackageName()).when(mJob).getPackageName();
doReturn(mSliceManager).when(mJob).getSliceViewManager();
doNothing().when(mJob).jobFinished(null, false);
}
@Test
public void testGetsSlices() {
setSlices();
mJob.updateIndex(null);
verify(mSliceManager).getSliceDescendants(eq(BASE_URI));
}
@Test
public void testIndexesSlices() {
setSlices(genSlice("path1"), genSlice("path2"));
mJob.mRunningJob = true;
mJob.updateIndex(null);
verify(mSliceManager).getSliceDescendants(eq(BASE_URI));
DeviceIndexFeatureProvider indexFeatureProvider = FakeFeatureFactory.getFactory(mActivity)
.getDeviceIndexFeatureProvider();
verify(indexFeatureProvider, times(2)).index(any(), any(), any(), any(), any());
}
@Test
public void testDoNotIndexWithoutTitle() {
Slice testSlice = genSlice("path2");
setSlices(genSlice("path1"), testSlice);
doReturn(null).when(mJob).findTitle(testSlice, mJob.getMetadata(testSlice));
mJob.mRunningJob = true;
mJob.updateIndex(null);
verify(mSliceManager).getSliceDescendants(eq(BASE_URI));
DeviceIndexFeatureProvider indexFeatureProvider = FakeFeatureFactory.getFactory(mActivity)
.getDeviceIndexFeatureProvider();
verify(indexFeatureProvider, times(1)).index(any(), any(), any(), any(), any());
}
@Test
public void testStopIndexing() {
Slice testSlice = genSlice("path1");
setSlices(testSlice, genSlice("path2"));
mJob.mRunningJob = true;
doAnswer(invocation -> {
// Stop running after the first iteration
mJob.mRunningJob = false;
return testSlice;
}).when(mJob).bindSliceSynchronous(mSliceManager, testSlice.getUri());
mJob.updateIndex(null);
verify(mSliceManager).getSliceDescendants(eq(BASE_URI));
DeviceIndexFeatureProvider indexFeatureProvider = FakeFeatureFactory.getFactory(mActivity)
.getDeviceIndexFeatureProvider();
verify(indexFeatureProvider).clearIndex(any());
verify(indexFeatureProvider, times(1)).index(any(), any(), any(), any(), any());
}
private Slice genSlice(String path) {
return new Slice.Builder(BASE_URI.buildUpon().path(path).build()).build();
}
private void setSlices(Slice... slice) {
List<Uri> mUris = new ArrayList<>();
for (Slice slouse : slice) {
SliceMetadata m = mock(SliceMetadata.class);
mUris.add(slouse.getUri());
doReturn(slouse).when(mJob).bindSliceSynchronous(mSliceManager, slouse.getUri());
doReturn(m).when(mJob).getMetadata(slouse);
doReturn(slouse.getUri().getPath()).when(mJob).findTitle(slouse, m);
}
when(mSliceManager.getSliceDescendants(BASE_URI)).thenReturn(mUris);
}
}