include roboelectric test to verify db backup is created when

initializing the db for the very first time.

Bug: 141472083
Change-Id: I84d3487bb296bf445323906e8a2d3734b1aa8594
This commit is contained in:
Pinyao Ting 2020-01-16 12:29:04 -08:00
parent ba9c557108
commit 9be1cfde79
2 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,54 @@
/*
* Copyright (C) 2020 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 com.android.launcher3.LauncherSettings.Favorites.BACKUP_TABLE_NAME;
import static com.android.launcher3.provider.LauncherDbUtils.tableExists;
import static org.junit.Assert.assertTrue;
import android.database.sqlite.SQLiteDatabase;
import com.android.launcher3.util.LauncherModelHelper;
import com.android.launcher3.util.LauncherRoboTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.LooperMode;
/**
* Tests to verify backup and restore flow.
*/
@RunWith(LauncherRoboTestRunner.class)
@LooperMode(LooperMode.Mode.PAUSED)
public class BackupRestoreTest {
private LauncherModelHelper mModelHelper;
private SQLiteDatabase mDb;
@Before
public void setUp() {
mModelHelper = new LauncherModelHelper();
mDb = mModelHelper.provider.getDbWithRestoreDbTask();
}
@Test
public void testOnCreateDbIfNotExists_CreatesBackup() {
assertTrue(tableExists(mDb, BACKUP_TABLE_NAME));
}
}

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.android.launcher3.LauncherProvider;
import com.android.launcher3.provider.RestoreDbTask;
/**
* An extension of LauncherProvider backed up by in-memory database.
@ -33,6 +34,12 @@ public class TestLauncherProvider extends LauncherProvider {
return mOpenHelper.getWritableDatabase();
}
public SQLiteDatabase getDbWithRestoreDbTask() {
RestoreDbTask.setPending(getContext(), true);
super.createDbIfNotExists();
return mOpenHelper.getWritableDatabase();
}
private static class MyDatabaseHelper extends DatabaseHelper {
private final boolean mAllowLoadDefaultFavorites;