package com.mumux.androidtesting.runner; import com.android.uiautomator.core.UiDevice; import com.android.uiautomator.testrunner.UiAutomatorTestCase; import com.mumux.androidtesting.actions.Action; import com.mumux.androidtesting.scenario.OptionalAction; import com.mumux.androidtesting.scenario.Scenario; import com.mumux.androidtesting.scenario.ScenarioParser; import com.mumux.androidtesting.scenario.ScenarioParsingException; import org.json.JSONException; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.modules.junit4.PowerMockRunner; import java.io.File; import java.io.Reader; import java.io.StringReader; import static org.mockito.Mockito.*; @RunWith(PowerMockRunner.class) public class ScenarioRunnerTest { private final Action actionOK = spy(new Action("TEST_ACTION", false, null, null, null) { @Override public String run(UiAutomatorTestCase uiAutomatorTestCase, Runtime runtime) { return null; } }); private final Action actionNOK = spy(new Action("TEST_ACTION", false, null, null, null) { @Override public String run(UiAutomatorTestCase uiAutomatorTestCase, Runtime runtime) { return null; } }); private final File OUTPUT_DIR = new File("/tmp"); @Before public void setup() { when(actionOK.run(any(UiAutomatorTestCase.class), any(Runtime.class))).thenReturn(null); when(actionNOK.run(any(UiAutomatorTestCase.class), any(Runtime.class))).thenReturn("There is an error"); } @Test public void testErrorParsingInvalidScenario() throws JSONException { ScenarioRunner runner = new ScenarioRunner(null, null, false, OUTPUT_DIR); ScenarioTestCase testCase; // No title testCase = runner.run(new StringReader(""), 0); Assert.assertEquals(TestStatus.ERROR, testCase.status); Assert.assertEquals("Invalid Scenario - Missing title", testCase.message); // Action not found testCase = runner.run(new StringReader("# Error scenario\nXXX"), 0); Assert.assertEquals(TestStatus.ERROR, testCase.status); Assert.assertEquals("Invalid Scenario - Action XXX not found", testCase.message); // Invalid Action definition testCase = runner.run(new StringReader("# Error scenario\nPRESS"), 0); Assert.assertEquals(TestStatus.ERROR, testCase.status); Assert.assertEquals("Invalid Scenario - Invalid number of arguments for action PRESS", testCase.message); } @Test public void testErrorsParsingRootRequired() throws JSONException { ScenarioRunner runner = new ScenarioRunner(null, null, false, OUTPUT_DIR); ScenarioTestCase testCase = runner.run(new StringReader("# Error scenario\nDATA on"), 0); Assert.assertEquals(TestStatus.ERROR, testCase.status); Assert.assertEquals("Scenario requires a rooted device", testCase.message); } private ScenarioRunner getScenarioRunner(Scenario scenario) { ScenarioParser scenarioParser = mock(ScenarioParser.class); try { when((scenarioParser.parse(any(Reader.class)))).thenReturn(scenario); } catch (ScenarioParsingException e) { e.printStackTrace(); } UiAutomatorTestCase uiAutomatorTestCase = mock(UiAutomatorTestCase.class); UiDevice uiDevice = mock(UiDevice.class); when(uiAutomatorTestCase.getUiDevice()).thenReturn(uiDevice); ScenarioRunner runner = new ScenarioRunner(uiAutomatorTestCase, null, false, OUTPUT_DIR); runner.setScenarioParser(scenarioParser); return runner; } @Test public void testFailure() throws ScenarioParsingException, JSONException { Scenario scenario = new Scenario("Mock Scenario"); scenario.addAction(actionOK); scenario.addAction(actionNOK); scenario.addAction(actionOK); ScenarioTestCase testCase = getScenarioRunner(scenario).run(null, 0); Assert.assertEquals(TestStatus.FAILURE, testCase.status); Assert.assertNull(testCase.message); Assert.assertEquals(TestStatus.SUCCESS, testCase.actionTestCases.get(0).status); Assert.assertEquals(TestStatus.FAILURE, testCase.actionTestCases.get(1).status); Assert.assertEquals("There is an error", testCase.actionTestCases.get(1).message); Assert.assertNull(testCase.actionTestCases.get(2).status); } @Test public void testSuccess() throws ScenarioParsingException, JSONException { Scenario scenario = new Scenario("Mock Scenario"); scenario.addAction(actionOK); scenario.addAction(new OptionalAction(actionNOK)); scenario.addAction(actionOK); ScenarioTestCase testCase = getScenarioRunner(scenario).run(null, 0); Assert.assertEquals(TestStatus.SUCCESS, testCase.status); Assert.assertNull(testCase.message); Assert.assertEquals(TestStatus.SUCCESS, testCase.actionTestCases.get(0).status); Assert.assertEquals(TestStatus.FAILURE, testCase.actionTestCases.get(1).status); Assert.assertEquals(TestStatus.SUCCESS, testCase.actionTestCases.get(2).status); } }