Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StofflR committed Aug 11, 2024
1 parent 389a096 commit 5ff18e9
Show file tree
Hide file tree
Showing 4 changed files with 575 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* (<https://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* https://developer.catrobat.org/license_additional_term
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.catrobat.catroid.test.content.actions

import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.scenes.scene2d.actions.Actions
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction
import junit.framework.Assert
import org.catrobat.catroid.ProjectManager
import org.catrobat.catroid.common.Constants
import org.catrobat.catroid.content.ActionFactory
import org.catrobat.catroid.content.Project
import org.catrobat.catroid.content.Sprite
import org.catrobat.catroid.content.actions.SavePlotAction
import org.catrobat.catroid.formulaeditor.Formula
import org.catrobat.catroid.io.XstreamSerializer
import org.catrobat.catroid.test.utils.TestUtils
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import java.io.File

@RunWith(AndroidJUnit4::class)
class SavePlotActionTest {
private val xMovement = Formula(X_MOVEMENT)
private var sprite: Sprite? = null
private val camera: OrthographicCamera = Mockito.spy(OrthographicCamera())
private lateinit var plotFile : File

@Before
@Throws(Exception::class)
fun setUp() {
sprite = Sprite("testSprite")
Mockito.doNothing().`when`(camera).update()
createTestProject()
plotFile = File(Constants.CACHE_DIRECTORY, "$projectName.svg")
if (plotFile.exists()) {
plotFile.delete()
}
if (!Constants.CACHE_DIRECTORY.exists()) {
Constants.CACHE_DIRECTORY.mkdirs()
}
plotFile.createNewFile()
}

@After
@Throws(Exception::class)
fun tearDown() {
TestUtils.deleteProjects(projectName)
}

@Test(expected = NullPointerException::class)
fun testNull() {
val action = sprite!!.actionFactory.createSavePlotAction(null, null, null)
Assert.assertTrue(action is SavePlotAction)
}


@Test
fun testSaveNoPositionChange() {
Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)

sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)
val action = sprite!!.actionFactory.createSavePlotAction(sprite, SequenceAction(), FILE)

Assert.assertTrue(action is SavePlotAction)
if(action is SavePlotAction)
action.writeToFile(plotFile)
Assert.assertTrue(plotFile.readText().isNotEmpty())
}

@Test
fun testSaveOnePositionChangeOpen() {
Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)

sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)
sprite!!.actionFactory.createChangeXByNAction(sprite, SequenceAction(), xMovement).act(1.0f)
val action = sprite!!.actionFactory.createSavePlotAction(sprite, SequenceAction(), FILE)

Assert.assertTrue(action is SavePlotAction)
if(action is SavePlotAction)
action.writeToFile(plotFile)
Assert.assertTrue(plotFile.readText().isNotEmpty())
}
@Test
fun testSaveOnePositionChangeClosed() {
Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)

sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)
sprite!!.actionFactory.createChangeXByNAction(sprite, SequenceAction(), xMovement).act(1.0f)
sprite!!.actionFactory.createStopPlotAction(sprite).act(1.0f)
val action = sprite!!.actionFactory.createSavePlotAction(sprite, SequenceAction(), FILE)

Assert.assertTrue(action is SavePlotAction)
if(action is SavePlotAction)
action.writeToFile(plotFile)
Assert.assertTrue(plotFile.readText().isNotEmpty())
}

@Test
fun testSaveMultiLineChange() {
Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)

sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)
sprite!!.actionFactory.createChangeXByNAction(sprite, SequenceAction(), xMovement).act(1.0f)
sprite!!.actionFactory.createStopPlotAction(sprite).act(1.0f)
sprite!!.actionFactory.createChangeXByNAction(sprite, SequenceAction(), xMovement).act(1.0f)
sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)
sprite!!.actionFactory.createChangeXByNAction(sprite, SequenceAction(), xMovement).act(1.0f)
sprite!!.actionFactory.createStopPlotAction(sprite).act(1.0f)
val action = sprite!!.actionFactory.createSavePlotAction(sprite, SequenceAction(), FILE)

Assert.assertTrue(action is SavePlotAction)
if(action is SavePlotAction)
action.writeToFile(plotFile)
Assert.assertTrue(plotFile.readText().isNotEmpty())
}

private fun createTestProject() {
val project = Project(ApplicationProvider.getApplicationContext(), projectName)
XstreamSerializer.getInstance().saveProject(project)
ProjectManager.getInstance().currentProject = project
}

