• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 package com.android.cellbroadcastservice.tests;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.database.sqlite.SQLiteDatabase;
25 import android.database.sqlite.SQLiteOpenHelper;
26 import android.provider.Telephony.CellBroadcasts;
27 import android.util.Log;
28 import com.android.cellbroadcastservice.CellBroadcastProvider;
29 import java.util.Arrays;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 
34 import androidx.test.InstrumentationRegistry;
35 
36 import org.junit.Before;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.JUnit4;
39 
40 @RunWith(JUnit4.class)
41 public class CellBroadcastDatabaseHelperTest {
42 
43     private final static String TAG = CellBroadcastDatabaseHelperTest.class.getSimpleName();
44 
45     private CellBroadcastProvider.CellBroadcastDatabaseHelper mHelper;
46     private SQLiteOpenHelper mInMemoryDbHelper; // used to give us an in-memory db
47     @Mock
48     Context mContext;
49 
50     @Before
setUp()51     public void setUp() {
52         Log.d(TAG, "setUp() +");
53         MockitoAnnotations.initMocks(this);
54         mHelper = new CellBroadcastProvider.CellBroadcastDatabaseHelper(mContext);
55         mInMemoryDbHelper = new InMemoryCellBroadcastProviderDbHelperV1();
56         Log.d(TAG, "setUp() -");
57     }
58 
59     @Test
dataBaseHelper_create()60     public void dataBaseHelper_create() {
61         Log.d(TAG, "dataBaseHelper_create");
62         SQLiteDatabase db = mInMemoryDbHelper.getWritableDatabase();
63         Cursor cursor = db.query(CellBroadcastProvider.CELL_BROADCASTS_TABLE_NAME,
64                 null, null, null, null, null, null);
65         String[] columns = cursor.getColumnNames();
66         Log.d(TAG, "cellbroadcastservice columns before upgrade: " + Arrays.toString(columns));
67         assertFalse(Arrays.asList(columns).contains(CellBroadcasts.SLOT_INDEX));
68         assertFalse(Arrays.asList(columns).contains(CellBroadcasts.LOCATION_CHECK_TIME));
69         assertFalse(Arrays.asList(columns).contains(CellBroadcasts.MESSAGE_DISPLAYED));
70         assertFalse(Arrays.asList(columns).contains(CellBroadcasts.DATA_CODING_SCHEME));
71         assertFalse(Arrays.asList(columns).contains("etws_is_primary"));
72         assertEquals(0, cursor.getCount());
73     }
74 
75     @Test
databaseHelperOnUpgrade_V1ToV4()76     public void databaseHelperOnUpgrade_V1ToV4() {
77         Log.d(TAG, "databaseHelperOnUpgrade_V1ToV4");
78         SQLiteDatabase db = mInMemoryDbHelper.getWritableDatabase();
79         // version 1 -> 4 trigger in onUpgrade
80         mHelper.onUpgrade(db, 1, CellBroadcastProvider.DATABASE_VERSION);
81         // the upgraded db must have the slot index field
82         Cursor upgradedCursor = db.query(CellBroadcastProvider.CELL_BROADCASTS_TABLE_NAME,
83                 null, null, null, null, null, null);
84         String[] upgradedColumns = upgradedCursor.getColumnNames();
85         Log.d(TAG, "cellbroadcastservice columns: " + Arrays.toString(upgradedColumns));
86         assertTrue(Arrays.asList(upgradedColumns).contains(CellBroadcasts.SLOT_INDEX));
87         assertTrue(Arrays.asList(upgradedColumns).contains(CellBroadcasts.LOCATION_CHECK_TIME));
88         assertTrue(Arrays.asList(upgradedColumns).contains(CellBroadcasts.MESSAGE_DISPLAYED));
89         assertTrue(Arrays.asList(upgradedColumns).contains(CellBroadcasts.DATA_CODING_SCHEME));
90         assertTrue(Arrays.asList(upgradedColumns).contains("etws_is_primary"));
91     }
92 
93     private static class InMemoryCellBroadcastProviderDbHelperV1 extends SQLiteOpenHelper {
94 
InMemoryCellBroadcastProviderDbHelperV1()95         public InMemoryCellBroadcastProviderDbHelperV1() {
96             super(InstrumentationRegistry.getTargetContext(),
97                     null,    // db file name is null for in-memory db
98                     null,    // CursorFactory is null by default
99                     1);      // db version is no-op for tests
100         }
101 
102         @Override
onCreate(SQLiteDatabase db)103         public void onCreate(SQLiteDatabase db) {
104             Log.d(TAG, "IN MEMORY DB CREATED");
105             // create database version 1
106             db.execSQL("CREATE TABLE " + CellBroadcastProvider.CELL_BROADCASTS_TABLE_NAME + " ("
107                     + CellBroadcasts._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
108                     + CellBroadcasts.SUBSCRIPTION_ID + " INTEGER,"
109                     + CellBroadcasts.GEOGRAPHICAL_SCOPE + " INTEGER,"
110                     + CellBroadcasts.PLMN + " TEXT,"
111                     + CellBroadcasts.LAC + " INTEGER,"
112                     + CellBroadcasts.CID + " INTEGER,"
113                     + CellBroadcasts.SERIAL_NUMBER + " INTEGER,"
114                     + CellBroadcasts.SERVICE_CATEGORY + " INTEGER,"
115                     + CellBroadcasts.LANGUAGE_CODE + " TEXT,"
116                     + CellBroadcasts.MESSAGE_BODY + " TEXT,"
117                     + CellBroadcasts.MESSAGE_FORMAT + " INTEGER,"
118                     + CellBroadcasts.MESSAGE_PRIORITY + " INTEGER,"
119                     + CellBroadcasts.ETWS_WARNING_TYPE + " INTEGER,"
120                     + CellBroadcasts.CMAS_MESSAGE_CLASS + " INTEGER,"
121                     + CellBroadcasts.CMAS_CATEGORY + " INTEGER,"
122                     + CellBroadcasts.CMAS_RESPONSE_TYPE + " INTEGER,"
123                     + CellBroadcasts.CMAS_SEVERITY + " INTEGER,"
124                     + CellBroadcasts.CMAS_URGENCY + " INTEGER,"
125                     + CellBroadcasts.CMAS_CERTAINTY + " INTEGER,"
126                     + CellBroadcasts.RECEIVED_TIME + " BIGINT,"
127                     + CellBroadcasts.MESSAGE_BROADCASTED + " BOOLEAN DEFAULT 0,"
128                     + CellBroadcasts.GEOMETRIES + " TEXT,"
129                     + CellBroadcasts.MAXIMUM_WAIT_TIME + " INTEGER);");
130         }
131 
132         @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)133         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
134         }
135     }
136 }
137 
138