|
1 |
| -package fr.free.nrw.commons; |
2 |
| - |
3 |
| -import android.content.Context; |
4 |
| - |
5 |
| -import com.google.gson.Gson; |
6 |
| - |
7 |
| -import org.junit.Before; |
8 |
| -import org.junit.Rule; |
9 |
| -import org.junit.Test; |
10 |
| -import org.junit.runner.RunWith; |
11 |
| - |
12 |
| -import androidx.test.InstrumentationRegistry; |
13 |
| -import androidx.test.espresso.Espresso; |
14 |
| -import androidx.test.espresso.matcher.PreferenceMatchers; |
15 |
| -import androidx.test.filters.LargeTest; |
16 |
| -import androidx.test.rule.ActivityTestRule; |
17 |
| -import androidx.test.runner.AndroidJUnit4; |
18 |
| -import fr.free.nrw.commons.kvstore.JsonKvStore; |
19 |
| -import fr.free.nrw.commons.settings.Prefs; |
20 |
| -import fr.free.nrw.commons.settings.SettingsActivity; |
21 |
| - |
22 |
| -import static androidx.test.espresso.action.ViewActions.click; |
23 |
| -import static androidx.test.espresso.action.ViewActions.replaceText; |
24 |
| -import static androidx.test.espresso.assertion.ViewAssertions.matches; |
25 |
| -import static androidx.test.espresso.matcher.ViewMatchers.isEnabled; |
26 |
| -import static androidx.test.espresso.matcher.ViewMatchers.withId; |
27 |
| -import static androidx.test.espresso.matcher.ViewMatchers.withText; |
28 |
| -import static org.hamcrest.Matchers.allOf; |
29 |
| -import static org.hamcrest.core.IsNot.not; |
30 |
| -import static org.junit.Assert.assertEquals; |
| 1 | +package fr.free.nrw.commons |
| 2 | + |
| 3 | +import androidx.test.espresso.Espresso |
| 4 | +import androidx.test.espresso.action.ViewActions.click |
| 5 | +import androidx.test.espresso.action.ViewActions.replaceText |
| 6 | +import androidx.test.espresso.assertion.ViewAssertions.matches |
| 7 | +import androidx.test.espresso.matcher.PreferenceMatchers |
| 8 | +import androidx.test.espresso.matcher.ViewMatchers.* |
| 9 | +import androidx.test.filters.LargeTest |
| 10 | +import androidx.test.platform.app.InstrumentationRegistry |
| 11 | +import androidx.test.rule.ActivityTestRule |
| 12 | +import androidx.test.runner.AndroidJUnit4 |
| 13 | +import com.google.gson.Gson |
| 14 | +import fr.free.nrw.commons.kvstore.JsonKvStore |
| 15 | +import fr.free.nrw.commons.settings.Prefs |
| 16 | +import fr.free.nrw.commons.settings.SettingsActivity |
| 17 | +import org.hamcrest.Matchers.allOf |
| 18 | +import org.hamcrest.core.IsNot.not |
| 19 | +import org.junit.Assert.assertEquals |
| 20 | +import org.junit.Before |
| 21 | +import org.junit.Rule |
| 22 | +import org.junit.Test |
| 23 | +import org.junit.runner.RunWith |
31 | 24 |
|
32 | 25 | @LargeTest
|
33 |
| -@RunWith(AndroidJUnit4.class) |
34 |
| -public class SettingsActivityTest { |
35 |
| - JsonKvStore defaultKvStore; |
| 26 | +@RunWith(AndroidJUnit4::class) |
| 27 | +class SettingsActivityTest { |
| 28 | + private lateinit var defaultKvStore: JsonKvStore |
36 | 29 |
|
37 |
| - @Rule |
38 |
| - public ActivityTestRule activityRule = new ActivityTestRule<>(SettingsActivity.class); |
| 30 | + @get:Rule |
| 31 | + var activityRule: ActivityTestRule<*> = ActivityTestRule(SettingsActivity::class.java) |
39 | 32 |
|
40 | 33 | @Before
|
41 |
| - public void setup() { |
42 |
| - Context context = InstrumentationRegistry.getTargetContext(); |
43 |
| - String storeName = context.getPackageName() + "_preferences"; |
44 |
| - defaultKvStore = new JsonKvStore(context, storeName, new Gson()); |
| 34 | + fun setup() { |
| 35 | + val context = InstrumentationRegistry.getInstrumentation().targetContext |
| 36 | + val storeName = context.packageName + "_preferences" |
| 37 | + defaultKvStore = JsonKvStore(context, storeName, Gson()) |
45 | 38 | }
|
46 | 39 |
|
47 | 40 | @Test
|
48 |
| - public void setRecentUploadLimitTo123() { |
| 41 | + fun setRecentUploadLimitTo123() { |
49 | 42 | // Open "Use external storage" preference
|
50 | 43 | Espresso.onData(PreferenceMatchers.withKey("uploads"))
|
51 | 44 | .inAdapterView(withId(android.R.id.list))
|
52 |
| - .perform(click()); |
| 45 | + .perform(click()) |
53 | 46 |
|
54 | 47 | // Try setting it to 123
|
55 | 48 | Espresso.onView(withId(android.R.id.edit))
|
56 |
| - .perform(replaceText("123")); |
| 49 | + .perform(replaceText("123")) |
57 | 50 |
|
58 | 51 | // Click "OK"
|
59 | 52 | Espresso.onView(allOf(withId(android.R.id.button1), withText("OK")))
|
60 |
| - .perform(click()); |
| 53 | + .perform(click()) |
61 | 54 |
|
62 | 55 | // Check setting set to 123 in SharedPreferences
|
63 | 56 | assertEquals(
|
64 | 57 | 123,
|
65 |
| - defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0) |
66 |
| - ); |
| 58 | + defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0).toLong() |
| 59 | + ) |
67 | 60 |
|
68 | 61 | // Check displaying 123 in summary text
|
69 | 62 | Espresso.onData(PreferenceMatchers.withKey("uploads"))
|
70 | 63 | .inAdapterView(withId(android.R.id.list))
|
71 | 64 | .onChildView(withId(android.R.id.summary))
|
72 |
| - .check(matches(withText("123"))); |
| 65 | + .check(matches(withText("123"))) |
73 | 66 | }
|
74 | 67 |
|
75 | 68 | @Test
|
76 |
| - public void setRecentUploadLimitTo0() { |
| 69 | + fun setRecentUploadLimitTo0() { |
77 | 70 | // Open "Use external storage" preference
|
78 | 71 | Espresso.onData(PreferenceMatchers.withKey("uploads"))
|
79 | 72 | .inAdapterView(withId(android.R.id.list))
|
80 |
| - .perform(click()); |
| 73 | + .perform(click()) |
81 | 74 |
|
82 | 75 | // Try setting it to 0
|
83 | 76 | Espresso.onView(withId(android.R.id.edit))
|
84 |
| - .perform(replaceText("0")); |
| 77 | + .perform(replaceText("0")) |
85 | 78 |
|
86 | 79 | // Click "OK"
|
87 | 80 | Espresso.onView(allOf(withId(android.R.id.button1), withText("OK")))
|
88 |
| - .perform(click()); |
| 81 | + .perform(click()) |
89 | 82 |
|
90 | 83 | // Check setting set to 100 in SharedPreferences
|
91 | 84 | assertEquals(
|
92 | 85 | 100,
|
93 |
| - defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0) |
94 |
| - ); |
| 86 | + defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0).toLong() |
| 87 | + ) |
95 | 88 |
|
96 | 89 | // Check displaying 100 in summary text
|
97 | 90 | Espresso.onData(PreferenceMatchers.withKey("uploads"))
|
98 | 91 | .inAdapterView(withId(android.R.id.list))
|
99 | 92 | .onChildView(withId(android.R.id.summary))
|
100 |
| - .check(matches(withText("100"))); |
| 93 | + .check(matches(withText("100"))) |
101 | 94 | }
|
102 | 95 |
|
103 | 96 | @Test
|
104 |
| - public void setRecentUploadLimitTo700() { |
| 97 | + fun setRecentUploadLimitTo700() { |
105 | 98 | // Open "Use external storage" preference
|
106 | 99 | Espresso.onData(PreferenceMatchers.withKey("uploads"))
|
107 | 100 | .inAdapterView(withId(android.R.id.list))
|
108 |
| - .perform(click()); |
| 101 | + .perform(click()) |
109 | 102 |
|
110 | 103 | // Try setting it to 700
|
111 | 104 | Espresso.onView(withId(android.R.id.edit))
|
112 |
| - .perform(replaceText("700")); |
| 105 | + .perform(replaceText("700")) |
113 | 106 |
|
114 | 107 | // Click "OK"
|
115 | 108 | Espresso.onView(allOf(withId(android.R.id.button1), withText("OK")))
|
116 |
| - .perform(click()); |
| 109 | + .perform(click()) |
117 | 110 |
|
118 | 111 | // Check setting set to 500 in SharedPreferences
|
119 | 112 | assertEquals(
|
120 | 113 | 500,
|
121 |
| - defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0) |
122 |
| - ); |
| 114 | + defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0).toLong() |
| 115 | + ) |
123 | 116 |
|
124 | 117 | // Check displaying 100 in summary text
|
125 | 118 | Espresso.onData(PreferenceMatchers.withKey("uploads"))
|
126 | 119 | .inAdapterView(withId(android.R.id.list))
|
127 | 120 | .onChildView(withId(android.R.id.summary))
|
128 |
| - .check(matches(withText("500"))); |
| 121 | + .check(matches(withText("500"))) |
129 | 122 | }
|
130 | 123 |
|
131 | 124 | @Test
|
132 |
| - public void useAuthorNameTogglesOn() { |
| 125 | + fun useAuthorNameTogglesOn() { |
133 | 126 | // Turn on "Use external storage" preference if currently off
|
134 | 127 | if (!defaultKvStore.getBoolean("useAuthorName", true)) {
|
135 | 128 | Espresso.onData(PreferenceMatchers.withKey("useAuthorName"))
|
136 | 129 | .inAdapterView(withId(android.R.id.list))
|
137 |
| - .perform(click()); |
| 130 | + .perform(click()) |
138 | 131 | }
|
139 | 132 |
|
140 | 133 | // Check authorName preference is enabled
|
141 | 134 | Espresso.onData(PreferenceMatchers.withKey("authorName"))
|
142 | 135 | .inAdapterView(withId(android.R.id.list))
|
143 |
| - .check(matches(isEnabled())); |
| 136 | + .check(matches(isEnabled())) |
144 | 137 | }
|
145 | 138 |
|
146 | 139 | @Test
|
147 |
| - public void useAuthorNameTogglesOff() { |
| 140 | + fun useAuthorNameTogglesOff() { |
148 | 141 | // Turn off "Use external storage" preference if currently on
|
149 | 142 | if (defaultKvStore.getBoolean("useAuthorName", false)) {
|
150 | 143 | Espresso.onData(PreferenceMatchers.withKey("useAuthorName"))
|
151 | 144 | .inAdapterView(withId(android.R.id.list))
|
152 |
| - .perform(click()); |
| 145 | + .perform(click()) |
153 | 146 | }
|
154 | 147 |
|
155 | 148 | // Check authorName preference is enabled
|
156 | 149 | Espresso.onData(PreferenceMatchers.withKey("authorName"))
|
157 | 150 | .inAdapterView(withId(android.R.id.list))
|
158 |
| - .check(matches(not(isEnabled()))); |
| 151 | + .check(matches(not(isEnabled()))) |
159 | 152 | }
|
160 | 153 | }
|
0 commit comments