Skip to content

Commit 7a7e1e7

Browse files
committed
Test license selection in Settings and run emulator on Travis
1 parent db81e9c commit 7a7e1e7

File tree

4 files changed

+144
-2
lines changed

4 files changed

+144
-2
lines changed

.travis.yml

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
language: android
2+
addons:
3+
apt:
4+
packages:
5+
- w3m
6+
env:
7+
global:
8+
- ANDROID_TARGET=android-22
9+
- ANDROID_ABI=armeabi-v7a
210
android:
311
components:
412
- platform-tools
513
- tools
614
- build-tools-25.0.1
715
- extra-google-m2repository
816
- extra-android-m2repository
17+
- extra-google-google_play_services
18+
- extra-android-support
19+
- ${ANDROID_TARGET}
920
- android-25
10-
- sys-img-x86-android-18
21+
- sys-img-${ANDROID_ABI}-${ANDROID_TARGET}
22+
before_script:
23+
- echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI
24+
- emulator -avd test -no-audio -no-window &
25+
- android-wait-for-emulator
26+
- adb shell input keyevent 82 &
27+
script:
28+
- ./gradlew test connectedAndroidTest -stacktrace
29+
after_failure:
30+
- w3m -dump ${TRAVIS_BUILD_DIR}/app/build/reports/androidTests/connected/*Test.html
1131
jdk:
1232
# - openjdk8 # not yet available
1333
- oraclejdk8

app/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ dependencies {
1313
compile "com.android.support:support-v4:${project.supportLibVersion}"
1414
compile "com.android.support:appcompat-v7:${project.supportLibVersion}"
1515
compile "com.android.support:design:${project.supportLibVersion}"
16+
compile "com.android.support:support-annotations:${project.supportLibVersion}"
1617
compile 'com.google.code.gson:gson:2.7'
1718
compile "com.jakewharton:butterknife:$BUTTERKNIFE_VERSION"
1819
annotationProcessor "com.jakewharton:butterknife-compiler:$BUTTERKNIFE_VERSION"
1920

2021
testCompile 'junit:junit:4.12'
22+
androidTestCompile "com.android.support:support-annotations:${project.supportLibVersion}"
23+
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
2124
}
2225

2326
android {
@@ -32,6 +35,7 @@ android {
3235
versionName '2.2'
3336
minSdkVersion project.minSdkVersion
3437
targetSdkVersion project.targetSdkVersion
38+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
3539
}
3640

3741
buildTypes {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package fr.free.nrw.commons;
2+
3+
import static org.hamcrest.Matchers.allOf;
4+
import static org.hamcrest.Matchers.anyOf;
5+
import static org.hamcrest.Matchers.anything;
6+
7+
import android.content.SharedPreferences;
8+
import android.preference.PreferenceManager;
9+
import android.support.test.espresso.Espresso;
10+
import android.support.test.espresso.action.ViewActions;
11+
import android.support.test.espresso.assertion.ViewAssertions;
12+
import android.support.test.espresso.matcher.ViewMatchers;
13+
import android.support.test.filters.LargeTest;
14+
import android.support.test.rule.ActivityTestRule;
15+
import android.support.test.runner.AndroidJUnit4;
16+
import android.view.View;
17+
18+
import fr.free.nrw.commons.settings.SettingsActivity;
19+
20+
import java.util.Map;
21+
22+
import org.hamcrest.Matcher;
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
@LargeTest
28+
@RunWith(AndroidJUnit4.class)
29+
public class SettingsActivityTest {
30+
private SharedPreferences prefs;
31+
private Map<String,?> prefValues;
32+
33+
@Rule
34+
public ActivityTestRule<SettingsActivity> activityRule =
35+
new ActivityTestRule<SettingsActivity>(SettingsActivity.class,
36+
false /* Initial touch mode */, true /* launch activity */) {
37+
38+
@Override
39+
protected void afterActivityLaunched() {
40+
// save preferences
41+
prefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
42+
prefValues = prefs.getAll();
43+
}
44+
45+
@Override
46+
protected void afterActivityFinished() {
47+
// restore preferences
48+
SharedPreferences.Editor editor = prefs.edit();
49+
for (Map.Entry<String,?> entry: prefValues.entrySet()) {
50+
String key = entry.getKey();
51+
Object val = entry.getValue();
52+
if (val instanceof String) {
53+
editor.putString(key, (String)val);
54+
} else if (val instanceof Boolean) {
55+
editor.putBoolean(key, (Boolean)val);
56+
} else if (val instanceof Integer) {
57+
editor.putInt(key, (Integer)val);
58+
} else {
59+
throw new RuntimeException("type not implemented: " + entry);
60+
}
61+
}
62+
editor.apply();
63+
}
64+
};
65+
66+
@Test
67+
public void oneLicenseIsChecked() {
68+
// click "License" (the first item)
69+
Espresso.onData(anything())
70+
.inAdapterView(findPreferenceList())
71+
.atPosition(0)
72+
.perform(ViewActions.click());
73+
74+
// test the selected item
75+
Espresso.onView(ViewMatchers.isChecked())
76+
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
77+
}
78+
79+
@Test
80+
public void afterClickingCcby4ItWillStay() {
81+
// click "License" (the first item)
82+
Espresso.onData(anything())
83+
.inAdapterView(findPreferenceList())
84+
.atPosition(0)
85+
.perform(ViewActions.click());
86+
87+
// click "CC BY-4.0"
88+
Espresso.onView(
89+
// FIXME: just R.string.license_name_cc_by_four should be fine but fails on Travis
90+
textAnyOf(R.string.license_name_cc_by_four, R.string.license_name_cc_by_4_0)
91+
).perform(ViewActions.click());
92+
93+
// click "License" (the first item)
94+
Espresso.onData(anything())
95+
.inAdapterView(findPreferenceList())
96+
.atPosition(0)
97+
.perform(ViewActions.click());
98+
99+
// test the value remains "CC BY-4.0"
100+
Espresso.onView(ViewMatchers.isChecked())
101+
.check(ViewAssertions.matches(
102+
textAnyOf(R.string.license_name_cc_by_four, R.string.license_name_cc_by_4_0)
103+
));
104+
}
105+
106+
private Matcher<View> textAnyOf(int id1, int id2) {
107+
return anyOf(ViewMatchers.withText(id1), ViewMatchers.withText(id2));
108+
}
109+
110+
private static Matcher<View> findPreferenceList() {
111+
return allOf(
112+
ViewMatchers.isDescendantOfA(ViewMatchers.withId(android.R.id.content)),
113+
ViewMatchers.withId(android.R.id.list),
114+
ViewMatchers.hasFocus()
115+
);
116+
}
117+
}

script/style/checkstyle.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080

8181
<module name="VisibilityModifier">
8282
<property name="packageAllowed" value="false"/>
83-
83+
<!-- Espresso -->
84+
<property name="ignoreAnnotationCanonicalNames" value="Rule" />
8485
<!-- Butter Knife -->
8586
<property name="ignoreAnnotationCanonicalNames" value="BindArray" />
8687
<property name="ignoreAnnotationCanonicalNames" value="BindBitmap" />

0 commit comments

Comments
 (0)