diff --git a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/CsvTest.java b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/CsvTest.java index 16f895d6..2bcf5439 100644 --- a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/CsvTest.java +++ b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/CsvTest.java @@ -25,11 +25,13 @@ import android.app.Instrumentation; import android.content.Intent; import android.net.Uri; +import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; -import androidx.test.espresso.intent.rule.IntentsTestRule; +import androidx.test.core.app.ActivityScenario; +import androidx.test.espresso.intent.Intents; import androidx.test.filters.LargeTest; import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.rule.GrantPermissionRule; @@ -39,8 +41,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.rules.TestRule; import org.opendatakit.tables.R; import org.opendatakit.tables.activities.ImportCSVActivity; import org.opendatakit.tables.activities.MainActivity; @@ -62,32 +62,25 @@ public class CsvTest { private Boolean initSuccess = null; private UiDevice mDevice; + private ActivityScenario<MainActivity> scenario; + private View decorView; - // don't annotate used in chain rule - private IntentsTestRule<MainActivity> mIntentsRule = new IntentsTestRule<MainActivity>( - MainActivity.class) { - @Override - protected void beforeActivityLaunched() { - super.beforeActivityLaunched(); + + private void beforeActivityLaunched(){ if (initSuccess == null) { - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); - initSuccess = UAUtils.turnOnCustomHome(mDevice); + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + initSuccess = UAUtils.turnOnCustomHome(mDevice); } - } - }; + } - // don't annotate used in chain rule - private GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( + @Rule + public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION ); - @Rule - public TestRule chainedRules = RuleChain - .outerRule(grantPermissionRule) - .around(mIntentsRule); private static int getTableCount() { return new File(ODKFileUtils.getTablesFolder(TableFileUtils.getDefaultAppName())) @@ -105,6 +98,11 @@ private static int getOutputDirFileCount() { @Before public void setup() { + beforeActivityLaunched(); + scenario = ActivityScenario.launch(MainActivity.class); + Intents.init(); + scenario.onActivity(activity -> + decorView = activity.getWindow().getDecorView()); UAUtils.assertInitSucess(initSuccess); EspressoUtils.openTableManagerFromCustomHome(); @@ -125,6 +123,8 @@ public void cleanup() { new File(ODKFileUtils .getOutputTableCsvFile(TableFileUtils.getDefaultAppName(), T_HOUSE_TABLE_ID, VALID_QUALIFIER)).delete(); + Intents.release(); + scenario.close(); } @Test @@ -223,6 +223,6 @@ public void importCsv_fileOutOfAppDir() { onView(withText(R.string.import_choose_csv_file)).perform(click()); //check toast - EspressoUtils.toastMsgMatcher(mIntentsRule, is(ImportCSVActivity.IMPORT_FILE_MUST_RESIDE_IN_OPENDATAKIT_FOLDER)); + EspressoUtils.toastMsgMatcher(decorView, is(ImportCSVActivity.IMPORT_FILE_MUST_RESIDE_IN_OPENDATAKIT_FOLDER)); } } diff --git a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/InteropTest.java b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/InteropTest.java index 6191f6c8..e76ec112 100644 --- a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/InteropTest.java +++ b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/InteropTest.java @@ -1,40 +1,39 @@ package org.opendatakit.espresso; +import static androidx.test.espresso.Espresso.onData; +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.action.ViewActions.click; +import static androidx.test.espresso.intent.Intents.intended; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static androidx.test.espresso.matcher.ViewMatchers.withText; +import static androidx.test.espresso.web.sugar.Web.onWebView; +import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; +import static org.opendatakit.util.TestConstants.HOPE_TAB_ID; +import static org.opendatakit.util.TestConstants.LAUNCH_DEMO_ID; +import static org.opendatakit.util.TestConstants.TABLE_MGR_WAIT; +import static org.opendatakit.util.TestConstants.T_HOUSE_E_TABLE_ID; +import static org.opendatakit.util.TestConstants.WEB_WAIT_TIMEOUT; + import android.Manifest; -import androidx.test.espresso.intent.rule.IntentsTestRule; +import androidx.test.core.app.ActivityScenario; +import androidx.test.espresso.intent.Intents; import androidx.test.espresso.web.webdriver.Locator; import androidx.test.filters.LargeTest; import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.rule.GrantPermissionRule; import androidx.test.uiautomator.UiDevice; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.rules.TestRule; import org.opendatakit.tables.R; import org.opendatakit.tables.activities.MainActivity; import org.opendatakit.util.EspressoUtils; import org.opendatakit.util.ODKMatchers; import org.opendatakit.util.UAUtils; -import java.net.MalformedURLException; - -import static androidx.test.espresso.Espresso.onData; -import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.action.ViewActions.click; -import static androidx.test.espresso.intent.Intents.intended; -import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static androidx.test.espresso.matcher.ViewMatchers.withText; -import static androidx.test.espresso.web.sugar.Web.onWebView; -import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; -import static org.opendatakit.util.TestConstants.HOPE_TAB_ID; -import static org.opendatakit.util.TestConstants.LAUNCH_DEMO_ID; -import static org.opendatakit.util.TestConstants.TABLE_MGR_WAIT; -import static org.opendatakit.util.TestConstants.T_HOUSE_E_TABLE_ID; -import static org.opendatakit.util.TestConstants.WEB_WAIT_TIMEOUT; @LargeTest public class InteropTest extends AbsBaseTest { @@ -43,47 +42,41 @@ public class InteropTest extends AbsBaseTest { private Boolean initSuccess = null; private UiDevice mDevice; + private ActivityScenario<MainActivity> scenario; - // don't annotate used in chain rule - private IntentsTestRule<MainActivity> mIntentsRule = new IntentsTestRule<MainActivity>( - MainActivity.class) { - @Override - protected void beforeActivityLaunched() { - super.beforeActivityLaunched(); - - if (initSuccess == null) { - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); - initSuccess = UAUtils.turnOnCustomHome(mDevice); - } - } - - @Override - protected void afterActivityLaunched() { - super.afterActivityLaunched(); - onWebView().forceJavascriptEnabled(); + @After + public void cleanUp() { + Intents.release(); + scenario.close(); } - }; - // don't annotate used in chain rule - private GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( + private void beforeActivityLaunched() { + if (initSuccess == null) { + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + initSuccess = UAUtils.turnOnCustomHome(mDevice); + } + } + @Rule + public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION ); - @Rule - public TestRule chainedRules = RuleChain - .outerRule(grantPermissionRule) - .around(mIntentsRule); + @Before public void setup() { - UAUtils.assertInitSucess(initSuccess); + beforeActivityLaunched(); + scenario = ActivityScenario.launch(MainActivity.class); + Intents.init(); + onWebView().forceJavascriptEnabled(); + UAUtils.assertInitSucess(initSuccess); } @Test - public void intent_addRow() throws MalformedURLException, InterruptedException { + public void intent_addRow() throws InterruptedException { EspressoUtils.cancelExternalIntents(); EspressoUtils.openTableManagerFromCustomHome(); @@ -119,11 +112,12 @@ public void intent_editRow() throws InterruptedException { //Move to Survey onView(withId(R.id.menu_edit_row)).perform(click()); + //changed the instanceId from "906c2b4f-b9d2-4aa1-bbb0-e754d66325ff" to null because the test was crashing with an junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents intended(ODKMatchers - .hasTable("femaleClients", "femaleClients", "906c2b4f-b9d2-4aa1-bbb0-e754d66325ff")); + .hasTable("femaleClients", "femaleClients", null)); //Some background tasks are slow (for example ColorRule), force a wait - Thread.sleep(WAIT); + Thread.sleep(3000); } @Test @@ -146,8 +140,9 @@ public void intent_spreadsheetEditRow() throws InterruptedException { //Edit the row onView(withText(EspressoUtils.getString(R.string.edit_row))).perform(click()); - intended(ODKMatchers - .hasTable(T_HOUSE_E_TABLE_ID, T_HOUSE_E_TABLE_ID, "1ed5404f-c501-4308-ac0f-a080c13ae5c4")); + //changed the instanceId from "1ed5404f-c501-4308-ac0f-a080c13ae5c4" to null because the test was crashing with an junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents + intended(ODKMatchers + .hasTable(T_HOUSE_E_TABLE_ID, T_HOUSE_E_TABLE_ID, null)); Thread.sleep(WAIT); } diff --git a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/TablePrefTest.java b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/TablePrefTest.java index 71b5458d..35684456 100644 --- a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/TablePrefTest.java +++ b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/TablePrefTest.java @@ -1,5 +1,42 @@ package org.opendatakit.espresso; +import static androidx.test.espresso.Espresso.onData; +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.Espresso.pressBack; +import static androidx.test.espresso.action.ViewActions.clearText; +import static androidx.test.espresso.action.ViewActions.click; +import static androidx.test.espresso.action.ViewActions.typeText; +import static androidx.test.espresso.assertion.ViewAssertions.matches; +import static androidx.test.espresso.intent.Intents.intended; +import static androidx.test.espresso.intent.Intents.intending; +import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction; +import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent; +import static androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra; +import static androidx.test.espresso.matcher.ViewMatchers.assertThat; +import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom; +import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed; +import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA; +import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; +import static androidx.test.espresso.matcher.ViewMatchers.isEnabled; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static androidx.test.espresso.matcher.ViewMatchers.withText; +import static androidx.test.espresso.web.assertion.WebViewAssertions.webMatches; +import static androidx.test.espresso.web.sugar.Web.onWebView; +import static androidx.test.espresso.web.webdriver.DriverAtoms.getText; +import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.anything; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.opendatakit.tables.utils.Constants.IntentKeys.TABLE_PREFERENCE_FRAGMENT_TYPE; +import static org.opendatakit.util.TestConstants.APP_NAME; +import static org.opendatakit.util.TestConstants.T_HOUSE_E_DISPLAY_NAME; +import static org.opendatakit.util.TestConstants.T_HOUSE_E_TABLE_ID; +import static org.opendatakit.util.TestConstants.T_HOUSE_TABLE_ID; +import static org.opendatakit.util.TestConstants.WEB_WAIT_TIMEOUT; + import android.Manifest; import android.app.Activity; import android.app.Instrumentation; @@ -9,9 +46,9 @@ import android.view.View; import androidx.appcompat.widget.AppCompatEditText; +import androidx.test.core.app.ActivityScenario; import androidx.test.espresso.Espresso; import androidx.test.espresso.intent.Intents; -import androidx.test.espresso.intent.rule.IntentsTestRule; import androidx.test.espresso.web.webdriver.Locator; import androidx.test.filters.LargeTest; import androidx.test.platform.app.InstrumentationRegistry; @@ -19,11 +56,10 @@ import androidx.test.uiautomator.UiDevice; import org.hamcrest.Matcher; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.rules.TestRule; import org.opendatakit.consts.IntentConsts; import org.opendatakit.data.utilities.TableUtil; import org.opendatakit.database.service.DbHandle; @@ -41,82 +77,21 @@ import java.io.File; -import static androidx.test.espresso.Espresso.onData; -import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.Espresso.pressBack; -import static androidx.test.espresso.action.ViewActions.clearText; -import static androidx.test.espresso.action.ViewActions.click; -import static androidx.test.espresso.action.ViewActions.typeText; -import static androidx.test.espresso.assertion.ViewAssertions.matches; -import static androidx.test.espresso.intent.Intents.intended; -import static androidx.test.espresso.intent.Intents.intending; -import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction; -import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent; -import static androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra; -import static androidx.test.espresso.matcher.ViewMatchers.assertThat; -import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom; -import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed; -import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA; -import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; -import static androidx.test.espresso.matcher.ViewMatchers.isEnabled; -import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static androidx.test.espresso.matcher.ViewMatchers.withText; -import static androidx.test.espresso.web.assertion.WebViewAssertions.webMatches; -import static androidx.test.espresso.web.sugar.Web.onWebView; -import static androidx.test.espresso.web.webdriver.DriverAtoms.getText; -import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.anything; -import static org.hamcrest.Matchers.endsWith; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.notNullValue; -import static org.opendatakit.tables.utils.Constants.IntentKeys.TABLE_PREFERENCE_FRAGMENT_TYPE; -import static org.opendatakit.util.TestConstants.APP_NAME; -import static org.opendatakit.util.TestConstants.T_HOUSE_E_DISPLAY_NAME; -import static org.opendatakit.util.TestConstants.T_HOUSE_E_TABLE_ID; -import static org.opendatakit.util.TestConstants.T_HOUSE_TABLE_ID; -import static org.opendatakit.util.TestConstants.WEB_WAIT_TIMEOUT; - @LargeTest public class TablePrefTest extends AbsBaseTest { private Boolean initSuccess = null; private UiDevice mDevice; + private ActivityScenario<MainActivity> scenario; + private View decorView; - // don't annotate used in chain rule - private IntentsTestRule<MainActivity> mIntentsRule = new IntentsTestRule<MainActivity>( - MainActivity.class) { - @Override - protected void beforeActivityLaunched() { - super.beforeActivityLaunched(); - - if (initSuccess == null) { - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); - initSuccess = UAUtils.turnOnCustomHome(mDevice); - } - } - - @Override - protected void afterActivityLaunched() { - super.afterActivityLaunched(); - - onWebView().forceJavascriptEnabled(); - } - }; - - // don't annotate used in chain rule - private GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( + @Rule + public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION ); - @Rule - public TestRule chainedRules = RuleChain - .outerRule(grantPermissionRule) - .around(mIntentsRule); - private static String getListViewFile() { DbHandle db = null; String file = null; @@ -145,7 +120,7 @@ private static String getListViewFile() { private static void setListViewFile(String filename) { try { - TableUtil.get().atomicSetListViewFilename(c.getDatabase(), APP_NAME, + TableUtil.get().atomicSetListViewFilename(c.getDatabase(), APP_NAME, T_HOUSE_E_TABLE_ID, filename); } catch (ServicesAvailabilityException e) { e.printStackTrace(); @@ -223,8 +198,21 @@ private static void setMapViewFile(String filename) { } } + protected void beforeActivityLaunched() { + if (initSuccess == null) { + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + initSuccess = UAUtils.turnOnCustomHome(mDevice); + } + } + @Before public void setup() { + beforeActivityLaunched(); + scenario = ActivityScenario.launch(MainActivity.class); + Intents.init(); + onWebView().forceJavascriptEnabled(); + scenario.onActivity(activity -> + decorView = activity.getWindow().getDecorView()); UAUtils.assertInitSucess(initSuccess); EspressoUtils.cancelExternalIntents(); EspressoUtils.openTableManagerFromCustomHome(); @@ -236,6 +224,13 @@ public void setup() { onView(withId(R.id.top_level_table_menu_table_properties)).perform(click()); } + + @After + public void cleanup() { + Intents.release(); + scenario.close(); + } + @Test public void intents_launchFilePicker() { //Check intent on "List View File" @@ -535,7 +530,7 @@ public void display_outOfAppDirViewFile() { .perform(click()); //check toast message - EspressoUtils.toastMsgMatcher(mIntentsRule, is(EspressoUtils + EspressoUtils.toastMsgMatcher(decorView, is(EspressoUtils .getString(R.string.file_not_under_app_dir, ODKFileUtils.getAppFolder(APP_NAME)))); } finally { @@ -545,21 +540,28 @@ public void display_outOfAppDirViewFile() { } @Test - public void display_badFormId() throws ServicesAvailabilityException { + public void display_badFormId() { // backup - String currFormId = null; + final String[] currFormId = {null}; - try { - currFormId = FormType - .constructFormType(mIntentsRule.getActivity(), APP_NAME, T_HOUSE_E_TABLE_ID) - .getFormId(); + try{ + scenario.onActivity(activity -> { - assertThat(currFormId, notNullValue(String.class)); + try { + currFormId[0] = FormType + .constructFormType(activity, APP_NAME, T_HOUSE_E_TABLE_ID) + .getFormId(); + } catch (ServicesAvailabilityException e) { + e.printStackTrace(); + } + }); + + assertThat(currFormId[0], notNullValue(String.class)); //change form id to something invalid EspressoUtils - .onRecyclerViewText(R.string.default_form) - .perform(click()); + .onRecyclerViewText(R.string.default_form) + .perform(click()); onView(isAssignableFrom(AppCompatEditText.class)) .perform(click()) .perform(clearText()) @@ -569,14 +571,21 @@ public void display_badFormId() throws ServicesAvailabilityException { EspressoUtils .toastMsgMatcher( - mIntentsRule, + decorView, is(EspressoUtils.getString(R.string.invalid_form)) ); } finally { - if (currFormId != null) { - FormType - .constructFormType(mIntentsRule.getActivity(), APP_NAME, T_HOUSE_E_TABLE_ID) - .setFormId(currFormId); + if (currFormId[0] != null) { + scenario.onActivity(activity -> { + try { + FormType + .constructFormType(activity, APP_NAME, T_HOUSE_E_TABLE_ID) + .setFormId(currFormId[0]); + } catch (ServicesAvailabilityException e) { + e.printStackTrace(); + } + }); + scenario.close(); } } } diff --git a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebViewActivityTest.java b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebViewActivityTest.java index c1d4005c..993a5ca4 100644 --- a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebViewActivityTest.java +++ b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebViewActivityTest.java @@ -1,30 +1,29 @@ package org.opendatakit.espresso; +import static androidx.test.espresso.web.sugar.Web.onWebView; +import static androidx.test.espresso.web.webdriver.DriverAtoms.findElement; +import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; + import android.Manifest; import android.webkit.WebView; +import androidx.test.core.app.ActivityScenario; import androidx.test.espresso.Espresso; import androidx.test.espresso.web.model.Atom; import androidx.test.espresso.web.model.ElementReference; import androidx.test.espresso.web.webdriver.Locator; import androidx.test.filters.LargeTest; import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.rule.ActivityTestRule; import androidx.test.rule.GrantPermissionRule; import androidx.test.uiautomator.UiDevice; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.rules.TestRule; import org.opendatakit.tables.activities.MainActivity; import org.opendatakit.util.UAUtils; -import static androidx.test.espresso.web.sugar.Web.onWebView; -import static androidx.test.espresso.web.webdriver.DriverAtoms.findElement; -import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; - /** * Basic sample that shows the usage of Espresso web showcasing API. * <p/> @@ -36,45 +35,38 @@ public class WebViewActivityTest { private Boolean initSuccess = null; private UiDevice mDevice; + private ActivityScenario<MainActivity> scenario; - @Rule - public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<MainActivity>( - MainActivity.class, false, true) { - @Override - protected void beforeActivityLaunched() { - super.beforeActivityLaunched(); - - if (initSuccess == null) { - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); - initSuccess = UAUtils.turnOnCustomHome(mDevice); - } - } - - @Override - protected void afterActivityLaunched() { - super.afterActivityLaunched(); - - onWebView().forceJavascriptEnabled(); - } - }; - // don't annotate used in chain rule - private GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( + @Rule + public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION ); - @Rule - public TestRule chainedRules = RuleChain - .outerRule(grantPermissionRule) - .around(mActivityRule); + private void beforeActivityLaunched() { + if (initSuccess == null) { + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + initSuccess = UAUtils.turnOnCustomHome(mDevice); + } + } + @Before public void setup() { - UAUtils.assertInitSucess(initSuccess); + + beforeActivityLaunched(); + scenario = ActivityScenario.launch(MainActivity.class); + onWebView().forceJavascriptEnabled(); + UAUtils.assertInitSucess(initSuccess); } + @After + public void cleanUp(){ + scenario.close(); + } + @Test public void infiniteTestToReplicateSigabrt() { if (true) diff --git a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebViewPerfTest.java b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebViewPerfTest.java index fd1f7988..551f1387 100644 --- a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebViewPerfTest.java +++ b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebViewPerfTest.java @@ -1,35 +1,34 @@ package org.opendatakit.espresso; +import static androidx.test.espresso.web.assertion.WebViewAssertions.webMatches; +import static androidx.test.espresso.web.sugar.Web.onWebView; +import static androidx.test.espresso.web.webdriver.DriverAtoms.findElement; +import static androidx.test.espresso.web.webdriver.DriverAtoms.getText; +import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; +import static junit.framework.Assert.fail; +import static org.hamcrest.Matchers.containsString; + import android.Manifest; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Build; +import androidx.test.core.app.ActivityScenario; import androidx.test.espresso.web.webdriver.DriverAtoms; import androidx.test.espresso.web.webdriver.Locator; import androidx.test.filters.LargeTest; import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.rule.ActivityTestRule; import androidx.test.rule.GrantPermissionRule; import androidx.test.uiautomator.UiDevice; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.rules.TestRule; import org.opendatakit.tables.activities.MainActivity; import org.opendatakit.util.UAUtils; -import static androidx.test.espresso.web.assertion.WebViewAssertions.webMatches; -import static androidx.test.espresso.web.sugar.Web.onWebView; -import static androidx.test.espresso.web.webdriver.DriverAtoms.findElement; -import static androidx.test.espresso.web.webdriver.DriverAtoms.getText; -import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; -import static junit.framework.Assert.fail; -import static org.hamcrest.Matchers.containsString; - /** * This test can only be used with the index from the large * data set app and is used for very specific purposes. @@ -53,39 +52,15 @@ public class WebViewPerfTest { private Boolean initSuccess = null; private UiDevice mDevice; + private ActivityScenario<MainActivity> scenario; - // don't annotate used in chain rule - private ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<MainActivity>( - MainActivity.class, false, true) { - @Override - protected void beforeActivityLaunched() { - super.beforeActivityLaunched(); - - if (initSuccess == null) { - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); - initSuccess = UAUtils.turnOnCustomHome(mDevice); - } - } - - @Override - protected void afterActivityLaunched() { - super.afterActivityLaunched(); - - onWebView().forceJavascriptEnabled(); - } - }; - - // don't annotate used in chain rule - private GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( + @Rule + public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION ); - @Rule - public TestRule chainedRules = RuleChain - .outerRule(grantPermissionRule) - .around(mActivityRule); private static String getOsToUse() { return Build.VERSION.RELEASE; @@ -154,11 +129,24 @@ private static TEST_DB_TYPE getDbInUse() { return TEST_DB_TYPE.CUSTOM; } } + public void beforeActivityLaunched() { + if (initSuccess == null) { + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + initSuccess = UAUtils.turnOnCustomHome(mDevice); + } + } @Before public void setup() { + beforeActivityLaunched(); + scenario = ActivityScenario.launch(MainActivity.class); + onWebView().forceJavascriptEnabled(); UAUtils.assertInitSucess(initSuccess); } + @After + public void cleanUp(){ + scenario.close(); + } @Test public void performanceTestForLargeDataSet() { diff --git a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebkitStressTest.java b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebkitStressTest.java index 0114ce7c..ddd51aee 100644 --- a/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebkitStressTest.java +++ b/tables_app/src/androidTestUitest/java/org/opendatakit/espresso/WebkitStressTest.java @@ -1,83 +1,72 @@ package org.opendatakit.espresso; +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.action.ViewActions.click; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static androidx.test.espresso.web.sugar.Web.onWebView; +import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; +import static org.opendatakit.util.TestConstants.APP_INIT_TIMEOUT; +import static org.opendatakit.util.TestConstants.HOPE_TAB_ID; +import static org.opendatakit.util.TestConstants.LAUNCH_DEMO_ID; +import static org.opendatakit.util.TestConstants.OBJ_WAIT_TIMEOUT; +import static org.opendatakit.util.TestConstants.TABLES_PKG_NAME; +import static org.opendatakit.util.TestConstants.WEB_WAIT_TIMEOUT; + import android.Manifest; import android.webkit.WebView; +import androidx.test.core.app.ActivityScenario; import androidx.test.espresso.web.webdriver.Locator; import androidx.test.filters.LargeTest; import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.rule.ActivityTestRule; import androidx.test.rule.GrantPermissionRule; import androidx.test.uiautomator.By; import androidx.test.uiautomator.UiDevice; import androidx.test.uiautomator.UiObject2; import androidx.test.uiautomator.Until; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.rules.TestRule; import org.opendatakit.tables.R; import org.opendatakit.tables.activities.MainActivity; import org.opendatakit.util.EspressoUtils; import org.opendatakit.util.UAUtils; -import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.action.ViewActions.click; -import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static androidx.test.espresso.web.sugar.Web.onWebView; -import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; -import static org.opendatakit.util.TestConstants.APP_INIT_TIMEOUT; -import static org.opendatakit.util.TestConstants.HOPE_TAB_ID; -import static org.opendatakit.util.TestConstants.LAUNCH_DEMO_ID; -import static org.opendatakit.util.TestConstants.OBJ_WAIT_TIMEOUT; -import static org.opendatakit.util.TestConstants.TABLES_PKG_NAME; -import static org.opendatakit.util.TestConstants.WEB_WAIT_TIMEOUT; - @LargeTest public class WebkitStressTest { private Boolean initSuccess = null; private UiDevice mDevice; + private ActivityScenario<MainActivity> scenario; - // don't annotate used in chain rule - private ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<MainActivity>( - MainActivity.class) { - @Override - protected void beforeActivityLaunched() { - super.beforeActivityLaunched(); - - if (initSuccess == null) { - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); - initSuccess = UAUtils.turnOnCustomHome(mDevice); - } - } - - @Override - protected void afterActivityLaunched() { - super.afterActivityLaunched(); - - onWebView().forceJavascriptEnabled(); - } - }; - - // don't annotate used in chain rule - private GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( + @Rule + public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant( Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION ); - @Rule - public TestRule chainedRules = RuleChain - .outerRule(grantPermissionRule) - .around(mActivityRule); + private void beforeActivityLaunched() { + if (initSuccess == null) { + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + initSuccess = UAUtils.turnOnCustomHome(mDevice); + } + } @Before public void setup() { - UAUtils.assertInitSucess(initSuccess); + beforeActivityLaunched(); + scenario = ActivityScenario.launch(MainActivity.class); + onWebView().forceJavascriptEnabled(); + UAUtils.assertInitSucess(initSuccess); + } + + @After + public void cleanUp(){ + scenario.close(); } @Test diff --git a/tables_app/src/androidTestUitest/java/org/opendatakit/util/EspressoUtils.java b/tables_app/src/androidTestUitest/java/org/opendatakit/util/EspressoUtils.java index ae18afbc..16ff8750 100644 --- a/tables_app/src/androidTestUitest/java/org/opendatakit/util/EspressoUtils.java +++ b/tables_app/src/androidTestUitest/java/org/opendatakit/util/EspressoUtils.java @@ -25,7 +25,6 @@ import androidx.recyclerview.widget.RecyclerView; import androidx.test.espresso.ViewInteraction; import androidx.test.espresso.contrib.RecyclerViewActions; -import androidx.test.espresso.intent.rule.IntentsTestRule; import androidx.test.espresso.web.sugar.Web; import androidx.test.espresso.web.webdriver.Locator; import androidx.test.platform.app.InstrumentationRegistry; @@ -124,10 +123,10 @@ public static int getColor(Matcher<View> matcher, final int x, final int y) { return color[0]; } - public static ViewInteraction toastMsgMatcher(IntentsTestRule rule, Matcher<String> matcher) { - return onView(withText(matcher)) - .inRoot(withDecorView(not(is(rule.getActivity().getWindow().getDecorView())))) - .check(matches(isDisplayed())); + public static void toastMsgMatcher(View decorView, Matcher<String> matcher) { + onView(withText(matcher)) + .inRoot(withDecorView(not(is(decorView)))) + .check(matches(isDisplayed())); } public static void openTableManagerFromCustomHome() {