Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated ActivityTestRule #474

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 84 additions & 78 deletions app/src/androidTest/java/net/osmtracker/layouts/DeleteLayoutTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}
}
Loading
Loading