|
| 1 | +/* |
| 2 | + * Catroid: An on-device visual programming system for Android devices |
| 3 | + * Copyright (C) 2010-2026 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.uiespresso.ui.actionbar |
| 24 | + |
| 25 | +import androidx.test.espresso.Espresso.onView |
| 26 | +import androidx.test.espresso.action.ViewActions.click |
| 27 | +import androidx.test.espresso.assertion.ViewAssertions.doesNotExist |
| 28 | +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed |
| 29 | +import androidx.test.espresso.matcher.ViewMatchers.withId |
| 30 | +import org.catrobat.catroid.ProjectManager |
| 31 | +import org.catrobat.catroid.R |
| 32 | +import org.catrobat.catroid.WaitForConditionAction.Companion.waitFor |
| 33 | +import org.catrobat.catroid.common.Constants |
| 34 | +import org.catrobat.catroid.common.LookData |
| 35 | +import org.catrobat.catroid.common.NfcTagData |
| 36 | +import org.catrobat.catroid.common.SoundInfo |
| 37 | +import org.catrobat.catroid.content.bricks.BroadcastBrick |
| 38 | +import org.catrobat.catroid.content.bricks.PlaySoundBrick |
| 39 | +import org.catrobat.catroid.content.bricks.SetLookBrick |
| 40 | +import org.catrobat.catroid.content.bricks.SetVariableBrick |
| 41 | +import org.catrobat.catroid.content.bricks.WhenNfcBrick |
| 42 | +import org.catrobat.catroid.formulaeditor.Formula |
| 43 | +import org.catrobat.catroid.formulaeditor.UserVariable |
| 44 | +import org.catrobat.catroid.io.XstreamSerializer |
| 45 | +import org.catrobat.catroid.nfc.NfcHandler |
| 46 | +import org.catrobat.catroid.test.utils.TestUtils |
| 47 | +import org.catrobat.catroid.ui.SpriteActivity |
| 48 | +import org.catrobat.catroid.uiespresso.content.brick.utils.BrickDataInteractionWrapper |
| 49 | +import org.catrobat.catroid.uiespresso.content.brick.utils.UiNFCTestUtils |
| 50 | +import org.catrobat.catroid.uiespresso.util.UiTestUtils.Companion.createProjectAndGetStartScript |
| 51 | +import org.catrobat.catroid.uiespresso.util.rules.FragmentActivityTestRule |
| 52 | +import org.junit.After |
| 53 | +import org.junit.Before |
| 54 | +import org.junit.Rule |
| 55 | +import org.junit.Test |
| 56 | +import org.junit.runner.RunWith |
| 57 | +import org.junit.runners.Parameterized |
| 58 | +import org.koin.java.KoinJavaComponent.inject |
| 59 | +import java.io.File |
| 60 | +import java.io.IOException |
| 61 | +import kotlin.getValue |
| 62 | + |
| 63 | +@RunWith(Parameterized::class) |
| 64 | +class ActionBarUndoSpinnerTest( |
| 65 | + private val brickName: String, |
| 66 | + private val brickPosition: Int, |
| 67 | + private val brickSpinnerViewId: Int |
| 68 | +) { |
| 69 | + private val waitThreshold: Long = 5000 |
| 70 | + |
| 71 | + @get:Rule |
| 72 | + var baseActivityTestRule: FragmentActivityTestRule<SpriteActivity?> = FragmentActivityTestRule( |
| 73 | + SpriteActivity::class.java, |
| 74 | + SpriteActivity.EXTRA_FRAGMENT_POSITION, |
| 75 | + SpriteActivity.FRAGMENT_SCRIPTS |
| 76 | + ) |
| 77 | + |
| 78 | + private val firstItem = "abc" |
| 79 | + private val secondItem = "def" |
| 80 | + |
| 81 | + private val projectManager by inject(ProjectManager::class.java) |
| 82 | + |
| 83 | + @After |
| 84 | + @Throws(IOException::class) |
| 85 | + fun tearDown() { |
| 86 | + TestUtils.deleteProjects(ActionBarUndoSpinnerTest::class.java.simpleName) |
| 87 | + } |
| 88 | + |
| 89 | + @Before |
| 90 | + @Throws(Exception::class) |
| 91 | + fun setUp() { |
| 92 | + createProject() |
| 93 | + baseActivityTestRule.launchActivity() |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun testUndoSpinnerActionVisible() { |
| 98 | + onView(withId(R.id.menu_undo)).check(doesNotExist()) |
| 99 | + BrickDataInteractionWrapper.onBrickAtPosition(brickPosition).onSpinner(brickSpinnerViewId) |
| 100 | + .performSelectNameable(secondItem) |
| 101 | + |
| 102 | + onView(withId(R.id.menu_undo)) |
| 103 | + .perform(waitFor(isDisplayed(), waitThreshold)) |
| 104 | + |
| 105 | + BrickDataInteractionWrapper.onBrickAtPosition(brickPosition).onSpinner(brickSpinnerViewId) |
| 106 | + .performSelectNameable(firstItem) |
| 107 | + |
| 108 | + onView(withId(R.id.menu_undo)) |
| 109 | + .perform(waitFor(isDisplayed(), waitThreshold)) |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun testUndoSpinnerAction() { |
| 114 | + BrickDataInteractionWrapper.onBrickAtPosition(brickPosition).onSpinner(brickSpinnerViewId) |
| 115 | + .performSelectNameable(secondItem) |
| 116 | + onView(withId(R.id.menu_undo)) |
| 117 | + .perform(waitFor(isDisplayed(), waitThreshold)) |
| 118 | + onView(withId(R.id.menu_undo)).perform(click()) |
| 119 | + onView(withId(R.id.menu_undo)).check(doesNotExist()) |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun testUndoSpinnerNotVisibleAfterNewOptionSelected() { |
| 124 | + if (brickPosition >= 4) { |
| 125 | + val newItem = "new" |
| 126 | + BrickDataInteractionWrapper.onBrickAtPosition(brickPosition) |
| 127 | + .onVariableSpinner(brickSpinnerViewId).performNewVariable(newItem) |
| 128 | + onView(withId(R.id.menu_undo)) |
| 129 | + .check(doesNotExist()) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private fun createProject() { |
| 134 | + val script = |
| 135 | + createProjectAndGetStartScript(ActionBarUndoSpinnerTest::class.java.simpleName) |
| 136 | + val currentProject = projectManager.currentProject |
| 137 | + val currentSprite = projectManager.currentSprite |
| 138 | + |
| 139 | + currentProject.addUserVariable(UserVariable(firstItem)) |
| 140 | + currentProject.addUserVariable(UserVariable(secondItem)) |
| 141 | + |
| 142 | + val soundDirectory = |
| 143 | + File(currentProject.defaultScene.directory, Constants.SOUND_DIRECTORY_NAME) |
| 144 | + val imageDirectory = |
| 145 | + File(currentProject.defaultScene.directory, Constants.IMAGE_DIRECTORY_NAME) |
| 146 | + |
| 147 | + val firstSoundFile = File(soundDirectory, "abc.mp3") |
| 148 | + val secondSoundFile = File(soundDirectory, "def.mp3") |
| 149 | + val firstLookFile = File(imageDirectory, "abc.png") |
| 150 | + val secondLookFile = File(imageDirectory, "def.png") |
| 151 | + |
| 152 | + val soundInfoList = currentSprite.soundList |
| 153 | + soundInfoList.add(SoundInfo(firstItem, firstSoundFile)) |
| 154 | + soundInfoList.add(SoundInfo(secondItem, secondSoundFile)) |
| 155 | + |
| 156 | + val lookDataList = currentSprite.lookList |
| 157 | + lookDataList.add(LookData(firstItem, firstLookFile)) |
| 158 | + lookDataList.add(LookData(secondItem, secondLookFile)) |
| 159 | + |
| 160 | + val nfcTagDataList = currentSprite.nfcTagList |
| 161 | + val firstTagData = NfcTagData() |
| 162 | + firstTagData.name = firstItem |
| 163 | + firstTagData.nfcTagUid = NfcHandler.byteArrayToHex(UiNFCTestUtils.FIRST_TEST_TAG_ID.toByteArray()) |
| 164 | + val secondTagData = NfcTagData() |
| 165 | + secondTagData.name = secondItem |
| 166 | + secondTagData.nfcTagUid = NfcHandler.byteArrayToHex(UiNFCTestUtils.SECOND_TEST_TAG_ID.toByteArray()) |
| 167 | + nfcTagDataList.add(firstTagData) |
| 168 | + nfcTagDataList.add(secondTagData) |
| 169 | + val whenNfcBrick = WhenNfcBrick() |
| 170 | + whenNfcBrick.onItemSelected(R.id.brick_when_nfc_spinner, firstTagData) |
| 171 | + |
| 172 | + script.addBrick(PlaySoundBrick()) |
| 173 | + script.addBrick(SetLookBrick()) |
| 174 | + script.addBrick(whenNfcBrick) |
| 175 | + script.addBrick(SetVariableBrick(Formula(1), UserVariable(firstItem))) |
| 176 | + script.addBrick(BroadcastBrick(firstItem)) |
| 177 | + script.addBrick(BroadcastBrick(secondItem)) |
| 178 | + |
| 179 | + XstreamSerializer.getInstance().saveProject(currentProject) |
| 180 | + } |
| 181 | + |
| 182 | + companion object { |
| 183 | + @JvmStatic |
| 184 | + @Parameterized.Parameters(name = "{0}") |
| 185 | + fun parameters() = listOf( |
| 186 | + arrayOf( |
| 187 | + PlaySoundBrick::class.java.name, 1, R.id.brick_play_sound_spinner |
| 188 | + ), arrayOf<Any?>( |
| 189 | + SetLookBrick::class.java.name, 2, R.id.brick_set_look_spinner |
| 190 | + ), arrayOf<Any?>( |
| 191 | + WhenNfcBrick::class.java.name, 3, R.id.brick_when_nfc_spinner |
| 192 | + ), arrayOf<Any?>( |
| 193 | + SetVariableBrick::class.java.name, 4, R.id.set_variable_spinner |
| 194 | + ), arrayOf<Any?>( |
| 195 | + BroadcastBrick::class.java.name, 5, R.id.brick_broadcast_spinner |
| 196 | + ) |
| 197 | + ) |
| 198 | + } |
| 199 | +} |
0 commit comments