package com.mumux.androidtesting.actions;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.mumux.androidtesting.actions.impl.ActionUtils;
import com.mumux.androidtesting.actions.impl.PressAction;
import com.mumux.androidtesting.actions.impl.TempoAction;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest({PressAction.class, ActionUtils.class})
public class ActionImplTest {
@Before
public void setup() {
UiAutomatorTestCase uiAutomatorTestCase = mock(UiAutomatorTestCase.class);
UiDevice uiDevice = mock(UiDevice.class);
when(uiAutomatorTestCase.getUiDevice()).thenReturn(uiDevice);
}
@Test
public void testTempo() {
Action action = new TempoAction();
action.setValues(new Object[]{1000});
Long start = System.currentTimeMillis();
String error = action.run(null, null);
Long end = System.currentTimeMillis();
Assert.assertNull(error);
Assert.assertTrue((end - start) >= 1000);
Assert.assertTrue((end - start) < 1500);
}
@Test
public void testPress() throws Exception {
Action action;
// Mocking
UiObject uiObject = mock(UiObject.class);
mockStatic(PressAction.class);
when(PressAction.getUiObject("Press Me")).thenReturn(uiObject);
when(PressAction.getUiObject("Not Found")).thenReturn(null);
// Test Success
action = new PressAction();
action.setValues(new Object[]{"Press Me"});
Assert.assertNull(action.run(null, null));
// Test Failure
action = new PressAction();
action.setValues(new Object[]{"Not Found"});
Assert.assertEquals("UI Object 'Not Found' not found", action.run(null, null));
}
}