Skip to content

Commit

Permalink
Adjust tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nowakweronika committed Dec 8, 2023
1 parent fab1b78 commit 8ae0ec6
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.appunite.loudius
import android.app.Activity
import android.app.Instrumentation
import android.content.Intent
import android.os.Bundle
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.espresso.intent.Intents.intended
Expand Down Expand Up @@ -47,8 +46,7 @@ import org.junit.Test
import org.koin.core.context.GlobalContext
import org.koin.dsl.module
import strikt.api.expectThat
import strikt.assertions.isEqualTo
import strikt.assertions.one
import strikt.assertions.containsExactly

abstract class AbsLoginScreenTest {

Expand Down Expand Up @@ -78,7 +76,7 @@ abstract class AbsLoginScreenTest {
}

@Test
fun whenLoginScreenIsVisible_LoginButtonOpensGithubAuth() = with(integrationTestRule) {
fun whenLoginScreenIsVisible_LoginButtonOpensGithubAuth(): Unit = with(integrationTestRule) {
intending(hasAction(Intent.ACTION_VIEW))
.respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, null))

Expand All @@ -98,18 +96,14 @@ abstract class AbsLoginScreenTest {
)
)

val bundle = Bundle()
bundle.putString("item_name", "log_in")

expectThat(analyticsRule.analytics.log) {
one {
this.isEqualTo(AnalyticsLog("button_click", bundle))
}
}
expectThat(analyticsRule.analytics.log).containsExactly(
AnalyticsLog("button_click", mapOf("item_name" to "log_in")),
AnalyticsLog("simple_action", mapOf("item_name" to "open_github_auth"))
)
}

@Test
fun whenClickingPermissionGrantedInXiaomiDialog_OpenGithubAuth() = with(integrationTestRule) {
fun whenClickingPermissionGrantedInXiaomiDialog_OpenGithubAuth(): Unit = with(integrationTestRule) {
every { githubHelper.shouldAskForXiaomiIntent() } returns true
intending(hasAction(Intent.ACTION_VIEW))
.respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, null))
Expand All @@ -130,10 +124,17 @@ abstract class AbsLoginScreenTest {
hasData("https://github.com/login/oauth/authorize?client_id=91131449e417c7e29912&scope=repo")
)
)

expectThat(analyticsRule.analytics.log).containsExactly(
AnalyticsLog("button_click", mapOf("item_name" to "log_in")),
AnalyticsLog("simple_action", mapOf("item_name" to "show_xiaomi_permission_dialog")),
AnalyticsLog("simple_action", mapOf("item_name" to "xiaomi_permission_dialog_permission_already_granted")),
AnalyticsLog("simple_action", mapOf("item_name" to "open_github_auth"))
)
}

@Test
fun whenClickingGrantPermissionInXiaomiDialog_OpenPermissionEditor() =
fun whenClickingGrantPermissionInXiaomiDialog_OpenPermissionEditor(): Unit =
with(integrationTestRule) {
every { githubHelper.shouldAskForXiaomiIntent() } returns true
intending(hasAction("miui.intent.action.APP_PERM_EDITOR"))
Expand Down Expand Up @@ -161,5 +162,11 @@ abstract class AbsLoginScreenTest {
)
)
)

