Skip to content

Commit d925e32

Browse files
Added AboutActivityTest (#3475)
* Added AboutActivityTest * Changes made as per suggestions * Removed File to resolve conflict * Removed hardcoded packagename * Changes as per suggestion * Removed Unrelated changes * Fixed Build Issues
1 parent 57ddc6a commit d925e32

File tree

4 files changed

+113
-5
lines changed

4 files changed

+113
-5
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package fr.free.nrw.commons
2+
3+
import android.app.Activity
4+
import android.app.Instrumentation
5+
import android.content.Intent
6+
import androidx.test.InstrumentationRegistry
7+
import androidx.test.core.app.ApplicationProvider.getApplicationContext
8+
import androidx.test.espresso.Espresso
9+
import androidx.test.espresso.action.ViewActions
10+
import androidx.test.espresso.assertion.ViewAssertions
11+
import androidx.test.espresso.intent.Intents
12+
import androidx.test.espresso.intent.matcher.IntentMatchers
13+
import androidx.test.espresso.matcher.ViewMatchers
14+
import androidx.test.espresso.matcher.ViewMatchers.withText
15+
import androidx.test.rule.ActivityTestRule
16+
import androidx.test.runner.AndroidJUnit4
17+
import fr.free.nrw.commons.utils.ConfigUtils
18+
import org.hamcrest.CoreMatchers
19+
import org.junit.Before
20+
import org.junit.Rule
21+
import org.junit.Test
22+
import org.junit.runner.RunWith
23+
24+
@RunWith(AndroidJUnit4::class)
25+
class AboutActivityTest {
26+
@get:Rule
27+
var activityRule: ActivityTestRule<*> = ActivityTestRule(AboutActivity::class.java)
28+
29+
@Before
30+
fun setup() {
31+
Intents.init()
32+
Intents.intending(CoreMatchers.not(IntentMatchers.isInternal()))
33+
.respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, null))
34+
}
35+
36+
@Test
37+
fun testBuildNumber() {
38+
Espresso.onView(ViewMatchers.withId(R.id.about_version))
39+
.check(ViewAssertions.matches(withText(ConfigUtils.getVersionNameWithSha(getApplicationContext()))))
40+
}
41+
42+
@Test
43+
fun testLaunchWebsite() {
44+
Espresso.onView(ViewMatchers.withId(R.id.website_launch_icon)).perform(ViewActions.click())
45+
Intents.intended(CoreMatchers.allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW),
46+
IntentMatchers.hasData(Urls.WEBSITE_URL)))
47+
}
48+
49+
@Test
50+
fun testLaunchFacebook() {
51+
Espresso.onView(ViewMatchers.withId(R.id.facebook_launch_icon)).perform(ViewActions.click())
52+
Intents.intended(IntentMatchers.hasAction(Intent.ACTION_VIEW))
53+
Intents.intended(CoreMatchers.anyOf(IntentMatchers.hasData(Urls.FACEBOOK_WEB_URL),
54+
IntentMatchers.hasPackage(Urls.FACEBOOK_PACKAGE_NAME)))
55+
}
56+
57+
@Test
58+
fun testLaunchGithub() {
59+
Espresso.onView(ViewMatchers.withId(R.id.github_launch_icon)).perform(ViewActions.click())
60+
Intents.intended(CoreMatchers.allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW),
61+
IntentMatchers.hasData(Urls.GITHUB_REPO_URL)))
62+
}
63+
64+
@Test
65+
fun testLaunchRateUs() {
66+
val appPackageName = InstrumentationRegistry.getInstrumentation().targetContext.packageName
67+
Espresso.onView(ViewMatchers.withId(R.id.about_rate_us)).perform(ViewActions.click())
68+
Intents.intended(IntentMatchers.hasAction(Intent.ACTION_VIEW))
69+
Intents.intended(CoreMatchers.anyOf(IntentMatchers.hasData("${Urls.PLAY_STORE_URL_PREFIX}$appPackageName"),
70+
IntentMatchers.hasData("${Urls.PLAY_STORE_URL_PREFIX}$appPackageName")))
71+
}
72+
73+
@Test
74+
fun testLaunchAboutPrivacyPolicy() {
75+
Espresso.onView(ViewMatchers.withId(R.id.about_privacy_policy)).perform(ViewActions.click())
76+
Intents.intended(CoreMatchers.allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW),
77+
IntentMatchers.hasData(BuildConfig.PRIVACY_POLICY_URL)))
78+
}
79+
80+
@Test
81+
fun testLaunchTranslate() {
82+
Espresso.onView(ViewMatchers.withId(R.id.about_translate)).perform(ViewActions.click())
83+
Espresso.onView(ViewMatchers.withId(android.R.id.button1)).perform(ViewActions.click())
84+
val langCode = CommonsApplication.getInstance().languageLookUpTable.codes[0]
85+
Intents.intended(CoreMatchers.allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW),
86+
IntentMatchers.hasData("${Urls.TRANSLATE_WIKI_URL}$langCode")))
87+
}
88+
89+
@Test
90+
fun testLaunchAboutCredits() {
91+
Espresso.onView(ViewMatchers.withId(R.id.about_credits)).perform(ViewActions.click())
92+
Intents.intended(CoreMatchers.allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW),
93+
IntentMatchers.hasData(Urls.CREDITS_URL)))
94+
}
95+
96+
@Test
97+
fun testLaunchAboutFaq() {
98+
Espresso.onView(ViewMatchers.withId(R.id.about_faq)).perform(ViewActions.click())
99+
Intents.intended(CoreMatchers.allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW),
100+
IntentMatchers.hasData(Urls.FAQ_URL)))
101+
}
102+
103+
@Test
104+
fun orientationChange() {
105+
UITestHelper.changeOrientation(activityRule)
106+
}
107+
}

