-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup a basic structure for e2e tests. - Add compose, hilt and networking dependencies - Run instrumented tests using the orchestrator - Setup the test runner - Setup the test network dependencies - Create abstraction to setup mock server responses - Add 3 basic tests
- Loading branch information
Showing
7 changed files
with
252 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
app/src/androidTest/kotlin/com/fibelatti/pinboard/EndToEndTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.fibelatti.pinboard | ||
|
||
import androidx.compose.ui.test.assertIsDisplayed | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.compose.ui.test.onNodeWithText | ||
import androidx.compose.ui.test.performClick | ||
import androidx.compose.ui.test.performTextInput | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.fibelatti.pinboard.core.util.DateFormatter | ||
import com.fibelatti.pinboard.features.MainActivity | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import javax.inject.Inject | ||
|
||
@HiltAndroidTest | ||
class EndToEndTests { | ||
|
||
@get:Rule | ||
val hiltRule = HiltAndroidRule(this) | ||
|
||
@get:Rule | ||
val composeRule = createAndroidComposeRule<MainActivity>() | ||
|
||
@Inject | ||
lateinit var dateFormatter: DateFormatter | ||
|
||
private val context by lazy { | ||
InstrumentationRegistry.getInstrumentation().targetContext | ||
} | ||
|
||
@Before | ||
fun setup() { | ||
hiltRule.inject() | ||
} | ||
|
||
@Test | ||
fun loginScreenIsVisibleWhenFirstLaunchingTheApp() { | ||
with(composeRule) { | ||
// Assert | ||
onNodeWithText(context.getString(R.string.auth_title)).assertIsDisplayed() | ||
} | ||
} | ||
|
||
@Test | ||
fun userCanLaunchAppReviewMode() { | ||
with(composeRule) { | ||
// Act | ||
onNodeWithText(context.getString(R.string.auth_token_hint)).performTextInput("app_review_mode") | ||
onNodeWithText(context.getString(R.string.auth_button)).performClick() | ||
|
||
// Assert | ||
onNodeWithText(context.getString(R.string.posts_title_all)).assertIsDisplayed() | ||
onNodeWithText(context.getString(R.string.posts_empty_title)).assertIsDisplayed() | ||
} | ||
} | ||
|
||
@Test | ||
fun userCanLoginAndFetchBookmarks() { | ||
// Arrange | ||
MockServer.loginResponses(updateTimestamp = dateFormatter.nowAsTzFormat()) | ||
|
||
with(composeRule) { | ||
// Act | ||
onNodeWithText(context.getString(R.string.auth_token_hint)).performTextInput(MockServer.TestData.TOKEN) | ||
onNodeWithText(context.getString(R.string.auth_button)).performClick() | ||
|
||
// Assert | ||
onNodeWithText(context.getString(R.string.posts_title_all)).assertIsDisplayed() | ||
onNodeWithText("Google").assertIsDisplayed() | ||
onNodeWithText("Private").assertIsDisplayed() | ||
onNodeWithText("Read later").assertIsDisplayed() | ||
onNodeWithText("Instrumented test").assertIsDisplayed() | ||
onNodeWithText("android").assertIsDisplayed() | ||
onNodeWithText("dev").assertIsDisplayed() | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/androidTest/kotlin/com/fibelatti/pinboard/HiltTestRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.fibelatti.pinboard | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import android.os.StrictMode | ||
import androidx.test.runner.AndroidJUnitRunner | ||
import dagger.hilt.android.testing.HiltTestApplication | ||
|
||
@Suppress("Unused") | ||
class HiltTestRunner : AndroidJUnitRunner() { | ||
|
||
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application { | ||
// Workaround to setup the MockWebServer | ||
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitAll().build()) | ||
return super.newApplication(cl, HiltTestApplication::class.java.name, context) | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
app/src/androidTest/kotlin/com/fibelatti/pinboard/MockServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.fibelatti.pinboard | ||
|
||
import okhttp3.mockwebserver.Dispatcher | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import okhttp3.mockwebserver.RecordedRequest | ||
|
||
object MockServer { | ||
|
||
val instance = MockWebServer() | ||
|
||
fun loginResponses(updateTimestamp: String) { | ||
setResponses( | ||
"/posts/update" to { | ||
MockResponse().setResponseCode(200) | ||
.setBody(TestData.updateResponse(timestamp = updateTimestamp)) | ||
}, | ||
"/posts/all" to { request -> | ||
MockResponse().setResponseCode(200).apply { | ||
if (request.requestUrl.toString().contains("start=0")) { | ||
setBody(TestData.allBookmarksResponse()) | ||
} else { | ||
setBody(TestData.emptyBookmarksResponse()) | ||
} | ||
} | ||
}, | ||
) | ||
} | ||
|
||
private fun setResponses(vararg responses: Pair<String, (RecordedRequest) -> MockResponse>) { | ||
instance.dispatcher = object : Dispatcher() { | ||
private val handlers = responses.toList() | ||
|
||
override fun dispatch(request: RecordedRequest): MockResponse { | ||
val requestPath = request.path.orEmpty() | ||
val handler = handlers.firstOrNull { (path, _) -> requestPath.contains(path) }?.second | ||
|
||
return handler?.invoke(request) ?: MockResponse().setResponseCode(404) | ||
} | ||
} | ||
} | ||
|
||
object TestData { | ||
|
||
const val TOKEN = "instrumented:1000" | ||
|
||
fun updateResponse(timestamp: String): String = """ | ||
{ | ||
"update_time":"$timestamp" | ||
} | ||
""".trimIndent() | ||
|
||
fun allBookmarksResponse(): String = """ | ||
[ | ||
{ | ||
"href":"https:\/\/www.google.com", | ||
"description":"Google", | ||
"extended":"Instrumented test", | ||
"meta":"d0ec3d2af45baa4365121cacab6166d3", | ||
"hash":"8ffdefbdec956b595d257f0aaeefd623", | ||
"time":"2023-08-05T11:51:33Z", | ||
"shared":"no", | ||
"toread":"yes", | ||
"tags":"android dev" | ||
} | ||
] | ||
""".trimIndent() | ||
|
||
fun emptyBookmarksResponse(): String = "[]" | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
app/src/androidTest/kotlin/com/fibelatti/pinboard/core/di/modules/TestNetworkModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.fibelatti.pinboard.core.di.modules | ||
|
||
import com.fibelatti.pinboard.MockServer | ||
import com.fibelatti.pinboard.core.di.UrlParser | ||
import com.fibelatti.pinboard.core.network.ApiInterceptor | ||
import com.fibelatti.pinboard.core.network.SkipBadElementsListAdapter | ||
import com.fibelatti.pinboard.core.network.UnauthorizedInterceptor | ||
import com.squareup.moshi.Moshi | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.components.SingletonComponent | ||
import dagger.hilt.testing.TestInstallIn | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.moshi.MoshiConverterFactory | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@TestInstallIn( | ||
components = [SingletonComponent::class], | ||
replaces = [NetworkModule::class], | ||
) | ||
object TestNetworkModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun retrofit(okHttpClient: OkHttpClient, moshi: Moshi): Retrofit = Retrofit.Builder() | ||
.baseUrl(MockServer.instance.url("/")) | ||
.client(okHttpClient) | ||
.addConverterFactory(MoshiConverterFactory.create(moshi)) | ||
.build() | ||
|
||
@Provides | ||
fun moshi(): Moshi = Moshi.Builder().add(SkipBadElementsListAdapter.Factory).build() | ||
|
||
@Provides | ||
fun okHttpClient( | ||
apiInterceptor: ApiInterceptor, | ||
unauthorizedInterceptor: UnauthorizedInterceptor, | ||
loggingInterceptor: HttpLoggingInterceptor, | ||
): OkHttpClient = OkHttpClient.Builder() | ||
.addInterceptor(apiInterceptor) | ||
.addInterceptor(unauthorizedInterceptor) | ||
.addInterceptor(loggingInterceptor) | ||
.build() | ||
|
||
@Provides | ||
@UrlParser | ||
fun urlParserOkHttpClient( | ||
loggingInterceptor: HttpLoggingInterceptor, | ||
): OkHttpClient = OkHttpClient.Builder() | ||
.followRedirects(true) | ||
.followSslRedirects(true) | ||
.addInterceptor(loggingInterceptor) | ||
.build() | ||
|
||
@Provides | ||
fun httpLoggingInterceptor(): HttpLoggingInterceptor = HttpLoggingInterceptor() | ||
.apply { level = HttpLoggingInterceptor.Level.BODY } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters