-
Notifications
You must be signed in to change notification settings - Fork 818
Refactor ShowTextColorSizeAlignmentAction to Kotlin and add unit tests #5217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| * Catroid: An on-device visual programming system for Android devices | ||
| * Copyright (C) 2010-2026 The Catrobat Team | ||
| * (<http://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 | ||
| * 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 <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| 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 | ||
| import org.catrobat.catroid.formulaeditor.UserVariable | ||
| import org.catrobat.catroid.stage.ShowTextActor | ||
| import org.catrobat.catroid.stage.StageActivity | ||
| import org.catrobat.catroid.utils.ShowTextUtils.AndroidStringProvider | ||
|
|
||
| class ShowTextColorSizeAlignmentAction : TemporalAction() { | ||
|
|
||
| companion object { | ||
| val TAG: String = ShowTextColorSizeAlignmentAction::class.java.simpleName | ||
| } | ||
|
|
||
| private var xPosition: Formula? = null | ||
| private var yPosition: Formula? = null | ||
| private var relativeTextSize: Formula? = null | ||
| private var color: Formula? = null | ||
| private var variableToShow: UserVariable? = null | ||
| private var scope: Scope? = null | ||
| private var alignment: Int = 0 | ||
| private var actor: ShowTextActor? = null | ||
| private var androidStringProvider: AndroidStringProvider? = null | ||
|
|
||
| override fun begin() { | ||
Check warningCode scanning / detekt Excessive nesting leads to hidden complexity. Prefer extracting code to make it easier to understand. Warning
Function begin is nested too deeply.
|
||
| try { | ||
| val scopeLocal = scope ?: return | ||
| val variableToShowLocal = variableToShow ?: return | ||
| val xPos = xPosition?.interpretInteger(scopeLocal) ?: 0 | ||
| val yPos = yPosition?.interpretInteger(scopeLocal) ?: 0 | ||
| val relativeTextSizeVal = (relativeTextSize?.interpretFloat(scopeLocal) ?: 0f) / 100f | ||
Check warningCode scanning / detekt Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning
This expression contains a magic number. Consider defining it to a well named constant.
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| val colorStr = color?.interpretString(scopeLocal) ?: "" | ||
|
|
||
Check warningCode scanning / detekt Detects trailing spaces Warning
Trailing space(s)
Check warningCode scanning / detekt Whitespaces at the end of a line are unnecessary and can be removed. Warning
Line 60 ends with a whitespace.
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| if (StageActivity.stageListener != null) { | ||
| val stageActors = StageActivity.stageListener.stage.actors | ||
| val dummyActor = ShowTextActor( | ||
| UserVariable("dummyActor"), 0, | ||
| 0, relativeTextSizeVal, colorStr, scopeLocal.sprite, alignment, androidStringProvider | ||
| ) | ||
| for (stageActor in stageActors) { | ||
| if (stageActor.javaClass == dummyActor.javaClass) { | ||
| val showTextActor = stageActor as ShowTextActor | ||
| if (showTextActor.variableNameToCompare == variableToShowLocal.name && | ||
| showTextActor.sprite == scopeLocal.sprite | ||
| ) { | ||
| stageActor.remove() | ||
| } | ||
| } | ||
| } | ||
| actor = ShowTextActor( | ||
| variableToShowLocal, xPos, yPos, relativeTextSizeVal, | ||
| colorStr, scopeLocal.sprite, alignment, androidStringProvider | ||
| ) | ||
| } | ||
| if (relativeTextSizeVal <= 0f) { | ||
| variableToShowLocal.visible = false | ||
| } else { | ||
| StageActivity.stageListener?.addActor(actor) | ||
| variableToShowLocal.visible = true | ||
| } | ||
| } catch (e: InterpretationException) { | ||
| Log.d(TAG, "InterpretationException: $e") | ||
| } | ||
| } | ||
|
|
||
| override fun update(percent: Float) { | ||
| try { | ||
| val scopeLocal = scope ?: return | ||
| val xPos = xPosition?.interpretInteger(scopeLocal) ?: 0 | ||
| val yPos = yPosition?.interpretInteger(scopeLocal) ?: 0 | ||
|
|
||
| actor?.apply { | ||
| setPositionX(xPos) | ||
| setPositionY(yPos) | ||
| } | ||
| } catch (e: InterpretationException) { | ||
Check warningCode scanning / detekt The caught exception is swallowed. The original exception could be lost. Warning
The caught exception is swallowed. The original exception could be lost.
|
||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| Log.d(TAG, "InterpretationException") | ||
| } | ||
| } | ||
|
|
||
| fun setPosition(xPosition: Formula?, yPosition: Formula?) { | ||
| this.xPosition = xPosition | ||
| this.yPosition = yPosition | ||
| } | ||
|
|
||
| fun setRelativeTextSize(relativeTextSize: Formula?) { | ||
| this.relativeTextSize = relativeTextSize | ||
| } | ||
|
|
||
| fun setColor(color: Formula?) { | ||
| this.color = color | ||
| } | ||
|
|
||
| fun setScope(scope: Scope?) { | ||
| this.scope = scope | ||
| } | ||
|
|
||
| fun setVariableToShow(userVariable: UserVariable?) { | ||
| this.variableToShow = userVariable | ||
| } | ||
|
|
||
| fun setAlignment(alignment: Int) { | ||
| this.alignment = alignment | ||
| } | ||
|
|
||
| fun setAndroidStringProvider(androidStringProvider: AndroidStringProvider?) { | ||
| this.androidStringProvider = androidStringProvider | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Catroid: An on-device visual programming system for Android devices | ||
| * Copyright (C) 2010-2026 The Catrobat Team | ||
| * (<http://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 | ||
| * 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 <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package org.catrobat.catroid.test.content.actions | ||
|
|
||
| import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction | ||
| import org.catrobat.catroid.content.Scope | ||
| import org.catrobat.catroid.content.Sprite | ||
| import org.catrobat.catroid.content.actions.ShowTextColorSizeAlignmentAction | ||
| import org.catrobat.catroid.formulaeditor.Formula | ||
| import org.catrobat.catroid.formulaeditor.UserVariable | ||
| import org.catrobat.catroid.stage.StageActivity | ||
| import org.catrobat.catroid.stage.StageListener | ||
| import org.catrobat.catroid.test.StaticSingletonInitializer.Companion.initializeStaticSingletonMethods | ||
| import org.junit.After | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.junit.runners.JUnit4 | ||
| import org.mockito.ArgumentMatchers.any | ||
| import org.mockito.Mockito.mock | ||
| import org.mockito.Mockito.never | ||
| import org.mockito.Mockito.verify | ||
| import org.mockito.Mockito.`when` as mockitoWhen | ||
|
|
||
| @RunWith(JUnit4::class) | ||
| class ShowTextColorSizeAlignmentActionTest { | ||
|
|
||
| private lateinit var sprite: Sprite | ||
| private lateinit var scope: Scope | ||
| private lateinit var userVariable: UserVariable | ||
| private lateinit var stageListenerMock: StageListener | ||
| private lateinit var stageMock: com.badlogic.gdx.scenes.scene2d.Stage | ||
| private val actorsArray = com.badlogic.gdx.utils.Array<com.badlogic.gdx.scenes.scene2d.Actor>() | ||
|
|
||
| @Before | ||
| @Throws(Exception::class) | ||
| fun setUp() { | ||
| initializeStaticSingletonMethods() | ||
| sprite = Sprite("testSprite") | ||
| scope = Scope(null, sprite, SequenceAction()) | ||
| userVariable = UserVariable("testVar") | ||
| userVariable.visible = false | ||
|
|
||
| stageListenerMock = mock(StageListener::class.java) | ||
| stageMock = mock(com.badlogic.gdx.scenes.scene2d.Stage::class.java) | ||
| mockitoWhen(stageListenerMock.stage).thenReturn(stageMock) | ||
| mockitoWhen(stageMock.actors).thenReturn(actorsArray) | ||
|
|
||
| StageActivity.stageListener = stageListenerMock | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| StageActivity.stageListener = null | ||
| } | ||
|
|
||
| @Test | ||
| fun testShowVariableColorSizeAlignmentVisibilityOnBegin() { | ||
| val action = ShowTextColorSizeAlignmentAction().apply { | ||
| setPosition(Formula(10), Formula(20)) | ||
| setRelativeTextSize(Formula(150)) | ||
| setColor(Formula("RED")) | ||
| setVariableToShow(userVariable) | ||
| setScope(scope) | ||
| setAlignment(1) | ||
| } | ||
|
|
||
| assertFalse(userVariable.visible) | ||
| action.act(1.0f) | ||
| assertTrue(userVariable.visible) | ||
| verify(stageListenerMock).addActor(any(com.badlogic.gdx.scenes.scene2d.Actor::class.java)) | ||
| } | ||
|
|
||
| @Test | ||
| fun testShowVariableColorSizeAlignmentInvisibleWhenSizeIsZeroOrLess() { | ||
| val action = ShowTextColorSizeAlignmentAction().apply { | ||
| setPosition(Formula(10), Formula(20)) | ||
| setRelativeTextSize(Formula(0)) | ||
| setColor(Formula("RED")) | ||
| setVariableToShow(userVariable) | ||
| setScope(scope) | ||
| setAlignment(1) | ||
| } | ||
|
|
||
| userVariable.visible = true | ||
| action.act(1.0f) | ||
| assertFalse(userVariable.visible) | ||
| verify(stageListenerMock, never()).addActor(any(com.badlogic.gdx.scenes.scene2d.Actor::class.java)) | ||
| } | ||
|
|
||
| @Test | ||
| fun testShowVariableNullSafety() { | ||
| val action = ShowTextColorSizeAlignmentAction() | ||
| // act with all null values should not throw exception | ||
| action.act(1.0f) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.