Skip to content

Commit c2a83d5

Browse files
authored
Deleted unused files. (#412)
Added mock server missing stub warnings. Introduced TestActivity to avoid loading Home in all tests.
1 parent c87c5c2 commit c2a83d5

File tree

11 files changed

+59
-68
lines changed

11 files changed

+59
-68
lines changed

app/src/androidTest/AndroidManifest.xml

Lines changed: 0 additions & 11 deletions
This file was deleted.

app/src/androidTest/java/com/mitteloupe/whoami/test/HistoryHighlightedIpAddressTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package com.mitteloupe.whoami.test
22

33
import androidx.compose.ui.test.ExperimentalTestApi
44
import androidx.compose.ui.test.junit4.createAndroidComposeRule
5+
import com.mitteloupe.whoami.di.TestActivity
56
import com.mitteloupe.whoami.launcher.fromScreen
67
import com.mitteloupe.whoami.localstore.KEY_VALUE_SAVED_HISTORY
78
import com.mitteloupe.whoami.screen.HistoryScreen
89
import com.mitteloupe.whoami.test.annotation.LocalStore
910
import com.mitteloupe.whoami.test.launcher.AppLauncher
1011
import com.mitteloupe.whoami.test.test.BaseTest
1112
import com.mitteloupe.whoami.test.test.retry
12-
import com.mitteloupe.whoami.ui.main.MainActivity
1313
import com.mitteloupe.whoami.ui.main.route.History
1414
import dagger.hilt.android.testing.HiltAndroidTest
1515
import javax.inject.Inject
@@ -20,7 +20,7 @@ private const val HIGHLIGHTED_IP_ADDRESS = "2.2.2.2"
2020
@HiltAndroidTest
2121
@ExperimentalTestApi
2222
class HistoryHighlightedIpAddressTest : BaseTest() {
23-
override val composeTestRule = createAndroidComposeRule<MainActivity>()
23+
override val composeTestRule = createAndroidComposeRule<TestActivity>()
2424

2525
override val startActivityLauncher: AppLauncher by lazy {
2626
fromScreen(composeTestRule, History(highlightedIpAddress = HIGHLIGHTED_IP_ADDRESS))

app/src/androidTest/java/com/mitteloupe/whoami/test/HistoryTest.kt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.mitteloupe.whoami.test
22

33
import androidx.compose.ui.test.ExperimentalTestApi
44
import androidx.compose.ui.test.junit4.createAndroidComposeRule
5+
import com.mitteloupe.whoami.di.TestActivity
56
import com.mitteloupe.whoami.launcher.fromScreen
67
import com.mitteloupe.whoami.localstore.KEY_VALUE_NO_HISTORY
78
import com.mitteloupe.whoami.localstore.KEY_VALUE_SAVED_HISTORY
@@ -11,7 +12,6 @@ import com.mitteloupe.whoami.test.launcher.AppLauncher
1112
import com.mitteloupe.whoami.test.test.BaseTest
1213
import com.mitteloupe.whoami.test.test.doesNot
1314
import com.mitteloupe.whoami.test.test.retry
14-
import com.mitteloupe.whoami.ui.main.MainActivity
1515
import com.mitteloupe.whoami.ui.main.route.History
1616
import dagger.hilt.android.testing.HiltAndroidTest
1717
import javax.inject.Inject
@@ -20,7 +20,7 @@ import org.junit.Test
2020
@HiltAndroidTest
2121
@ExperimentalTestApi
2222
class HistoryTest : BaseTest() {
23-
override val composeTestRule = createAndroidComposeRule<MainActivity>()
23+
override val composeTestRule = createAndroidComposeRule<TestActivity>()
2424

2525
override val startActivityLauncher: AppLauncher by lazy {
2626
fromScreen(composeTestRule, History(highlightedIpAddress = null))
@@ -30,9 +30,7 @@ class HistoryTest : BaseTest() {
3030
lateinit var historyScreen: HistoryScreen
3131

3232
@Test
33-
@LocalStore(
34-
localStoreDataIds = [KEY_VALUE_SAVED_HISTORY]
35-
)
33+
@LocalStore(localStoreDataIds = [KEY_VALUE_SAVED_HISTORY])
3634
fun givenSavedHistoryWhenOnHistoryScreenThenSeesHistory() {
3735
with(historyScreen) {
3836
seeRecord(position = 1, ipAddress = "2.2.2.2", city = "Stockholm", postCode = "12345")
@@ -41,9 +39,7 @@ class HistoryTest : BaseTest() {
4139
}
4240

4341
@Test
44-
@LocalStore(
45-
localStoreDataIds = [KEY_VALUE_SAVED_HISTORY]
46-
)
42+
@LocalStore(localStoreDataIds = [KEY_VALUE_SAVED_HISTORY])
4743
fun givenSavedHistoryWhenTappingDeleteThenRecordDeleted() {
4844
with(historyScreen) {
4945
retry(repeat = 20) {
@@ -76,9 +72,7 @@ class HistoryTest : BaseTest() {
7672
}
7773

7874
@Test
79-
@LocalStore(
80-
localStoreDataIds = [KEY_VALUE_NO_HISTORY]
81-
)
75+
@LocalStore(localStoreDataIds = [KEY_VALUE_NO_HISTORY])
8276
fun givenNoHistoryWhenOnHistoryScreenThenSeesNoRecords() {
8377
with(historyScreen) {
8478
retry(repeat = 20) {

app/src/androidTest/java/com/mitteloupe/whoami/test/HomeTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mitteloupe.whoami.test
33
import androidx.compose.ui.test.ExperimentalTestApi
44
import androidx.compose.ui.test.junit4.createAndroidComposeRule
55
import androidx.test.espresso.intent.Intents
6+
import com.mitteloupe.whoami.di.TestActivity
67
import com.mitteloupe.whoami.launcher.fromScreen
78
import com.mitteloupe.whoami.screen.HomeScreen
89
import com.mitteloupe.whoami.screen.OpenSourceNoticesScreen
@@ -11,7 +12,6 @@ import com.mitteloupe.whoami.server.REQUEST_RESPONSE_GET_IP_DETAILS
1112
import com.mitteloupe.whoami.test.annotation.ServerRequestResponse
1213
import com.mitteloupe.whoami.test.launcher.AppLauncher
1314
import com.mitteloupe.whoami.test.test.BaseTest
14-
import com.mitteloupe.whoami.ui.main.MainActivity
1515
import com.mitteloupe.whoami.ui.main.route.Home
1616
import dagger.hilt.android.testing.HiltAndroidTest
1717
import javax.inject.Inject
@@ -20,7 +20,7 @@ import org.junit.Test
2020
@HiltAndroidTest
2121
@ExperimentalTestApi
2222
class HomeTest : BaseTest() {
23-
override val composeTestRule = createAndroidComposeRule<MainActivity>()
23+
override val composeTestRule = createAndroidComposeRule<TestActivity>()
2424

2525
override val startActivityLauncher: AppLauncher by lazy {
2626
fromScreen(composeTestRule, Home)
@@ -60,6 +60,7 @@ class HomeTest : BaseTest() {
6060
}
6161

6262
@Test
63+
@ServerRequestResponse([REQUEST_RESPONSE_GET_IP, REQUEST_RESPONSE_GET_IP_DETAILS])
6364
fun whenTappingOnOpenSourceNoticesLaunchesActivity() {
6465
Intents.init()
6566
with(composeTestRule) {

app/src/androidTest/res/raw/teststore.pem

Lines changed: 0 additions & 22 deletions
This file was deleted.

app/src/androidTest/res/xml/network_security_config.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

app/src/espresso/AndroidManifest.xml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
44

5-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />
6-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />
5+
<uses-permission
6+
android:name="android.permission.READ_EXTERNAL_STORAGE"
7+
tools:remove="android:maxSdkVersion" />
8+
<uses-permission
9+
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
10+
tools:remove="android:maxSdkVersion" />
711

812
<uses-sdk tools:overrideLibrary="android.support.test.uiautomator.v18" />
913

10-
<application android:networkSecurityConfig="@xml/network_security_config"
11-
tools:targetApi="n" />
14+
<application
15+
android:networkSecurityConfig="@xml/network_security_config"
16+
tools:targetApi="n">
17+
<activity
18+
android:name=".di.TestActivity"
19+
android:exported="true">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
</application>
1226
</manifest>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.mitteloupe.whoami.di
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import dagger.hilt.android.AndroidEntryPoint
5+
6+
@AndroidEntryPoint
7+
class TestActivity : AppCompatActivity()

architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/WebServerRule.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ class WebServerRule(
3737
)
3838
}
3939

40+
mockDispatcher.testName = description.displayName
4041
requestResponses.forEach { requestResponse ->
4142
mockDispatcher.bindResponse(requestResponse)
4243
}
4344
val stubbedResponseKeys = requestResponses
44-
.map { requestResponse -> requestResponse.request.url }
45+
.map { requestResponse -> requestResponse.request }
4546
.toSet()
4647

4748
base.evaluate()

architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/server/MockDispatcher.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mitteloupe.whoami.test.server
22

3+
import android.util.Log
34
import com.mitteloupe.whoami.test.server.response.MockResponseFactory
45
import okhttp3.Headers
56
import okhttp3.Response
@@ -12,17 +13,19 @@ import okhttp3.mockwebserver.RecordedRequest
1213
class MockDispatcher :
1314
Dispatcher(),
1415
ResponseBinder {
15-
override val usedEndpoints: Set<String>
16-
field = mutableSetOf<String>()
16+
override var testName: String = ""
1717

18-
private val responses = mutableMapOf<String, MockResponseFactory>()
18+
override val usedEndpoints: Set<MockRequest>
19+
field = mutableSetOf()
20+
21+
private val responses = mutableMapOf<MockRequest, MockResponseFactory>()
1922

2023
var webSocket: WebSocket? = null
2124

2225
override var onWebSocketMessage: (String) -> Unit = {}
2326

2427
override fun bindResponse(requestResponseFactory: MockRequestResponseFactory) {
25-
responses[requestResponseFactory.request.url] = requestResponseFactory.responseFactory
28+
responses[requestResponseFactory.request] = requestResponseFactory.responseFactory
2629
}
2730

2831
override fun reset() {
@@ -32,8 +35,14 @@ class MockDispatcher :
3235

3336
override fun dispatch(request: RecordedRequest): MockResponse {
3437
val endPoint = request.path!!.substringBefore("?")
35-
usedEndpoints.add(endPoint)
36-
val response = responses[endPoint]?.mockResponse() ?: MockResponse(code = 404)
38+
val matchingRequest = responses.entries.firstOrNull { requestResponse ->
39+
requestResponse.key.url == endPoint
40+
}?.also { requestResponse ->
41+
usedEndpoints.add(requestResponse.key)
42+
}
43+
val response = matchingRequest?.value?.mockResponse() ?: MockResponse(code = 404).also {
44+
Log.w(TAG, "$testName: ${request.path} not stubbed!")
45+
}
3746
return if (response.upgradeToWebSocket) {
3847
MockResponse().withWebSocketUpgrade(
3948
object : WebSocketListener() {
@@ -60,4 +69,8 @@ class MockDispatcher :
6069

6170
private fun Collection<Pair<String, String>>.toArray(): Array<String> =
6271
flatMap { listOf(it.first, it.second) }.toTypedArray()
72+
73+
companion object {
74+
const val TAG = "MockDispatcher"
75+
}
6376
}

architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/server/ResponseBinder.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.mitteloupe.whoami.test.server
22

33
interface ResponseBinder {
4+
var testName: String
5+
46
fun bindResponse(requestResponseFactory: MockRequestResponseFactory)
57

6-
val usedEndpoints: Set<String>
8+
val usedEndpoints: Set<MockRequest>
79

810
fun reset()
911

0 commit comments

Comments
 (0)