Skip to content

Commit 7fac143

Browse files
committed
Replace deprecated ActivityTestRule
1 parent a1db1c3 commit 7fac143

File tree

3 files changed

+296
-265
lines changed

3 files changed

+296
-265
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
package net.osmtracker.layouts;
22

3-
import android.Manifest;
4-
import androidx.test.rule.ActivityTestRule;
5-
import androidx.test.rule.GrantPermissionRule;
6-
7-
import net.osmtracker.R;
8-
import net.osmtracker.activity.ButtonsPresets;
9-
import net.osmtracker.activity.Preferences;
10-
import net.osmtracker.util.CustomLayoutsUtils;
11-
import net.osmtracker.util.TestUtils;
12-
13-
import org.junit.Rule;
14-
import org.junit.Test;
15-
16-
import java.io.IOException;
17-
import java.util.ArrayList;
18-
193
import static androidx.test.espresso.Espresso.onView;
204
import static androidx.test.espresso.action.ViewActions.click;
215
import static androidx.test.espresso.action.ViewActions.longClick;
@@ -26,72 +10,94 @@
2610
import static net.osmtracker.util.TestUtils.getStringResource;
2711
import static net.osmtracker.util.TestUtils.injectMockLayout;
2812
import static net.osmtracker.util.TestUtils.listFiles;
13+
import static org.apache.commons.io.FileUtils.deleteDirectory;
2914
import static org.hamcrest.Matchers.equalToIgnoringCase;
3015
import static org.junit.Assert.assertFalse;
31-
import static org.apache.commons.io.FileUtils.deleteDirectory;
3216

17+
import android.Manifest;
18+
19+
import androidx.lifecycle.Lifecycle;
20+
import androidx.test.core.app.ActivityScenario;
21+
import androidx.test.rule.GrantPermissionRule;
22+
23+
import net.osmtracker.R;
24+
import net.osmtracker.activity.ButtonsPresets;
25+
import net.osmtracker.activity.Preferences;
26+
import net.osmtracker.util.CustomLayoutsUtils;
27+
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.Rule;
31+
import org.junit.Test;
32+
33+
import java.io.IOException;
34+
import java.util.ArrayList;
3335

3436
public class DeleteLayoutTest {
3537

36-
@Rule
37-
public GrantPermissionRule storagePermission = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
38-
39-
@Rule
40-
public ActivityTestRule<ButtonsPresets> mRule = new ActivityTestRule(ButtonsPresets.class) {
41-
@Override
42-
protected void beforeActivityLaunched() {
43-
//Makes sure that only the mock layout exists
44-
try {
45-
deleteDirectory(getLayoutsDirectory());
46-
injectMockLayout(layoutName, ISOLanguageCode);
47-
48-
} catch (IOException e) {
49-
e.printStackTrace();
50-
}
51-
}
52-
};
53-
54-
private static String layoutName = "mock";
55-
private static String ISOLanguageCode = "es";
56-
57-
/**
58-
* Assumes being in the ButtonsPresets activity
59-
* Deletes the layout with the received name
60-
*/
61-
private void deleteLayout(String layoutName){
62-
onView(withText(layoutName)).perform(longClick());
63-
onView(withText(getStringResource(R.string.buttons_presets_context_menu_delete))).perform(click());
64-
String textToMatch = getStringResource(R.string.buttons_presets_delete_positive_confirmation);
65-
onView(withText(equalToIgnoringCase(textToMatch))).perform(click());
66-
}
67-
68-
/**
69-
* Deletes the mock layout and then checks that:
70-
* - The UI option doesn't appear anymore
71-
* - The XML file is deleted
72-
* - A Toast is shown to inform about what happened
73-
* - The icons directory is deleted
74-
*/
75-
@Test
76-
public void layoutDeletionTest(){
77-
78-
deleteLayout(layoutName);
79-
80-
// Check the informative Toast is shown
81-
checkToastIsShownWith(getStringResource(R.string.buttons_presets_successful_delete));
82-
83-
// Check the layout doesn't appear anymore
84-
onView(withText(layoutName)).check(doesNotExist());
85-
86-
// List files after the deletion
87-
ArrayList<String> filesAfterDeletion = listFiles(getLayoutsDirectory());
88-
89-
// Check the xml file was deleted
90-
String layoutFileName = CustomLayoutsUtils.createFileName(layoutName, ISOLanguageCode);
91-
assertFalse(filesAfterDeletion.contains(layoutFileName));
92-
93-
// Check the icons folder was deleted
94-
assertFalse(filesAfterDeletion.contains(layoutName+ Preferences.ICONS_DIR_SUFFIX));
95-
96-
}
38+
@Rule
39+
public GrantPermissionRule storagePermission = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
40+
41+
public ActivityScenario<ButtonsPresets> activity;
42+
43+
private static final String layoutName = "mock";
44+
private static final String ISOLanguageCode = "es";
45+
46+
@Before
47+
public void setUp() {
48+
// Makes sure that only the mock layout exists
49+
try {
50+
deleteDirectory(getLayoutsDirectory());
51+
injectMockLayout(layoutName, ISOLanguageCode);
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
}
55+
// Launch activity
56+
activity = ActivityScenario.launch(ButtonsPresets.class);
57+
activity.moveToState(Lifecycle.State.RESUMED);
58+
}
59+
60+
@After
61+
public void tearDown() {
62+
activity.close();
63+
}
64+
65+
/**
66+
* Assumes being in the ButtonsPresets activity
67+
* Deletes the layout with the received name
68+
*/
69+
private void deleteLayout() {
70+
onView(withText(layoutName)).perform(longClick());
71+
onView(withText(getStringResource(R.string.buttons_presets_context_menu_delete))).perform(click());
72+
String textToMatch = getStringResource(R.string.buttons_presets_delete_positive_confirmation);
73+
onView(withText(equalToIgnoringCase(textToMatch))).perform(click());
74+
}
75+
76+
/**
77+
* Deletes the mock layout and then checks that:
78+
* - The UI option doesn't appear anymore
79+
* - The XML file is deleted
80+
* - A Toast is shown to inform about what happened
81+
* - The icons directory is deleted
82+
*/
83+
@Test
84+
public void layoutDeletionTest() {
85+
deleteLayout();
86+
87+
// Check the informative Toast is shown
88+
checkToastIsShownWith(getStringResource(R.string.buttons_presets_successful_delete));
89+
90+
// Check the layout doesn't appear anymore
91+
onView(withText(layoutName)).check(doesNotExist());
92+
93+
// List files after the deletion
94+
ArrayList<String> filesAfterDeletion = listFiles(getLayoutsDirectory());
95+
96+
// Check the xml file was deleted
97+
String layoutFileName = CustomLayoutsUtils.createFileName(layoutName, ISOLanguageCode);
98+
assertFalse(filesAfterDeletion.contains(layoutFileName));
99+
100+
// Check the icons folder was deleted
101+
assertFalse(filesAfterDeletion.contains(layoutName + Preferences.ICONS_DIR_SUFFIX));
102+
}
97103
}

0 commit comments

Comments
 (0)