-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
574 additions
and
16 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
34 changes: 34 additions & 0 deletions
34
app/src/androidTest/java/tech/relaycorp/ping/test/ActivityAssertions.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,34 @@ | ||
package tech.relaycorp.ping.test | ||
|
||
import android.app.Activity | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry | ||
import androidx.test.runner.lifecycle.Stage | ||
import org.junit.Assert | ||
import tech.relaycorp.ping.test.WaitAssertions.waitFor | ||
import kotlin.reflect.KClass | ||
|
||
object ActivityAssertions { | ||
val currentActivity: Activity? | ||
get() { | ||
InstrumentationRegistry.getInstrumentation().waitForIdleSync() | ||
val activity = arrayOfNulls<Activity>(1) | ||
InstrumentationRegistry.getInstrumentation().runOnMainSync { | ||
val activities = | ||
ActivityLifecycleMonitorRegistry.getInstance() | ||
.getActivitiesInStage(Stage.RESUMED) | ||
if (activities.iterator().hasNext()) { | ||
activity[0] = activities.iterator().next() | ||
} | ||
} | ||
return activity[0] | ||
} | ||
|
||
fun assertCurrentActivity(activityKlass: KClass<out Activity>) { | ||
Assert.assertEquals(activityKlass.java.name, currentActivity?.componentName?.className) | ||
} | ||
|
||
fun waitForCurrentActivityToBe(activityKlass: KClass<out Activity>) { | ||
waitFor { assertCurrentActivity(activityKlass) } | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
app/src/androidTest/java/tech/relaycorp/ping/test/IntentsRule.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 tech.relaycorp.ping.test | ||
|
||
import androidx.test.espresso.intent.Intents | ||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
|
||
class IntentsRule : TestRule { | ||
override fun apply(base: Statement, description: Description?) = | ||
object : Statement() { | ||
override fun evaluate() { | ||
Intents.init() | ||
base.evaluate() | ||
Intents.release() | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
app/src/androidTest/java/tech/relaycorp/ping/ui/peers/AddPublicPeerActivityTest.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,89 @@ | ||
package tech.relaycorp.ping.ui.peers | ||
|
||
import android.app.Activity | ||
import android.app.Instrumentation | ||
import android.content.Intent | ||
import android.net.Uri | ||
import androidx.test.espresso.intent.Intents.intending | ||
import androidx.test.espresso.intent.matcher.IntentMatchers.hasAction | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.schibsted.spain.barista.assertion.BaristaEnabledAssertions.assertDisabled | ||
import com.schibsted.spain.barista.assertion.BaristaEnabledAssertions.assertEnabled | ||
import com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertDisplayed | ||
import com.schibsted.spain.barista.interaction.BaristaClickInteractions.clickOn | ||
import com.schibsted.spain.barista.interaction.BaristaEditTextInteractions.writeTo | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import tech.relaycorp.ping.R | ||
import tech.relaycorp.ping.test.ActivityAssertions.waitForCurrentActivityToBe | ||
import tech.relaycorp.ping.test.AppTestProvider.component | ||
import tech.relaycorp.ping.test.AppTestProvider.context | ||
import tech.relaycorp.ping.test.BaseActivityTestRule | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class AddPublicPeerActivityTest { | ||
|
||
@Rule | ||
@JvmField | ||
val testRule = BaseActivityTestRule(PeersActivity::class, false) | ||
|
||
@Before | ||
fun setUp() { | ||
component.inject(this) | ||
} | ||
|
||
@Test | ||
fun addPublicPeerSuccessfully() { | ||
testRule.start() | ||
clickOn(R.id.addPeer) | ||
clickOn(R.string.peer_public) | ||
|
||
assertDisabled(R.id.save) | ||
|
||
val address = "ping.awala.services" | ||
writeTo(R.id.addressEdit, address) | ||
|
||
intending(hasAction(Intent.ACTION_OPEN_DOCUMENT)) | ||
.respondWith( | ||
Instrumentation.ActivityResult( | ||
Activity.RESULT_OK, | ||
Intent().setData( | ||
Uri.parse("android.resource://${context.packageName}/${R.raw.ping_awala_identity}") | ||
) | ||
) | ||
) | ||
clickOn(R.string.peer_certificate_button) | ||
|
||
assertEnabled(R.id.save) | ||
clickOn(R.id.save) | ||
|
||
waitForCurrentActivityToBe(PeersActivity::class) | ||
assertDisplayed(address) | ||
} | ||
|
||
@Test | ||
fun addPublicPeerFailure() { | ||
testRule.start() | ||
clickOn(R.id.addPeer) | ||
clickOn(R.string.peer_public) | ||
|
||
val address = "ping.awala.services" | ||
writeTo(R.id.addressEdit, address) | ||
|
||
intending(hasAction(Intent.ACTION_OPEN_DOCUMENT)) | ||
.respondWith( | ||
Instrumentation.ActivityResult( | ||
Activity.RESULT_OK, | ||
Intent().setData( | ||
Uri.parse("android.resource://invalid") | ||
) | ||
) | ||
) | ||
clickOn(R.string.peer_certificate_button) | ||
|
||
clickOn(R.id.save) | ||
assertDisplayed(R.string.peer_add_error) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tech.relaycorp.ping.data | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import javax.inject.Inject | ||
|
||
class ReadFile | ||
@Inject constructor( | ||
private val context: Context | ||
) { | ||
|
||
fun read(uri: Uri) = | ||
context.contentResolver.openInputStream(uri)?.use { | ||
it.readBytes() | ||
} ?: ByteArray(0) | ||
} |
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
9 changes: 9 additions & 0 deletions
9
app/src/main/java/tech/relaycorp/ping/ui/common/ActivityResult.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,9 @@ | ||
package tech.relaycorp.ping.ui.common | ||
|
||
import android.content.Intent | ||
|
||
data class ActivityResult( | ||
val requestCode: Int, | ||
val resultCode: Int, | ||
val data: Intent? | ||
) |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/tech/relaycorp/ping/ui/common/SimpleTextWatcher.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,18 @@ | ||
package tech.relaycorp.ping.ui.common | ||
|
||
import android.text.Editable | ||
import android.text.TextWatcher | ||
|
||
class SimpleTextWatcher( | ||
private val textChanged: (String) -> Unit | ||
) : TextWatcher { | ||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { | ||
} | ||
|
||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { | ||
textChanged(s?.toString() ?: "") | ||
} | ||
|
||
override fun afterTextChanged(s: Editable?) { | ||
} | ||
} |
Oops, something went wrong.