diff options
author | Jason Monk <jmonk@google.com> | 2017-10-19 09:30:56 -0400 |
---|---|---|
committer | Jason Monk <jmonk@google.com> | 2017-10-19 09:30:56 -0400 |
commit | d439404c9988df6001e4ff8bce31537e2692660e (patch) | |
tree | b1462a7177b8a2791140964761eb49d173cdc878 /android/arch/lifecycle/PartiallyCoveredActivityTest.java | |
parent | 93b7ee4fce01df52a6607f0b1965cbafdfeaf1a6 (diff) | |
download | android-28-d439404c9988df6001e4ff8bce31537e2692660e.tar.gz |
Import Android SDK Platform P [4402356]
/google/data/ro/projects/android/fetch_artifact \
--bid 4386628 \
--target sdk_phone_armv7-win_sdk \
sdk-repo-linux-sources-4402356.zip
AndroidVersion.ApiLevel has been modified to appear as 28
Change-Id: Ie49e24e1f4ae9dc96306111e953d3db1e1495b53
Diffstat (limited to 'android/arch/lifecycle/PartiallyCoveredActivityTest.java')
-rw-r--r-- | android/arch/lifecycle/PartiallyCoveredActivityTest.java | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/android/arch/lifecycle/PartiallyCoveredActivityTest.java b/android/arch/lifecycle/PartiallyCoveredActivityTest.java new file mode 100644 index 00000000..07a9dc5a --- /dev/null +++ b/android/arch/lifecycle/PartiallyCoveredActivityTest.java @@ -0,0 +1,200 @@ +/* + * Copyright (C) 2017 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 android.arch.lifecycle; + +import static android.arch.lifecycle.Lifecycle.Event.ON_RESUME; +import static android.arch.lifecycle.Lifecycle.Event.ON_START; +import static android.arch.lifecycle.Lifecycle.Event.ON_STOP; +import static android.arch.lifecycle.TestUtils.OrderedTuples.CREATE; +import static android.arch.lifecycle.TestUtils.OrderedTuples.DESTROY; +import static android.arch.lifecycle.TestUtils.OrderedTuples.PAUSE; +import static android.arch.lifecycle.TestUtils.OrderedTuples.RESUME; +import static android.arch.lifecycle.TestUtils.OrderedTuples.START; +import static android.arch.lifecycle.TestUtils.OrderedTuples.STOP; +import static android.arch.lifecycle.TestUtils.flatMap; +import static android.arch.lifecycle.testapp.TestEvent.LIFECYCLE_EVENT; +import static android.arch.lifecycle.testapp.TestEvent.OWNER_CALLBACK; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; + +import android.app.Instrumentation; +import android.arch.lifecycle.testapp.CollectingLifecycleOwner; +import android.arch.lifecycle.testapp.CollectingSupportActivity; +import android.arch.lifecycle.testapp.CollectingSupportFragment; +import android.arch.lifecycle.testapp.NavigationDialogActivity; +import android.arch.lifecycle.testapp.TestEvent; +import android.content.Intent; +import android.os.Build; +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.LargeTest; +import android.support.test.rule.ActivityTestRule; +import android.support.v4.app.FragmentActivity; +import android.support.v4.util.Pair; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.List; +import java.util.concurrent.ExecutionException; + +/** + * Runs tests about the state when an activity is partially covered by another activity. Pre + * API 24, framework behavior changes so the test rely on whether state is saved or not and makes + * assertions accordingly. + */ +@SuppressWarnings("unchecked") +@RunWith(Parameterized.class) +@LargeTest +public class PartiallyCoveredActivityTest { + private static final List[] IF_SAVED = new List[]{ + // when overlaid + flatMap(CREATE, START, RESUME, PAUSE, + singletonList(new Pair<>(LIFECYCLE_EVENT, ON_STOP))), + // post dialog dismiss + asList(new Pair<>(OWNER_CALLBACK, ON_RESUME), + new Pair<>(LIFECYCLE_EVENT, ON_START), + new Pair<>(LIFECYCLE_EVENT, ON_RESUME)), + // post finish + flatMap(PAUSE, STOP, DESTROY)}; + + private static final List[] IF_NOT_SAVED = new List[]{ + // when overlaid + flatMap(CREATE, START, RESUME, PAUSE), + // post dialog dismiss + flatMap(RESUME), + // post finish + flatMap(PAUSE, STOP, DESTROY)}; + + private static final boolean sShouldSave = Build.VERSION.SDK_INT < Build.VERSION_CODES.N; + private static final List<Pair<TestEvent, Lifecycle.Event>>[] EXPECTED = + sShouldSave ? IF_SAVED : IF_NOT_SAVED; + + @Rule + public ActivityTestRule<CollectingSupportActivity> activityRule = + new ActivityTestRule<CollectingSupportActivity>( + CollectingSupportActivity.class) { + @Override + protected Intent getActivityIntent() { + // helps with less flaky API 16 tests + Intent intent = new Intent(InstrumentationRegistry.getTargetContext(), + CollectingSupportActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + return intent; + } + }; + private final boolean mDismissDialog; + + @Parameterized.Parameters(name = "dismissDialog_{0}") + public static List<Boolean> dismissDialog() { + return asList(true, false); + } + + public PartiallyCoveredActivityTest(boolean dismissDialog) { + mDismissDialog = dismissDialog; + } + + @Test + public void coveredWithDialog_activity() throws Throwable { + final CollectingSupportActivity activity = activityRule.getActivity(); + runTest(activity); + } + + @Test + public void coveredWithDialog_fragment() throws Throwable { + CollectingSupportFragment fragment = new CollectingSupportFragment(); + activityRule.runOnUiThread(() -> activityRule.getActivity().replaceFragment(fragment)); + runTest(fragment); + } + + @Test + public void coveredWithDialog_childFragment() throws Throwable { + CollectingSupportFragment parentFragment = new CollectingSupportFragment(); + CollectingSupportFragment childFragment = new CollectingSupportFragment(); + activityRule.runOnUiThread(() -> { + activityRule.getActivity().replaceFragment(parentFragment); + parentFragment.replaceFragment(childFragment); + }); + runTest(childFragment); + } + + private void runTest(CollectingLifecycleOwner owner) throws Throwable { + TestUtils.waitTillResumed(owner, activityRule); + FragmentActivity dialog = launchDialog(); + assertStateSaving(); + waitForIdle(); + assertThat(owner.copyCollectedEvents(), is(EXPECTED[0])); + List<Pair<TestEvent, Lifecycle.Event>> expected; + if (mDismissDialog) { + dialog.finish(); + TestUtils.waitTillResumed(activityRule.getActivity(), activityRule); + assertThat(owner.copyCollectedEvents(), is(flatMap(EXPECTED[0], EXPECTED[1]))); + expected = flatMap(EXPECTED[0], EXPECTED[1], EXPECTED[2]); + } else { + expected = flatMap(CREATE, START, RESUME, PAUSE, STOP, DESTROY); + } + CollectingSupportActivity activity = activityRule.getActivity(); + activityRule.finishActivity(); + TestUtils.waitTillDestroyed(activity, activityRule); + assertThat(owner.copyCollectedEvents(), is(expected)); + } + + // test sanity + private void assertStateSaving() throws ExecutionException, InterruptedException { + final CollectingSupportActivity activity = activityRule.getActivity(); + if (sShouldSave) { + // state should be saved. wait for it to be saved + assertThat("test sanity", + activity.waitForStateSave(20), is(true)); + assertThat("test sanity", activity.getSupportFragmentManager() + .isStateSaved(), is(true)); + } else { + // should should not be saved + assertThat("test sanity", activity.getSupportFragmentManager() + .isStateSaved(), is(false)); + } + } + + private void waitForIdle() { + InstrumentationRegistry.getInstrumentation().waitForIdleSync(); + } + + private FragmentActivity launchDialog() throws Throwable { + Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor( + NavigationDialogActivity.class.getCanonicalName(), null, false); + Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); + instrumentation.addMonitor(monitor); + + FragmentActivity activity = activityRule.getActivity(); + + Intent intent = new Intent(activity, NavigationDialogActivity.class); + // disabling animations helps with less flaky API 16 tests + intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + activity.startActivity(intent); + FragmentActivity fragmentActivity = (FragmentActivity) monitor.waitForActivity(); + TestUtils.waitTillResumed(fragmentActivity, activityRule); + return fragmentActivity; + } +} |