1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.search; 16 17 import static org.mockito.ArgumentMatchers.any; 18 import static org.mockito.Mockito.never; 19 import static org.mockito.Mockito.spy; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.app.Activity; 24 import android.app.job.JobScheduler; 25 import android.os.Binder; 26 import android.provider.Settings; 27 28 import com.android.settings.testutils.FakeFeatureFactory; 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 31 import org.junit.After; 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.Robolectric; 38 import org.robolectric.shadows.ShadowBinder; 39 40 @RunWith(SettingsRobolectricTestRunner.class) 41 public class DeviceIndexFeatureProviderTest { 42 43 @Mock 44 private JobScheduler mJobScheduler; 45 private DeviceIndexFeatureProvider mProvider; 46 private Activity mActivity; 47 48 @Before setUp()49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 ShadowBinder.reset(); 52 FakeFeatureFactory.setupForTest(); 53 mActivity = spy(Robolectric.buildActivity(Activity.class).create().visible().get()); 54 mProvider = spy(new DeviceIndexFeatureProviderImpl()); 55 when(mActivity.getSystemService(JobScheduler.class)).thenReturn(mJobScheduler); 56 } 57 58 @After tearDown()59 public void tearDown() { 60 ShadowBinder.reset(); 61 } 62 63 @Test updateIndex_disabled_shouldDoNothing()64 public void updateIndex_disabled_shouldDoNothing() { 65 when(mProvider.isIndexingEnabled()).thenReturn(false); 66 67 mProvider.updateIndex(mActivity, false); 68 verify(mJobScheduler, never()).schedule(any()); 69 } 70 71 @Test updateIndex_enabled_unprovisioned_shouldDoNothing()72 public void updateIndex_enabled_unprovisioned_shouldDoNothing() { 73 when(mProvider.isIndexingEnabled()).thenReturn(true); 74 Settings.Global.putInt(mActivity.getContentResolver(), 75 Settings.Global.DEVICE_PROVISIONED, 0); 76 77 mProvider.updateIndex(mActivity, false); 78 79 verify(mJobScheduler, never()).schedule(any()); 80 } 81 82 @Test updateIndex_enabled_provisioned_shouldIndex()83 public void updateIndex_enabled_provisioned_shouldIndex() { 84 Settings.Global.putInt(mActivity.getContentResolver(), 85 Settings.Global.DEVICE_PROVISIONED, 1); 86 when(mProvider.isIndexingEnabled()).thenReturn(true); 87 88 mProvider.updateIndex(mActivity, false); 89 verify(mJobScheduler).schedule(any()); 90 } 91 92 @Test updateIndex_enabled_provisioned_newBuild_shouldIndex()93 public void updateIndex_enabled_provisioned_newBuild_shouldIndex() { 94 Settings.Global.putInt(mActivity.getContentResolver(), 95 Settings.Global.DEVICE_PROVISIONED, 1); 96 DeviceIndexFeatureProvider.setIndexState(mActivity); 97 Settings.Global.putString(mActivity.getContentResolver(), 98 DeviceIndexFeatureProvider.INDEX_VERSION, "new version"); 99 Settings.Global.putString(mActivity.getContentResolver(), 100 DeviceIndexFeatureProvider.LANGUAGE.toString(), 101 DeviceIndexFeatureProvider.INDEX_LANGUAGE); 102 when(mProvider.isIndexingEnabled()).thenReturn(true); 103 104 mProvider.updateIndex(mActivity, false); 105 verify(mJobScheduler).schedule(any()); 106 } 107 108 @Test updateIndex_enabled_provisioned_differentUid_shouldNotIndex()109 public void updateIndex_enabled_provisioned_differentUid_shouldNotIndex() { 110 Settings.Global.putInt(mActivity.getContentResolver(), 111 Settings.Global.DEVICE_PROVISIONED, 1); 112 when(mProvider.isIndexingEnabled()).thenReturn(true); 113 114 ShadowBinder.setCallingUid(Binder.getCallingUid() + 2000); 115 116 mProvider.updateIndex(mActivity, false); 117 verify(mJobScheduler, never()).schedule(any()); 118 } 119 120 @Test updateIndex_enabled_provisioned_newIndex_shouldIndex()121 public void updateIndex_enabled_provisioned_newIndex_shouldIndex() { 122 Settings.Global.putInt(mActivity.getContentResolver(), 123 Settings.Global.DEVICE_PROVISIONED, 1); 124 DeviceIndexFeatureProvider.setIndexState(mActivity); 125 Settings.Global.putString(mActivity.getContentResolver(), 126 DeviceIndexFeatureProvider.INDEX_LANGUAGE, "new language"); 127 128 when(mProvider.isIndexingEnabled()).thenReturn(true); 129 130 mProvider.updateIndex(mActivity, false); 131 verify(mJobScheduler).schedule(any()); 132 } 133 134 @Test updateIndex_enabled_provisioned_sameBuild_sameLang_shouldNotIndex()135 public void updateIndex_enabled_provisioned_sameBuild_sameLang_shouldNotIndex() { 136 // Enabled 137 when(mProvider.isIndexingEnabled()).thenReturn(true); 138 // Provisioned 139 Settings.Global.putInt(mActivity.getContentResolver(), 140 Settings.Global.DEVICE_PROVISIONED, 1); 141 // Same build and same language 142 DeviceIndexFeatureProvider.setIndexState(mActivity); 143 144 mProvider.updateIndex(mActivity, false); 145 146 verify(mJobScheduler, never()).schedule(any()); 147 } 148 } 149