Skip to content

Commit d1bf6d0

Browse files
Add CommonPlaceClickActions Unit Tests (#4674)
1 parent 0269894 commit d1bf6d0

File tree

2 files changed

+115
-9
lines changed

2 files changed

+115
-9
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/fragments/CommonPlaceClickActions.kt

+13-9
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class CommonPlaceClickActions @Inject constructor(
7474

7575
private fun openWebView(link: Uri): Boolean {
7676
Utils.handleWebUrl(activity, link)
77-
return true;
77+
return true
7878
}
7979

8080
private fun PopupMenu.enableBy(menuId: Int, hasLink: Boolean) {
@@ -85,15 +85,19 @@ class CommonPlaceClickActions @Inject constructor(
8585
AlertDialog.Builder(activity)
8686
.setMessage(R.string.login_alert_message)
8787
.setPositiveButton(R.string.login) { dialog, which ->
88-
ActivityUtils.startActivityWithFlags(
89-
activity,
90-
LoginActivity::class.java,
91-
Intent.FLAG_ACTIVITY_CLEAR_TOP,
92-
Intent.FLAG_ACTIVITY_SINGLE_TOP
93-
)
94-
applicationKvStore.putBoolean("login_skipped", false)
95-
activity.finish()
88+
setPositiveButton()
9689
}
9790
.show()
9891
}
92+
93+
private fun setPositiveButton() {
94+
ActivityUtils.startActivityWithFlags(
95+
activity,
96+
LoginActivity::class.java,
97+
Intent.FLAG_ACTIVITY_CLEAR_TOP,
98+
Intent.FLAG_ACTIVITY_SINGLE_TOP
99+
)
100+
applicationKvStore.putBoolean("login_skipped", false)
101+
activity.finish()
102+
}
99103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package fr.free.nrw.commons.nearby
2+
3+
import android.net.Uri
4+
import fr.free.nrw.commons.TestCommonsApplication
5+
import fr.free.nrw.commons.contributions.ContributionController
6+
import fr.free.nrw.commons.kvstore.JsonKvStore
7+
import fr.free.nrw.commons.nearby.fragments.CommonPlaceClickActions
8+
import fr.free.nrw.commons.profile.ProfileActivity
9+
import org.junit.Assert
10+
import org.junit.Before
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
import org.mockito.Mock
14+
import org.mockito.MockitoAnnotations
15+
import org.robolectric.Robolectric
16+
import org.robolectric.RobolectricTestRunner
17+
import org.robolectric.annotation.Config
18+
import org.robolectric.annotation.LooperMode
19+
import java.lang.reflect.Method
20+
21+
@RunWith(RobolectricTestRunner::class)
22+
@Config(sdk = [21], application = TestCommonsApplication::class)
23+
@LooperMode(LooperMode.Mode.PAUSED)
24+
class CommonPlaceClickActionsUnitTest {
25+
26+
private lateinit var commonPlaceClickActions: CommonPlaceClickActions
27+
28+
@Mock
29+
private lateinit var store: JsonKvStore
30+
31+
@Mock
32+
private lateinit var contributionController: ContributionController
33+
34+
@Mock
35+
private lateinit var place: Place
36+
37+
@Mock
38+
private lateinit var uri: Uri
39+
40+
@Before
41+
fun setUp() {
42+
MockitoAnnotations.initMocks(this)
43+
val activity = Robolectric.buildActivity(ProfileActivity::class.java).create().get()
44+
commonPlaceClickActions = CommonPlaceClickActions(store, activity, contributionController)
45+
}
46+
47+
@Test
48+
fun testNonNull() {
49+
Assert.assertNotNull(commonPlaceClickActions)
50+
}
51+
52+
@Test
53+
fun testFunctionDeclaration() {
54+
Assert.assertNotNull(commonPlaceClickActions.onCameraClicked())
55+
Assert.assertNotNull(commonPlaceClickActions.onGalleryClicked())
56+
Assert.assertNotNull(commonPlaceClickActions.onOverflowClicked())
57+
Assert.assertNotNull(commonPlaceClickActions.onDirectionsClicked())
58+
}
59+
60+
@Test
61+
@Throws(Exception::class)
62+
fun testStoreSharedPrefs() {
63+
val method: Method = CommonPlaceClickActions::class.java.getDeclaredMethod(
64+
"storeSharedPrefs",
65+
Place::class.java
66+
)
67+
method.isAccessible = true
68+
method.invoke(commonPlaceClickActions, place)
69+
}
70+
71+
@Test
72+
@Throws(Exception::class)
73+
fun testOpenWebView() {
74+
val method: Method = CommonPlaceClickActions::class.java.getDeclaredMethod(
75+
"openWebView",
76+
Uri::class.java
77+
)
78+
method.isAccessible = true
79+
Assert.assertEquals(method.invoke(commonPlaceClickActions, uri), true)
80+
}
81+
82+
@Test
83+
@Throws(Exception::class)
84+
fun testShowLoginDialog() {
85+
val method: Method = CommonPlaceClickActions::class.java.getDeclaredMethod(
86+
"showLoginDialog"
87+
)
88+
method.isAccessible = true
89+
method.invoke(commonPlaceClickActions)
90+
}
91+
92+
@Test
93+
@Throws(Exception::class)
94+
fun testSetPositiveButton() {
95+
val method: Method = CommonPlaceClickActions::class.java.getDeclaredMethod(
96+
"setPositiveButton"
97+
)
98+
method.isAccessible = true
99+
method.invoke(commonPlaceClickActions)
100+
}
101+
102+
}

0 commit comments

Comments
 (0)