expectThat(analyticsRule.analytics.log).containsExactly(
AnalyticsLog("button_click", mapOf("item_name" to "log_in")),
AnalyticsLog("simple_action", mapOf("item_name" to "show_xiaomi_permission_dialog")),
AnalyticsLog("simple_action", mapOf("item_name" to "xiaomi_permission_dialog_permission_granted"))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package com.appunite.loudius

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.onNodeWithText
import com.appunite.loudius.analytics.AnalyticsLog
import com.appunite.loudius.analytics.AnalyticsRule
import com.appunite.loudius.components.theme.LoudiusTheme
import com.appunite.loudius.ui.pullrequests.PullRequestsScreen
import com.appunite.loudius.util.IntegrationTestRule
Expand All @@ -26,6 +28,8 @@ import com.appunite.loudius.util.Register
import com.appunite.loudius.util.waitUntilLoadingDoesNotExist
import org.junit.Rule
import org.junit.Test
import strikt.api.expectThat
import strikt.assertions.containsExactly

abstract class AbsPullRequestsScreenTest {

Expand All @@ -35,8 +39,11 @@ abstract class AbsPullRequestsScreenTest {
@get:Rule(order = 1)
var mockWebServer: MockWebServerRule = MockWebServerRule()

@get:Rule
val analyticsRule = AnalyticsRule()

@Test
fun whenResponseIsCorrectThenPullRequestItemIsVisible() {
fun whenResponseIsCorrectThenPullRequestItemIsVisible(): Unit {
with(integrationTestRule) {
Register.user(mockWebServer)
Register.issues(mockWebServer)
Expand All @@ -51,6 +58,12 @@ abstract class AbsPullRequestsScreenTest {

composeTestRule.onNodeWithText("First Pull-Request title")
.assertIsDisplayed()

expectThat(analyticsRule.analytics.log).containsExactly(
AnalyticsLog("screen_opened", mapOf("item_name" to "pull_requests_screen")),
AnalyticsLog("action_start", mapOf("item_name" to "fetch_pull_requests_data")),
AnalyticsLog("action_finished", mapOf("item_name" to "fetch_pull_requests_data", "success" to true))
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.appunite.loudius
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.appunite.loudius.analytics.AnalyticsLog
import com.appunite.loudius.analytics.AnalyticsRule
import com.appunite.loudius.components.theme.LoudiusTheme
import com.appunite.loudius.ui.reviewers.ReviewersScreen
import com.appunite.loudius.util.IntegrationTestRule
Expand All @@ -28,6 +30,8 @@ import com.appunite.loudius.util.waitUntilLoadingDoesNotExist
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import strikt.api.expectThat
import strikt.assertions.containsExactly

abstract class AbsReviewersScreenTest {

Expand All @@ -37,6 +41,9 @@ abstract class AbsReviewersScreenTest {
@get:Rule(order = 1)
var mockWebServer: MockWebServerRule = MockWebServerRule()

@get:Rule
val analyticsRule = AnalyticsRule()

@Before
fun setUp() {
integrationTestRule.initTests()
Expand All @@ -54,6 +61,11 @@ abstract class AbsReviewersScreenTest {
composeTestRule.waitUntilLoadingDoesNotExist()

composeTestRule.onNodeWithText("userLogin").assertIsDisplayed()

expectThat(analyticsRule.analytics.log).containsExactly(
AnalyticsLog("action_start", mapOf("item_name" to "fetch_reviewers_data")),
AnalyticsLog("action_finished", mapOf("item_name" to "fetch_reviewers_data", "success" to true))
)
}
}

Expand All @@ -78,6 +90,13 @@ abstract class AbsReviewersScreenTest {
.onNodeWithText(
"Awesome! Your collaborator have been pinged for some serious code review action! \uD83C\uDF89"
).assertIsDisplayed()

expectThat(analyticsRule.analytics.log).containsExactly(
AnalyticsLog("action_start", mapOf("item_name" to "fetch_reviewers_data")),
AnalyticsLog("action_finished", mapOf("item_name" to "fetch_reviewers_data", "success" to true)),
AnalyticsLog("button_click", mapOf("item_name" to "notify")),
AnalyticsLog("action_finished", mapOf("item_name" to "notify", "success" to true))
)
}
}

Expand All @@ -99,6 +118,13 @@ abstract class AbsReviewersScreenTest {
composeTestRule
.onNodeWithText("Uh-oh, it seems that Loudius has taken a vacation. Don't worry, we're sending a postcard to bring it back ASAP!")
.assertIsDisplayed()

expectThat(analyticsRule.analytics.log).containsExactly(
AnalyticsLog("action_start", mapOf("item_name" to "fetch_reviewers_data")),
AnalyticsLog("action_finished", mapOf("item_name" to "fetch_reviewers_data", "success" to true)),
AnalyticsLog("button_click", mapOf("item_name" to "notify")),
AnalyticsLog("action_finished", mapOf("item_name" to "notify", "success" to false))
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.appunite.loudius
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.appunite.loudius.analytics.AnalyticsRule
import com.appunite.loudius.util.IntegrationTestRule
import com.appunite.loudius.util.waitUntilLoadingDoesNotExist
import org.junit.Rule
Expand All @@ -29,6 +30,9 @@ abstract class UniversalWalkThroughAppTest {
@get:Rule(order = 0)
val integrationTestRule by lazy { IntegrationTestRule(MainActivity::class.java) }

@get:Rule
val analyticsRule = AnalyticsRule()

@Test
fun whenLoginScreenIsVisible_LoginButtonOpensGithubAuth(): Unit = with(integrationTestRule) {
composeTestRule.onNodeWithText("Log in").performClick()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@

package com.appunite.loudius.analytics

import android.os.Bundle

data class AnalyticsLog(
val eventName: String,
val parameters: Bundle
val parameters: Map<String, Any?>
)


class AnalyticsLogger(
private val converter: EventParametersConverter
) : EventTracker {
class AnalyticsLogger : EventTracker {

val log: MutableList<AnalyticsLog> = mutableListOf()

override fun trackEvent(event: Event) {
log.add(AnalyticsLog(event.name, converter.convert(event.parameters)))
log.add(AnalyticsLog(event.name, convert(event.parameters)))
}

private fun convert(parameters: List<EventParameter>): Map<String, Any?> =
parameters.associate {
it.name to when (it) {
is EventParameter.String -> it.value
is EventParameter.Boolean -> it.value
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import org.koin.dsl.module

class AnalyticsRule : TestWatcher() {

private val converter = EventParametersConverter()
val analytics = AnalyticsLogger(converter)
val analytics = AnalyticsLogger()

override fun starting(description: Description) {
super.starting(description)
Expand Down

0 comments on commit 8ae0ec6

Please sign in to comment.