|
| 1 | +/* |
| 2 | + * Catroid: An on-device visual programming system for Android devices |
| 3 | + * Copyright (C) 2010-2022 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 | + |
| 24 | +package org.catrobat.catroid.uiespresso.content.brick.app |
| 25 | + |
| 26 | +import androidx.test.core.app.ApplicationProvider.getApplicationContext |
| 27 | +import androidx.test.espresso.Espresso.onView |
| 28 | +import androidx.test.espresso.Espresso.pressBack |
| 29 | +import androidx.test.espresso.NoMatchingViewException |
| 30 | +import androidx.test.espresso.action.ViewActions.click |
| 31 | +import androidx.test.espresso.intent.Intents |
| 32 | +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed |
| 33 | +import androidx.test.espresso.matcher.ViewMatchers.isRoot |
| 34 | +import androidx.test.espresso.matcher.ViewMatchers.withId |
| 35 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 36 | +import com.google.common.base.Stopwatch |
| 37 | +import org.catrobat.catroid.ProjectManager |
| 38 | +import org.catrobat.catroid.R |
| 39 | +import org.catrobat.catroid.WaitForConditionAction.Companion.waitFor |
| 40 | +import org.catrobat.catroid.content.Project |
| 41 | +import org.catrobat.catroid.content.Sprite |
| 42 | +import org.catrobat.catroid.content.StartScript |
| 43 | +import org.catrobat.catroid.content.bricks.DeleteLookBrick |
| 44 | +import org.catrobat.catroid.content.bricks.PaintNewLookBrick |
| 45 | +import org.catrobat.catroid.test.utils.TestUtils |
| 46 | +import org.catrobat.catroid.testsuites.annotations.Cat.AppUi |
| 47 | +import org.catrobat.catroid.testsuites.annotations.Level.Smoke |
| 48 | +import org.catrobat.catroid.ui.SpriteActivity |
| 49 | +import org.catrobat.catroid.uiespresso.util.actions.CustomActions.wait |
| 50 | +import org.catrobat.catroid.uiespresso.util.rules.FragmentActivityTestRule |
| 51 | +import org.junit.After |
| 52 | +import org.junit.Assert.assertEquals |
| 53 | +import org.junit.Before |
| 54 | +import org.junit.Rule |
| 55 | +import org.junit.Test |
| 56 | +import org.junit.experimental.categories.Category |
| 57 | +import org.junit.runner.RunWith |
| 58 | +import org.koin.java.KoinJavaComponent.inject |
| 59 | +import java.util.concurrent.TimeUnit.MILLISECONDS |
| 60 | + |
| 61 | +@Category(AppUi::class, Smoke::class) |
| 62 | +@RunWith(AndroidJUnit4::class) |
| 63 | +class DeleteLookBrickTest { |
| 64 | + private lateinit var sprite: Sprite |
| 65 | + private lateinit var script: StartScript |
| 66 | + private val projectName = "DeleteLookBrickTest" |
| 67 | + private val projectManager by inject(ProjectManager::class.java) |
| 68 | + |
| 69 | + @get:Rule |
| 70 | + var baseActivityTestRule = FragmentActivityTestRule( |
| 71 | + SpriteActivity::class.java, |
| 72 | + SpriteActivity.EXTRA_FRAGMENT_POSITION, |
| 73 | + SpriteActivity.FRAGMENT_SCRIPTS |
| 74 | + ) |
| 75 | + |
| 76 | + @Before |
| 77 | + fun setUp() { |
| 78 | + val project = Project(getApplicationContext(), projectName) |
| 79 | + sprite = Sprite("testSprite") |
| 80 | + script = StartScript() |
| 81 | + script.addBrick(PaintNewLookBrick()) |
| 82 | + sprite.addScript(script) |
| 83 | + project.defaultScene.addSprite(sprite) |
| 84 | + projectManager.currentProject = project |
| 85 | + projectManager.currentSprite = sprite |
| 86 | + projectManager.currentlyEditedScene = project.defaultScene |
| 87 | + Intents.init() |
| 88 | + baseActivityTestRule.launchActivity() |
| 89 | + } |
| 90 | + |
| 91 | + @After |
| 92 | + fun tearDown() { |
| 93 | + Intents.release() |
| 94 | + baseActivityTestRule.finishActivity() |
| 95 | + TestUtils.deleteProjects(projectName) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun testPaintLookWithoutDelete() { |
| 100 | + onView(withId(R.id.button_play)) |
| 101 | + .perform(waitFor(isDisplayed(), 3000)) |
| 102 | + .perform(click()) |
| 103 | + waitOnViewAndClick(R.id.pocketpaint_btn_skip) |
| 104 | + |
| 105 | + onView(withId(R.id.pocketpaint_drawing_surface_view)) |
| 106 | + .perform(waitFor(isDisplayed(), 3000)) |
| 107 | + .perform(click()) |
| 108 | + pressBack() |
| 109 | + |
| 110 | + onView(isRoot()).perform(wait(500)) |
| 111 | + assertEquals(1, sprite.lookList?.size) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun testPaintAndDeleteLook() { |
| 116 | + script.addBrick(DeleteLookBrick()) |
| 117 | + |
| 118 | + onView(withId(R.id.button_play)) |
| 119 | + .perform(waitFor(isDisplayed(), 3000)) |
| 120 | + .perform(click()) |
| 121 | + waitOnViewAndClick(R.id.pocketpaint_btn_skip) |
| 122 | + |
| 123 | + onView(withId(R.id.pocketpaint_drawing_surface_view)) |
| 124 | + .perform(waitFor(isDisplayed(), 3000)) |
| 125 | + .perform(click()) |
| 126 | + pressBack() |
| 127 | + |
| 128 | + onView(isRoot()).perform(wait(500)) |
| 129 | + assertEquals(0, sprite.lookList?.size) |
| 130 | + } |
| 131 | + |
| 132 | + private fun waitOnViewAndClick(viewId: Int, timeout: Int = 1000): Boolean { |
| 133 | + val stopWatch = Stopwatch.createStarted() |
| 134 | + var viewFound: Boolean |
| 135 | + do { |
| 136 | + viewFound = true |
| 137 | + try { |
| 138 | + onView(withId(viewId)).perform(click()) |
| 139 | + } catch (_: NoMatchingViewException) { |
| 140 | + viewFound = false |
| 141 | + if (stopWatch.elapsed(MILLISECONDS) >= timeout) { |
| 142 | + break |
| 143 | + } |
| 144 | + } |
| 145 | + } while (!viewFound) |
| 146 | + return viewFound |
| 147 | + } |
| 148 | +} |
0 commit comments