-
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.
Refactoring android tests to use utils/android (pt4).
- Loading branch information
Showing
13 changed files
with
167 additions
and
180 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 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
110 changes: 51 additions & 59 deletions
110
formula-android-tests/src/test/java/com/instacart/formula/FragmentLifecycleTest.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 |
---|---|---|
@@ -1,93 +1,85 @@ | ||
package com.instacart.formula | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth.assertThat | ||
import com.instacart.formula.android.ActivityStore | ||
import com.instacart.formula.android.Feature | ||
import com.instacart.formula.android.FeatureFactory | ||
import com.instacart.formula.android.FormulaFragment | ||
import com.instacart.formula.android.FragmentStore | ||
import com.instacart.formula.android.ViewFactory | ||
import com.instacart.formula.test.TestFragmentActivity | ||
import com.instacart.formula.test.TestFragmentLifecycleCallback | ||
import com.instacart.formula.test.TestLifecycleKey | ||
import com.instacart.testutils.android.R as TestR | ||
import io.reactivex.rxjava3.core.Observable | ||
import org.junit.Rule | ||
import com.instacart.testutils.android.NoOpFeatureFactory | ||
import com.instacart.testutils.android.TestFormulaActivity | ||
import com.instacart.testutils.android.TestViewFactory | ||
import com.instacart.testutils.android.showFragment | ||
import com.instacart.testutils.android.withFormulaAndroid | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class FragmentLifecycleTest { | ||
|
||
private lateinit var lifecycleCallback: TestFragmentLifecycleCallback | ||
|
||
@get:Rule val formulaRule = TestFormulaRule(initFormula = { app -> | ||
FormulaAndroid.init(app) { | ||
activity<TestFragmentActivity> { | ||
lifecycleCallback = TestFragmentLifecycleCallback() | ||
ActivityStore( | ||
configureActivity = { activity -> | ||
activity.initialKey = TestLifecycleKey() | ||
}, | ||
fragmentStore = FragmentStore.init { | ||
val featureFactory = object : FeatureFactory<Unit, TestLifecycleKey> { | ||
override fun initialize(dependencies: Unit, key: TestLifecycleKey): Feature { | ||
return Feature( | ||
state = Observable.empty(), | ||
viewFactory = ViewFactory.fromLayout(TestR.layout.test_fragment_layout) { | ||
featureView(lifecycleCallback) {} | ||
} | ||
) | ||
} | ||
} | ||
bind(featureFactory) | ||
}, | ||
) | ||
private fun runTest( | ||
continuation: (ActivityScenario<TestFormulaActivity>, TestFragmentLifecycleCallback) -> Unit | ||
) { | ||
val lifecycleCallback = TestFragmentLifecycleCallback() | ||
withFormulaAndroid( | ||
configure = { | ||
activity<TestFormulaActivity> { | ||
ActivityStore( | ||
fragmentStore = FragmentStore.init { | ||
val featureFactory = NoOpFeatureFactory<TestLifecycleKey>( | ||
viewFactory = TestViewFactory(lifecycleCallback) | ||
) | ||
bind(featureFactory) | ||
}, | ||
) | ||
} | ||
} | ||
) { | ||
val scenario = ActivityScenario.launch(TestFormulaActivity::class.java) | ||
scenario.showFragment(TestLifecycleKey()) | ||
continuation(scenario, lifecycleCallback) | ||
} | ||
}) | ||
} | ||
|
||
@Test fun `creation callbacks`() { | ||
ActivityScenario.launch(TestFragmentActivity::class.java) | ||
|
||
assertThat(lifecycleCallback.hasOnViewCreated).isTrue() | ||
assertThat(lifecycleCallback.hasOnActivityCreated).isTrue() | ||
assertThat(lifecycleCallback.hasOnStart).isTrue() | ||
assertThat(lifecycleCallback.hasOnResume).isTrue() | ||
runTest { _, lifecycleCallback -> | ||
assertThat(lifecycleCallback.hasOnViewCreated).isTrue() | ||
assertThat(lifecycleCallback.hasOnActivityCreated).isTrue() | ||
assertThat(lifecycleCallback.hasOnStart).isTrue() | ||
assertThat(lifecycleCallback.hasOnResume).isTrue() | ||
} | ||
} | ||
|
||
@Test fun `destroy callbacks`() { | ||
val scenario = ActivityScenario.launch(TestFragmentActivity::class.java) | ||
scenario.close() | ||
runTest { scenario, lifecycleCallback -> | ||
scenario.close() | ||
|
||
assertThat(lifecycleCallback.hasOnPauseEvent).isTrue() | ||
assertThat(lifecycleCallback.hasOnStop).isTrue() | ||
assertThat(lifecycleCallback.hasOnDestroyView).isTrue() | ||
assertThat(lifecycleCallback.hasOnPauseEvent).isTrue() | ||
assertThat(lifecycleCallback.hasOnStop).isTrue() | ||
assertThat(lifecycleCallback.hasOnDestroyView).isTrue() | ||
} | ||
} | ||
|
||
@Test fun `save instance state callback`() { | ||
val scenario = ActivityScenario.launch(TestFragmentActivity::class.java) | ||
|
||
assertThat(lifecycleCallback.hasOnSaveInstanceState).isFalse() | ||
scenario.recreate() | ||
assertThat(lifecycleCallback.hasOnSaveInstanceState).isTrue() | ||
runTest { scenario, lifecycleCallback -> | ||
assertThat(lifecycleCallback.hasOnSaveInstanceState).isFalse() | ||
scenario.recreate() | ||
assertThat(lifecycleCallback.hasOnSaveInstanceState).isTrue() | ||
} | ||
} | ||
|
||
@Test fun `low memory`() { | ||
val scenario = ActivityScenario.launch(TestFragmentActivity::class.java) | ||
scenario.onActivity { | ||
val fragment = it.supportFragmentManager.fragments | ||
.filterIsInstance<FormulaFragment>() | ||
.first() | ||
runTest { scenario, lifecycleCallback -> | ||
scenario.onActivity { | ||
val fragment = it.supportFragmentManager.fragments | ||
.filterIsInstance<FormulaFragment>() | ||
.first() | ||
|
||
fragment.onLowMemory() | ||
fragment.onLowMemory() | ||
} | ||
assertThat(lifecycleCallback.hasCalledLowMemory).isTrue() | ||
} | ||
assertThat(lifecycleCallback.hasCalledLowMemory).isTrue() | ||
} | ||
} |
Oops, something went wrong.