|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Alex Odorico <alex.odorico03@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free Software |
| 6 | + * Foundation; either version 3 of the License, or (at your option) any later |
| 7 | + * version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 12 | + * details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along with |
| 15 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.ichi2.anki.dialogs |
| 19 | + |
| 20 | +import androidx.test.core.app.ActivityScenario |
| 21 | +import androidx.test.espresso.Espresso.onView |
| 22 | +import androidx.test.espresso.assertion.ViewAssertions.matches |
| 23 | +import androidx.test.espresso.matcher.RootMatchers.isDialog |
| 24 | +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed |
| 25 | +import androidx.test.espresso.matcher.ViewMatchers.withText |
| 26 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 27 | +import com.ichi2.anki.CollectionHelper.getCurrentAnkiDroidDirectory |
| 28 | +import com.ichi2.anki.CollectionManager |
| 29 | +import com.ichi2.anki.DeckPicker |
| 30 | +import com.ichi2.anki.R |
| 31 | +import com.ichi2.anki.tests.InstrumentedTest |
| 32 | +import com.ichi2.anki.tests.Shared |
| 33 | +import com.ichi2.anki.testutil.discardPreliminaryViews |
| 34 | +import org.junit.After |
| 35 | +import org.junit.Assert |
| 36 | +import org.junit.Before |
| 37 | +import org.junit.Test |
| 38 | +import org.junit.runner.RunWith |
| 39 | +import timber.log.Timber |
| 40 | +import java.io.File |
| 41 | + |
| 42 | +@RunWith(AndroidJUnit4::class) |
| 43 | +class DatabaseCorruptDialogTest : InstrumentedTest() { |
| 44 | + private lateinit var targetColFile: File |
| 45 | + private lateinit var corruptColFile: File |
| 46 | + |
| 47 | + @Before |
| 48 | + fun setup() { |
| 49 | + val dbDir = getCurrentAnkiDroidDirectory(testContext) |
| 50 | + targetColFile = File(dbDir, "collection.anki2") |
| 51 | + corruptColFile = Shared.getTestFile(testContext, "initial_version_2_12_1_corrupt_regular.anki2") |
| 52 | + |
| 53 | + corruptColFile.copyTo(targetColFile, overwrite = true) |
| 54 | + Timber.i("Copied %d bytes to target", targetColFile.length()) |
| 55 | + |
| 56 | + ActivityScenario.launch(DeckPicker::class.java) |
| 57 | + discardPreliminaryViews() |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun testCorruptDialogFlagSet() { |
| 62 | + Assert.assertTrue(DatabaseErrorDialog.databaseCorruptFlag) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun testOpenCollectionFailedDialog() { |
| 67 | + onView(withText(R.string.open_collection_failed_title)) |
| 68 | + .inRoot(isDialog()) |
| 69 | + .check(matches(isDisplayed())) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun testCorruptCollectionDialog() { |
| 74 | + val corruptMsg = |
| 75 | + testContext.getString( |
| 76 | + R.string.corrupt_db_message, |
| 77 | + testContext.getString(R.string.repair_deck), |
| 78 | + ) |
| 79 | + onView(withText(corruptMsg)) |
| 80 | + .inRoot(isDialog()) |
| 81 | + .check(matches(isDisplayed())) |
| 82 | + } |
| 83 | + |
| 84 | + @After |
| 85 | + fun cleanupCorruptColFile() { |
| 86 | + CollectionManager.closeCollectionBlocking() |
| 87 | + if (!testContext.deleteDatabase(targetColFile.path)) { |
| 88 | + Timber.e("Cleanup: Failed to delete %s", targetColFile.path) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments