Skip to content

Commit c8ff887

Browse files
committed
CATROID-1071 GoToRandomPositionAction refactor
Refactor GoToRandomPositionAction.java and GoToRandomPositionActionTest.java to Kotlin
1 parent 12b8803 commit c8ff887

File tree

4 files changed

+128
-137
lines changed

4 files changed

+128
-137
lines changed

Diff for: catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.java

-58
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Catroid: An on-device visual programming system for Android devices
3+
* Copyright (C) 2010-2022 The Catrobat Team
4+
* (<http://developer.catrobat.org/credits>)
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* An additional term exception under section 7 of the GNU Affero
12+
* General Public License, version 3, is available at
13+
* http://developer.catrobat.org/license_additional_term
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
package org.catrobat.catroid.content.actions
24+
25+
import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction
26+
import org.catrobat.catroid.common.ScreenValues
27+
import org.catrobat.catroid.content.Sprite
28+
29+
class GoToRandomPositionAction : TemporalAction() {
30+
private var sprite: Sprite? = null
31+
var randomXPosition: Float = 0f
32+
private set
33+
var randomYPosition: Float = 0f
34+
private set
35+
36+
override fun update(percent: Float) {
37+
randomXPosition = Math.random()
38+
.toFloat() * (ScreenValues.currentScreenResolution.width + 1) - (ScreenValues.currentScreenResolution.width / 2)
39+
randomYPosition = Math.random()
40+
.toFloat() * (ScreenValues.currentScreenResolution.height + 1) - (ScreenValues.currentScreenResolution.height / 2)
41+
42+
sprite!!.look.setPositionInUserInterfaceDimensionUnit(randomXPosition, randomYPosition)
43+
}
44+
45+
fun setSprite(sprite: Sprite?) {
46+
this.sprite = sprite
47+
}
48+
}

Diff for: catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.java

-79
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Catroid: An on-device visual programming system for Android devices
3+
* Copyright (C) 2010-2022 The Catrobat Team
4+
* (<http://developer.catrobat.org/credits>)
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* An additional term exception under section 7 of the GNU Affero
12+
* General Public License, version 3, is available at
13+
* http://developer.catrobat.org/license_additional_term
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
package org.catrobat.catroid.test.content.actions
24+
25+
import junit.framework.Assert
26+
import org.catrobat.catroid.common.BrickValues
27+
import org.catrobat.catroid.common.ScreenValues
28+
import org.catrobat.catroid.content.ActionFactory
29+
import org.catrobat.catroid.content.Sprite
30+
import org.catrobat.catroid.content.actions.GoToRandomPositionAction
31+
import org.junit.Before
32+
import org.junit.Rule
33+
import org.junit.Test
34+
import org.junit.rules.ExpectedException
35+
import org.junit.runner.RunWith
36+
import org.junit.runners.JUnit4
37+
38+
@RunWith(JUnit4::class)
39+
class GoToRandomPositionActionTest {
40+
@JvmField
41+
@Rule
42+
val exception: ExpectedException = ExpectedException.none()
43+
44+
private var sprite: Sprite? = null
45+
private var dummySprite: Sprite? = null
46+
private var action: GoToRandomPositionAction? = null
47+
48+
@Before
49+
@Throws(Exception::class)
50+
fun setUp() {
51+
sprite = Sprite("testSprite")
52+
dummySprite = Sprite("dummySprite")
53+
ScreenValues.setToDefaultScreenSize()
54+
action = sprite!!.actionFactory.createGoToAction(
55+
sprite, dummySprite, BrickValues.GO_TO_RANDOM_POSITION
56+
) as GoToRandomPositionAction
57+
}
58+
59+
@Test
60+
fun testGoToOtherSpriteAction() {
61+
sprite!!.look.xInUserInterfaceDimensionUnit = 0f
62+
sprite!!.look.yInUserInterfaceDimensionUnit = 0f
63+
64+
Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
65+
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)
66+
67+
action!!.act(1f)
68+
69+
Assert.assertEquals(action!!.randomXPosition, sprite!!.look.xInUserInterfaceDimensionUnit)
70+
Assert.assertEquals(action!!.randomYPosition, sprite!!.look.yInUserInterfaceDimensionUnit)
71+
}
72+
73+
@Test
74+
fun testNullActor() {
75+
val factory = ActionFactory()
76+
val action = factory.createGoToAction(null, dummySprite, BrickValues.GO_TO_RANDOM_POSITION)
77+
exception.expect(NullPointerException::class.java)
78+
action.act(1.0f)
79+
}
80+
}

0 commit comments

Comments
 (0)