Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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 package android.app.cts;
     17 
     18 import static junit.framework.TestCase.assertFalse;
     19 import static junit.framework.TestCase.assertTrue;
     20 
     21 import android.app.Fragment;
     22 import android.app.Instrumentation;
     23 import android.app.stubs.FragmentTestActivity;
     24 import android.app.stubs.R;
     25 import android.os.Bundle;
     26 import android.support.test.InstrumentationRegistry;
     27 import android.support.test.rule.ActivityTestRule;
     28 import android.support.test.runner.AndroidJUnit4;
     29 
     30 import org.junit.Before;
     31 import org.junit.Rule;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 
     35 /**
     36  * Tests usage of the {@link android.app.FragmentTransaction} class.
     37  */
     38 @RunWith(AndroidJUnit4.class)
     39 public class FragmentTransactionTest {
     40 
     41     @Rule
     42     public ActivityTestRule<FragmentTestActivity> mActivityRule =
     43             new ActivityTestRule<>(FragmentTestActivity.class);
     44 
     45     private FragmentTestActivity mActivity;
     46 
     47     @Before
     48     public void setUp() {
     49         mActivity = mActivityRule.getActivity();
     50     }
     51 
     52     @Test
     53     public void testAddTransactionWithValidFragment() {
     54         final Fragment fragment = new CorrectFragment();
     55         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
     56             @Override
     57             public void run() {
     58                 mActivity.getFragmentManager().beginTransaction()
     59                         .add(R.id.content, fragment)
     60                         .addToBackStack(null)
     61                         .commit();
     62                 mActivity.getFragmentManager().executePendingTransactions();
     63             }
     64         });
     65         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
     66         assertTrue(fragment.isAdded());
     67     }
     68 
     69     @Test
     70     public void testAddTransactionWithPrivateFragment() {
     71         final Fragment fragment = new PrivateFragment();
     72         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
     73             @Override
     74             public void run() {
     75                 boolean exceptionThrown = false;
     76                 try {
     77                     mActivity.getFragmentManager().beginTransaction()
     78                             .add(R.id.content, fragment)
     79                             .addToBackStack(null)
     80                             .commit();
     81                     mActivity.getFragmentManager().executePendingTransactions();
     82                 } catch (IllegalStateException e) {
     83                     exceptionThrown = true;
     84                 } finally {
     85                     assertTrue("Exception should be thrown", exceptionThrown);
     86                     assertFalse("Fragment shouldn't be added", fragment.isAdded());
     87                 }
     88             }
     89         });
     90         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
     91     }
     92 
     93     @Test
     94     public void testAddTransactionWithPackagePrivateFragment() {
     95         final Fragment fragment = new PackagePrivateFragment();
     96         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
     97             @Override
     98             public void run() {
     99                 boolean exceptionThrown = false;
    100                 try {
    101                     mActivity.getFragmentManager().beginTransaction()
    102                             .add(R.id.content, fragment)
    103                             .addToBackStack(null)
    104                             .commit();
    105                     mActivity.getFragmentManager().executePendingTransactions();
    106                 } catch (IllegalStateException e) {
    107                     exceptionThrown = true;
    108                 } finally {
    109                     assertTrue("Exception should be thrown", exceptionThrown);
    110                     assertFalse("Fragment shouldn't be added", fragment.isAdded());
    111                 }
    112             }
    113         });
    114         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
    115     }
    116 
    117     @Test
    118     public void testAddTransactionWithAnonymousFragment() {
    119         final Fragment fragment = new Fragment() {};
    120         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    121             @Override
    122             public void run() {
    123                 boolean exceptionThrown = false;
    124                 try {
    125                     mActivity.getFragmentManager().beginTransaction()
    126                             .add(R.id.content, fragment)
    127                             .addToBackStack(null)
    128                             .commit();
    129                     mActivity.getFragmentManager().executePendingTransactions();
    130                 } catch (IllegalStateException e) {
    131                     exceptionThrown = true;
    132                 } finally {
    133                     assertTrue("Exception should be thrown", exceptionThrown);
    134                     assertFalse("Fragment shouldn't be added", fragment.isAdded());
    135                 }
    136             }
    137         });
    138         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
    139     }
    140 
    141     @Test
    142     public void testAddTransactionWithNonStaticFragment() {
    143         final Fragment fragment = new NonStaticFragment();
    144         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    145             @Override
    146             public void run() {
    147                 boolean exceptionThrown = false;
    148                 try {
    149                     mActivity.getFragmentManager().beginTransaction()
    150                             .add(R.id.content, fragment)
    151                             .addToBackStack(null)
    152                             .commit();
    153                     mActivity.getFragmentManager().executePendingTransactions();
    154                 } catch (IllegalStateException e) {
    155                     exceptionThrown = true;
    156                 } finally {
    157                     assertTrue("Exception should be thrown", exceptionThrown);
    158                     assertFalse("Fragment shouldn't be added", fragment.isAdded());
    159                 }
    160             }
    161         });
    162         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
    163     }
    164 
    165     /**
    166      * Test to ensure that when onBackPressed() is received that there is no crash.
    167      */
    168     @Test
    169     public void crashOnBackPressed() throws Throwable {
    170         mActivityRule.runOnUiThread(() -> {
    171             Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    172             Bundle outState = new Bundle();
    173             instrumentation.callActivityOnSaveInstanceState(mActivity, outState);
    174             mActivity.onBackPressed();
    175         });
    176     }
    177 
    178     public static class CorrectFragment extends Fragment {}
    179 
    180     private static class PrivateFragment extends Fragment {}
    181 
    182     private static class PackagePrivateFragment extends Fragment {}
    183 
    184     private class NonStaticFragment extends Fragment {}
    185 }
    186