/* * Catroid: An on-device visual programming system for Android devices * Copyright (C) 2010-2016 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/>. */ /** * Created by Robert Riedl on 29.07.2015. */ package org.catrobat.catroid.uitest.content.brick; import android.widget.EditText; import android.widget.ListView; import org.catrobat.catroid.ProjectManager; import org.catrobat.catroid.R; import org.catrobat.catroid.content.Project; import org.catrobat.catroid.content.Script; import org.catrobat.catroid.content.SingleSprite; import org.catrobat.catroid.content.Sprite; import org.catrobat.catroid.content.StartScript; import org.catrobat.catroid.content.bricks.Brick; import org.catrobat.catroid.content.bricks.HideTextBrick; import org.catrobat.catroid.content.bricks.SetVariableBrick; import org.catrobat.catroid.content.bricks.ShowTextBrick; import org.catrobat.catroid.content.bricks.UserVariableBrick; import org.catrobat.catroid.content.bricks.WaitBrick; import org.catrobat.catroid.formulaeditor.UserVariable; import org.catrobat.catroid.stage.StageActivity; import org.catrobat.catroid.test.utils.Reflection; import org.catrobat.catroid.ui.ScriptActivity; import org.catrobat.catroid.ui.adapter.BrickAdapter; import org.catrobat.catroid.uitest.util.BaseActivityInstrumentationTestCase; import org.catrobat.catroid.uitest.util.UiTestUtils; import java.util.ArrayList; public class ShowTextBrickTest extends BaseActivityInstrumentationTestCase<ScriptActivity> { private Project project; private SetVariableBrick setVariableBrick; private ShowTextBrick showTextBrick; private HideTextBrick hideTextBrick; private WaitBrick waitBrick; public ShowTextBrickTest() { super(ScriptActivity.class); } @Override public void setUp() throws Exception { // normally super.setUp should be called first // but kept the test failing due to view is null // when starting in ScriptActivity createProject(); super.setUp(); } public void testShowHideBrick() { ListView dragDropListView = UiTestUtils.getScriptListView(solo); BrickAdapter adapter = (BrickAdapter) dragDropListView.getAdapter(); int childrenCount = adapter.getChildCountFromLastGroup(); int groupCount = adapter.getScriptCount(); assertEquals("Incorrect number of bricks.", 5, dragDropListView.getChildCount()); assertEquals("Incorrect number of bricks.", 4, childrenCount); ArrayList<Brick> projectBrickList = project.getDefaultScene().getSpriteList().get(0).getScript(0).getBrickList(); assertEquals("Incorrect number of bricks.", 4, projectBrickList.size()); assertEquals("Wrong Brick instance.", projectBrickList.get(0), adapter.getChild(groupCount - 1, 0)); assertNotNull("TextView does not exist.", solo.getText(solo.getString(R.string.brick_set_variable))); assertTrue("ScriptFragment not visible", solo.waitForText(solo.getString(R.string.brick_set_variable))); UiTestUtils.testBrickWithFormulaEditor(solo, ProjectManager.getInstance().getCurrentSprite(), R.id.brick_show_variable_edit_text_x, 10, Brick.BrickField.X_POSITION, showTextBrick); UiTestUtils.testBrickWithFormulaEditor(solo, ProjectManager.getInstance().getCurrentSprite(), R.id.brick_show_variable_edit_text_y, 10, Brick.BrickField.Y_POSITION, showTextBrick); UiTestUtils.clickOnBottomBar(solo, R.id.button_play); solo.waitForActivity(StageActivity.class.getSimpleName()); solo.goBack(); solo.waitForText(solo.getString(R.string.stage_dialog_back)); solo.clickOnText(solo.getString(R.string.stage_dialog_back)); solo.clickOnText(getInstrumentation().getTargetContext().getString( R.string.brick_variable_spinner_create_new_variable), 1, true); EditText varName = (EditText) solo.getView(R.id.dialog_formula_editor_data_name_edit_text); solo.enterText(varName, "newVar"); solo.clickOnButton(solo.getString(R.string.ok)); // solo.clickOnText("newVar"); // ONLY on Android 5.0.1 UiTestUtils.testBrickWithFormulaEditor(solo, ProjectManager.getInstance().getCurrentSprite(), R.id.brick_set_variable_edit_text, 12345, Brick.BrickField.VARIABLE, setVariableBrick); UserVariable userVariable = (UserVariable) Reflection.getPrivateField(UserVariableBrick.class, setVariableBrick, "userVariable"); assertNotNull("UserVariable is null", userVariable); assertTrue("UserVariable Name not as expected", userVariable.getName().equals("newVar")); UiTestUtils.clickOnBottomBar(solo, R.id.button_play); } private void createProject() { project = new Project(null, UiTestUtils.DEFAULT_TEST_PROJECT_NAME); Sprite sprite = new SingleSprite("cat"); Script script = new StartScript(); setVariableBrick = new SetVariableBrick(); script.addBrick(setVariableBrick); showTextBrick = new ShowTextBrick(0, 0); script.addBrick(showTextBrick); waitBrick = new WaitBrick(3000); script.addBrick(waitBrick); hideTextBrick = new HideTextBrick(); script.addBrick(hideTextBrick); sprite.addScript(script); project.getDefaultScene().addSprite(sprite); ProjectManager.getInstance().setProject(project); ProjectManager.getInstance().setCurrentSprite(sprite); ProjectManager.getInstance().setCurrentScript(script); } }