Test FragmentScenario lifecycle ordering
Add a test to confirm that Lifecycle observers
added as part of a FragmentFactory allow
developers to access Lifecycle methods prior to
FragmentScenario returning.
Test: newly added test
Change-Id: I53de1ea4052a96f8d1beb366a4a80a15fbc75556
diff --git a/fragment/testing/src/androidTest/java/androidx/fragment/app/testing/FragmentScenarioTest.kt b/fragment/testing/src/androidTest/java/androidx/fragment/app/testing/FragmentScenarioTest.kt
index 127026f..2e23e94 100644
--- a/fragment/testing/src/androidTest/java/androidx/fragment/app/testing/FragmentScenarioTest.kt
+++ b/fragment/testing/src/androidTest/java/androidx/fragment/app/testing/FragmentScenarioTest.kt
@@ -18,7 +18,10 @@
import android.os.Bundle
import androidx.fragment.app.FragmentFactory
+import androidx.fragment.testing.test.R.id.view_tag_id
import androidx.fragment.testing.test.R.style.ThemedFragmentTheme
+import androidx.lifecycle.GenericLifecycleObserver
+import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
@@ -188,6 +191,33 @@
}
@Test
+ fun launchInContainerWithEarlyLifecycleCallbacks() {
+ var tagSetBeforeOnStart = false
+ with(launchFragmentInContainer {
+ StateRecordingFragment().also { fragment ->
+ fragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
+ if (viewLifecycleOwner != null) {
+ fragment.requireView().setTag(view_tag_id, "fakeNavController")
+ }
+ }
+ fragment.lifecycle.addObserver(GenericLifecycleObserver { _, event ->
+ if (event == Lifecycle.Event.ON_START) {
+ tagSetBeforeOnStart =
+ fragment.requireView().getTag(view_tag_id) == "fakeNavController"
+ }
+ })
+ }
+ }) {
+ assertThat(tagSetBeforeOnStart).isTrue()
+ onFragment { fragment ->
+ assertThat(fragment.state).isEqualTo(State.RESUMED)
+ assertThat(fragment.numberOfRecreations).isEqualTo(0)
+ assertThat(fragment.isViewAttachedToWindow).isTrue()
+ }
+ }
+ }
+
+ @Test
fun fromResumedToCreated() {
with(launchFragmentInContainer<StateRecordingFragment>()) {
moveToState(State.CREATED)
diff --git a/fragment/testing/src/androidTest/res/values/ids.xml b/fragment/testing/src/androidTest/res/values/ids.xml
new file mode 100644
index 0000000..6f11f71
--- /dev/null
+++ b/fragment/testing/src/androidTest/res/values/ids.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2019 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+<resources>
+ <item type="id" name="view_tag_id" />
+</resources>
\ No newline at end of file