From 7fac143862c39d714c7f332c827e962294229f54 Mon Sep 17 00:00:00 2001 From: Binnette <binnette@gmail.com> Date: Wed, 4 Dec 2024 09:25:27 +0100 Subject: [PATCH] Replace deprecated ActivityTestRule --- .../osmtracker/layouts/DeleteLayoutTest.java | 162 ++++++------- .../layouts/DownloadLayoutTest.java | 217 +++++++++--------- .../layouts/RepositorySettingsDialogTest.java | 182 ++++++++------- 3 files changed, 296 insertions(+), 265 deletions(-) diff --git a/app/src/androidTest/java/net/osmtracker/layouts/DeleteLayoutTest.java b/app/src/androidTest/java/net/osmtracker/layouts/DeleteLayoutTest.java index 63fd0a46..339caacc 100644 --- a/app/src/androidTest/java/net/osmtracker/layouts/DeleteLayoutTest.java +++ b/app/src/androidTest/java/net/osmtracker/layouts/DeleteLayoutTest.java @@ -1,21 +1,5 @@ package net.osmtracker.layouts; -import android.Manifest; -import androidx.test.rule.ActivityTestRule; -import androidx.test.rule.GrantPermissionRule; - -import net.osmtracker.R; -import net.osmtracker.activity.ButtonsPresets; -import net.osmtracker.activity.Preferences; -import net.osmtracker.util.CustomLayoutsUtils; -import net.osmtracker.util.TestUtils; - -import org.junit.Rule; -import org.junit.Test; - -import java.io.IOException; -import java.util.ArrayList; - import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.longClick; @@ -26,72 +10,94 @@ import static net.osmtracker.util.TestUtils.getStringResource; import static net.osmtracker.util.TestUtils.injectMockLayout; import static net.osmtracker.util.TestUtils.listFiles; +import static org.apache.commons.io.FileUtils.deleteDirectory; import static org.hamcrest.Matchers.equalToIgnoringCase; import static org.junit.Assert.assertFalse; -import static org.apache.commons.io.FileUtils.deleteDirectory; +import android.Manifest; + +import androidx.lifecycle.Lifecycle; +import androidx.test.core.app.ActivityScenario; +import androidx.test.rule.GrantPermissionRule; + +import net.osmtracker.R; +import net.osmtracker.activity.ButtonsPresets; +import net.osmtracker.activity.Preferences; +import net.osmtracker.util.CustomLayoutsUtils; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; public class DeleteLayoutTest { - @Rule - public GrantPermissionRule storagePermission = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE); - - @Rule - public ActivityTestRule<ButtonsPresets> mRule = new ActivityTestRule(ButtonsPresets.class) { - @Override - protected void beforeActivityLaunched() { - //Makes sure that only the mock layout exists - try { - deleteDirectory(getLayoutsDirectory()); - injectMockLayout(layoutName, ISOLanguageCode); - - } catch (IOException e) { - e.printStackTrace(); - } - } - }; - - private static String layoutName = "mock"; - private static String ISOLanguageCode = "es"; - - /** - * Assumes being in the ButtonsPresets activity - * Deletes the layout with the received name - */ - private void deleteLayout(String layoutName){ - onView(withText(layoutName)).perform(longClick()); - onView(withText(getStringResource(R.string.buttons_presets_context_menu_delete))).perform(click()); - String textToMatch = getStringResource(R.string.buttons_presets_delete_positive_confirmation); - onView(withText(equalToIgnoringCase(textToMatch))).perform(click()); - } - - /** - * Deletes the mock layout and then checks that: - * - The UI option doesn't appear anymore - * - The XML file is deleted - * - A Toast is shown to inform about what happened - * - The icons directory is deleted - */ - @Test - public void layoutDeletionTest(){ - - deleteLayout(layoutName); - - // Check the informative Toast is shown - checkToastIsShownWith(getStringResource(R.string.buttons_presets_successful_delete)); - - // Check the layout doesn't appear anymore - onView(withText(layoutName)).check(doesNotExist()); - - // List files after the deletion - ArrayList<String> filesAfterDeletion = listFiles(getLayoutsDirectory()); - - // Check the xml file was deleted - String layoutFileName = CustomLayoutsUtils.createFileName(layoutName, ISOLanguageCode); - assertFalse(filesAfterDeletion.contains(layoutFileName)); - - // Check the icons folder was deleted - assertFalse(filesAfterDeletion.contains(layoutName+ Preferences.ICONS_DIR_SUFFIX)); - - } + @Rule + public GrantPermissionRule storagePermission = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE); + + public ActivityScenario<ButtonsPresets> activity; + + private static final String layoutName = "mock"; + private static final String ISOLanguageCode = "es"; + + @Before + public void setUp() { + // Makes sure that only the mock layout exists + try { + deleteDirectory(getLayoutsDirectory()); + injectMockLayout(layoutName, ISOLanguageCode); + } catch (IOException e) { + e.printStackTrace(); + } + // Launch activity + activity = ActivityScenario.launch(ButtonsPresets.class); + activity.moveToState(Lifecycle.State.RESUMED); + } + + @After + public void tearDown() { + activity.close(); + } + + /** + * Assumes being in the ButtonsPresets activity + * Deletes the layout with the received name + */ + private void deleteLayout() { + onView(withText(layoutName)).perform(longClick()); + onView(withText(getStringResource(R.string.buttons_presets_context_menu_delete))).perform(click()); + String textToMatch = getStringResource(R.string.buttons_presets_delete_positive_confirmation); + onView(withText(equalToIgnoringCase(textToMatch))).perform(click()); + } + + /** + * Deletes the mock layout and then checks that: + * - The UI option doesn't appear anymore + * - The XML file is deleted + * - A Toast is shown to inform about what happened + * - The icons directory is deleted + */ + @Test + public void layoutDeletionTest() { + deleteLayout(); + + // Check the informative Toast is shown + checkToastIsShownWith(getStringResource(R.string.buttons_presets_successful_delete)); + + // Check the layout doesn't appear anymore + onView(withText(layoutName)).check(doesNotExist()); + + // List files after the deletion + ArrayList<String> filesAfterDeletion = listFiles(getLayoutsDirectory()); + + // Check the xml file was deleted + String layoutFileName = CustomLayoutsUtils.createFileName(layoutName, ISOLanguageCode); + assertFalse(filesAfterDeletion.contains(layoutFileName)); + + // Check the icons folder was deleted + assertFalse(filesAfterDeletion.contains(layoutName + Preferences.ICONS_DIR_SUFFIX)); + } } diff --git a/app/src/androidTest/java/net/osmtracker/layouts/DownloadLayoutTest.java b/app/src/androidTest/java/net/osmtracker/layouts/DownloadLayoutTest.java index 1c20d9c7..7882dbea 100644 --- a/app/src/androidTest/java/net/osmtracker/layouts/DownloadLayoutTest.java +++ b/app/src/androidTest/java/net/osmtracker/layouts/DownloadLayoutTest.java @@ -19,8 +19,9 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; +import androidx.lifecycle.Lifecycle; +import androidx.test.core.app.ActivityScenario; import androidx.test.espresso.Espresso; -import androidx.test.rule.ActivityTestRule; import androidx.test.rule.GrantPermissionRule; import net.osmtracker.OSMTracker; @@ -29,6 +30,8 @@ import net.osmtracker.util.TestUtils; import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -36,107 +39,115 @@ public class DownloadLayoutTest { - private final int WAIT_VIEW_TIMEOUT = 5000; + private final int WAIT_VIEW_TIMEOUT = 5000; - @Rule - public GrantPermissionRule fineLocationPermission = GrantPermissionRule.grant(Manifest.permission.ACCESS_FINE_LOCATION); - @Rule - public GrantPermissionRule coarseLocationPermission = GrantPermissionRule.grant(Manifest.permission.ACCESS_COARSE_LOCATION); - @Rule - public GrantPermissionRule writeStoragePermission = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE); - - @Rule - public ActivityTestRule<TrackManager> mRule = new ActivityTestRule(TrackManager.class) { - @Override - protected void beforeActivityLaunched() { - // Skip cool intro - SharedPreferences dtPrefs = PreferenceManager - .getDefaultSharedPreferences(getInstrumentation().getTargetContext()); - dtPrefs.edit().putBoolean(OSMTracker.Preferences.KEY_DISPLAY_APP_INTRO, false).apply(); - } - }; - - @Test - public void downloadLayoutTest() { - deleteLayoutsDirectory(); - - TestUtils.setLayoutsTestingRepository(); - - String layoutName = "abc"; - - navigateToAvailableLayouts(); - - clickButtonsToDownloadLayout(layoutName); - - makePostDownloadAssertions(layoutName); - } - - - public void deleteLayoutsDirectory(){ - try { - FileUtils.deleteDirectory(TestUtils.getLayoutsDirectory()); - }catch (Exception e){ - e.printStackTrace(); - fail(); - } - } - - - /** - * Assuming being in TrackManager - */ - public void navigateToAvailableLayouts() { - // Open options menu in the Action Bar - openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext()); - // Click on "Settings" in this menu - onView(withText(TestUtils.getStringResource(R.string.menu_settings))).perform(click()); - // Click on "Buttons presets" settings - onData(withTitleText(TestUtils.getStringResource(R.string.prefs_ui_buttons_layout))).perform(scrollTo(), click()); - // Wait for "+" to be visible - onView(isRoot()).perform(waitForView(R.id.launch_available, WAIT_VIEW_TIMEOUT)); - // Perform a click action on the "+" button - onView(withId(R.id.launch_available)).perform(click()); - } - - - /** - * Check the new layouts appears as a new option - * Select the layout and check its buttons are shown when tracking - * @param layoutName - */ - private void makePostDownloadAssertions(String layoutName) { - Espresso.pressBack(); - - // Check the layout appears as a new option in AvailableLayouts - onView(withText(layoutName.toLowerCase())).check(matches(isDisplayed())); - - // Select the layout - onView(withText(layoutName.toLowerCase())).perform(click()); - - // Go to TrackLogger - Espresso.pressBack(); - Espresso.pressBack(); - onView(withId(R.id.trackmgr_fab)).perform(click()); - - // Check the buttons are loaded correctly - String expectedButtonsLabels[] = new String[]{"A", "B", "C"}; - for(String label : expectedButtonsLabels) - onView(withText(label)).check(matches(isDisplayed())); - - } - - - private void clickButtonsToDownloadLayout(String layoutName) { - onView(withText(layoutName)).perform(click()); - - // Catch languages available dialog that shows up when the cell phone is not in English - if (! Locale.getDefault().getLanguage().equalsIgnoreCase("en")) { - onView(withText("English")).perform(click()); - } - - onView(withText(TestUtils.getStringResource(R.string.available_layouts_description_dialog_positive_confirmation))). - perform(click()); - - TestUtils.checkToastIsShownWith(TestUtils.getStringResource(R.string.available_layouts_successful_download)); - } + @Rule + public GrantPermissionRule fineLocationPermission = GrantPermissionRule.grant(Manifest.permission.ACCESS_FINE_LOCATION); + @Rule + public GrantPermissionRule coarseLocationPermission = GrantPermissionRule.grant(Manifest.permission.ACCESS_COARSE_LOCATION); + @Rule + public GrantPermissionRule writeStoragePermission = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE); + + public ActivityScenario<TrackManager> activity; + + @Before + public void setUp() { + // Skip cool intro + SharedPreferences dtPrefs = PreferenceManager + .getDefaultSharedPreferences(getInstrumentation().getTargetContext()); + dtPrefs.edit().putBoolean(OSMTracker.Preferences.KEY_DISPLAY_APP_INTRO, false).apply(); + // Launch activity + activity = ActivityScenario.launch(TrackManager.class); + activity.moveToState(Lifecycle.State.RESUMED); + } + + @After + public void tearDown() { + activity.close(); + } + + @Test + public void downloadLayoutTest() { + deleteLayoutsDirectory(); + + TestUtils.setLayoutsTestingRepository(); + + String layoutName = "abc"; + + navigateToAvailableLayouts(); + + clickButtonsToDownloadLayout(layoutName); + + makePostDownloadAssertions(layoutName); + } + + + public void deleteLayoutsDirectory() { + try { + FileUtils.deleteDirectory(TestUtils.getLayoutsDirectory()); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + + /** + * Assuming being in TrackManager + */ + public void navigateToAvailableLayouts() { + // Open options menu in the Action Bar + openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext()); + // Click on "Settings" in this menu + onView(withText(TestUtils.getStringResource(R.string.menu_settings))).perform(click()); + // Click on "Buttons presets" settings + onData(withTitleText(TestUtils.getStringResource(R.string.prefs_ui_buttons_layout))).perform(scrollTo(), click()); + // Wait for "+" to be visible + onView(isRoot()).perform(waitForView(R.id.launch_available, WAIT_VIEW_TIMEOUT)); + // Perform a click action on the "+" button + onView(withId(R.id.launch_available)).perform(click()); + } + + + /** + * Check the new layouts appears as a new option + * Select the layout and check its buttons are shown when tracking + * + * @param layoutName layout name + */ + private void makePostDownloadAssertions(String layoutName) { + Espresso.pressBack(); + + // Check the layout appears as a new option in AvailableLayouts + onView(withText(layoutName.toLowerCase())).check(matches(isDisplayed())); + + // Select the layout + onView(withText(layoutName.toLowerCase())).perform(click()); + + // Go to TrackLogger + Espresso.pressBack(); + Espresso.pressBack(); + onView(withId(R.id.trackmgr_fab)).perform(click()); + + // Check the buttons are loaded correctly + String[] expectedButtonsLabels = new String[]{"A", "B", "C"}; + for (String label : expectedButtonsLabels) + onView(withText(label)).check(matches(isDisplayed())); + + } + + + private void clickButtonsToDownloadLayout(String layoutName) { + onView(withText(layoutName)).perform(click()); + + // Catch languages available dialog that shows up when the cell phone is not in English + if (!Locale.getDefault().getLanguage().equalsIgnoreCase("en")) { + onView(withText("English")).perform(click()); + } + + onView(withText(TestUtils.getStringResource(R.string.available_layouts_description_dialog_positive_confirmation))). + perform(click()); + + TestUtils.checkToastIsShownWith(TestUtils.getStringResource(R.string.available_layouts_successful_download)); + } } \ No newline at end of file diff --git a/app/src/androidTest/java/net/osmtracker/layouts/RepositorySettingsDialogTest.java b/app/src/androidTest/java/net/osmtracker/layouts/RepositorySettingsDialogTest.java index 607587fc..bac3b7d7 100644 --- a/app/src/androidTest/java/net/osmtracker/layouts/RepositorySettingsDialogTest.java +++ b/app/src/androidTest/java/net/osmtracker/layouts/RepositorySettingsDialogTest.java @@ -16,101 +16,115 @@ import static net.osmtracker.util.TestUtils.getStringResource; import static org.hamcrest.core.IsNot.not; +import android.view.View; + +import androidx.lifecycle.Lifecycle; +import androidx.test.core.app.ActivityScenario; import androidx.test.espresso.ViewAssertion; -import androidx.test.rule.ActivityTestRule; import net.osmtracker.OSMTracker; import net.osmtracker.R; import net.osmtracker.activity.AvailableLayouts; import org.hamcrest.Matcher; -import org.junit.Rule; +import org.junit.After; +import org.junit.Before; import org.junit.Test; public class RepositorySettingsDialogTest { - @Rule - public ActivityTestRule<AvailableLayouts> mRule = new ActivityTestRule<>(AvailableLayouts.class); - - - @Test - public void testToggleBehaviour(){ - onView(withId(R.id.github_config)).perform(click()); - - onView(withId(R.id.default_server)).perform(click(), closeSoftKeyboard()); - checkStateAfterToggle(R.id.default_server, R.id.custom_server); - checkTextFieldsState(not(isEnabled())); - checkTextFieldsDefaultValues(); - - onView(withId(R.id.custom_server)).perform(click(), closeSoftKeyboard()); - checkStateAfterToggle(R.id.custom_server, R.id.default_server); - checkTextFieldsState(isEnabled()); - } - - @Test - public void testRepositoryValidation(){ - String validUser = OSMTracker.Preferences.VAL_GITHUB_USERNAME; - String validRepository = OSMTracker.Preferences.VAL_REPOSITORY_NAME; - String validBranch = OSMTracker.Preferences.VAL_BRANCH_NAME; - String invalidBranch = "NONE"; - - checkRepositoryValidity(validUser,validRepository,validBranch, true); - checkRepositoryValidity(validUser,validRepository,invalidBranch, false); - } - - - public void checkStateAfterToggle(int expectedActiveId, int expectedInactiveId){ - onView(withId(expectedActiveId)).check(matches(not(isEnabled()))); - onView(withId(expectedActiveId)).check(matches(isChecked())); - onView(withId(expectedInactiveId)).check(matches(not(isChecked()))); - onView(withId(expectedInactiveId)).check(matches(isEnabled())); - } - - public void checkRepositoryValidity(String user, String repo, String branch, boolean isValid) { - onView(withId(R.id.github_config)).perform(click()); - - onView(withId(R.id.custom_server)).perform(click(), closeSoftKeyboard()); - - onView(withId(R.id.github_username)).perform(clearText(), typeText(user), closeSoftKeyboard()); - onView(withId(R.id.repository_name)).perform(clearText(), typeText(repo), closeSoftKeyboard()); - onView(withId(R.id.branch_name)).perform(clearText(), typeText(branch), closeSoftKeyboard()); - - onView(withText(getStringResource(R.string.menu_save))).perform(click()); - - String expectedMessage = (isValid) ? getStringResource(R.string.github_repository_settings_valid_server) : - getStringResource(R.string.github_repository_settings_invalid_server); - - checkToastIsShownWith(expectedMessage); - - ViewAssertion expectedDialogState = (isValid) ? doesNotExist() : matches(isDisplayed()); - checkDialogState(expectedDialogState); - } - - /** - * Check if the dialog is shown by looking for its title on the screen - */ - private void checkDialogState(ViewAssertion assertion) { - onView(withText(getStringResource(R.string.prefs_ui_github_repository_settings))).check(assertion); - } - - /** - * Check that the text fields values match the expected default ones - */ - private void checkTextFieldsDefaultValues() { - onView(withId(R.id.repository_name)).check(matches(withText(OSMTracker.Preferences.VAL_REPOSITORY_NAME))); - onView(withId(R.id.branch_name)).check(matches(withText(OSMTracker.Preferences.VAL_BRANCH_NAME))); - onView(withId(R.id.github_username)).check(matches(withText(OSMTracker.Preferences.VAL_GITHUB_USERNAME))); - } - - /** - * @param matcher can be isEnabled or not(isEnabled()) or any matcher - */ - public void checkTextFieldsState(Matcher matcher){ - - onView(withId(R.id.github_username)).check(matches(matcher)); - onView(withId(R.id.repository_name)).check(matches(matcher)); - onView(withId(R.id.branch_name)).check(matches(matcher)); - } + public ActivityScenario<AvailableLayouts> activity; + + @Before + public void setUp() { + // Launch activity + activity = ActivityScenario.launch(AvailableLayouts.class); + activity.moveToState(Lifecycle.State.RESUMED); + } + + @After + public void tearDown() { + activity.close(); + } + + @Test + public void testToggleBehaviour() { + onView(withId(R.id.github_config)).perform(click()); + + onView(withId(R.id.default_server)).perform(click(), closeSoftKeyboard()); + checkStateAfterToggle(R.id.default_server, R.id.custom_server); + checkTextFieldsState(not(isEnabled())); + checkTextFieldsDefaultValues(); + + onView(withId(R.id.custom_server)).perform(click(), closeSoftKeyboard()); + checkStateAfterToggle(R.id.custom_server, R.id.default_server); + checkTextFieldsState(isEnabled()); + } + + @Test + public void testRepositoryValidation() { + String validUser = OSMTracker.Preferences.VAL_GITHUB_USERNAME; + String validRepository = OSMTracker.Preferences.VAL_REPOSITORY_NAME; + String validBranch = OSMTracker.Preferences.VAL_BRANCH_NAME; + String invalidBranch = "NONE"; + + checkRepositoryValidity(validUser, validRepository, validBranch, true); + checkRepositoryValidity(validUser, validRepository, invalidBranch, false); + } + + + public void checkStateAfterToggle(int expectedActiveId, int expectedInactiveId) { + onView(withId(expectedActiveId)).check(matches(not(isEnabled()))); + onView(withId(expectedActiveId)).check(matches(isChecked())); + onView(withId(expectedInactiveId)).check(matches(not(isChecked()))); + onView(withId(expectedInactiveId)).check(matches(isEnabled())); + } + + public void checkRepositoryValidity(String user, String repo, String branch, boolean isValid) { + onView(withId(R.id.github_config)).perform(click()); + + onView(withId(R.id.custom_server)).perform(click(), closeSoftKeyboard()); + + onView(withId(R.id.github_username)).perform(clearText(), typeText(user), closeSoftKeyboard()); + onView(withId(R.id.repository_name)).perform(clearText(), typeText(repo), closeSoftKeyboard()); + onView(withId(R.id.branch_name)).perform(clearText(), typeText(branch), closeSoftKeyboard()); + + onView(withText(getStringResource(R.string.menu_save))).perform(click()); + + String expectedMessage = (isValid) ? getStringResource(R.string.github_repository_settings_valid_server) : + getStringResource(R.string.github_repository_settings_invalid_server); + + checkToastIsShownWith(expectedMessage); + + ViewAssertion expectedDialogState = (isValid) ? doesNotExist() : matches(isDisplayed()); + checkDialogState(expectedDialogState); + } + + /** + * Check if the dialog is shown by looking for its title on the screen + */ + private void checkDialogState(ViewAssertion assertion) { + onView(withText(getStringResource(R.string.prefs_ui_github_repository_settings))).check(assertion); + } + + /** + * Check that the text fields values match the expected default ones + */ + private void checkTextFieldsDefaultValues() { + onView(withId(R.id.repository_name)).check(matches(withText(OSMTracker.Preferences.VAL_REPOSITORY_NAME))); + onView(withId(R.id.branch_name)).check(matches(withText(OSMTracker.Preferences.VAL_BRANCH_NAME))); + onView(withId(R.id.github_username)).check(matches(withText(OSMTracker.Preferences.VAL_GITHUB_USERNAME))); + } + + /** + * @param matcher can be isEnabled or not(isEnabled()) or any matcher + */ + public void checkTextFieldsState(Matcher<View> matcher) { + + onView(withId(R.id.github_username)).check(matches(matcher)); + onView(withId(R.id.repository_name)).check(matches(matcher)); + onView(withId(R.id.branch_name)).check(matches(matcher)); + } }