-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase code coverage (pt5). (#392)
- Loading branch information
Showing
15 changed files
with
188 additions
and
61 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
This file was deleted.
Oops, something went wrong.
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
10 changes: 4 additions & 6 deletions
10
formula-android-tests/src/test/java/com/instacart/formula/NonBoundFragmentActivityTest.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
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
92 changes: 92 additions & 0 deletions
92
...-android/src/test/java/com/instacart/formula/android/internal/ActivityStoreContextTest.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,92 @@ | ||
package com.instacart.formula.android.internal | ||
|
||
import android.os.Looper | ||
import androidx.fragment.app.FragmentActivity | ||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth.assertThat | ||
import com.instacart.testutils.android.TestFragmentActivity | ||
import com.instacart.testutils.android.activity | ||
import com.instacart.testutils.android.executeOnBackgroundThread | ||
import com.instacart.testutils.android.throwOnTimeout | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.util.concurrent.CountDownLatch | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class ActivityStoreContextTest { | ||
|
||
@Test fun `started activity returns null until onActivityStarted is called`() { | ||
val scenario = ActivityScenario.launch(TestFragmentActivity::class.java) | ||
val storeContext = ActivityStoreContextImpl<TestFragmentActivity>() | ||
|
||
// Initially null | ||
assertThat(storeContext.startedActivity()).isNull() | ||
|
||
// After attach | ||
storeContext.attachActivity(scenario.activity()) | ||
assertThat(storeContext.startedActivity()).isNull() | ||
|
||
// After on started | ||
storeContext.onActivityStarted(scenario.activity()) | ||
assertThat(storeContext.startedActivity()).isEqualTo(scenario.activity()) | ||
} | ||
|
||
@Test fun `detaches only if the activity matches`() { | ||
val scenario = ActivityScenario.launch(TestFragmentActivity::class.java) | ||
val storeContext = ActivityStoreContextImpl<TestFragmentActivity>() | ||
|
||
val oldActivity = scenario.activity() | ||
val newActivity = scenario.recreate().activity() | ||
|
||
storeContext.attachActivity(newActivity) | ||
storeContext.onActivityStarted(newActivity) | ||
storeContext.detachActivity(oldActivity) | ||
|
||
assertThat(storeContext.startedActivity()).isEqualTo(newActivity) | ||
} | ||
|
||
@Test fun `send posts events on the main thread`() { | ||
val scenario = ActivityScenario.launch(TestFragmentActivity::class.java) | ||
val storeContext = ActivityStoreContextImpl<TestFragmentActivity>() | ||
storeContext.attachActivity(scenario.activity()) | ||
storeContext.onActivityStarted(scenario.activity()) | ||
|
||
val effectThread = mutableListOf<Looper?>() | ||
storeContext.send { effectThread.add(Looper.myLooper()) } | ||
storeContext.sendOnBackgroundThread { effectThread.add(Looper.myLooper()) } | ||
|
||
assertThat(effectThread).containsExactly( | ||
Looper.getMainLooper(), Looper.getMainLooper() | ||
) | ||
} | ||
|
||
@Test | ||
fun `send drops the action if there is no started activity`() { | ||
val storeContext = ActivityStoreContextImpl<TestFragmentActivity>() | ||
|
||
val effectThread = mutableListOf<Looper?>() | ||
storeContext.send { effectThread.add(Looper.myLooper()) } | ||
|
||
val result = runCatching { | ||
storeContext.sendOnBackgroundThread { effectThread.add(Looper.myLooper()) } | ||
} | ||
assertThat(effectThread).isEmpty() | ||
assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"timeout" | ||
) | ||
} | ||
|
||
private fun <ActivityType : FragmentActivity> ActivityStoreContextImpl<ActivityType>.sendOnBackgroundThread( | ||
action: ActivityType.() -> Unit | ||
) { | ||
val sendLatch = CountDownLatch(1) | ||
executeOnBackgroundThread { | ||
send { | ||
action() | ||
sendLatch.countDown() | ||
} | ||
} | ||
sendLatch.throwOnTimeout() | ||
} | ||
} |
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 |
---|---|---|
|
@@ -17,3 +17,6 @@ include( | |
":samples:stopwatch-compose", | ||
":samples:todoapp" | ||
) | ||
include( | ||
":test-utils:android" | ||
) |
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 @@ | ||
/build |
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,20 @@ | ||
plugins { | ||
id("com.android.library") | ||
id("kotlin-android") | ||
id("kotlin-parcelize") | ||
} | ||
|
||
android { | ||
namespace = "com.instacart.testutils.android" | ||
} | ||
|
||
dependencies { | ||
implementation(project(":formula-rxjava3")) | ||
implementation(project(":formula-android")) | ||
|
||
implementation(libs.kotlin) | ||
implementation(libs.androidx.appcompat) | ||
implementation(libs.androidx.test.core.ktx) | ||
implementation(libs.lifecycle.extensions) | ||
implementation(libs.robolectric) | ||
} |
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,14 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<application> | ||
<activity | ||
android:name="com.instacart.testutils.android.TestFragmentActivity" | ||
android:launchMode="standard" | ||
android:exported="false" /> | ||
|
||
<activity | ||
android:name="com.instacart.testutils.android.TestActivity" | ||
android:launchMode="standard" | ||
android:exported="false" /> | ||
</application> | ||
</manifest> |
5 changes: 5 additions & 0 deletions
5
test-utils/android/src/main/java/com/instacart/testutils/android/TestActivity.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,5 @@ | ||
package com.instacart.testutils.android | ||
|
||
import android.app.Activity | ||
|
||
class TestActivity : Activity() |
37 changes: 37 additions & 0 deletions
37
test-utils/android/src/main/java/com/instacart/testutils/android/TestExtensions.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,37 @@ | ||
package com.instacart.testutils.android | ||
|
||
import android.app.Activity | ||
import android.os.Looper | ||
import androidx.test.core.app.ActivityScenario | ||
import org.robolectric.Shadows | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
|
||
fun <A: Activity> ActivityScenario<A>.activity(): A { | ||
return get { this } | ||
} | ||
|
||
fun <A: Activity, T> ActivityScenario<A>.get(select: A.() -> T): T { | ||
val list: MutableList<T> = mutableListOf() | ||
onActivity { | ||
list.add(it.select()) | ||
} | ||
return list.first() | ||
} | ||
|
||
fun CountDownLatch.throwOnTimeout() { | ||
if (!await(100, TimeUnit.MILLISECONDS)) { | ||
throw IllegalStateException("timeout") | ||
} | ||
} | ||
|
||
fun executeOnBackgroundThread(action: () -> Unit) { | ||
val initLatch = CountDownLatch(1) | ||
Executors.newSingleThreadExecutor().execute { | ||
action() | ||
initLatch.countDown() | ||
} | ||
initLatch.throwOnTimeout() | ||
Shadows.shadowOf(Looper.getMainLooper()).idle() | ||
} |
5 changes: 5 additions & 0 deletions
5
test-utils/android/src/main/java/com/instacart/testutils/android/TestFragmentActivity.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,5 @@ | ||
package com.instacart.testutils.android | ||
|
||
import androidx.fragment.app.FragmentActivity | ||
|
||
class TestFragmentActivity : FragmentActivity() |