Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9be1cfde authored by Pinyao Ting's avatar Pinyao Ting
Browse files

include roboelectric test to verify db backup is created when

initializing the db for the very first time.

Bug: 141472083
Change-Id: I84d3487bb296bf445323906e8a2d3734b1aa8594
parent ba9c5571
Loading
Loading
Loading
Loading
+54 −0
Original line number Diff line number Diff line
/*
 * 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));
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -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;