mirror of https://github.com/iSoron/uhabits.git
parent
b0cedde0a9
commit
5ea19c9475
@ -1,67 +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.platform.concurrency
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A TaskRunner provides the ability of running tasks in different queues. The
|
|
||||||
* class is also observable, and notifies listeners when new tasks are started
|
|
||||||
* or finished.
|
|
||||||
*
|
|
||||||
* Two queues are available: a foreground queue and a background queue. These
|
|
||||||
* two queues may run in parallel, depending on the hardware. Multiple tasks
|
|
||||||
* submitted to the same queue, however, always run sequentially, in the order
|
|
||||||
* they were enqueued.
|
|
||||||
*/
|
|
||||||
interface TaskRunner {
|
|
||||||
|
|
||||||
val listeners: MutableList<Listener>
|
|
||||||
|
|
||||||
val activeTaskCount: Int
|
|
||||||
|
|
||||||
fun runInBackground(task: () -> Unit)
|
|
||||||
|
|
||||||
fun runInForeground(task: () -> Unit)
|
|
||||||
|
|
||||||
interface Listener {
|
|
||||||
fun onTaskStarted()
|
|
||||||
fun onTaskFinished()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sequential implementation of TaskRunner. Both background and foreground
|
|
||||||
* queues run in the same thread, so they block each other.
|
|
||||||
*/
|
|
||||||
class SequentialTaskRunner : TaskRunner {
|
|
||||||
|
|
||||||
override val listeners = mutableListOf<TaskRunner.Listener>()
|
|
||||||
|
|
||||||
override var activeTaskCount = 0
|
|
||||||
|
|
||||||
override fun runInBackground(task: () -> Unit) {
|
|
||||||
activeTaskCount += 1
|
|
||||||
for (l in listeners) l.onTaskStarted()
|
|
||||||
task()
|
|
||||||
activeTaskCount -= 1
|
|
||||||
for (l in listeners) l.onTaskFinished()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun runInForeground(task: () -> Unit) = runInBackground(task)
|
|
||||||
}
|
|
@ -1,30 +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
|
|
||||||
|
|
||||||
import org.isoron.platform.gui.*
|
|
||||||
import org.isoron.platform.io.*
|
|
||||||
|
|
||||||
expect class DependencyResolver() {
|
|
||||||
suspend fun getFileOpener(): FileOpener
|
|
||||||
suspend fun getDatabase(): Database
|
|
||||||
fun createCanvas(width: Int, height: Int): Canvas
|
|
||||||
fun exportCanvas(canvas: Canvas, filename: String)
|
|
||||||
}
|
|
@ -1,50 +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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@file:Suppress("UNCHECKED_CAST")
|
|
||||||
|
|
||||||
package org.isoron
|
|
||||||
|
|
||||||
import org.isoron.platform.gui.*
|
|
||||||
import org.isoron.platform.io.*
|
|
||||||
import platform.CoreGraphics.*
|
|
||||||
import platform.Foundation.*
|
|
||||||
import platform.UIKit.*
|
|
||||||
|
|
||||||
actual class DependencyResolver {
|
|
||||||
actual suspend fun getFileOpener(): FileOpener {
|
|
||||||
return IosFileOpener()
|
|
||||||
}
|
|
||||||
|
|
||||||
actual suspend fun getDatabase(): Database = TODO()
|
|
||||||
|
|
||||||
actual fun createCanvas(width: Int, height: Int): Canvas {
|
|
||||||
UIGraphicsBeginImageContext(CGSizeMake(width=500.0, height=600.0))
|
|
||||||
return IosCanvas()
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun exportCanvas(canvas: Canvas, filename: String): Unit {
|
|
||||||
val image = UIGraphicsGetImageFromCurrentImageContext()!!
|
|
||||||
val manager = NSFileManager.defaultManager
|
|
||||||
val paths = manager.URLsForDirectory(NSDocumentDirectory, NSUserDomainMask) as List<NSURL>
|
|
||||||
val filePath = paths.first().URLByAppendingPathComponent("IosCanvasTest.png")!!.path!!
|
|
||||||
val data = UIImagePNGRepresentation(image)!!
|
|
||||||
data.writeToFile(filePath, false)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,42 +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
|
|
||||||
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
import org.isoron.platform.io.*
|
|
||||||
import org.isoron.uhabits.models.*
|
|
||||||
import kotlin.test.*
|
|
||||||
|
|
||||||
class JsAsyncTests {
|
|
||||||
@Test
|
|
||||||
fun testFiles() = GlobalScope.promise { FilesTest().testLines() }
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testDatabase() = GlobalScope.promise { DatabaseTest().testUsage() }
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testCheckmarkRepository() = GlobalScope.promise { CheckmarkRepositoryTest().testCRUD() }
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testHabitRepository() = GlobalScope.promise { HabitRepositoryTest().testCRUD() }
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testPreferencesRepository() = GlobalScope.promise { PreferencesRepositoryTest().testUsage() }
|
|
||||||
}
|
|
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import org.isoron.platform.io.*
|
||||||
|
import org.isoron.uhabits.*
|
||||||
|
import org.isoron.uhabits.models.*
|
||||||
|
import kotlin.test.*
|
||||||
|
|
||||||
|
class JsAsyncTests {
|
||||||
|
var fs: JsFileStorage? = null
|
||||||
|
|
||||||
|
suspend fun getFileOpener(): FileOpener {
|
||||||
|
if (fs == null) {
|
||||||
|
fs = JsFileStorage()
|
||||||
|
fs?.init()
|
||||||
|
}
|
||||||
|
return JsFileOpener(fs!!)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun getDatabase(): Database {
|
||||||
|
val nativeDB = eval("new SQL.Database()")
|
||||||
|
val db = JsDatabase(nativeDB)
|
||||||
|
db.migrateTo(LOOP_DATABASE_VERSION, getFileOpener(), StandardLog())
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFiles() = GlobalScope.promise {
|
||||||
|
FilesTest(getFileOpener()).testLines()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testDatabase() = GlobalScope.promise {
|
||||||
|
DatabaseTest(getDatabase()).testUsage()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testCheckmarkRepository() = GlobalScope.promise {
|
||||||
|
CheckmarkRepositoryTest(getDatabase()).testCRUD()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testHabitRepository() = GlobalScope.promise {
|
||||||
|
HabitRepositoryTest(getDatabase()).testCRUD()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testPreferencesRepository() = GlobalScope.promise {
|
||||||
|
PreferencesRepositoryTest(getDatabase()).testUsage()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue