Skip to content
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

CATROID-1493 Fix Look gets deleted in all projects #4637

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 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.sprite

import android.app.Activity
import android.app.Instrumentation
import android.content.Intent
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.platform.app.InstrumentationRegistry
import org.catrobat.catroid.ProjectManager
import org.catrobat.catroid.R
import org.catrobat.catroid.common.Constants
import org.catrobat.catroid.common.DefaultProjectHandler
import org.catrobat.catroid.content.Project
import org.catrobat.catroid.io.StorageOperations
import org.catrobat.catroid.io.XstreamSerializer
import org.catrobat.catroid.testsuites.annotations.Cat.AppUi
import org.catrobat.catroid.testsuites.annotations.Level.Smoke
import org.catrobat.catroid.ui.ProjectActivity
import org.catrobat.catroid.ui.ProjectListActivity
import org.catrobat.catroid.ui.recyclerview.controller.SpriteController
import org.catrobat.catroid.uiespresso.util.rules.FragmentActivityTestRule
import org.hamcrest.Matcher
import org.hamcrest.core.AllOf
import org.junit.After
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.experimental.categories.Category
import org.koin.java.KoinJavaComponent
import java.io.File

class DeleteImportedSpriteTest {
private lateinit var project: Project
private lateinit var localProject: Project
private var expectedIntent: Matcher<Intent>? = null
private var projectManager = KoinJavaComponent.inject(ProjectManager::class.java).value

private val projectName = javaClass.simpleName
private val tmpPath = File(
Constants.CACHE_DIRECTORY.absolutePath, "Pocket Code Test Temp"
)

@get:Rule
var baseActivityTestRule = FragmentActivityTestRule(
ProjectActivity::class.java,
ProjectActivity.EXTRA_FRAGMENT_POSITION,
ProjectActivity.FRAGMENT_SPRITES
)

@Before
fun setUp() {
createProjects(projectName)
baseActivityTestRule.launchActivity()
Intents.init()

expectedIntent = AllOf.allOf(IntentMatchers.hasExtra(
ProjectListActivity.IMPORT_LOCAL_INTENT,
baseActivityTestRule.activity.getString(R.string.import_sprite_from_project_launcher)
))

if (!tmpPath.exists()) {
tmpPath.mkdirs()
}

val resultData = Intent()
resultData.putExtra(ProjectListActivity.IMPORT_LOCAL_INTENT,
localProject.directory.absoluteFile.absolutePath)

val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)
Intents.intending(expectedIntent).respondWith(result)
}

@After
@Throws(Exception::class)
fun tearDown() {
Intents.release()
baseActivityTestRule.finishActivity()
StorageOperations.deleteDir(tmpPath)
}

@Category(AppUi::class, Smoke::class)
@Test
fun testDeleteOriginalAndImportedSprites() {
importSprite()
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
Espresso.onView(ViewMatchers.withId(R.id.confirm)).perform(ViewActions.click())
SpriteController().delete(localProject.defaultScene.getSprite("Animal"))
project.defaultScene.spriteList[1].lookList.forEach {
assertTrue(it.file.exists())
}
project.defaultScene.spriteList[1].soundList.forEach {
assertTrue(it.file.exists())
}
}

@Category(AppUi::class, Smoke::class)
@Test
fun testOriginalLooksAndSoundsExistAfterDeleteImport() {
importSprite()
Espresso.onView(ViewMatchers.withId(R.id.confirm)).perform(ViewActions.click())
SpriteController().delete(project.defaultScene.getSprite("Animal"))
localProject.defaultScene.spriteList[1].lookList.forEach {
assertTrue(it.file.exists())
}
localProject.defaultScene.spriteList[1].soundList.forEach {
assertTrue(it.file.exists())
}
}

