1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.settings.slices; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 24 import android.content.ContentValues; 25 import android.content.Context; 26 import android.database.Cursor; 27 import android.database.sqlite.SQLiteDatabase; 28 import android.net.Uri; 29 30 import com.android.settings.slices.SlicesDatabaseHelper.IndexColumns; 31 import com.android.settings.testutils.DatabaseTestUtils; 32 33 import org.junit.After; 34 import org.junit.Before; 35 import org.junit.Ignore; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.RuntimeEnvironment; 40 41 import java.util.ArrayList; 42 import java.util.List; 43 44 @RunWith(RobolectricTestRunner.class) 45 public class SlicesIndexerTest { 46 47 private static final String[] KEYS = new String[]{"key1", "key2", "key3"}; 48 private static final String[] TITLES = new String[]{"title1", "title2", "title3"}; 49 private static final String SUMMARY = "subtitle"; 50 private static final String SCREEN_TITLE = "screen title"; 51 private static final String KEYWORDS = "a, b, c"; 52 private static final String FRAGMENT_NAME = "fragment name"; 53 private static final int ICON = 1234; // I declare a thumb war 54 private static final Uri URI = Uri.parse("content://com.android.settings.slices/test"); 55 private static final String PREF_CONTROLLER = "com.android.settings.slices.tester"; 56 private static final int SLICE_TYPE = SliceData.SliceType.SLIDER; 57 private static final String UNAVAILABLE_SLICE_SUBTITLE = "subtitleOfUnavailableSlice"; 58 private static final int HIGHLIGHT_MENU_KEY = 5678; // I declare a thumb war 59 60 private Context mContext; 61 62 private SlicesIndexer mManager; 63 64 65 @Before setUp()66 public void setUp() { 67 mContext = RuntimeEnvironment.application; 68 mManager = spy(new SlicesIndexer(mContext)); 69 } 70 71 @After cleanUp()72 public void cleanUp() { 73 DatabaseTestUtils.clearDb(mContext); 74 } 75 76 @Test 77 @Ignore testAlreadyIndexed_doesNotIndexAgain()78 public void testAlreadyIndexed_doesNotIndexAgain() { 79 String newKey = "newKey"; 80 String newTitle = "newTitle"; 81 SlicesDatabaseHelper.getInstance(mContext).setIndexedState(); 82 insertSpecialCase(newKey, newTitle); 83 84 // Attempt indexing - should not do anything. 85 mManager.run(); 86 87 final SQLiteDatabase db = SlicesDatabaseHelper.getInstance(mContext).getWritableDatabase(); 88 try (final Cursor cursor = db.rawQuery("SELECT * FROM slices_index", null)) { 89 cursor.moveToFirst(); 90 assertThat(cursor.getCount()).isEqualTo(1); 91 assertThat(cursor.getString(cursor.getColumnIndex(IndexColumns.KEY))).isEqualTo(newKey); 92 assertThat(cursor.getString(cursor.getColumnIndex(IndexColumns.TITLE))) 93 .isEqualTo(newTitle); 94 } finally { 95 db.close(); 96 } 97 } 98 99 @Test testInsertSliceData_indexedStateSet()100 public void testInsertSliceData_indexedStateSet() { 101 final SlicesDatabaseHelper helper = SlicesDatabaseHelper.getInstance(mContext); 102 helper.setIndexedState(); 103 doReturn(new ArrayList<SliceData>()).when(mManager).getSliceData(); 104 105 mManager.run(); 106 107 assertThat(helper.isSliceDataIndexed()).isTrue(); 108 } 109 110 @Test 111 @Ignore testInsertSliceData_nonPublicSlice_mockDataInserted()112 public void testInsertSliceData_nonPublicSlice_mockDataInserted() { 113 final List<SliceData> sliceData = getMockIndexableData(false); 114 doReturn(sliceData).when(mManager).getSliceData(); 115 116 mManager.run(); 117 118 final SQLiteDatabase db = SlicesDatabaseHelper.getInstance(mContext).getWritableDatabase(); 119 try (final Cursor cursor = db.rawQuery("SELECT * FROM slices_index", null)) { 120 assertThat(cursor.getCount()).isEqualTo(sliceData.size()); 121 122 cursor.moveToFirst(); 123 for (int i = 0; i < sliceData.size(); i++) { 124 assertThat(cursor.getString(cursor.getColumnIndex(IndexColumns.KEY))) 125 .isEqualTo(KEYS[i]); 126 assertThat(cursor.getString(cursor.getColumnIndex(IndexColumns.TITLE))) 127 .isEqualTo(TITLES[i]); 128 assertThat( 129 cursor.getString(cursor.getColumnIndex(IndexColumns.FRAGMENT))) 130 .isEqualTo(FRAGMENT_NAME); 131 assertThat(cursor.getString( 132 cursor.getColumnIndex(IndexColumns.SCREENTITLE))).isEqualTo(SCREEN_TITLE); 133 assertThat( 134 cursor.getString(cursor.getColumnIndex(IndexColumns.KEYWORDS))) 135 .isEqualTo(KEYWORDS); 136 assertThat( 137 cursor.getInt(cursor.getColumnIndex(IndexColumns.ICON_RESOURCE))) 138 .isEqualTo(ICON); 139 assertThat( 140 cursor.getString(cursor.getColumnIndex(IndexColumns.CONTROLLER))) 141 .isEqualTo(PREF_CONTROLLER); 142 assertThat(cursor.getInt(cursor.getColumnIndex(IndexColumns.SLICE_TYPE))) 143 .isEqualTo(SLICE_TYPE); 144 assertThat(cursor.getString( 145 cursor.getColumnIndex(IndexColumns.UNAVAILABLE_SLICE_SUBTITLE))) 146 .isEqualTo(UNAVAILABLE_SLICE_SUBTITLE); 147 assertThat(cursor.getInt( 148 cursor.getColumnIndex(IndexColumns.PUBLIC_SLICE))).isEqualTo(0); 149 assertThat(cursor.getInt( 150 cursor.getColumnIndex(IndexColumns.HIGHLIGHT_MENU_RESOURCE))) 151 .isEqualTo(HIGHLIGHT_MENU_KEY); 152 cursor.moveToNext(); 153 } 154 } finally { 155 db.close(); 156 } 157 } 158 159 @Test 160 @Ignore insertSliceData_publicSlice_mockDataInserted()161 public void insertSliceData_publicSlice_mockDataInserted() { 162 final List<SliceData> sliceData = getMockIndexableData(true); 163 doReturn(sliceData).when(mManager).getSliceData(); 164 165 mManager.run(); 166 167 final SQLiteDatabase db = SlicesDatabaseHelper.getInstance(mContext).getWritableDatabase(); 168 try (Cursor cursor = db.rawQuery("SELECT * FROM slices_index", null)) { 169 assertThat(cursor.getCount()).isEqualTo(sliceData.size()); 170 171 cursor.moveToFirst(); 172 for (int i = 0; i < sliceData.size(); i++) { 173 assertThat(cursor.getString(cursor.getColumnIndex(IndexColumns.KEY))) 174 .isEqualTo(KEYS[i]); 175 assertThat(cursor.getString(cursor.getColumnIndex(IndexColumns.TITLE))) 176 .isEqualTo(TITLES[i]); 177 assertThat( 178 cursor.getString(cursor.getColumnIndex(IndexColumns.FRAGMENT))) 179 .isEqualTo(FRAGMENT_NAME); 180 assertThat(cursor.getString( 181 cursor.getColumnIndex(IndexColumns.SCREENTITLE))).isEqualTo(SCREEN_TITLE); 182 assertThat( 183 cursor.getString(cursor.getColumnIndex(IndexColumns.KEYWORDS))) 184 .isEqualTo(KEYWORDS); 185 assertThat( 186 cursor.getInt(cursor.getColumnIndex(IndexColumns.ICON_RESOURCE))) 187 .isEqualTo(ICON); 188 assertThat( 189 cursor.getString(cursor.getColumnIndex(IndexColumns.CONTROLLER))) 190 .isEqualTo(PREF_CONTROLLER); 191 assertThat(cursor.getInt(cursor.getColumnIndex(IndexColumns.SLICE_TYPE))) 192 .isEqualTo(SLICE_TYPE); 193 assertThat(cursor.getString( 194 cursor.getColumnIndex(IndexColumns.UNAVAILABLE_SLICE_SUBTITLE))) 195 .isEqualTo(UNAVAILABLE_SLICE_SUBTITLE); 196 assertThat(cursor.getInt( 197 cursor.getColumnIndex(IndexColumns.PUBLIC_SLICE))).isEqualTo(1); 198 assertThat(cursor.getInt( 199 cursor.getColumnIndex(IndexColumns.HIGHLIGHT_MENU_RESOURCE))) 200 .isEqualTo(HIGHLIGHT_MENU_KEY); 201 cursor.moveToNext(); 202 } 203 } finally { 204 db.close(); 205 } 206 } 207 insertSpecialCase(String key, String title)208 private void insertSpecialCase(String key, String title) { 209 final ContentValues values = new ContentValues(); 210 values.put(IndexColumns.KEY, key); 211 values.put(IndexColumns.TITLE, title); 212 final SQLiteDatabase db = SlicesDatabaseHelper.getInstance(mContext).getWritableDatabase(); 213 db.beginTransaction(); 214 try { 215 db.replaceOrThrow(SlicesDatabaseHelper.Tables.TABLE_SLICES_INDEX, null, values); 216 db.setTransactionSuccessful(); 217 } finally { 218 db.endTransaction(); 219 } 220 db.close(); 221 } 222 getMockIndexableData(boolean isPublicSlice)223 private List<SliceData> getMockIndexableData(boolean isPublicSlice) { 224 final List<SliceData> sliceData = new ArrayList<>(); 225 final SliceData.Builder builder = new SliceData.Builder() 226 .setSummary(SUMMARY) 227 .setScreenTitle(SCREEN_TITLE) 228 .setKeywords(KEYWORDS) 229 .setFragmentName(FRAGMENT_NAME) 230 .setIcon(ICON) 231 .setUri(URI) 232 .setPreferenceControllerClassName(PREF_CONTROLLER) 233 .setSliceType(SLICE_TYPE) 234 .setUnavailableSliceSubtitle(UNAVAILABLE_SLICE_SUBTITLE) 235 .setHighlightMenuRes(HIGHLIGHT_MENU_KEY); 236 237 if (isPublicSlice) { 238 builder.setIsPublicSlice(true); 239 } 240 241 for (int i = 0; i < KEYS.length; i++) { 242 builder.setKey(KEYS[i]).setTitle(TITLES[i]); 243 sliceData.add(builder.build()); 244 } 245 246 return sliceData; 247 } 248 }