|
| 1 | +/* |
| 2 | + * Catroid: An on-device visual programming system for Android devices |
| 3 | + * Copyright (C) 2010-2025 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.ui.fragment |
| 25 | + |
| 26 | +import android.content.Context |
| 27 | +import android.content.SharedPreferences |
| 28 | +import android.preference.PreferenceManager.getDefaultSharedPreferences |
| 29 | +import androidx.appcompat.widget.SwitchCompat |
| 30 | +import androidx.test.core.app.ApplicationProvider.getApplicationContext |
| 31 | +import androidx.test.espresso.Espresso.onView |
| 32 | +import androidx.test.espresso.action.ViewActions.click |
| 33 | +import androidx.test.espresso.assertion.ViewAssertions.matches |
| 34 | +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed |
| 35 | +import androidx.test.espresso.matcher.ViewMatchers.withId |
| 36 | +import androidx.test.espresso.matcher.ViewMatchers.withText |
| 37 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 38 | +import org.catrobat.catroid.R |
| 39 | +import org.catrobat.catroid.common.BrickValues |
| 40 | +import org.catrobat.catroid.common.Constants.CATROBAT_TERMS_OF_USE_ACCEPTED |
| 41 | +import org.catrobat.catroid.common.SharedPreferenceKeys.AGREED_TO_PRIVACY_POLICY_VERSION |
| 42 | +import org.catrobat.catroid.content.Project |
| 43 | +import org.catrobat.catroid.content.Script |
| 44 | +import org.catrobat.catroid.content.Sprite |
| 45 | +import org.catrobat.catroid.content.StartScript |
| 46 | +import org.catrobat.catroid.content.bricks.SetXBrick |
| 47 | +import org.catrobat.catroid.formulaeditor.Formula |
| 48 | +import org.catrobat.catroid.io.XstreamSerializer |
| 49 | +import org.catrobat.catroid.ui.MainMenuActivity |
| 50 | +import org.catrobat.catroid.ui.settingsfragments.SettingsFragment |
| 51 | +import org.catrobat.catroid.uiespresso.util.rules.BaseActivityTestRule |
| 52 | +import org.hamcrest.CoreMatchers.allOf |
| 53 | +import org.junit.After |
| 54 | +import org.junit.Before |
| 55 | +import org.junit.Rule |
| 56 | +import org.junit.Test |
| 57 | +import org.junit.runner.RunWith |
| 58 | + |
| 59 | +@RunWith(AndroidJUnit4::class) |
| 60 | +class ImportActorTest { |
| 61 | + private var bufferedChromeCastSetting = false |
| 62 | + private var bufferedPrivacyPolicyPreferenceSetting = 0 |
| 63 | + private lateinit var sharedPreferences: SharedPreferences |
| 64 | + private val newProjectName = ImportActorTest::class.simpleName |
| 65 | + private val project1Name = "project1" |
| 66 | + private val project2Name = "project2" |
| 67 | + private val sprite1Name = "sprite1" |
| 68 | + private val sprite2Name = "sprite2" |
| 69 | + |
| 70 | + @get:Rule |
| 71 | + var baseActivityTestRule = BaseActivityTestRule( |
| 72 | + MainMenuActivity::class.java, false, false |
| 73 | + ) |
| 74 | + |
| 75 | + @Before |
| 76 | + fun setUp() { |
| 77 | + sharedPreferences = getDefaultSharedPreferences(getApplicationContext()) |
| 78 | + |
| 79 | + bufferedChromeCastSetting = sharedPreferences.getBoolean(SettingsFragment.SETTINGS_CAST_GLOBALLY_ENABLED, false) |
| 80 | + bufferedPrivacyPolicyPreferenceSetting = sharedPreferences.getInt(AGREED_TO_PRIVACY_POLICY_VERSION, 0) |
| 81 | + |
| 82 | + sharedPreferences |
| 83 | + .edit() |
| 84 | + .putBoolean(SettingsFragment.SETTINGS_CAST_GLOBALLY_ENABLED, true) |
| 85 | + .putInt(AGREED_TO_PRIVACY_POLICY_VERSION, CATROBAT_TERMS_OF_USE_ACCEPTED) |
| 86 | + .commit() |
| 87 | + |
| 88 | + baseActivityTestRule.launchActivity(null) |
| 89 | + } |
| 90 | + |
| 91 | + @After |
| 92 | + fun tearDown() { |
| 93 | + sharedPreferences |
| 94 | + .edit() |
| 95 | + .putBoolean(SettingsFragment.SETTINGS_CAST_GLOBALLY_ENABLED, bufferedChromeCastSetting) |
| 96 | + .putInt(AGREED_TO_PRIVACY_POLICY_VERSION, bufferedPrivacyPolicyPreferenceSetting) |
| 97 | + .commit() |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun testImportFromLocal() { |
| 102 | + createProject(project1Name, sprite1Name) |
| 103 | + createProject(project2Name, sprite2Name) |
| 104 | + |
| 105 | + onView(withId(R.id.myProjectsTextView)) |
| 106 | + .perform(click()) |
| 107 | + |
| 108 | + onView(withText(project1Name)) |
| 109 | + .perform(click()) |
| 110 | + |
| 111 | + onView(withId(R.id.button_add)) |
| 112 | + .perform(click()) |
| 113 | + |
| 114 | + onView(withId(R.id.dialog_new_look_from_local)) |
| 115 | + .perform(click()) |
| 116 | + |
| 117 | + onView(withText(project2Name)) |
| 118 | + .perform(click()) |
| 119 | + |
| 120 | + onView(withId(R.id.place_visually_sprite_switch)) |
| 121 | + .perform(click()) |
| 122 | + |
| 123 | + onView(withText("OK")) |
| 124 | + .perform(click()) |
| 125 | + |
| 126 | + onView(allOf(withId(R.id.place_visually_sprite_switch), isDisplayed())) |
| 127 | + .check { view, _ -> |
| 128 | + val switchCompat = view as SwitchCompat |
| 129 | + if (switchCompat.isChecked) { |
| 130 | + switchCompat.callOnClick() // toggles it OFF |
| 131 | + } |
| 132 | + } |
| 133 | + onView(withText(sprite1Name)) |
| 134 | + .check(matches(isDisplayed())) |
| 135 | + |
| 136 | + onView(withText(sprite2Name)) |
| 137 | + .check(matches(isDisplayed())) |
| 138 | + } |
| 139 | + |
| 140 | + private fun createProject(projectName: String?, spriteName: String?) { |
| 141 | + val project = Project(getApplicationContext<Context?>(), projectName) |
| 142 | + val sprite = Sprite(spriteName) |
| 143 | + |
| 144 | + val script: Script = StartScript() |
| 145 | + script.addBrick(SetXBrick(Formula(BrickValues.X_POSITION))) |
| 146 | + script.addBrick(SetXBrick(Formula(BrickValues.X_POSITION))) |
| 147 | + sprite.addScript(script) |
| 148 | + |
| 149 | + project.defaultScene.addSprite(sprite) |
| 150 | + |
| 151 | + XstreamSerializer.getInstance().saveProject(project) |
| 152 | + } |
| 153 | +} |
0 commit comments