That fuck shit the fascists are using
at master 89 lines 2.5 kB view raw
1package org.tm.archive.testing 2 3import android.database.Cursor 4import android.util.Base64 5import org.hamcrest.MatcherAssert.assertThat 6import org.hamcrest.Matchers.hasSize 7import org.hamcrest.Matchers.`is` 8import org.hamcrest.Matchers.not 9import org.hamcrest.Matchers.notNullValue 10import org.hamcrest.Matchers.nullValue 11import org.signal.core.util.logging.Log 12import org.signal.core.util.readToList 13import org.signal.core.util.select 14import org.tm.archive.database.MessageTable 15import org.tm.archive.database.SignalDatabase 16import org.tm.archive.util.MessageTableTestUtils 17import java.util.concurrent.CountDownLatch 18import java.util.concurrent.TimeUnit 19import java.util.concurrent.TimeoutException 20import kotlin.time.Duration 21 22/** 23 * Run the given [runnable] on a new thread and wait for it to finish. 24 */ 25fun runSync(runnable: () -> Unit) { 26 val lock = CountDownLatch(1) 27 Thread { 28 try { 29 runnable.invoke() 30 } finally { 31 lock.countDown() 32 } 33 }.start() 34 lock.await() 35} 36 37/* Various kotlin-ifications of hamcrest matchers */ 38 39fun <T : Any?> T.assertIsNull() { 40 assertThat(this, nullValue()) 41} 42 43fun <T : Any?> T.assertIsNotNull() { 44 assertThat(this, notNullValue()) 45} 46 47infix fun <T : Any?> T.assertIs(expected: T) { 48 assertThat(this, `is`(expected)) 49} 50 51infix fun <T : Any> T.assertIsNot(expected: T) { 52 assertThat(this, not(`is`(expected))) 53} 54 55infix fun <E, T : Collection<E>> T.assertIsSize(expected: Int) { 56 assertThat(this, hasSize(expected)) 57} 58 59fun CountDownLatch.awaitFor(duration: Duration) { 60 if (!await(duration.inWholeMilliseconds, TimeUnit.MILLISECONDS)) { 61 throw TimeoutException("Latch await took longer than ${duration.inWholeMilliseconds}ms") 62 } 63} 64 65fun dumpTableToLogs(tag: String = "TestUtils", table: String) { 66 dumpTable(table).forEach { Log.d(tag, it.toString()) } 67} 68 69fun dumpTable(table: String): List<List<Pair<String, String?>>> { 70 return SignalDatabase.rawDatabase 71 .select() 72 .from(table) 73 .run() 74 .readToList { cursor -> 75 val map: List<Pair<String, String?>> = cursor.columnNames.map { column -> 76 val index = cursor.getColumnIndex(column) 77 var data: String? = when (cursor.getType(index)) { 78 Cursor.FIELD_TYPE_BLOB -> Base64.encodeToString(cursor.getBlob(index), 0) 79 else -> cursor.getString(index) 80 } 81 if (table == MessageTable.TABLE_NAME && column == MessageTable.TYPE) { 82 data = MessageTableTestUtils.typeColumnToString(cursor.getLong(index)) 83 } 84 85 column to data 86 } 87 map 88 } 89}