summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2017-05-03 12:42:18 -0700
committerSunny Goyal <sunnygoyal@google.com>2017-05-09 12:59:04 -0700
commit05f30889d6ba0f803c720813067b28c9c4cf2bfd (patch)
tree9e517f1ccd46bf7400172ed1f6f6026c75fbef85 /tests
parent07557e81a5a2dab455e724345095e2b4a32b7d17 (diff)
downloadandroid_packages_apps_Trebuchet-05f30889d6ba0f803c720813067b28c9c4cf2bfd.tar.gz
android_packages_apps_Trebuchet-05f30889d6ba0f803c720813067b28c9c4cf2bfd.tar.bz2
android_packages_apps_Trebuchet-05f30889d6ba0f803c720813067b28c9c4cf2bfd.zip
Adding support for DB downgrade
Adding a schema file for handling DB downgrade. This schema file is part of the backup/restore set, and hence is available on a device with lower app version. Bug: 37257575 Change-Id: I69c8ef5f28d5209be6e6679412c7459d4eeda5d0
Diffstat (limited to 'tests')
-rw-r--r--tests/res/raw/db_schema_v10.json4
-rw-r--r--tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java194
2 files changed, 198 insertions, 0 deletions
diff --git a/tests/res/raw/db_schema_v10.json b/tests/res/raw/db_schema_v10.json
new file mode 100644
index 000000000..a5e290ef7
--- /dev/null
+++ b/tests/res/raw/db_schema_v10.json
@@ -0,0 +1,4 @@
+{
+ "version" : 10,
+ "downgrade_to_9" : []
+} \ No newline at end of file
diff --git a/tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java b/tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java
new file mode 100644
index 000000000..1d9148ba1
--- /dev/null
+++ b/tests/src/com/android/launcher3/model/DbDowngradeHelperTest.java
@@ -0,0 +1,194 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.launcher3.model;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
+import static junit.framework.Assert.assertNotSame;
+import static junit.framework.Assert.assertTrue;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+
+import com.android.launcher3.LauncherProvider;
+import com.android.launcher3.LauncherProvider.DatabaseHelper;
+import com.android.launcher3.LauncherSettings.Favorites;
+import com.android.launcher3.R;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.File;
+
+/**
+ * Tests for {@link DbDowngradeHelper}
+ */
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class DbDowngradeHelperTest {
+
+ private static final String SCHEMA_FILE = "test_schema.json";
+ private static final String DB_FILE = "test.db";
+
+ private Context mContext;
+ private File mSchemaFile;
+ private File mDbFile;
+
+ @Before
+ public void setup() {
+ mContext = InstrumentationRegistry.getTargetContext();
+ mSchemaFile = mContext.getFileStreamPath(SCHEMA_FILE);
+ mDbFile = mContext.getDatabasePath(DB_FILE);
+ }
+
+ @Test
+ public void testUpdateSchemaFile() throws Exception {
+ Context myContext = InstrumentationRegistry.getContext();
+ int testResId = myContext.getResources().getIdentifier(
+ "db_schema_v10", "raw", myContext.getPackageName());
+ mSchemaFile.delete();
+ assertFalse(mSchemaFile.exists());
+
+ DbDowngradeHelper.updateSchemaFile(mSchemaFile, 10, myContext, testResId);
+ assertTrue(mSchemaFile.exists());
+ assertEquals(10, DbDowngradeHelper.parse(mSchemaFile).version);
+
+ // Schema is updated on version upgrade
+ assertTrue(mSchemaFile.setLastModified(0));
+ DbDowngradeHelper.updateSchemaFile(mSchemaFile, 11, myContext, testResId);
+ assertNotSame(0, mSchemaFile.lastModified());
+
+ // Schema is not updated when version is same
+ assertTrue(mSchemaFile.setLastModified(0));
+ DbDowngradeHelper.updateSchemaFile(mSchemaFile, 10, myContext, testResId);
+ assertEquals(0, mSchemaFile.lastModified());
+
+ // Schema is not updated on version downgrade
+ DbDowngradeHelper.updateSchemaFile(mSchemaFile, 3, myContext, testResId);
+ assertEquals(0, mSchemaFile.lastModified());
+ }
+
+ @Test
+ public void testDowngrade_success_v24() throws Exception {
+ setupTestDb();
+
+ TestOpenHelper helper = new TestOpenHelper(24);
+ assertEquals(24, helper.getReadableDatabase().getVersion());
+ helper.close();
+ }
+
+ @Test
+ public void testDowngrade_success_v22() throws Exception {
+ setupTestDb();
+
+ SQLiteOpenHelper helper = new TestOpenHelper(22);
+ assertEquals(22, helper.getWritableDatabase().getVersion());
+
+ // Check column does not exist
+ try (Cursor c = helper.getWritableDatabase().query(Favorites.TABLE_NAME,
+ null, null, null, null, null, null)) {
+ assertEquals(-1, c.getColumnIndex(Favorites.OPTIONS));
+
+ // Check data is present
+ assertEquals(10, c.getCount());
+ }
+ helper.close();
+
+ helper = new DatabaseHelper(mContext, null, DB_FILE) {
+ @Override
+ public void onOpen(SQLiteDatabase db) { }
+ };
+ assertEquals(LauncherProvider.SCHEMA_VERSION, helper.getWritableDatabase().getVersion());
+
+ try (Cursor c = helper.getWritableDatabase().query(Favorites.TABLE_NAME,
+ null, null, null, null, null, null)) {
+ // Check column exists
+ assertNotSame(-1, c.getColumnIndex(Favorites.OPTIONS));
+
+ // Check data is present
+ assertEquals(10, c.getCount());
+ }
+ helper.close();
+ }
+
+ @Test(expected = DowngradeFailException.class)
+ public void testDowngrade_fail_v20() throws Exception {
+ setupTestDb();
+
+ TestOpenHelper helper = new TestOpenHelper(20);
+ helper.getReadableDatabase().getVersion();
+ }
+
+ private void setupTestDb() throws Exception {
+ mSchemaFile.delete();
+ mDbFile.delete();
+
+ DbDowngradeHelper.updateSchemaFile(mSchemaFile, LauncherProvider.SCHEMA_VERSION, mContext,
+ R.raw.downgrade_schema);
+
+ DatabaseHelper dbHelper = new DatabaseHelper(mContext, null, DB_FILE) {
+ @Override
+ public void onOpen(SQLiteDatabase db) { }
+ };
+ // Insert dummy data
+ for (int i = 0; i < 10; i++) {
+ ContentValues values = new ContentValues();
+ values.put(Favorites._ID, i);
+ values.put(Favorites.TITLE, "title " + i);
+ dbHelper.getWritableDatabase().insert(Favorites.TABLE_NAME, null, values);
+ }
+ dbHelper.close();
+ }
+
+ private class TestOpenHelper extends SQLiteOpenHelper {
+
+ public TestOpenHelper(int version) {
+ super(mContext, DB_FILE, null, version);
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase sqLiteDatabase) {
+ throw new RuntimeException("DB should already be created");
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ throw new RuntimeException("Only downgrade supported");
+ }
+
+ @Override
+ public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ try {
+ DbDowngradeHelper.parse(mSchemaFile).onDowngrade(db, oldVersion, newVersion);
+ } catch (Exception e) {
+ throw new DowngradeFailException(e);
+ }
+ }
+ }
+
+ private static class DowngradeFailException extends RuntimeException {
+ public DowngradeFailException(Exception e) {
+ super(e);
+ }
+ }
+}