Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 37 KiB |
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 35 KiB |
After Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 9.5 KiB |
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 9.0 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 9.0 KiB |
Before Width: | Height: | Size: 61 KiB |
Before Width: | Height: | Size: 58 KiB |
Before Width: | Height: | Size: 31 KiB |
Before Width: | Height: | Size: 56 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 9.2 KiB |
Before Width: | Height: | Size: 8.6 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 22 KiB |
Before Width: | Height: | Size: 22 KiB |
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.platform.io
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
class StringsTest {
|
||||
@Test
|
||||
fun testSprintf() {
|
||||
assertEquals(" 5", sprintf("%3d", 5))
|
||||
assertEquals("005", sprintf("%03d", 5))
|
||||
assertEquals("005", sprintf("%03d", 5))
|
||||
assertEquals(" 45", sprintf("%3d", 45))
|
||||
assertEquals("145", sprintf("%3d", 145))
|
||||
assertEquals(" 9 9", sprintf("%3d%3d", 9, 9))
|
||||
assertEquals(" 13.42", sprintf("%8.2f", 13.419187263))
|
||||
assertEquals("00013.42", sprintf("%08.2f", 13.419187263))
|
||||
assertEquals("13.42 ", sprintf("%-8.2f", 13.419187263))
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits
|
||||
|
||||
import org.isoron.*
|
||||
import org.isoron.platform.gui.*
|
||||
import org.isoron.uhabits.components.*
|
||||
import kotlin.test.*
|
||||
|
||||
var SIMILARITY_THRESHOLD = 5.0
|
||||
|
||||
open class BaseViewTest {
|
||||
var theme = LightTheme()
|
||||
suspend fun assertRenders(width: Int,
|
||||
height: Int,
|
||||
expectedPath: String,
|
||||
component: Component) {
|
||||
|
||||
val helper = DependencyResolver.getCanvasHelper()
|
||||
val canvas = helper.createCanvas(width, height)
|
||||
component.draw(canvas)
|
||||
assertRenders(expectedPath, canvas)
|
||||
}
|
||||
|
||||
suspend fun assertRenders(expectedPath: String,
|
||||
canvas: Canvas) {
|
||||
|
||||
val helper = DependencyResolver.getCanvasHelper()
|
||||
val fileOpener = DependencyResolver.getFileOpener()
|
||||
val expectedFile = fileOpener.openResourceFile(expectedPath)
|
||||
val actualPath = "/failed/${expectedPath}"
|
||||
|
||||
if (expectedFile.exists()) {
|
||||
val d = helper.compare(expectedFile, canvas)
|
||||
if (d >= SIMILARITY_THRESHOLD) {
|
||||
helper.exportCanvas(canvas, actualPath)
|
||||
val expectedCopy = expectedPath.replace(".png", ".expected.png")
|
||||
expectedFile.copyTo(fileOpener.openUserFile("/failed/$expectedCopy"))
|
||||
fail("Images differ (distance=${d}). Actual rendered saved to ${actualPath}.")
|
||||
}
|
||||
} else {
|
||||
helper.exportCanvas(canvas, actualPath)
|
||||
fail("Expected file is missing. Actual render saved to $actualPath")
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.components
|
||||
|
||||
import org.isoron.*
|
||||
import org.isoron.uhabits.*
|
||||
import kotlin.test.*
|
||||
|
||||
class NumberButtonTest : BaseViewTest() {
|
||||
val base = "components/NumberButton"
|
||||
|
||||
@Test
|
||||
fun testFormatValue() = asyncTest{
|
||||
assertEquals("0.12", 0.1235.toShortString())
|
||||
assertEquals("0.1", 0.1000.toShortString())
|
||||
assertEquals("5", 5.0.toShortString())
|
||||
assertEquals("5.25", 5.25.toShortString())
|
||||
assertEquals("12.3", 12.3456.toShortString())
|
||||
assertEquals("123", 123.123.toShortString())
|
||||
assertEquals("321", 321.2.toShortString())
|
||||
assertEquals("4.3k", 4321.2.toShortString())
|
||||
assertEquals("54.3k", 54321.2.toShortString())
|
||||
assertEquals("654k", 654321.2.toShortString())
|
||||
assertEquals("7.7M", 7654321.2.toShortString())
|
||||
assertEquals("87.7M", 87654321.2.toShortString())
|
||||
assertEquals("988M", 987654321.2.toShortString())
|
||||
assertEquals("2.0G", 1987654321.2.toShortString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRenderAbove() = asyncTest {
|
||||
val btn = NumberButton(theme.color(8), 500.0, 100.0, "steps", theme)
|
||||
assertRenders(48, 48, "$base/render_above.png", btn)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRenderBelow() = asyncTest {
|
||||
val btn = NumberButton(theme.color(8), 99.0, 100.0, "steps", theme)
|
||||
assertRenders(48, 48, "$base/render_below.png", btn)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRenderZero() = asyncTest {
|
||||
val btn = NumberButton(theme.color(8), 0.0, 100.0, "steps", theme)
|
||||
assertRenders(48, 48, "$base/render_zero.png", btn)
|
||||
}
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import org.isoron.platform.gui.*
|
||||
import org.isoron.platform.io.*
|
||||
import org.isoron.uhabits.components.*
|
||||
import java.awt.image.*
|
||||
import java.io.*
|
||||
import javax.imageio.*
|
||||
import kotlin.math.*
|
||||
|
||||
open class BaseViewTest {
|
||||
val theme = LightTheme()
|
||||
|
||||
private fun distance(actual: BufferedImage,
|
||||
expected: BufferedImage): Double {
|
||||
|
||||
if (actual.width != expected.width) return Double.POSITIVE_INFINITY
|
||||
if (actual.height != expected.height) return Double.POSITIVE_INFINITY
|
||||
|
||||
var distance = 0.0;
|
||||
for (x in 0 until actual.width) {
|
||||
for (y in 0 until actual.height) {
|
||||
val p1 = Color(actual.getRGB(x, y))
|
||||
val p2 = Color(expected.getRGB(x, y))
|
||||
distance += abs(p1.red - p2.red)
|
||||
distance += abs(p1.green - p2.green)
|
||||
distance += abs(p1.blue - p2.blue)
|
||||
}
|
||||
}
|
||||
|
||||
return 255 * distance / (actual.width * actual.height)
|
||||
}
|
||||
|
||||
fun assertRenders(width: Int,
|
||||
height: Int,
|
||||
expectedPath: String,
|
||||
component: Component,
|
||||
threshold: Double = 1e-3) {
|
||||
|
||||
val actual = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
|
||||
val canvas = JavaCanvas(actual)
|
||||
val expectedFile: JavaResourceFile
|
||||
val actualPath = "/tmp/${expectedPath}"
|
||||
|
||||
component.draw(canvas)
|
||||
expectedFile = JavaFileOpener().openResourceFile(expectedPath) as JavaResourceFile
|
||||
|
||||
runBlocking<Unit> {
|
||||
if (expectedFile.exists()) {
|
||||
val expected = ImageIO.read(expectedFile.stream())
|
||||
val d = distance(actual, expected)
|
||||
if (d >= threshold) {
|
||||
File(actualPath).parentFile.mkdirs()
|
||||
ImageIO.write(actual, "png", File(actualPath))
|
||||
ImageIO.write(expected, "png", File(actualPath.replace(".png", ".expected.png")))
|
||||
//fail("Images differ (distance=${d}). Actual rendered saved to ${actualPath}.")
|
||||
}
|
||||
} else {
|
||||
File(actualPath).parentFile.mkdirs()
|
||||
ImageIO.write(actual, "png", File(actualPath))
|
||||
//fail("Expected file is missing. Actual render saved to $actualPath")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.components
|
||||
|
||||
import org.hamcrest.CoreMatchers.*
|
||||
import org.isoron.uhabits.*
|
||||
import org.junit.*
|
||||
import org.junit.Assert.*
|
||||
|
||||
class NumberButtonTest : BaseViewTest() {
|
||||
val base = "components/NumberButton/"
|
||||
|
||||
@Test
|
||||
fun testFormatValue() {
|
||||
assertThat(0.1235.toShortString(), equalTo("0.12"))
|
||||
assertThat(0.1000.toShortString(), equalTo("0.1"))
|
||||
assertThat(5.0.toShortString(), equalTo("5"))
|
||||
assertThat(5.25.toShortString(), equalTo("5.25"))
|
||||
assertThat(12.3456.toShortString(), equalTo("12.3"))
|
||||
assertThat(123.123.toShortString(), equalTo("123"))
|
||||
assertThat(321.2.toShortString(), equalTo("321"))
|
||||
assertThat(4321.2.toShortString(), equalTo("4.3k"))
|
||||
assertThat(54321.2.toShortString(), equalTo("54.3k"))
|
||||
assertThat(654321.2.toShortString(), equalTo("654k"))
|
||||
assertThat(7654321.2.toShortString(), equalTo("7.7M"))
|
||||
assertThat(87654321.2.toShortString(), equalTo("87.7M"))
|
||||
assertThat(987654321.2.toShortString(), equalTo("988M"))
|
||||
assertThat(1987654321.2.toShortString(), equalTo("2.0G"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRenderAbove() {
|
||||
val btn = NumberButton(theme.color(8), 500.0, 100.0, "steps", theme)
|
||||
assertRenders(96, 96, "$base/render_above.png", btn)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRenderBelow() {
|
||||
val btn = NumberButton(theme.color(8), 99.0, 100.0, "steps", theme)
|
||||
assertRenders(96, 96, "$base/render_below.png", btn)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRenderZero() {
|
||||
val btn = NumberButton(theme.color(8), 0.0, 100.0, "steps", theme)
|
||||
assertRenders(96, 96, "$base/render_zero.png", btn)
|
||||
}
|
||||
}
|