blob: 70007836ebb1341bdc206c16be26c61ec76b62c7 [file] [log] [blame]
/*
* Copyright 2018 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.
*/
package androidx.fragment.app
import android.content.Context
import androidx.fragment.app.test.FragmentTestActivity
import androidx.fragment.app.test.FragmentTestActivity.ParentFragment
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import androidx.test.rule.ActivityTestRule
import com.google.common.truth.Truth.assertWithMessage
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
@RunWith(AndroidJUnit4::class)
class NestedFragmentRestoreTest {
@get:Rule
val activityRule = ActivityTestRule(FragmentTestActivity::class.java)
@Test
@MediumTest
fun recreateActivity() {
val activity = activityRule.activity
activityRule.runOnUiThread {
val parent = ParentFragment()
parent.setRetainChildInstance(true)
activity.supportFragmentManager.beginTransaction()
.add(parent, "parent")
.commitNow()
}
val fm = activity.supportFragmentManager
val parent = fm.findFragmentByTag("parent") as ParentFragment
val child = parent.childFragment
var attachedTo: Context? = null
val latch = CountDownLatch(1)
child.setOnAttachListener { context, _ ->
attachedTo = context
latch.countDown()
}
activityRule.runOnUiThread { activity.recreate() }
assertWithMessage("timeout waiting for recreate")
.that(latch.await(10, TimeUnit.SECONDS))
.isTrue()
assertWithMessage("attached as part of recreate").that(attachedTo).isNotNull()
assertWithMessage("attached to new context")
.that(attachedTo)
.isNotSameAs(activity)
assertWithMessage("attached to new parent fragment")
.that(child)
.isNotSameAs(parent)
}
}