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 29 import com.android.settings.slices.SlicesDatabaseHelper.IndexColumns; 30 import com.android.settings.testutils.DatabaseTestUtils; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Ignore; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.RuntimeEnvironment; 39 40 import java.util.Locale; 41 42 @RunWith(RobolectricTestRunner.class) 43 public class SlicesDatabaseHelperTest { 44 45 private Context mContext; 46 private SlicesDatabaseHelper mSlicesDatabaseHelper; 47 private SQLiteDatabase mDatabase; 48 49 @Before setUp()50 public void setUp() { 51 mContext = RuntimeEnvironment.application; 52 mSlicesDatabaseHelper = spy(SlicesDatabaseHelper.getInstance(mContext)); 53 mDatabase = mSlicesDatabaseHelper.getWritableDatabase(); 54 } 55 56 @After cleanUp()57 public void cleanUp() { 58 DatabaseTestUtils.clearDb(mContext); 59 mDatabase.close(); 60 } 61 62 @Test 63 @Ignore testDatabaseSchema()64 public void testDatabaseSchema() { 65 Cursor cursor = mDatabase.rawQuery("SELECT * FROM slices_index", null); 66 String[] columnNames = cursor.getColumnNames(); 67 68 String[] expectedNames = { 69 IndexColumns.KEY, 70 IndexColumns.SLICE_URI, 71 IndexColumns.TITLE, 72 IndexColumns.SUMMARY, 73 IndexColumns.SCREENTITLE, 74 IndexColumns.KEYWORDS, 75 IndexColumns.ICON_RESOURCE, 76 IndexColumns.FRAGMENT, 77 IndexColumns.CONTROLLER, 78 IndexColumns.SLICE_TYPE, 79 IndexColumns.UNAVAILABLE_SLICE_SUBTITLE, 80 IndexColumns.PUBLIC_SLICE 81 }; 82 83 assertThat(columnNames).isEqualTo(expectedNames); 84 } 85 86 @Test 87 @Ignore testUpgrade_dropsOldData()88 public void testUpgrade_dropsOldData() { 89 ContentValues mockValues = getMockRow(); 90 91 mDatabase.replaceOrThrow(SlicesDatabaseHelper.Tables.TABLE_SLICES_INDEX, null, mockValues); 92 Cursor baseline = mDatabase.rawQuery("SELECT * FROM slices_index", null); 93 assertThat(baseline.getCount()).isEqualTo(1); 94 95 mSlicesDatabaseHelper.onUpgrade(mDatabase, 0, 1); 96 97 Cursor newCursor = mDatabase.rawQuery("SELECT * FROM slices_index", null); 98 assertThat(newCursor.getCount()).isEqualTo(0); 99 } 100 101 @Test 102 @Ignore testIndexState_buildAndLocaleSet()103 public void testIndexState_buildAndLocaleSet() { 104 mSlicesDatabaseHelper.reconstruct(mDatabase); 105 106 boolean baseState = mSlicesDatabaseHelper.isSliceDataIndexed(); 107 assertThat(baseState).isFalse(); 108 109 mSlicesDatabaseHelper.setIndexedState(); 110 boolean indexedState = mSlicesDatabaseHelper.isSliceDataIndexed(); 111 assertThat(indexedState).isTrue(); 112 } 113 114 @Test 115 @Ignore testLocaleChanges_newIndexingState()116 public void testLocaleChanges_newIndexingState() { 117 mSlicesDatabaseHelper.reconstruct(mDatabase); 118 mSlicesDatabaseHelper.setIndexedState(); 119 120 Locale.setDefault(new Locale("ca")); 121 122 assertThat(mSlicesDatabaseHelper.isSliceDataIndexed()).isFalse(); 123 } 124 125 @Test 126 @Ignore testBuildFingerprintChanges_newIndexingState()127 public void testBuildFingerprintChanges_newIndexingState() { 128 mSlicesDatabaseHelper.reconstruct(mDatabase); 129 mSlicesDatabaseHelper.setIndexedState(); 130 doReturn("newBuild").when(mSlicesDatabaseHelper).getBuildTag(); 131 132 assertThat(mSlicesDatabaseHelper.isSliceDataIndexed()).isFalse(); 133 } 134 getMockRow()135 private ContentValues getMockRow() { 136 final ContentValues values = new ContentValues(); 137 values.put(IndexColumns.KEY, "key"); 138 values.put(IndexColumns.TITLE, "title"); 139 values.put(IndexColumns.SUMMARY, "summary"); 140 values.put(IndexColumns.ICON_RESOURCE, 99); 141 values.put(IndexColumns.FRAGMENT, "fragmentClassName"); 142 values.put(IndexColumns.CONTROLLER, "preferenceController"); 143 144 return values; 145 } 146 }