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