mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Update
This commit is contained in:
@@ -47,4 +47,6 @@ interface Canvas {
|
|||||||
swipeAngle: Double)
|
swipeAngle: Double)
|
||||||
fun fillCircle(centerX: Double, centerY: Double, radius: Double)
|
fun fillCircle(centerX: Double, centerY: Double, radius: Double)
|
||||||
fun setTextAlign(align: TextAlign)
|
fun setTextAlign(align: TextAlign)
|
||||||
|
fun toImage(): Image
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
64
core/src/main/common/org/isoron/platform/gui/Image.kt
Normal file
64
core/src/main/common/org/isoron/platform/gui/Image.kt
Normal file
@@ -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.platform.gui
|
||||||
|
|
||||||
|
import kotlin.math.*
|
||||||
|
|
||||||
|
interface Image {
|
||||||
|
val width: Int
|
||||||
|
val height: Int
|
||||||
|
|
||||||
|
fun getPixel(x: Int, y: Int): Color
|
||||||
|
fun setPixel(x: Int, y: Int, color: Color)
|
||||||
|
|
||||||
|
suspend fun export(path: String)
|
||||||
|
|
||||||
|
fun diff(other: Image) {
|
||||||
|
if (width != other.width) error("Width must match")
|
||||||
|
if (height != other.height) error("Height must match")
|
||||||
|
|
||||||
|
for (x in 0 until width) {
|
||||||
|
for (y in 0 until height) {
|
||||||
|
val p1 = getPixel(x, y)
|
||||||
|
var l = 1.0
|
||||||
|
for (dx in -2..2) {
|
||||||
|
if (x + dx < 0 || x + dx >= width) continue
|
||||||
|
for (dy in -2..2) {
|
||||||
|
if (y + dy < 0 || y + dy >= height) continue
|
||||||
|
val p2 = other.getPixel(x + dx, y + dy)
|
||||||
|
l = min(l, abs(p1.luminosity - p2.luminosity))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setPixel(x, y, Color(l, l, l, 1.0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val averageLuminosity: Double
|
||||||
|
get() {
|
||||||
|
var luminosity = 0.0
|
||||||
|
for (x in 0 until width) {
|
||||||
|
for (y in 0 until height) {
|
||||||
|
luminosity += getPixel(x, y).luminosity
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return luminosity / (width * height)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
package org.isoron.platform.io
|
package org.isoron.platform.io
|
||||||
|
|
||||||
|
import org.isoron.platform.gui.*
|
||||||
|
|
||||||
interface FileOpener {
|
interface FileOpener {
|
||||||
/**
|
/**
|
||||||
* Opens a file which was shipped bundled with the application, such as a
|
* Opens a file which was shipped bundled with the application, such as a
|
||||||
@@ -88,4 +90,9 @@ interface ResourceFile {
|
|||||||
* Returns true if the file exists.
|
* Returns true if the file exists.
|
||||||
*/
|
*/
|
||||||
suspend fun exists(): Boolean
|
suspend fun exists(): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads resource file as an image.
|
||||||
|
*/
|
||||||
|
suspend fun toImage(): Image
|
||||||
}
|
}
|
||||||
@@ -28,8 +28,7 @@ val Color.uicolor: UIColor
|
|||||||
val Color.cgcolor: CGColorRef?
|
val Color.cgcolor: CGColorRef?
|
||||||
get() = uicolor.CGColor
|
get() = uicolor.CGColor
|
||||||
|
|
||||||
class IosCanvas() : Canvas {
|
class IosCanvas(val ctx: CGContextRef) : Canvas {
|
||||||
val ctx = UIGraphicsGetCurrentContext()
|
|
||||||
var textColor = UIColor.blackColor
|
var textColor = UIColor.blackColor
|
||||||
|
|
||||||
override fun setColor(color: Color) {
|
override fun setColor(color: Color) {
|
||||||
@@ -79,4 +78,8 @@ class IosCanvas() : Canvas {
|
|||||||
|
|
||||||
override fun setTextAlign(align: TextAlign) {
|
override fun setTextAlign(align: TextAlign) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun toImage(): Image {
|
||||||
|
TODO()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
package org.isoron.platform.io
|
package org.isoron.platform.io
|
||||||
|
|
||||||
|
import org.isoron.platform.gui.*
|
||||||
import platform.Foundation.*
|
import platform.Foundation.*
|
||||||
|
|
||||||
class IosFileOpener : FileOpener {
|
class IosFileOpener : FileOpener {
|
||||||
@@ -54,6 +55,11 @@ class IosFile(val path: String) : UserFile, ResourceFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun copyTo(dest: UserFile) {
|
override suspend fun copyTo(dest: UserFile) {
|
||||||
NSFileManager.defaultManager.copyItemAtPath(path, (dest as IosFile).path, null)
|
val manager = NSFileManager.defaultManager
|
||||||
|
manager.copyItemAtPath(path, (dest as IosFile).path, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun toImage(): Image {
|
||||||
|
TODO()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,7 @@ import kotlin.math.*
|
|||||||
class JsCanvas(val element: HTMLCanvasElement,
|
class JsCanvas(val element: HTMLCanvasElement,
|
||||||
val pixelScale: Double) : Canvas {
|
val pixelScale: Double) : Canvas {
|
||||||
|
|
||||||
|
|
||||||
val ctx = element.getContext("2d") as CanvasRenderingContext2D
|
val ctx = element.getContext("2d") as CanvasRenderingContext2D
|
||||||
var fontSize = 12.0
|
var fontSize = 12.0
|
||||||
var fontFamily = "NotoRegular"
|
var fontFamily = "NotoRegular"
|
||||||
@@ -57,7 +58,7 @@ class JsCanvas(val element: HTMLCanvasElement,
|
|||||||
ctx.font = "${fontSize}px ${fontFamily}"
|
ctx.font = "${fontSize}px ${fontFamily}"
|
||||||
ctx.textAlign = align
|
ctx.textAlign = align
|
||||||
ctx.textBaseline = CanvasTextBaseline.MIDDLE
|
ctx.textBaseline = CanvasTextBaseline.MIDDLE
|
||||||
ctx.fillText(text, toPixel(x), toPixel(y + fontSize * 0.05))
|
ctx.fillText(text, toPixel(x), toPixel(y + fontSize * 0.025))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun fillRect(x: Double, y: Double, width: Double, height: Double) {
|
override fun fillRect(x: Double, y: Double, width: Double, height: Double) {
|
||||||
@@ -132,14 +133,11 @@ class JsCanvas(val element: HTMLCanvasElement,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun loadImage(src: String) {
|
override fun toImage(): Image {
|
||||||
Promise<Int> { resolve, reject ->
|
return JsImage(this,
|
||||||
val img = Image()
|
ctx.getImageData(0.0,
|
||||||
img.onload = {
|
0.0,
|
||||||
ctx.drawImage(img, 0.0, 0.0)
|
element.width.toDouble(),
|
||||||
resolve(0)
|
element.height.toDouble()))
|
||||||
}
|
|
||||||
img.src = src
|
|
||||||
}.await()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
72
core/src/main/js/org/isoron/platform/gui/JsImage.kt
Normal file
72
core/src/main/js/org/isoron/platform/gui/JsImage.kt
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* 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.gui
|
||||||
|
|
||||||
|
import org.khronos.webgl.*
|
||||||
|
import org.w3c.dom.*
|
||||||
|
import kotlin.browser.*
|
||||||
|
import kotlin.math.*
|
||||||
|
|
||||||
|
class JsImage(val canvas: JsCanvas,
|
||||||
|
val imageData: ImageData) : Image {
|
||||||
|
|
||||||
|
override val width: Int
|
||||||
|
get() = imageData.width
|
||||||
|
|
||||||
|
override val height: Int
|
||||||
|
get() = imageData.height
|
||||||
|
|
||||||
|
val pixels = imageData.unsafeCast<Uint16Array>()
|
||||||
|
|
||||||
|
init {
|
||||||
|
console.log(width, height, imageData.data.length)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun export(path: String) {
|
||||||
|
canvas.ctx.putImageData(imageData, 0.0, 0.0)
|
||||||
|
val container = document.createElement("div")
|
||||||
|
container.className = "export"
|
||||||
|
val title = document.createElement("div")
|
||||||
|
title.innerHTML = path
|
||||||
|
document.body?.appendChild(container)
|
||||||
|
container.appendChild(title)
|
||||||
|
container.appendChild(canvas.element)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getPixel(x: Int, y: Int): Color {
|
||||||
|
val offset = 4 * (y * width + x)
|
||||||
|
return Color(imageData.data[offset + 0] / 255.0,
|
||||||
|
imageData.data[offset + 1] / 255.0,
|
||||||
|
imageData.data[offset + 2] / 255.0,
|
||||||
|
imageData.data[offset + 3] / 255.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setPixel(x: Int, y: Int, color: Color) {
|
||||||
|
val offset = 4 * (y * width + x)
|
||||||
|
inline fun map(x: Double): Byte {
|
||||||
|
return (x * 255).roundToInt().unsafeCast<Byte>()
|
||||||
|
}
|
||||||
|
imageData.data.set(offset + 0, map(color.red))
|
||||||
|
imageData.data.set(offset + 1, map(color.green))
|
||||||
|
imageData.data.set(offset + 2, map(color.blue))
|
||||||
|
imageData.data.set(offset + 3, map(color.alpha))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -20,7 +20,11 @@
|
|||||||
package org.isoron.platform.io
|
package org.isoron.platform.io
|
||||||
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
import org.isoron.platform.gui.*
|
||||||
|
import org.isoron.platform.gui.Image
|
||||||
|
import org.w3c.dom.*
|
||||||
import org.w3c.xhr.*
|
import org.w3c.xhr.*
|
||||||
|
import kotlin.browser.*
|
||||||
import kotlin.js.*
|
import kotlin.js.*
|
||||||
|
|
||||||
class JsFileStorage {
|
class JsFileStorage {
|
||||||
@@ -142,4 +146,20 @@ class JsResourceFile(val filename: String) : ResourceFile {
|
|||||||
val fs = (dest as JsUserFile).fs
|
val fs = (dest as JsUserFile).fs
|
||||||
fs.put(dest.filename, lines().joinToString("\n"))
|
fs.put(dest.filename, lines().joinToString("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun toImage(): Image {
|
||||||
|
return Promise<Image> { resolve, reject ->
|
||||||
|
val img = org.w3c.dom.Image()
|
||||||
|
img.onload = {
|
||||||
|
val canvas = JsCanvas(document.createElement("canvas") as HTMLCanvasElement, 1.0)
|
||||||
|
canvas.element.width = img.naturalWidth
|
||||||
|
canvas.element.height = img.naturalHeight
|
||||||
|
canvas.setColor(Color(0xffffff))
|
||||||
|
canvas.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight())
|
||||||
|
canvas.ctx.drawImage(img, 0.0, 0.0)
|
||||||
|
resolve(canvas.toImage())
|
||||||
|
}
|
||||||
|
img.src = "/assets/$filename"
|
||||||
|
}.await()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ import kotlin.math.*
|
|||||||
|
|
||||||
class JavaCanvas(val image: BufferedImage,
|
class JavaCanvas(val image: BufferedImage,
|
||||||
val pixelScale: Double = 2.0) : Canvas {
|
val pixelScale: Double = 2.0) : Canvas {
|
||||||
|
override fun toImage(): Image {
|
||||||
|
return JavaImage(image)
|
||||||
|
}
|
||||||
|
|
||||||
private val frc = FontRenderContext(null, true, true)
|
private val frc = FontRenderContext(null, true, true)
|
||||||
private var fontSize = 12.0
|
private var fontSize = 12.0
|
||||||
|
|||||||
48
core/src/main/jvm/org/isoron/platform/gui/JavaImage.kt
Normal file
48
core/src/main/jvm/org/isoron/platform/gui/JavaImage.kt
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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.gui
|
||||||
|
|
||||||
|
import java.awt.image.*
|
||||||
|
import java.io.*
|
||||||
|
import javax.imageio.*
|
||||||
|
|
||||||
|
class JavaImage(val bufferedImage: BufferedImage) : Image {
|
||||||
|
override fun setPixel(x: Int, y: Int, color: Color) {
|
||||||
|
bufferedImage.setRGB(x, y, java.awt.Color(color.red.toFloat(),
|
||||||
|
color.green.toFloat(),
|
||||||
|
color.blue.toFloat()).rgb)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun export(path: String) {
|
||||||
|
val file = File(path)
|
||||||
|
file.parentFile.mkdirs()
|
||||||
|
ImageIO.write(bufferedImage, "png", file)
|
||||||
|
}
|
||||||
|
|
||||||
|
override val width: Int
|
||||||
|
get() = bufferedImage.width
|
||||||
|
|
||||||
|
override val height: Int
|
||||||
|
get() = bufferedImage.height
|
||||||
|
|
||||||
|
override fun getPixel(x: Int, y: Int): Color {
|
||||||
|
return Color(bufferedImage.getRGB(x, y))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,8 +19,10 @@
|
|||||||
|
|
||||||
package org.isoron.platform.io
|
package org.isoron.platform.io
|
||||||
|
|
||||||
|
import org.isoron.platform.gui.*
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import java.nio.file.*
|
import java.nio.file.*
|
||||||
|
import javax.imageio.*
|
||||||
|
|
||||||
class JavaResourceFile(val path: String) : ResourceFile {
|
class JavaResourceFile(val path: String) : ResourceFile {
|
||||||
private val javaPath: Path
|
private val javaPath: Path
|
||||||
@@ -49,6 +51,10 @@ class JavaResourceFile(val path: String) : ResourceFile {
|
|||||||
fun stream(): InputStream {
|
fun stream(): InputStream {
|
||||||
return Files.newInputStream(javaPath)
|
return Files.newInputStream(javaPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun toImage(): Image {
|
||||||
|
return JavaImage(ImageIO.read(stream()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class JavaUserFile(val path: Path) : UserFile {
|
class JavaUserFile(val path: Path) : UserFile {
|
||||||
|
|||||||
@@ -27,17 +27,9 @@ enum class Locale {
|
|||||||
US, JAPAN
|
US, JAPAN
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CanvasHelper {
|
|
||||||
fun createCanvas(width: Int, height: Int): Canvas
|
|
||||||
suspend fun exportCanvas(canvas: Canvas, filename: String)
|
|
||||||
suspend fun compare(imageFile: ResourceFile, canvas: Canvas): Double
|
|
||||||
}
|
|
||||||
|
|
||||||
expect object DependencyResolver {
|
expect object DependencyResolver {
|
||||||
val supportsDatabaseTests: Boolean
|
|
||||||
val supportsCanvasTests: Boolean
|
|
||||||
suspend fun getFileOpener(): FileOpener
|
suspend fun getFileOpener(): FileOpener
|
||||||
suspend fun getDatabase(): Database
|
suspend fun getDatabase(): Database
|
||||||
fun getCanvasHelper(): CanvasHelper
|
|
||||||
fun getDateFormatter(locale: Locale): LocalDateFormatter
|
fun getDateFormatter(locale: Locale): LocalDateFormatter
|
||||||
|
fun createCanvas(width: Int, height: Int): Canvas
|
||||||
}
|
}
|
||||||
@@ -26,9 +26,7 @@ import kotlin.test.*
|
|||||||
class CanvasTest: BaseViewTest() {
|
class CanvasTest: BaseViewTest() {
|
||||||
@Test
|
@Test
|
||||||
fun run() = asyncTest{
|
fun run() = asyncTest{
|
||||||
if (!DependencyResolver.supportsCanvasTests) return@asyncTest
|
val canvas = DependencyResolver.createCanvas(500, 400)
|
||||||
val helper = DependencyResolver.getCanvasHelper()
|
|
||||||
val canvas = helper.createCanvas(500, 400)
|
|
||||||
|
|
||||||
canvas.setColor(Color(0x303030))
|
canvas.setColor(Color(0x303030))
|
||||||
canvas.fillRect(0.0, 0.0, 500.0, 400.0)
|
canvas.fillRect(0.0, 0.0, 500.0, 400.0)
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ class DatabaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testUsage() = asyncTest{
|
fun testUsage() = asyncTest{
|
||||||
if (!DependencyResolver.supportsDatabaseTests) return@asyncTest
|
|
||||||
val db = DependencyResolver.getDatabase()
|
val db = DependencyResolver.getDatabase()
|
||||||
|
|
||||||
db.setVersion(0)
|
db.setVersion(0)
|
||||||
|
|||||||
@@ -24,8 +24,6 @@ import org.isoron.platform.gui.*
|
|||||||
import org.isoron.uhabits.components.*
|
import org.isoron.uhabits.components.*
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
var SIMILARITY_THRESHOLD = 5.0
|
|
||||||
|
|
||||||
open class BaseViewTest {
|
open class BaseViewTest {
|
||||||
var theme = LightTheme()
|
var theme = LightTheme()
|
||||||
suspend fun assertRenders(width: Int,
|
suspend fun assertRenders(width: Int,
|
||||||
@@ -33,31 +31,34 @@ open class BaseViewTest {
|
|||||||
expectedPath: String,
|
expectedPath: String,
|
||||||
component: Component) {
|
component: Component) {
|
||||||
|
|
||||||
val helper = DependencyResolver.getCanvasHelper()
|
val canvas = DependencyResolver.createCanvas(width, height)
|
||||||
val canvas = helper.createCanvas(width, height)
|
|
||||||
component.draw(canvas)
|
component.draw(canvas)
|
||||||
assertRenders(expectedPath, canvas)
|
assertRenders(expectedPath, canvas)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun assertRenders(expectedPath: String,
|
suspend fun assertRenders(path: String,
|
||||||
canvas: Canvas) {
|
canvas: Canvas) {
|
||||||
|
|
||||||
val helper = DependencyResolver.getCanvasHelper()
|
val actualImage = canvas.toImage()
|
||||||
|
val failedActualPath = "/tmp/failed/${path}"
|
||||||
|
val failedExpectedPath = failedActualPath.replace(".png", ".expected.png")
|
||||||
|
val failedDiffPath = failedActualPath.replace(".png", ".diff.png")
|
||||||
val fileOpener = DependencyResolver.getFileOpener()
|
val fileOpener = DependencyResolver.getFileOpener()
|
||||||
val expectedFile = fileOpener.openResourceFile(expectedPath)
|
val expectedFile = fileOpener.openResourceFile(path)
|
||||||
val actualPath = "/failed/${expectedPath}"
|
|
||||||
|
|
||||||
if (expectedFile.exists()) {
|
if (expectedFile.exists()) {
|
||||||
val d = helper.compare(expectedFile, canvas)
|
val expectedImage = expectedFile.toImage()
|
||||||
if (d >= SIMILARITY_THRESHOLD) {
|
val diffImage = expectedFile.toImage()
|
||||||
helper.exportCanvas(canvas, actualPath)
|
diffImage.diff(actualImage)
|
||||||
val expectedCopy = expectedPath.replace(".png", ".expected.png")
|
val distance = diffImage.averageLuminosity * 100
|
||||||
expectedFile.copyTo(fileOpener.openUserFile("/failed/$expectedCopy"))
|
if (distance >= 1.0) {
|
||||||
fail("Images differ (distance=${d}). Actual rendered saved to ${actualPath}.")
|
expectedImage.export(failedExpectedPath)
|
||||||
|
actualImage.export(failedActualPath)
|
||||||
|
diffImage.export(failedDiffPath)
|
||||||
|
fail("Images differ (distance=${distance})")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
helper.exportCanvas(canvas, actualPath)
|
actualImage.export(failedActualPath)
|
||||||
fail("Expected file is missing. Actual render saved to $actualPath")
|
fail("Expected image file is missing.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,6 @@ import kotlin.test.*
|
|||||||
class CheckmarkRepositoryTest() {
|
class CheckmarkRepositoryTest() {
|
||||||
@Test
|
@Test
|
||||||
fun testCRUD() = asyncTest {
|
fun testCRUD() = asyncTest {
|
||||||
if (!DependencyResolver.supportsDatabaseTests) return@asyncTest
|
|
||||||
val db = DependencyResolver.getDatabase()
|
val db = DependencyResolver.getDatabase()
|
||||||
|
|
||||||
val habitA = 10
|
val habitA = 10
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import kotlin.test.*
|
|||||||
class HabitRepositoryTest() {
|
class HabitRepositoryTest() {
|
||||||
@Test
|
@Test
|
||||||
fun testCRUD() = asyncTest{
|
fun testCRUD() = asyncTest{
|
||||||
if (!DependencyResolver.supportsDatabaseTests) return@asyncTest
|
|
||||||
val db = DependencyResolver.getDatabase()
|
val db = DependencyResolver.getDatabase()
|
||||||
val original0 = Habit(id = 0,
|
val original0 = Habit(id = 0,
|
||||||
name = "Wake up early",
|
name = "Wake up early",
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import kotlin.test.*
|
|||||||
class PreferencesRepositoryTest() {
|
class PreferencesRepositoryTest() {
|
||||||
@Test
|
@Test
|
||||||
fun testUsage() = asyncTest{
|
fun testUsage() = asyncTest{
|
||||||
if (!DependencyResolver.supportsDatabaseTests) return@asyncTest
|
|
||||||
val db = DependencyResolver.getDatabase()
|
val db = DependencyResolver.getDatabase()
|
||||||
val prefs = PreferencesRepository(db)
|
val prefs = PreferencesRepository(db)
|
||||||
assertEquals("default", prefs.getString("non_existing_key", "default"))
|
assertEquals("default", prefs.getString("non_existing_key", "default"))
|
||||||
|
|||||||
@@ -19,8 +19,11 @@
|
|||||||
|
|
||||||
package org.isoron
|
package org.isoron
|
||||||
|
|
||||||
|
import org.isoron.platform.gui.*
|
||||||
import org.isoron.platform.io.*
|
import org.isoron.platform.io.*
|
||||||
import org.isoron.platform.time.*
|
import org.isoron.platform.time.*
|
||||||
|
import platform.CoreGraphics.*
|
||||||
|
import platform.UIKit.*
|
||||||
|
|
||||||
actual object DependencyResolver {
|
actual object DependencyResolver {
|
||||||
actual suspend fun getFileOpener(): FileOpener = IosFileOpener()
|
actual suspend fun getFileOpener(): FileOpener = IosFileOpener()
|
||||||
@@ -32,10 +35,12 @@ actual object DependencyResolver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IosDatabase and IosCanvas are currently implemented in Swift, so we
|
actual fun createCanvas(width: Int, height: Int): Canvas {
|
||||||
// cannot test these classes here. The tests will be skipped.
|
UIGraphicsBeginImageContext(CGSizeMake(width.toDouble(), height.toDouble()))
|
||||||
actual suspend fun getDatabase(): Database = TODO()
|
val ctx = UIGraphicsGetCurrentContext()!!
|
||||||
actual fun getCanvasHelper(): CanvasHelper = TODO()
|
return IosCanvas(ctx)
|
||||||
actual val supportsDatabaseTests = false
|
}
|
||||||
actual val supportsCanvasTests = false
|
|
||||||
|
actual suspend fun getDatabase(): Database = TODO()
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -23,14 +23,10 @@ import org.isoron.platform.gui.*
|
|||||||
import org.isoron.platform.io.*
|
import org.isoron.platform.io.*
|
||||||
import org.isoron.platform.time.*
|
import org.isoron.platform.time.*
|
||||||
import org.isoron.uhabits.*
|
import org.isoron.uhabits.*
|
||||||
import org.khronos.webgl.*
|
|
||||||
import org.w3c.dom.*
|
import org.w3c.dom.*
|
||||||
import kotlin.browser.*
|
import kotlin.browser.*
|
||||||
import kotlin.math.*
|
|
||||||
|
|
||||||
actual object DependencyResolver {
|
actual object DependencyResolver {
|
||||||
actual val supportsDatabaseTests = true
|
|
||||||
actual val supportsCanvasTests = true
|
|
||||||
var fileOpener: JsFileOpener? = null
|
var fileOpener: JsFileOpener? = null
|
||||||
|
|
||||||
actual suspend fun getFileOpener(): FileOpener {
|
actual suspend fun getFileOpener(): FileOpener {
|
||||||
@@ -49,78 +45,22 @@ actual object DependencyResolver {
|
|||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun getCanvasHelper(): CanvasHelper {
|
|
||||||
return JsCanvasHelper()
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun getDateFormatter(locale: Locale): LocalDateFormatter {
|
actual fun getDateFormatter(locale: Locale): LocalDateFormatter {
|
||||||
return when (locale) {
|
return when (locale) {
|
||||||
Locale.US -> JsDateFormatter("en-US")
|
Locale.US -> JsDateFormatter("en-US")
|
||||||
Locale.JAPAN -> JsDateFormatter("ja-JP")
|
Locale.JAPAN -> JsDateFormatter("ja-JP")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class JsCanvasHelper : CanvasHelper {
|
actual fun createCanvas(width: Int, height: Int): Canvas {
|
||||||
override suspend fun compare(imageFile: ResourceFile,
|
val element = document.createElement("canvas") as HTMLCanvasElement
|
||||||
canvas: Canvas): Double {
|
element.width = 2 * width
|
||||||
canvas as JsCanvas
|
element.height = 2 * height
|
||||||
imageFile as JsResourceFile
|
element.style.width = "${2 * width}px"
|
||||||
val width = canvas.element.width
|
element.style.height = "${2 * height}px"
|
||||||
val height = canvas.element.height
|
val canvas = JsCanvas(element, 2.0)
|
||||||
|
canvas.setColor(Color(0xffffff))
|
||||||
val expectedCanvasElement = document.createElement("canvas") as HTMLCanvasElement
|
canvas.fillRect(0.0, 0.0, width.toDouble(), height.toDouble())
|
||||||
expectedCanvasElement.width = width
|
return canvas
|
||||||
expectedCanvasElement.height = height
|
|
||||||
expectedCanvasElement.style.width = canvas.element.style.width
|
|
||||||
expectedCanvasElement.style.height = canvas.element.style.height
|
|
||||||
expectedCanvasElement.className = "canvasTest"
|
|
||||||
document.body?.appendChild(expectedCanvasElement)
|
|
||||||
val expectedCanvas = JsCanvas(expectedCanvasElement, 1.0)
|
|
||||||
expectedCanvas.loadImage("../assets/${imageFile.filename}")
|
|
||||||
|
|
||||||
val actualData = canvas.ctx.getImageData(0.0,
|
|
||||||
0.0,
|
|
||||||
width.toDouble(),
|
|
||||||
height.toDouble()).data
|
|
||||||
val expectedData = expectedCanvas.ctx.getImageData(0.0,
|
|
||||||
0.0,
|
|
||||||
width.toDouble(),
|
|
||||||
height.toDouble()).data
|
|
||||||
|
|
||||||
var distance = 0.0;
|
|
||||||
for (x in 0 until width) {
|
|
||||||
for (y in 0 until height) {
|
|
||||||
val k = (y * width + x) * 4
|
|
||||||
distance += abs(actualData[k] - expectedData[k])
|
|
||||||
distance += abs(actualData[k + 1] - expectedData[k + 1])
|
|
||||||
distance += abs(actualData[k + 2] - expectedData[k + 2])
|
|
||||||
distance += abs(actualData[k + 3] - expectedData[k + 3])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val adjustedDistance = distance / 255.0 / 4 / 1000
|
|
||||||
|
|
||||||
if (adjustedDistance > SIMILARITY_THRESHOLD) {
|
|
||||||
expectedCanvasElement.style.display = "block"
|
|
||||||
canvas.element.style.display = "block"
|
|
||||||
}
|
|
||||||
|
|
||||||
return adjustedDistance
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun createCanvas(width: Int, height: Int): Canvas {
|
|
||||||
val canvasElement = document.createElement("canvas") as HTMLCanvasElement
|
|
||||||
canvasElement.width = width * 2
|
|
||||||
canvasElement.height = height * 2
|
|
||||||
canvasElement.style.width = "${width}px"
|
|
||||||
canvasElement.style.height = "${height}px"
|
|
||||||
canvasElement.className = "canvasTest"
|
|
||||||
document.body?.appendChild(canvasElement)
|
|
||||||
return JsCanvas(canvasElement, 2.0)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun exportCanvas(canvas: Canvas, filename: String) {
|
|
||||||
// do nothing
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,11 +30,7 @@ import java.nio.file.*
|
|||||||
import javax.imageio.*
|
import javax.imageio.*
|
||||||
|
|
||||||
actual object DependencyResolver {
|
actual object DependencyResolver {
|
||||||
actual val supportsDatabaseTests = true
|
|
||||||
actual val supportsCanvasTests = true
|
|
||||||
|
|
||||||
actual suspend fun getFileOpener(): FileOpener = JavaFileOpener()
|
actual suspend fun getFileOpener(): FileOpener = JavaFileOpener()
|
||||||
actual fun getCanvasHelper(): CanvasHelper = JavaCanvasHelper()
|
|
||||||
|
|
||||||
actual suspend fun getDatabase(): Database {
|
actual suspend fun getDatabase(): Database {
|
||||||
val log = StandardLog()
|
val log = StandardLog()
|
||||||
@@ -54,45 +50,13 @@ actual object DependencyResolver {
|
|||||||
Locale.JAPAN -> JavaLocalDateFormatter(java.util.Locale.JAPAN)
|
Locale.JAPAN -> JavaLocalDateFormatter(java.util.Locale.JAPAN)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class JavaCanvasHelper : CanvasHelper {
|
actual fun createCanvas(width: Int, height: Int): Canvas {
|
||||||
override suspend fun compare(imageFile: ResourceFile, canvas: Canvas): Double {
|
|
||||||
val actual = (canvas as JavaCanvas).image
|
|
||||||
val expected = ImageIO.read((imageFile as JavaResourceFile).stream())
|
|
||||||
return compare(expected, actual)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun compare(expected: BufferedImage,
|
|
||||||
actual: 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 distance / 4.0
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun createCanvas(width: Int, height: Int): Canvas {
|
|
||||||
val widthPx = width * 2
|
val widthPx = width * 2
|
||||||
val heightPx = height * 2
|
val heightPx = height * 2
|
||||||
val image = BufferedImage(widthPx, heightPx, BufferedImage.TYPE_INT_ARGB)
|
val image = BufferedImage(widthPx,
|
||||||
|
heightPx,
|
||||||
|
BufferedImage.TYPE_INT_ARGB)
|
||||||
return JavaCanvas(image, pixelScale = 2.0)
|
return JavaCanvas(image, pixelScale = 2.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun exportCanvas(canvas: Canvas, filename: String) {
|
|
||||||
val file = File("/tmp/$filename")
|
|
||||||
file.parentFile.mkdirs()
|
|
||||||
ImageIO.write((canvas as JavaCanvas).image, "png", file)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -19,10 +19,12 @@
|
|||||||
font-family: "NotoBold";
|
font-family: "NotoBold";
|
||||||
src: url(../assets/fonts/NotoSans-Bold.ttf) format("truetype");
|
src: url(../assets/fonts/NotoSans-Bold.ttf) format("truetype");
|
||||||
}
|
}
|
||||||
.canvasTest {
|
.export {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.export canvas {
|
||||||
border: 1px solid #000;
|
border: 1px solid #000;
|
||||||
display: none;
|
margin: 10px;
|
||||||
margin: 10px auto;
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
Reference in New Issue
Block a user