|
| 1 | +package com.openless.app |
| 2 | + |
| 3 | +import android.content.ContextWrapper |
| 4 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 5 | +import java.io.File |
| 6 | +import org.json.JSONArray |
| 7 | +import org.junit.After |
| 8 | +import org.junit.Assert.assertEquals |
| 9 | +import org.junit.Assert.assertFalse |
| 10 | +import org.junit.Assert.assertTrue |
| 11 | +import org.junit.Before |
| 12 | +import org.junit.Rule |
| 13 | +import org.junit.Test |
| 14 | +import org.junit.rules.TemporaryFolder |
| 15 | +import org.junit.runner.RunWith |
| 16 | + |
| 17 | +@RunWith(AndroidJUnit4::class) |
| 18 | +class OpenLessClipboardHistoryInstrumentedTest { |
| 19 | + @get:Rule val temporaryFolder = TemporaryFolder() |
| 20 | + private lateinit var directory: File |
| 21 | + private lateinit var context: ContextWrapper |
| 22 | + private val history get() = File(directory, "openless_clipboard_history.json") |
| 23 | + private val staging get() = File(directory, "openless_clipboard_history.json.tmp") |
| 24 | + |
| 25 | + @Before fun setUp() { |
| 26 | + directory = temporaryFolder.newFolder("clipboard") |
| 27 | + context = object : ContextWrapper(null) { |
| 28 | + override fun getFilesDir(): File = directory |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @After fun tearDown() { |
| 33 | + unblock(staging) |
| 34 | + unblock(history) |
| 35 | + // A successful save clears the singleton's temporary copy before the next test. |
| 36 | + OpenLessClipboardHistory.clearNonFavorites(context) |
| 37 | + } |
| 38 | + |
| 39 | + private fun block(path: File) { |
| 40 | + assertTrue(path.mkdir()) |
| 41 | + File(path, "blocker").writeText("keep the directory nonempty") |
| 42 | + } |
| 43 | + |
| 44 | + private fun unblock(path: File) { |
| 45 | + if (!path.isDirectory) return |
| 46 | + assertTrue(File(path, "blocker").delete()) |
| 47 | + assertTrue(path.delete()) |
| 48 | + } |
| 49 | + |
| 50 | + private fun texts() = OpenLessClipboardHistory.load(context).map { it.text } |
| 51 | + |
| 52 | + @Test fun blankTextDoesNotClaimPersistence() { |
| 53 | + assertFalse(OpenLessClipboardHistory.recordCopy(context, " ")) |
| 54 | + assertFalse(history.exists()) |
| 55 | + assertTrue(texts().isEmpty()) |
| 56 | + } |
| 57 | + |
| 58 | + @Test fun successfulWritesPreserveFavoriteAndDeduplicate() { |
| 59 | + assertTrue(OpenLessClipboardHistory.recordCopy(context, "favorite")) |
| 60 | + OpenLessClipboardHistory.toggleFavorite(context, "favorite") |
| 61 | + assertTrue(OpenLessClipboardHistory.recordCopy(context, "favorite")) |
| 62 | + val entries = OpenLessClipboardHistory.load(context) |
| 63 | + assertEquals(listOf("favorite"), entries.map { it.text }) |
| 64 | + assertTrue(entries.single().favorite) |
| 65 | + assertTrue(JSONArray(history.readText()).getJSONObject(0).getBoolean("favorite")) |
| 66 | + } |
| 67 | + |
| 68 | + @Test fun failedWritesKeepBothNotesAndLeaveThePreviousFileIntact() { |
| 69 | + assertTrue(OpenLessClipboardHistory.recordCopy(context, "saved")) |
| 70 | + val before = history.readText() |
| 71 | + block(staging) |
| 72 | + |
| 73 | + assertFalse(OpenLessClipboardHistory.recordCopy(context, "first failed note")) |
| 74 | + assertFalse(OpenLessClipboardHistory.recordCopy(context, "second failed note")) |
| 75 | + assertEquals(listOf("second failed note", "first failed note", "saved"), texts()) |
| 76 | + assertEquals(before, history.readText()) |
| 77 | + |
| 78 | + unblock(staging) |
| 79 | + assertTrue(OpenLessClipboardHistory.recordCopy(context, "second failed note")) |
| 80 | + assertEquals(listOf("second failed note", "first failed note", "saved"), texts()) |
| 81 | + val persisted = JSONArray(history.readText()) |
| 82 | + assertEquals(3, persisted.length()) |
| 83 | + assertEquals("first failed note", persisted.getJSONObject(1).getString("text")) |
| 84 | + assertFalse(staging.exists()) |
| 85 | + } |
| 86 | + |
| 87 | + @Test fun failedReplacementAlsoRetainsTheText() { |
| 88 | + block(history) |
| 89 | + assertFalse(OpenLessClipboardHistory.recordCopy(context, "not replaced")) |
| 90 | + assertEquals(listOf("not replaced"), texts()) |
| 91 | + assertTrue(File(history, "blocker").isFile) |
| 92 | + assertFalse(staging.exists()) |
| 93 | + |
| 94 | + unblock(history) |
| 95 | + assertTrue(OpenLessClipboardHistory.recordCopy(context, "retry")) |
| 96 | + assertEquals(listOf("retry", "not replaced"), texts()) |
| 97 | + assertEquals(2, JSONArray(history.readText()).length()) |
| 98 | + } |
| 99 | + |
| 100 | + @Test fun temporaryHistoryStillSupportsFavoriteClearAndDelete() { |
| 101 | + block(staging) |
| 102 | + assertFalse(OpenLessClipboardHistory.recordCopy(context, "keep")) |
| 103 | + assertFalse(OpenLessClipboardHistory.recordCopy(context, "discard")) |
| 104 | + OpenLessClipboardHistory.toggleFavorite(context, "keep") |
| 105 | + OpenLessClipboardHistory.clearNonFavorites(context) |
| 106 | + assertEquals(listOf("keep"), texts()) |
| 107 | + assertTrue(OpenLessClipboardHistory.load(context).single().favorite) |
| 108 | + OpenLessClipboardHistory.delete(context, "keep") |
| 109 | + assertTrue(texts().isEmpty()) |
| 110 | + } |
| 111 | +} |
0 commit comments