1 /* 2 * Copyright (C) 2015 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 17 package androidx.appcompat.app; 18 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assert.assertTrue; 21 22 import android.app.Dialog; 23 import android.os.Bundle; 24 25 import androidx.appcompat.test.R; 26 import androidx.fragment.app.DialogFragment; 27 import androidx.test.ext.junit.runners.AndroidJUnit4; 28 import androidx.test.filters.LargeTest; 29 import androidx.test.platform.app.InstrumentationRegistry; 30 31 import org.jspecify.annotations.NonNull; 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @LargeTest 37 @RunWith(AndroidJUnit4.class) 38 public class AppCompatDialogFragmentTest { 39 @SuppressWarnings("deprecation") 40 @Rule 41 public final androidx.test.rule.ActivityTestRule<WindowDecorAppCompatActivity> mTestRule = 42 new androidx.test.rule.ActivityTestRule<>(WindowDecorAppCompatActivity.class); 43 44 private DialogFragment mFragment; 45 46 @Test testDialogFragmentShows()47 public void testDialogFragmentShows() { 48 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 49 50 InstrumentationRegistry.getInstrumentation().runOnMainSync( 51 () -> mFragment = new TestDialogFragment() 52 ); 53 mFragment.show(mTestRule.getActivity().getSupportFragmentManager(), null); 54 55 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 56 57 assertNotNull("Dialog was null", mFragment.getDialog()); 58 assertTrue("Dialog was not being shown", mFragment.getDialog().isShowing()); 59 60 // And make sure we dismiss the dialog 61 mFragment.dismissAllowingStateLoss(); 62 } 63 64 @Test testDialogFragmentWithLayout()65 public void testDialogFragmentWithLayout() { 66 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 67 68 InstrumentationRegistry.getInstrumentation().runOnMainSync( 69 () -> mFragment = new AppCompatDialogFragment(R.layout.dialog_layout) 70 ); 71 mFragment.show(mTestRule.getActivity().getSupportFragmentManager(), null); 72 73 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 74 75 assertNotNull("Dialog is not null", mFragment.getDialog()); 76 assertTrue("Dialog is showing", mFragment.getDialog().isShowing()); 77 assertNotNull("Dialog is using custom layout", 78 mFragment.getDialog().findViewById(R.id.dialog_content)); 79 80 // And make sure we dismiss the dialog 81 mFragment.dismissAllowingStateLoss(); 82 } 83 84 public static class TestDialogFragment extends AppCompatDialogFragment { 85 @Override onCreateDialog(Bundle savedInstanceState)86 public @NonNull Dialog onCreateDialog(Bundle savedInstanceState) { 87 return new AlertDialog.Builder(requireContext()) 88 .setTitle("Test") 89 .setMessage("Message") 90 .setPositiveButton("Button", null) 91 .create(); 92 } 93 } 94 } 95 96