From 4933aed135ddc447eccda4a5da454a97b1df34e6 Mon Sep 17 00:00:00 2001 From: harshsomankar123-tech Date: Tue, 12 May 2026 20:10:17 +0530 Subject: [PATCH] Refactor ChangeTransparencyByNAction and ChangeTransparencyByNActionTest to Kotlin --- ...on.java => ChangeTransparencyByNAction.kt} | 56 ++++----- .../ChangeTransparencyByNActionTest.java | 98 ---------------- .../ChangeTransparencyByNActionTest.kt | 109 ++++++++++++++++++ 3 files changed, 131 insertions(+), 132 deletions(-) rename catroid/src/main/java/org/catrobat/catroid/content/actions/{ChangeTransparencyByNAction.java => ChangeTransparencyByNAction.kt} (50%) delete mode 100644 catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeTransparencyByNActionTest.java create mode 100644 catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeTransparencyByNActionTest.kt diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeTransparencyByNAction.java b/catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeTransparencyByNAction.kt similarity index 50% rename from catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeTransparencyByNAction.java rename to catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeTransparencyByNAction.kt index c4113ebaac1..3742aefb0f4 100644 --- a/catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeTransparencyByNAction.java +++ b/catroid/src/main/java/org/catrobat/catroid/content/actions/ChangeTransparencyByNAction.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,37 +20,25 @@ * 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 ChangeTransparencyByNAction extends TemporalAction { - - private Scope scope; - private Formula changeTransparency; - - @Override - protected void update(float delta) { - try { - Float newChangeTransparency = changeTransparency == null ? Float.valueOf(0f) : changeTransparency - .interpretFloat(scope); - scope.getSprite().look.changeTransparencyInUserInterfaceDimensionUnit(newChangeTransparency); - } 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 setTransparency(Formula value) { - this.changeTransparency = value; - } +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 ChangeTransparencyByNAction : TemporalAction() { + var scope: Scope? = null + var transparency: Formula? = null + + override fun update(percent: Float) { + val notNullScope = scope ?: return + try { + val newChangeTransparency: Float = transparency?.interpretFloat(notNullScope) ?: 0f + notNullScope.sprite.look.changeTransparencyInUserInterfaceDimensionUnit(newChangeTransparency) + } 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/ChangeTransparencyByNActionTest.java b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeTransparencyByNActionTest.java deleted file mode 100644 index 9d6d215f10e..00000000000 --- a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeTransparencyByNActionTest.java +++ /dev/null @@ -1,98 +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.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 ChangeTransparencyByNActionTest { - - @Rule - public final ExpectedException exception = ExpectedException.none(); - - private static final float DELTA = 0.01f; - private static final float INCREASE_VALUE = 98.7f; - private static final float DECREASE_VALUE = -33.3f; - private static final String NOT_NUMERICAL_STRING = "ghosts"; - private Sprite sprite; - - @Before - public void setUp() throws Exception { - initializeStaticSingletonMethods(); - sprite = new Sprite("testSprite"); - } - - @Test - public void testNormalBehavior() { - assertEquals(0f, sprite.look.getTransparencyInUserInterfaceDimensionUnit()); - - sprite.getActionFactory().createChangeTransparencyByNAction(sprite, new SequenceAction(), new Formula(INCREASE_VALUE)).act(1.0f); - assertEquals(INCREASE_VALUE, sprite.look.getTransparencyInUserInterfaceDimensionUnit()); - - sprite.getActionFactory().createChangeTransparencyByNAction(sprite, new SequenceAction(), new Formula(DECREASE_VALUE)).act(1.0f); - assertEquals(INCREASE_VALUE + DECREASE_VALUE, sprite.look.getTransparencyInUserInterfaceDimensionUnit()); - } - - @Test(expected = NullPointerException.class) - public void testNullSprite() { - Action action = sprite.getActionFactory().createChangeTransparencyByNAction(null, - new SequenceAction(), new Formula(INCREASE_VALUE)); - action.act(1.0f); - } - - @Test - public void testBrickWithStringFormula() { - sprite.getActionFactory().createChangeTransparencyByNAction(sprite, new SequenceAction(), new Formula(String.valueOf(INCREASE_VALUE))) - .act(1.0f); - assertEquals(INCREASE_VALUE, sprite.look.getTransparencyInUserInterfaceDimensionUnit(), DELTA); - - sprite.getActionFactory().createChangeTransparencyByNAction(sprite, new SequenceAction(), new Formula(NOT_NUMERICAL_STRING)).act(1.0f); - assertEquals(INCREASE_VALUE, sprite.look.getTransparencyInUserInterfaceDimensionUnit(), DELTA); - } - - @Test - public void testNullFormula() { - sprite.getActionFactory().createChangeTransparencyByNAction(sprite, new SequenceAction(), null).act(1.0f); - assertEquals(0f, sprite.look.getTransparencyInUserInterfaceDimensionUnit()); - } - - @Test - public void testNotANumberFormula() { - sprite.getActionFactory().createChangeTransparencyByNAction(sprite, new SequenceAction(), new Formula(Double.NaN)).act(1.0f); - assertEquals(0f, sprite.look.getTransparencyInUserInterfaceDimensionUnit()); - } -} diff --git a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeTransparencyByNActionTest.kt b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeTransparencyByNActionTest.kt new file mode 100644 index 00000000000..b13e68a21d6 --- /dev/null +++ b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/ChangeTransparencyByNActionTest.kt @@ -0,0 +1,109 @@ +/* + * 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.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 ChangeTransparencyByNActionTest { + @Rule + @JvmField + val exception: ExpectedException = ExpectedException.none() + + private lateinit var sprite: Sprite + + @Before + @Throws(Exception::class) + fun setUp() { + initializeStaticSingletonMethods() + sprite = Sprite("testSprite") + } + + @Test + fun testNormalBehavior() { + assertEquals(0f, sprite.look.transparencyInUserInterfaceDimensionUnit, DELTA) + + sprite.actionFactory.createChangeTransparencyByNAction( + sprite, SequenceAction(), Formula(INCREASE_VALUE) + ).act(1.0f) + assertEquals(INCREASE_VALUE, sprite.look.transparencyInUserInterfaceDimensionUnit, DELTA) + + sprite.actionFactory.createChangeTransparencyByNAction( + sprite, SequenceAction(), Formula(DECREASE_VALUE) + ).act(1.0f) + assertEquals(INCREASE_VALUE + DECREASE_VALUE, sprite.look.transparencyInUserInterfaceDimensionUnit, DELTA) + } + + @Test(expected = NullPointerException::class) + fun testNullSprite() { + sprite.actionFactory.createChangeTransparencyByNAction( + null, SequenceAction(), Formula(INCREASE_VALUE) + ).act(1.0f) + } + + @Test + fun testBrickWithStringFormula() { + sprite.actionFactory.createChangeTransparencyByNAction( + sprite, SequenceAction(), Formula(INCREASE_VALUE.toString()) + ).act(1.0f) + assertEquals(INCREASE_VALUE, sprite.look.transparencyInUserInterfaceDimensionUnit, DELTA) + + sprite.actionFactory.createChangeTransparencyByNAction( + sprite, SequenceAction(), Formula(NOT_NUMERICAL_STRING) + ).act(1.0f) + assertEquals(INCREASE_VALUE, sprite.look.transparencyInUserInterfaceDimensionUnit, DELTA) + } + + @Test + fun testNullFormula() { + sprite.actionFactory.createChangeTransparencyByNAction( + sprite, SequenceAction(), null + ).act(1.0f) + assertEquals(0f, sprite.look.transparencyInUserInterfaceDimensionUnit, DELTA) + } + + @Test + fun testNotANumberFormula() { + sprite.actionFactory.createChangeTransparencyByNAction( + sprite, SequenceAction(), Formula(Double.NaN) + ).act(1.0f) + assertEquals(0f, sprite.look.transparencyInUserInterfaceDimensionUnit, DELTA) + } + + companion object { + private const val DELTA = 0.01f + private const val INCREASE_VALUE = 98.7f + private const val DECREASE_VALUE = -33.3f + private const val NOT_NUMERICAL_STRING = "ghosts" + } +}