Skip to content

Commit 268ba07

Browse files
domdomeggmaskaravivek
authored andcommitted
Convert instrumentation tests to Kotlin (#2689)
* Convert SettingsActivityTest to Kotlin * Convert NavigationBaseActivityTest to Kotlin * Convert UploadTest to Kotlin
1 parent 0612519 commit 268ba07

File tree

5 files changed

+318
-344
lines changed

5 files changed

+318
-344
lines changed

app/src/androidTest/java/fr/free/nrw/commons/NavigationBaseActivityTest.java

-67
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package fr.free.nrw.commons
2+
3+
import androidx.test.espresso.Espresso.onView
4+
import androidx.test.espresso.contrib.DrawerActions
5+
import androidx.test.espresso.contrib.NavigationViewActions
6+
import androidx.test.espresso.matcher.ViewMatchers.withId
7+
import androidx.test.filters.LargeTest
8+
import androidx.test.rule.ActivityTestRule
9+
import androidx.test.runner.AndroidJUnit4
10+
import org.junit.Rule
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
14+
@LargeTest
15+
@RunWith(AndroidJUnit4::class)
16+
class NavigationBaseActivityTest {
17+
@get:Rule
18+
var activityRule: ActivityTestRule<*> = ActivityTestRule(AboutActivity::class.java)
19+
20+
/**
21+
* Goes through all the activities in the app and checks we don't crash
22+
* NB: This is not realistic if we're not logged in; we can access 'home', 'notifications', 'settings' and 'achievements' which we wouldn't otherwise be able to.
23+
*/
24+
@Test
25+
fun goThroughNavigationBaseActivityActivities() {
26+
// Home
27+
openNavigationDrawerAndNavigateTo(R.id.action_home)
28+
29+
// Explore
30+
openNavigationDrawerAndNavigateTo(R.id.action_explore)
31+
32+
// Bookmarks
33+
openNavigationDrawerAndNavigateTo(R.id.action_bookmarks)
34+
35+
// About
36+
openNavigationDrawerAndNavigateTo(R.id.action_about)
37+
38+
// Settings
39+
openNavigationDrawerAndNavigateTo(R.id.action_settings)
40+
41+
// Achievements
42+
openNavigationDrawerAndNavigateTo(R.id.action_login)
43+
}
44+
45+
/**
46+
* Clicks 'Explore' in the navigation drawer twice, then clicks 'home'
47+
* Testing to avoid regression of #2200
48+
*/
49+
@Test
50+
fun doubleNavigateToExploreThenReturnHome() {
51+
// Explore
52+
openNavigationDrawerAndNavigateTo(R.id.action_explore)
53+
54+
// Explore
55+
openNavigationDrawerAndNavigateTo(R.id.action_explore)
56+
57+
// Home
58+
openNavigationDrawerAndNavigateTo(R.id.action_home)
59+
}
60+
61+
private fun openNavigationDrawerAndNavigateTo(menuItemId: Int) {
62+
onView(withId(R.id.drawer_layout)).perform(DrawerActions.open())
63+
onView(withId(R.id.navigation_view)).perform(NavigationViewActions.navigateTo(menuItemId))
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,153 @@
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
3124

3225
@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
3629

37-
@Rule
38-
public ActivityTestRule activityRule = new ActivityTestRule<>(SettingsActivity.class);
30+
@get:Rule
31+
var activityRule: ActivityTestRule<*> = ActivityTestRule(SettingsActivity::class.java)
3932

4033
@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())
4538
}
4639

4740
@Test
48-
public void setRecentUploadLimitTo123() {
41+
fun setRecentUploadLimitTo123() {
4942
// Open "Use external storage" preference
5043
Espresso.onData(PreferenceMatchers.withKey("uploads"))
5144
.inAdapterView(withId(android.R.id.list))
52-
.perform(click());
45+
.perform(click())
5346

5447
// Try setting it to 123
5548
Espresso.onView(withId(android.R.id.edit))
56-
.perform(replaceText("123"));
49+
.perform(replaceText("123"))
5750

5851
// Click "OK"
5952
Espresso.onView(allOf(withId(android.R.id.button1), withText("OK")))
60-
.perform(click());
53+
.perform(click())
6154

6255
// Check setting set to 123 in SharedPreferences
6356
assertEquals(
6457
123,
65-
defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0)
66-
);
58+
defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0).toLong()
59+
)
6760

