1/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.fragment.app;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertNull;
22import static org.junit.Assert.assertTrue;
23
24import android.app.AlertDialog;
25import android.app.Dialog;
26import android.os.Bundle;
27import android.support.test.InstrumentationRegistry;
28import android.support.test.filters.MediumTest;
29import android.support.test.rule.ActivityTestRule;
30import android.support.test.runner.AndroidJUnit4;
31
32import androidx.annotation.NonNull;
33import androidx.fragment.app.test.EmptyFragmentTestActivity;
34
35import org.junit.Rule;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38
39@MediumTest
40@RunWith(AndroidJUnit4.class)
41public class DialogFragmentTest {
42    @Rule
43    public final ActivityTestRule<EmptyFragmentTestActivity> mActivityTestRule =
44            new ActivityTestRule<>(EmptyFragmentTestActivity.class);
45
46    @Test
47    public void testDialogFragmentShows() {
48        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
49
50        TestDialogFragment fragment = new TestDialogFragment();
51        fragment.show(mActivityTestRule.getActivity().getSupportFragmentManager(), null);
52
53        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
54
55        assertNotNull("Dialog was null", fragment.getDialog());
56        assertTrue("Dialog was not being shown", fragment.getDialog().isShowing());
57    }
58
59    @Test
60    public void testDialogFragmentShowsNow() throws Throwable {
61        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
62
63        final TestDialogFragment fragment = new TestDialogFragment();
64        mActivityTestRule.runOnUiThread(new Runnable() {
65            @Override
66            public void run() {
67                fragment.showNow(mActivityTestRule.getActivity().getSupportFragmentManager(),
68                        null);
69            }
70        });
71
72        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
73
74        assertNotNull("Dialog was null", fragment.getDialog());
75        assertTrue("Dialog was not being shown", fragment.getDialog().isShowing());
76    }
77
78    @Test
79    public void testDialogFragmentDismiss() {
80        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
81
82        TestDialogFragment fragment = new TestDialogFragment();
83        fragment.show(mActivityTestRule.getActivity().getSupportFragmentManager(), null);
84
85        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
86
87        assertNotNull("Dialog was null", fragment.getDialog());
88        assertTrue("Dialog was not being shown", fragment.getDialog().isShowing());
89
90        fragment.dismiss();
91
92        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
93
94        assertNull("Dialog should be null after dismiss()", fragment.getDialog());
95    }
96
97    public static class TestDialogFragment extends DialogFragment {
98        private boolean mManualDismiss;
99
100        @NonNull
101        @Override
102        public Dialog onCreateDialog(Bundle savedInstanceState) {
103            return new AlertDialog.Builder(getContext())
104                    .setTitle("Test")
105                    .setMessage("Message")
106                    .setPositiveButton("Button", null)
107                    .create();
108        }
109
110        @Override
111        public void dismiss() {
112            super.dismiss();
113            mManualDismiss = true;
114        }
115
116        @Override
117        public void onStop() {
118            super.onStop();
119            assertNotNull(getDialog());
120            if (mManualDismiss) {
121                assertFalse("Dialog should not be showing in onStop() "
122                        + "when manually dismissed", getDialog().isShowing());
123            } else {
124                assertTrue("Dialog should still be showing in onStop() "
125                        + "during the normal lifecycle", getDialog().isShowing());
126            }
127        }
128    }
129}
130
131