-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathDownloadLayoutTest.java
More file actions
156 lines (122 loc) · 5.01 KB
/
DownloadLayoutTest.java
File metadata and controls
156 lines (122 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package net.osmtracker.layouts;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static junit.framework.TestCase.fail;
import static net.osmtracker.util.WaitForView.waitForView;
import android.Manifest;
import android.content.SharedPreferences;
import androidx.lifecycle.Lifecycle;
import androidx.preference.PreferenceManager;
import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.rule.GrantPermissionRule;
import net.osmtracker.OSMTracker;
import net.osmtracker.R;
import net.osmtracker.activity.TrackManager;
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;
import java.util.Locale;
public class DownloadLayoutTest {
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);
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
onView(ViewMatchers.withId(androidx.preference.R.id.recycler_view))
.perform(RecyclerViewActions.actionOnItem(
ViewMatchers.hasDescendant(withText(R.string.prefs_ui_buttons_layout)),
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));
}
}