6861
// Check displaying 123 in summary text
6962
Espresso.onData(PreferenceMatchers.withKey("uploads"))
7063
.inAdapterView(withId(android.R.id.list))
7164
.onChildView(withId(android.R.id.summary))
72-
.check(matches(withText("123")));
65+
.check(matches(withText("123")))
7366
}
7467

7568
@Test
76-
public void setRecentUploadLimitTo0() {
69+
fun setRecentUploadLimitTo0() {
7770
// Open "Use external storage" preference
7871
Espresso.onData(PreferenceMatchers.withKey("uploads"))
7972
.inAdapterView(withId(android.R.id.list))
80-
.perform(click());
73+
.perform(click())
8174

8275
// Try setting it to 0
8376
Espresso.onView(withId(android.R.id.edit))
84-
.perform(replaceText("0"));
77+
.perform(replaceText("0"))
8578

8679
// Click "OK"
8780
Espresso.onView(allOf(withId(android.R.id.button1), withText("OK")))
88-
.perform(click());
81+
.perform(click())
8982

9083
// Check setting set to 100 in SharedPreferences
9184
assertEquals(
9285
100,
93-
defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0)
94-
);
86+
defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0).toLong()
87+
)
9588

9689
// Check displaying 100 in summary text
9790
Espresso.onData(PreferenceMatchers.withKey("uploads"))
9891
.inAdapterView(withId(android.R.id.list))
9992
.onChildView(withId(android.R.id.summary))
100-
.check(matches(withText("100")));
93+
.check(matches(withText("100")))
10194
}
10295

10396
@Test
104-
public void setRecentUploadLimitTo700() {
97+
fun setRecentUploadLimitTo700() {
10598
// Open "Use external storage" preference
10699
Espresso.onData(PreferenceMatchers.withKey("uploads"))
107100
.inAdapterView(withId(android.R.id.list))
108-
.perform(click());
101+
.perform(click())
109102

110103
// Try setting it to 700
111104
Espresso.onView(withId(android.R.id.edit))
112-
.perform(replaceText("700"));
105+
.perform(replaceText("700"))
113106

114107
// Click "OK"
115108
Espresso.onView(allOf(withId(android.R.id.button1), withText("OK")))
116-
.perform(click());
109+
.perform(click())
117110

118111
// Check setting set to 500 in SharedPreferences
119112
assertEquals(
120113
500,
121-
defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0)
122-
);
114+
defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0).toLong()
115+
)
123116

124117
// Check displaying 100 in summary text
125118
Espresso.onData(PreferenceMatchers.withKey("uploads"))
126119
.inAdapterView(withId(android.R.id.list))
127120
.onChildView(withId(android.R.id.summary))
128-
.check(matches(withText("500")));
121+
.check(matches(withText("500")))
129122
}
130123

131124
@Test
132-
public void useAuthorNameTogglesOn() {
125+
fun useAuthorNameTogglesOn() {
133126
// Turn on "Use external storage" preference if currently off
134127
if (!defaultKvStore.getBoolean("useAuthorName", true)) {
135128
Espresso.onData(PreferenceMatchers.withKey("useAuthorName"))
136129
.inAdapterView(withId(android.R.id.list))
137-
.perform(click());
130+
.perform(click())
138131
}
139132

140133
// Check authorName preference is enabled
141134
Espresso.onData(PreferenceMatchers.withKey("authorName"))
142135
.inAdapterView(withId(android.R.id.list))
143-
.check(matches(isEnabled()));
136+
.check(matches(isEnabled()))
144137
}
145138

146139
@Test
147-
public void useAuthorNameTogglesOff() {
140+
fun useAuthorNameTogglesOff() {
148141
// Turn off "Use external storage" preference if currently on
149142
if (defaultKvStore.getBoolean("useAuthorName", false)) {
150143
Espresso.onData(PreferenceMatchers.withKey("useAuthorName"))
151144
.inAdapterView(withId(android.R.id.list))
152-
.perform(click());
145+
.perform(click())
153146
}
154147

155148
// Check authorName preference is enabled
156149
Espresso.onData(PreferenceMatchers.withKey("authorName"))
157150
.inAdapterView(withId(android.R.id.list))
158-
.check(matches(not(isEnabled())));
151+
.check(matches(not(isEnabled())))
159152
}
160153
}

0 commit comments

Comments
 (0)