private fun importSprite() {
Espresso.onView(ViewMatchers.withId(R.id.button_add))
.perform(ViewActions.click())
Espresso.onView(ViewMatchers.withId(R.id.dialog_new_look_from_local))
.perform(ViewActions.click())
Intents.intended(expectedIntent)
Espresso.onView(withText(R.string.new_sprite_dialog_title))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
Espresso.onView(withText(R.string.ok))
.perform(ViewActions.click())
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
}

private fun createProjects(projectName: String) {
project = Project(ApplicationProvider.getApplicationContext(), projectName)
projectManager.currentProject = project
projectManager.currentlyEditedScene = project.defaultScene
XstreamSerializer.getInstance().saveProject(project)
localProject = DefaultProjectHandler.createAndSaveDefaultProject(
"local",
ApplicationProvider.getApplicationContext(),
false
)
localProject.defaultScene.spriteList.removeAt(2)
localProject.defaultScene.spriteList.removeAt(1)
XstreamSerializer.getInstance().saveProject(localProject)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import android.net.Uri
import android.util.Log
mseiser marked this conversation as resolved.
Show resolved Hide resolved
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.intent.Intents
import androidx.test.runner.AndroidJUnit4
import org.catrobat.catroid.ProjectManager
import org.catrobat.catroid.common.Constants
import org.catrobat.catroid.common.DefaultProjectHandler
Expand All @@ -50,20 +49,18 @@ import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.koin.java.KoinJavaComponent
import java.io.File

@RunWith(AndroidJUnit4::class)
class ImportObjectIntoProjectFromContextMenuTest {
var project: Project? = null
var importedProject: Project? = null
var importName = "IMPORTED"
var defaultProjectName = "defaultProject"
private var importedProject: Project? = null
private var importName = "IMPORTED"
private var defaultProjectName = "defaultProject"
var uri: Uri? = null
var spriteToBeImported: Sprite? = null
private var spriteToBeImported: Sprite? = null
private lateinit var scriptForVisualPlacement: Script
val TAG: String = ImportObjectIntoProjectTest::class.java.simpleName
val tag: String = ImportObjectIntoProjectTest::class.java.simpleName
private val projectManager = KoinJavaComponent.inject(ProjectManager::class.java)

@get:Rule
Expand All @@ -79,7 +76,7 @@ class ImportObjectIntoProjectFromContextMenuTest {
try {
Constants.MEDIA_LIBRARY_CACHE_DIRECTORY.mkdirs()
} catch (e: Exception) {
Log.e(TAG, Log.getStackTraceString(e))
Log.e(tag, Log.getStackTraceString(e))
}

project = DefaultProjectHandler
Expand Down Expand Up @@ -114,7 +111,7 @@ class ImportObjectIntoProjectFromContextMenuTest {
baseActivityTestRule.launchActivity()
}

fun initProjectVars() {
private fun initProjectVars() {
spriteToBeImported = importedProject!!.defaultScene.spriteList[1]

spriteToBeImported!!.userVariables.add(UserVariable("localVariable1"))
Expand All @@ -124,8 +121,8 @@ class ImportObjectIntoProjectFromContextMenuTest {

importedProject!!.addUserVariable(UserVariable("globalVariable1", 1))
importedProject!!.addUserVariable(UserVariable("globalVariable2", 2))
importedProject!!.addUserList(UserList("globalListe1"))
importedProject!!.addUserList(UserList("globalListe2"))
importedProject!!.addUserList(UserList("globalList1"))
importedProject!!.addUserList(UserList("globalList2"))

scriptForVisualPlacement = StartScript().apply {
addBrick(PlaceAtBrick(100, 200))
Expand Down Expand Up @@ -244,7 +241,7 @@ class ImportObjectIntoProjectFromContextMenuTest {

val currentScene = project!!.defaultScene
val activity = baseActivityTestRule.activity
val resolvedFileName = StorageOperations.resolveFileName(activity.getContentResolver(), uri)
val resolvedFileName = StorageOperations.resolveFileName(activity.contentResolver, uri)
val lookFileName: String? = resolvedFileName

val importProjectHelper = ImportProjectHelper(
Expand Down
Loading