companion object {
private const val X_MOVEMENT = 100.0f
private const val projectName = "testProject"
private const val FILENAME = "$projectName.svg"
private val FILE = Formula(FILENAME)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* (<https://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* https://developer.catrobat.org/license_additional_term
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.catrobat.catroid.test.content.actions

import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction
import junit.framework.Assert
import org.catrobat.catroid.ProjectManager
import org.catrobat.catroid.content.ActionFactory
import org.catrobat.catroid.content.Project
import org.catrobat.catroid.content.Sprite
import org.catrobat.catroid.formulaeditor.Formula
import org.catrobat.catroid.io.XstreamSerializer
import org.catrobat.catroid.stage.CameraPositioner
import org.catrobat.catroid.test.utils.TestUtils
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito

@RunWith(AndroidJUnit4::class)
class StartPlotActionTest {

private val xMovement = Formula(X_MOVEMENT)
private val yMovement = Formula(Y_MOVEMENT)
private var sprite: Sprite? = null
private val camera: OrthographicCamera = Mockito.spy(OrthographicCamera())
private val cameraPositioner = CameraPositioner(camera, 960.0f, 540.0f)
private val projectName = "testProject"

@Before
@Throws(Exception::class)
fun setUp() {
sprite = Sprite("testSprite")
Mockito.doNothing().`when`(camera).update()
createTestProject()
}

@After
@Throws(Exception::class)
fun tearDown() {
TestUtils.deleteProjects(projectName)
}

@Test(expected = NullPointerException::class)
fun testNullSprite() {
val factory = ActionFactory()
val action = factory.createStartPlotAction(null)
action.act(1.0f)
}

@Test
fun testSaveOnePositionChange() {
Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)

sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)
sprite!!.actionFactory.createChangeXByNAction(sprite, SequenceAction(), xMovement).act(1.0f)

val positions = sprite!!.plot.data()
Assert.assertEquals(0f, positions.first().removeFirst().x)
Assert.assertEquals(X_MOVEMENT, positions.first().removeFirst().x)
}

@Test
fun testSimultaneousPositionChangeXY() {
Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)

sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)
sprite!!.actionFactory.createPlaceAtAction(
sprite, SequenceAction(), xMovement,
yMovement
).act(1.0f)

val positions = sprite!!.plot.data()
Assert.assertEquals(0f, positions.first().removeFirst().x)
Assert.assertEquals(X_MOVEMENT, positions.first().first().x)
Assert.assertEquals(Y_MOVEMENT, positions.first().removeFirst().y)
}

@Test
fun testPositionChangeX() {
Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)

sprite!!.actionFactory.createPlaceAtAction(
sprite, SequenceAction(), Formula(0f),
Formula(0f)
).act(1.0f)

sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)
sprite!!.actionFactory.createPlaceAtAction(
sprite, SequenceAction(), xMovement,
Formula(0f)
).act(1.0f)

val positions = sprite!!.plot.data()
Assert.assertEquals(0f, positions.first().removeFirst().x)
Assert.assertEquals(X_MOVEMENT, positions.first().first().x)
Assert.assertEquals(0f, positions.first().removeFirst().y)
}

@Test
fun testAfterBecomeFocusPoint() {
Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)

cameraPositioner.horizontalFlex = 0f
cameraPositioner.verticalFlex = 0f
cameraPositioner.spriteToFocusOn = sprite

sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)
sprite!!.actionFactory.createPlaceAtAction(
sprite, SequenceAction(), xMovement,
yMovement
).act(1.0f)
cameraPositioner.updateCameraPositionForFocusedSprite()

val positions = sprite!!.plot.data()
Assert.assertEquals(0f, positions.first().removeFirst().x)
Assert.assertEquals(X_MOVEMENT, positions.first().first().x)
Assert.assertEquals(Y_MOVEMENT, positions.first().removeFirst().y)
cameraPositioner.reset()
}

@Test
fun testMultiplePenDownActions() {
Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)

sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)
sprite!!.actionFactory.createStartPlotAction(sprite).act(1.0f)

val positions = sprite!!.plot.data()
Assert.assertEquals(0f, positions.first().removeFirst().x)
Assert.assertTrue(positions.first().isEmpty())
}

private fun createTestProject() {
val project = Project(ApplicationProvider.getApplicationContext(), projectName)
XstreamSerializer.getInstance().saveProject(project)
ProjectManager.getInstance().currentProject = project
}

companion object {
private const val X_MOVEMENT = 100.0f
private const val Y_MOVEMENT = 50.0f
}
}
Loading

0 comments on commit 5ff18e9

Please sign in to comment.