From 4b131ccf5b54bff53bbd958d09a972f4444c7241 Mon Sep 17 00:00:00 2001 From: harshsomankar123-tech Date: Tue, 12 May 2026 20:03:43 +0530 Subject: [PATCH] Refactor ChangeSizeByNAction and ChangeSizeByNActionTest to Kotlin --- ...eByNAction.java => ChangeSizeByNAction.kt} | 59 ++++----- .../actions/ChangeSizeByNActionTest.java | 103 --------------- .../actions/ChangeSizeByNActionTest.kt | 118 ++++++++++++++++++ 3 files changed, 144 insertions(+), 136 deletions(-) rename catroid/src/main/java/org/catrobat/catroid/content/actions/{ChangeSizeByNAction.java => ChangeSizeByNAction.kt} (53%) delete mode 100644 catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeSizeByNActionTest.java create mode 100644 catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeSizeByNActionTest.kt diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeSizeByNAction.java b/catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeSizeByNAction.kt similarity index 53% rename from catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeSizeByNAction.java rename to catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeSizeByNAction.kt index 881149048c5..26aa8b5be2e 100644 --- a/catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeSizeByNAction.java +++ b/catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeSizeByNAction.kt @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2025 The Catrobat Team + * Copyright (C) 2010-2026 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -20,36 +20,29 @@ * 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 android.util.Log; - -import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction; - -import org.catrobat.catroid.content.Scope; -import org.catrobat.catroid.formulaeditor.Formula; -import org.catrobat.catroid.formulaeditor.InterpretationException; - -public class ChangeSizeByNAction extends TemporalAction { - - private Scope scope; - private Formula size; - - @Override - protected void update(float percent) { - try { - Float newSize = size == null ? Float.valueOf(0f) : size.interpretFloat(scope); - scope.getSprite().look.changeSizeInUserInterfaceDimensionUnit(newSize); - } catch (InterpretationException interpretationException) { - Log.d(getClass().getSimpleName(), "Formula interpretation for this specific Brick failed.", interpretationException); - } - } - - public void setScope(Scope scope) { - this.scope = scope; - } - - public void setSize(Formula size) { - this.size = size; - } +package org.catrobat.catroid.content.actions + +import android.util.Log +import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction +import org.catrobat.catroid.content.Scope +import org.catrobat.catroid.formulaeditor.Formula +import org.catrobat.catroid.formulaeditor.InterpretationException + +class ChangeSizeByNAction : TemporalAction() { + var scope: Scope? = null + var size: Formula? = null + + override fun update(percent: Float) { + val notNullScope = scope ?: return + try { + val newSize = size?.interpretFloat(notNullScope) ?: 0f + notNullScope.sprite.look.changeSizeInUserInterfaceDimensionUnit(newSize) + } catch (interpretationException: InterpretationException) { + Log.d( + javaClass.simpleName, + "Formula interpretation for this specific Brick failed.", + interpretationException + ) + } + } } diff --git a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeSizeByNActionTest.java b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeSizeByNActionTest.java deleted file mode 100644 index 2bfc548e96f..00000000000 --- a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeSizeByNActionTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2025 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 com.badlogic.gdx.scenes.scene2d.actions.SequenceAction; - -import org.catrobat.catroid.content.ActionFactory; -import org.catrobat.catroid.content.Sprite; -import org.catrobat.catroid.formulaeditor.Formula; -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; - -import static org.catrobat.catroid.test.StaticSingletonInitializer.initializeStaticSingletonMethods; - -@RunWith(JUnit4.class) -public class ChangeSizeByNActionTest { - - @Rule - public final ExpectedException exception = ExpectedException.none(); - - private static final float INITIALIZED_VALUE = 100f; - private static final float CHANGE_VALUE = 44.4f; - private static final String NOT_NUMERICAL_STRING = "size"; - private static final float CHANGE_SIZE = 20f; - private static final float DELTA = 0.0001f; - private Sprite sprite; - - @Before - public void setUp() throws Exception { - initializeStaticSingletonMethods(); - sprite = new Sprite("testSprite"); - } - - @Test - public void testSize() { - assertEquals(INITIALIZED_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit()); - - sprite.getActionFactory().createChangeSizeByNAction(sprite, - new SequenceAction(), new Formula(CHANGE_SIZE)).act(1.0f); - assertEquals(INITIALIZED_VALUE + CHANGE_SIZE, sprite.look.getSizeInUserInterfaceDimensionUnit(), DELTA); - - sprite.getActionFactory().createChangeSizeByNAction(sprite, new SequenceAction(), - new Formula(-CHANGE_SIZE)).act(1.0f); - assertEquals(INITIALIZED_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit(), DELTA); - } - - @Test(expected = NullPointerException.class) - public void testNullSprite() { - ActionFactory factory = new ActionFactory(); - Action action = factory.createChangeSizeByNAction(null, new SequenceAction(), - new Formula(CHANGE_SIZE)); - action.act(1.0f); - } - - @Test - public void testBrickWithStringFormula() { - sprite.getActionFactory().createChangeSizeByNAction(sprite, new SequenceAction(), new Formula(String.valueOf(CHANGE_VALUE))) - .act(1.0f); - assertEquals(INITIALIZED_VALUE + CHANGE_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit()); - - sprite.getActionFactory().createChangeSizeByNAction(sprite, new SequenceAction(), new Formula(NOT_NUMERICAL_STRING)).act(1.0f); - assertEquals(INITIALIZED_VALUE + CHANGE_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit()); - } - - @Test - public void testNullFormula() { - sprite.getActionFactory().createChangeSizeByNAction(sprite, new SequenceAction(), null).act(1.0f); - assertEquals(INITIALIZED_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit()); - } - - @Test - public void testNotANumberFormula() { - sprite.getActionFactory().createChangeSizeByNAction(sprite, new SequenceAction(), new Formula(Double.NaN)).act(1.0f); - assertEquals(INITIALIZED_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit()); - } -} diff --git a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeSizeByNActionTest.kt b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeSizeByNActionTest.kt new file mode 100644 index 00000000000..c0991bb860d --- /dev/null +++ b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeSizeByNActionTest.kt @@ -0,0 +1,118 @@ +/* + * Catroid: An on-device visual programming system for Android devices + * Copyright (C) 2010-2026 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.actions.SequenceAction +import org.catrobat.catroid.content.ActionFactory +import org.catrobat.catroid.content.Sprite +import org.catrobat.catroid.formulaeditor.Formula +import org.catrobat.catroid.test.StaticSingletonInitializer.Companion.initializeStaticSingletonMethods +import org.junit.Assert.assertEquals +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 ChangeSizeByNActionTest { + + @Rule + @JvmField + val exception: ExpectedException = ExpectedException.none() + + private lateinit var sprite: Sprite + + @Before + @Throws(Exception::class) + fun setUp() { + initializeStaticSingletonMethods() + sprite = Sprite("testSprite") + } + + @Test + fun testSize() { + assertEquals(INITIALIZED_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit(), DELTA) + + sprite.actionFactory.createChangeSizeByNAction( + sprite, + SequenceAction(), Formula(CHANGE_SIZE) + ).act(1.0f) + assertEquals(INITIALIZED_VALUE + CHANGE_SIZE, sprite.look.getSizeInUserInterfaceDimensionUnit(), DELTA) + + sprite.actionFactory.createChangeSizeByNAction( + sprite, SequenceAction(), + Formula(-CHANGE_SIZE) + ).act(1.0f) + assertEquals(INITIALIZED_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit(), DELTA) + } + + @Test(expected = NullPointerException::class) + fun testNullSprite() { + val factory = ActionFactory() + val action = factory.createChangeSizeByNAction( + null, SequenceAction(), + Formula(CHANGE_SIZE) + ) + action.act(1.0f) + } + + @Test + fun testBrickWithStringFormula() { + sprite.actionFactory.createChangeSizeByNAction( + sprite, + SequenceAction(), + Formula(CHANGE_VALUE.toString()) + ) + .act(1.0f) + assertEquals(INITIALIZED_VALUE + CHANGE_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit(), DELTA) + + sprite.actionFactory.createChangeSizeByNAction( + sprite, + SequenceAction(), + Formula(NOT_NUMERICAL_STRING) + ).act(1.0f) + assertEquals(INITIALIZED_VALUE + CHANGE_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit(), DELTA) + } + + @Test + fun testNullFormula() { + sprite.actionFactory.createChangeSizeByNAction(sprite, SequenceAction(), null).act(1.0f) + assertEquals(INITIALIZED_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit(), DELTA) + } + + @Test + fun testNotANumberFormula() { + sprite.actionFactory.createChangeSizeByNAction(sprite, SequenceAction(), Formula(Double.NaN)).act(1.0f) + assertEquals(INITIALIZED_VALUE, sprite.look.getSizeInUserInterfaceDimensionUnit(), DELTA) + } + + companion object { + private const val INITIALIZED_VALUE = 100f + private const val CHANGE_VALUE = 44.4f + private const val NOT_NUMERICAL_STRING = "size" + private const val CHANGE_SIZE = 20f + private const val DELTA = 0.0001f + } +}