69 lines · 3.3 KB · KotlinRaw
package com.cottondb.books.readerimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.Fileclass SeekTest { private fun json(text: String): JsonObject = Json.parseToJsonElement(text).jsonObject /** `reader.seek`'s answer becomes the line: here, the ticks with the chapter marked, Back, the words and the steps. */ @Test fun the_core_answer_is_the_line() { val state = SeekLineState.of(json(""" {"here": 0.3, "pages": null, "ticks": [{"at": 0.0, "title": "One", "here": false}, {"at": 0.25, "title": "Two", "here": true}], "back": {"at": 0.1, "words": "Back to 10%", "ms_left": 12000}, "words": {"here": "Two · 30%", "line": "Place in the book", "contents": "Contents"}, "steps": {"previous": 0.25, "next": null}} """))!! assertEquals(0.3f, state.here) assertEquals(listOf(SeekTick(0f, "One", false), SeekTick(0.25f, "Two", true)), state.ticks) assertEquals(SeekBack(0.1f, "Back to 10%", 12_000), state.back) assertEquals("Two · 30%", state.hereWords) assertEquals("Place in the book", state.lineWords) assertEquals("Contents", state.contentsWords) assertEquals(0.25f, SeekGeometry.step(state, forward = false)) assertNull(SeekGeometry.step(state, forward = true)) assertNull(SeekBack.of(json("""{"words": "no place"}"""))) assertNull(SeekLineState.of(null)) } /** A finger's x is its share of the line, kept on it; a touch within the slop is a tap; the slip stays inside the line. */ @Test fun a_finger_maps_to_a_share_of_the_book() { assertEquals(0.25f, SeekGeometry.at(100f, 400f)) assertEquals(0f, SeekGeometry.at(-20f, 400f)) assertEquals(1f, SeekGeometry.at(420f, 400f)) assertEquals(0f, SeekGeometry.at(10f, 0f)) assertEquals(200f, SeekGeometry.x(0.5f, 400f)) assertTrue(SeekGeometry.isTap(3f, 8f)) assertFalse(SeekGeometry.isTap(9f, 8f)) assertEquals(150f, SeekGeometry.slipLeft(200f, 100f, 400f)) assertEquals(0f, SeekGeometry.slipLeft(10f, 100f, 400f)) assertEquals(300f, SeekGeometry.slipLeft(395f, 100f, 400f)) } /** Each resource's start is its first position's share; one unknown, or out of order, tells none. */ @Test fun the_resource_starts_come_from_the_first_positions() { assertEquals(listOf(0.0, 0.25, 0.8), SeekStarts.of(listOf(0.0, 0.25, 0.8))) assertNull(SeekStarts.of(listOf(0.0, null))) assertNull(SeekStarts.of(listOf(0.5, 0.2))) assertNull(SeekStarts.of(emptyList())) } /** A PDF tells its page count as its contents, and a jump's Locator names the page the pager turns to. */ @Test fun a_pdf_tells_its_pages_and_lands_on_the_page_named() { val contents = Pdf.contents(File("orchard.pdf"), 201) assertEquals("""{"toc":[],"reading_order":["orchard.pdf"],"starts":[0.0],"pages":201}""", contents.toString()) assertEquals(147, Pdf.page("""{"href":"orchard.pdf","type":"application/pdf","locations":{"position":148,"totalProgression":0.735}}""")) }}