app/src/main/java/fr/free/nrw/commons/AboutActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
122122
public boolean onOptionsItemSelected(MenuItem item) {
123123
switch (item.getItemId()) {
124124
case R.id.share_app_icon:
125-
String shareText = String.format(getString(R.string.share_text), Urls.PLAY_STORE_URL);
125+
String shareText = String.format(getString(R.string.share_text), Urls.PLAY_STORE_URL_PREFIX + this.getPackageName());
126126
Intent sendIntent = new Intent();
127127
sendIntent.setAction(Intent.ACTION_SEND);
128128
sendIntent.putExtra(Intent.EXTRA_TEXT, shareText);

app/src/main/java/fr/free/nrw/commons/Urls.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ internal object Urls {
66
const val WEBSITE_URL = "https://commons-app.github.io"
77
const val CREDITS_URL = "https://github.com/commons-app/apps-android-commons/blob/master/CREDITS"
88
const val FAQ_URL = "https://github.com/commons-app/apps-android-commons/wiki/Frequently-Asked-Questions"
9-
const val PLAY_STORE_URL = "https://play.google.com/store/apps/details?id=fr.free.nrw.commons"
9+
const val PLAY_STORE_PREFIX = "market://details?id="
10+
const val PLAY_STORE_URL_PREFIX = "https://play.google.com/store/apps/details?id="
1011
const val TRANSLATE_WIKI_URL = "https://translatewiki.net/w/i.php?title=Special:Translate&group=commons-android-strings&filter=%21translated&action=translate&language="
1112
const val FACEBOOK_WEB_URL = "https://www.facebook.com/1921335171459985"
1213
const val FACEBOOK_APP_URL = "fb://page/1921335171459985"

app/src/main/java/fr/free/nrw/commons/Utils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ public static String fixExtension(String title, String extension) {
115115
* @param context
116116
*/
117117
public static void rateApp(Context context) {
118-
final String appPackageName = BuildConfig.class.getPackage().getName();
118+
final String appPackageName = context.getPackageName();
119119
try {
120-
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
120+
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Urls.PLAY_STORE_PREFIX + appPackageName)));
121121
}
122122
catch (android.content.ActivityNotFoundException anfe) {
123-
handleWebUrl(context, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
123+
handleWebUrl(context, Uri.parse(Urls.PLAY_STORE_URL_PREFIX + appPackageName));
124124
}
125125
}
126126

0 commit comments

Comments
 (0)