From c8ff8871460bbcd6958b4f270d19c21d71bc95c6 Mon Sep 17 00:00:00 2001 From: Erik Coccagna Date: Fri, 8 Nov 2024 12:04:45 +0100 Subject: [PATCH] CATROID-1071 GoToRandomPositionAction refactor Refactor GoToRandomPositionAction.java and GoToRandomPositionActionTest.java to Kotlin --- .../actions/GoToRandomPositionAction.java | 58 -------------- .../actions/GoToRandomPositionAction.kt | 48 +++++++++++ .../actions/GoToRandomPositionActionTest.java | 79 ------------------ .../actions/GoToRandomPositionActionTest.kt | 80 +++++++++++++++++++ 4 files changed, 128 insertions(+), 137 deletions(-) delete mode 100644 catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.java create mode 100644 catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.kt delete mode 100644 catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.java create mode 100644 catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.kt diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.java b/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.java deleted file mode 100644 index 3dc31399967..00000000000 --- a/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team - * () - * - * 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 - * http://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 . - */ - -package org.catrobat.catroid.content.actions; - -import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction; - -import org.catrobat.catroid.common.ScreenValues; -import org.catrobat.catroid.content.Sprite; - -public class GoToRandomPositionAction extends TemporalAction { - - private Sprite sprite; - private float randomXPosition; - private float randomYPosition; - - @Override - protected void update(float percent) { - randomXPosition = (float) Math.random() - * (ScreenValues.currentScreenResolution.getWidth() + 1) - (ScreenValues.currentScreenResolution.getWidth() / 2); - randomYPosition = (float) Math.random() - * (ScreenValues.currentScreenResolution.getHeight() + 1) - (ScreenValues.currentScreenResolution.getHeight() / 2); - - sprite.look.setPositionInUserInterfaceDimensionUnit(randomXPosition, randomYPosition); - } - - public void setSprite(Sprite sprite) { - this.sprite = sprite; - } - - public float getRandomXPosition() { - return this.randomXPosition; - } - - public float getRandomYPosition() { - return this.randomYPosition; - } -} diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.kt b/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.kt new file mode 100644 index 00000000000..5e1d99b0cbe --- /dev/null +++ b/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.kt @@ -0,0 +1,48 @@ +/* + * Catroid: An on-device visual programming system for Android devices + * Copyright (C) 2010-2022 The Catrobat Team + * () + * + * 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 + * http://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 . + */ +package org.catrobat.catroid.content.actions + +import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction +import org.catrobat.catroid.common.ScreenValues +import org.catrobat.catroid.content.Sprite + +class GoToRandomPositionAction : TemporalAction() { + private var sprite: Sprite? = null + var randomXPosition: Float = 0f + private set + var randomYPosition: Float = 0f + private set + + override fun update(percent: Float) { + randomXPosition = Math.random() + .toFloat() * (ScreenValues.currentScreenResolution.width + 1) - (ScreenValues.currentScreenResolution.width / 2) + randomYPosition = Math.random() + .toFloat() * (ScreenValues.currentScreenResolution.height + 1) - (ScreenValues.currentScreenResolution.height / 2) + + sprite!!.look.setPositionInUserInterfaceDimensionUnit(randomXPosition, randomYPosition) + } + + fun setSprite(sprite: Sprite?) { + this.sprite = sprite + } +} diff --git a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.java b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.java deleted file mode 100644 index 7d391e08fac..00000000000 --- a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team - * () - * - * 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 - * http://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 . - */ - -package org.catrobat.catroid.test.content.actions; - -import com.badlogic.gdx.scenes.scene2d.Action; - -import org.catrobat.catroid.common.BrickValues; -import org.catrobat.catroid.content.ActionFactory; -import org.catrobat.catroid.content.Sprite; -import org.catrobat.catroid.content.actions.GoToRandomPositionAction; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import static junit.framework.Assert.assertEquals; - -@RunWith(JUnit4.class) -public class GoToRandomPositionActionTest { - @Rule - public final ExpectedException exception = ExpectedException.none(); - - private Sprite sprite; - private Sprite dummySprite; - private GoToRandomPositionAction action; - - @Before - public void setUp() throws Exception { - sprite = new Sprite("testSprite"); - dummySprite = new Sprite("dummySprite"); - action = (GoToRandomPositionAction) sprite.getActionFactory().createGoToAction( - sprite, dummySprite, BrickValues.GO_TO_RANDOM_POSITION); - } - - @Test - public void testGoToOtherSpriteAction() { - sprite.look.setXInUserInterfaceDimensionUnit(0f); - sprite.look.setYInUserInterfaceDimensionUnit(0f); - - assertEquals(0f, sprite.look.getXInUserInterfaceDimensionUnit()); - assertEquals(0f, sprite.look.getYInUserInterfaceDimensionUnit()); - - action.act(1f); - - assertEquals(action.getRandomXPosition(), sprite.look.getXInUserInterfaceDimensionUnit()); - assertEquals(action.getRandomYPosition(), sprite.look.getYInUserInterfaceDimensionUnit()); - } - - @Test - public void testNullActor() { - ActionFactory factory = new ActionFactory(); - Action action = factory.createGoToAction(null, dummySprite, BrickValues.GO_TO_RANDOM_POSITION); - exception.expect(NullPointerException.class); - action.act(1.0f); - } -} diff --git a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.kt b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.kt new file mode 100644 index 00000000000..e26f44e0022 --- /dev/null +++ b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.kt @@ -0,0 +1,80 @@ +/* + * Catroid: An on-device visual programming system for Android devices + * Copyright (C) 2010-2022 The Catrobat Team + * () + * + * 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 + * http://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 . + */ +package org.catrobat.catroid.test.content.actions + +import junit.framework.Assert +import org.catrobat.catroid.common.BrickValues +import org.catrobat.catroid.common.ScreenValues +import org.catrobat.catroid.content.ActionFactory +import org.catrobat.catroid.content.Sprite +import org.catrobat.catroid.content.actions.GoToRandomPositionAction +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.ExpectedException +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(JUnit4::class) +class GoToRandomPositionActionTest { + @JvmField + @Rule + val exception: ExpectedException = ExpectedException.none() + + private var sprite: Sprite? = null + private var dummySprite: Sprite? = null + private var action: GoToRandomPositionAction? = null + + @Before + @Throws(Exception::class) + fun setUp() { + sprite = Sprite("testSprite") + dummySprite = Sprite("dummySprite") + ScreenValues.setToDefaultScreenSize() + action = sprite!!.actionFactory.createGoToAction( + sprite, dummySprite, BrickValues.GO_TO_RANDOM_POSITION + ) as GoToRandomPositionAction + } + + @Test + fun testGoToOtherSpriteAction() { + sprite!!.look.xInUserInterfaceDimensionUnit = 0f + sprite!!.look.yInUserInterfaceDimensionUnit = 0f + + Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit) + Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit) + + action!!.act(1f) + + Assert.assertEquals(action!!.randomXPosition, sprite!!.look.xInUserInterfaceDimensionUnit) + Assert.assertEquals(action!!.randomYPosition, sprite!!.look.yInUserInterfaceDimensionUnit) + } + + @Test + fun testNullActor() { + val factory = ActionFactory() + val action = factory.createGoToAction(null, dummySprite, BrickValues.GO_TO_RANDOM_POSITION) + exception.expect(NullPointerException::class.java) + action.act(1.0f) + } +}