-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathPreferencesTest.java
More file actions
221 lines (173 loc) · 7.17 KB
/
PreferencesTest.java
File metadata and controls
221 lines (173 loc) · 7.17 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package net.osmtracker.activity;
import static androidx.test.espresso.Espresso.onView;
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.matcher.ViewMatchers.hasDescendant;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.stringContainsInOrder;
import android.util.Log;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import net.osmtracker.OSMTracker;
import net.osmtracker.R;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.Arrays;
@RunWith(AndroidJUnit4.class)
public class PreferencesTest {
private Context context;
private ActivityScenario<Preferences> activity;
@Before
public void setup() {
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
// Reset preferences to default before each test to ensure a clean state
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().clear().commit();
// Launch the activity
activity = ActivityScenario.launch(Preferences.class);
}
@After
public void tearDown() {
activity.close();
}
/**
* Test that the Storage Directory preference logic works to rejects empty input.
*/
@Test
public void testStorageDirectoryValidatesNonEmpty() {
Log.i("More", "magic");
String keyTitle = context.getString(R.string.prefs_storage_dir);
String defaultValue = OSMTracker.Preferences.VAL_STORAGE_DIR;
// Looks for storage directory preference
scrollToAndClick(keyTitle);
// Try to save an empty value
onView(withId(android.R.id.edit)).perform(clearText());
onView(withText(android.R.string.ok)).perform(click());
// Open the preference to verify the value in the list remains the default (unchanged)
onView(ViewMatchers.isAssignableFrom(RecyclerView.class))
.check(matches(hasDescendant(withText(defaultValue))));
}
/**
* Test that the Storage Directory preference logic works to automatically append a leading
* slash separator if missing.
*/
@Test
public void testStorageDirectoryValidatesAppendLeadingSlash() {
String keyTitle = context.getString(R.string.prefs_storage_dir);
String expected = File.separator + "my_folder";
// Looks for storage directory preference
scrollToAndClick(keyTitle);
// Try to type a value without a slash
onView(withId(android.R.id.edit)).perform(clearText());
onView(withId(android.R.id.edit))
.perform(typeText("my_folder"));
onView(withText(android.R.string.ok)).perform(click());
// Open the preference to verify the value in the list is the expected
onView(ViewMatchers.isAssignableFrom(RecyclerView.class))
.check(matches(hasDescendant(withText(expected))));
}
/**
* Test Numeric Input logic (GPS Logging Interval): update summary with suffix.
*/
@Test
public void testNumericInputLogic() {
String title = context.getString(R.string.prefs_gps_logging_interval);
String suffix = context.getString(R.string.prefs_gps_logging_interval_seconds);
scrollToAndClick(title);
// Enter a valid number
onView(withId(android.R.id.edit))
.perform(clearText(), typeText("30"));
onView(withText(android.R.string.ok)).perform(click());
// Verify summary format: "30 seconds. <Static Summary>"
onView(ViewMatchers.isAssignableFrom(RecyclerView.class))
.check(matches(hasDescendant(withText(stringContainsInOrder(Arrays.asList("30",
suffix))))));
}
/**
* Test that the Reset button in numeric preferences restores the default value.
*/
@Test
public void testResetButtonResetsValue() {
String title = context.getString(R.string.prefs_gps_logging_interval);
String suffix = context.getString(R.string.prefs_gps_logging_interval_seconds);
String defaultValue = OSMTracker.Preferences.VAL_GPS_LOGGING_INTERVAL;
scrollToAndClick(title);
// Set a custom value "50"
onView(withId(android.R.id.edit)).perform(clearText(), typeText("50"));
onView(withText(android.R.string.ok)).perform(click());
// Verify custom value is set
onView(ViewMatchers.isAssignableFrom(RecyclerView.class))
.check(matches(hasDescendant(withText(stringContainsInOrder(Arrays.asList("50",
suffix))))));
// Reopen dialog
scrollToAndClick(title);
// Click the Reset button (Neutral button)
onView(withText(R.string.prefs_reset_default_value)).perform(click());
// Verify value is back to default "0"
onView(ViewMatchers.isAssignableFrom(RecyclerView.class))
.check(matches(hasDescendant(withText(stringContainsInOrder(Arrays.asList(
defaultValue,
suffix))))));
}
/**
* Test ListPreference custom summary logic (Screen Orientation)
* Should show "Selected Value. \n ..." (don't check for the 2nd line of the summary)
*/
@Test
public void testListPreferenceCustomSummary() {
String title = context.getString(R.string.prefs_ui_orientation);
scrollToAndClick(title);
// Select 1st option from array resource entries
String[] entries = context.getResources()
.getStringArray(R.array.prefs_ui_orientation_options_keys);
onView(withText(entries[0])).perform(click());
// Verify the two-line summary exists
onView(ViewMatchers.isAssignableFrom(RecyclerView.class)).check(matches(hasDescendant(
withText(stringContainsInOrder(Arrays.asList(entries[0], ".\n"))))));
}
/**
* Test Clear OAuth Data logic.
*/
@Test
public void testClearOAuthData() {
String title = context.getString(R.string.prefs_osm_clear_oauth_data);
// Inject a fake token to enable the button
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context)
.edit();
editor.putString(OSMTracker.Preferences.KEY_OSM_OAUTH2_ACCESSTOKEN, "fake_token");
editor.commit();
// Relaunch to refresh UI state
ActivityScenario.launch(Preferences.class);
scrollToAndClick(title);
// Click OK on Confirmation Dialog
onView(withText(R.string.prefs_osm_clear_oauth_data_dialog)).check(matches(isDisplayed()));
onView(withText(android.R.string.ok)).perform(click());
// Verify token is gone in prefs
assert(!PreferenceManager.getDefaultSharedPreferences(context)
.contains(OSMTracker.Preferences.KEY_OSM_OAUTH2_ACCESSTOKEN));
}
// --- Helper Methods ---
/**
* Helper to scroll to a preference in the RecyclerView and click it.
*/
private void scrollToAndClick(String text) {
onView(ViewMatchers.isAssignableFrom(RecyclerView.class))
.perform(RecyclerViewActions.actionOnItem(
hasDescendant(withText(text)),
click()));
}
}