FragmentTest.java revision ff22d81f6561f6cdd2a91eb63238c41079927a22
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 */ 16package android.support.v4.app; 17 18import android.os.Bundle; 19import android.support.fragment.test.R; 20import android.support.v4.app.test.FragmentTestActivity; 21import android.test.ActivityInstrumentationTestCase2; 22import android.test.UiThreadTest; 23import android.test.suitebuilder.annotation.MediumTest; 24import android.test.suitebuilder.annotation.SmallTest; 25import android.view.LayoutInflater; 26import android.view.View; 27import android.view.ViewGroup; 28 29import java.util.concurrent.atomic.AtomicInteger; 30 31/** 32 * Miscellaneous tests for fragments that aren't big enough to belong to their own classes. 33 */ 34public class FragmentTest extends 35 ActivityInstrumentationTestCase2<FragmentTestActivity> { 36 private FragmentTestActivity mActivity; 37 38 public FragmentTest() { 39 super(FragmentTestActivity.class); 40 } 41 42 @Override 43 protected void setUp() throws Exception { 44 super.setUp(); 45 mActivity = getActivity(); 46 } 47 48 @SmallTest 49 @UiThreadTest 50 public void testOnCreateOrder() throws Throwable { 51 OrderFragment fragment1 = new OrderFragment(); 52 OrderFragment fragment2 = new OrderFragment(); 53 mActivity.getSupportFragmentManager() 54 .beginTransaction() 55 .add(R.id.content, fragment1) 56 .add(R.id.content, fragment2) 57 .commitNow(); 58 assertEquals(0, fragment1.createOrder); 59 assertEquals(1, fragment2.createOrder); 60 } 61 62 @SmallTest 63 public void testChildFragmentManagerGone() throws Throwable { 64 final FragmentA fragmentA = new FragmentA(); 65 final FragmentB fragmentB = new FragmentB(); 66 runTestOnUiThread(new Runnable() { 67 @Override 68 public void run() { 69 mActivity.getSupportFragmentManager().beginTransaction() 70 .add(R.id.content, fragmentA) 71 .commitNow(); 72 } 73 }); 74 getInstrumentation().waitForIdleSync(); 75 runTestOnUiThread(new Runnable() { 76 @Override 77 public void run() { 78 mActivity.getSupportFragmentManager().beginTransaction() 79 .setCustomAnimations(R.anim.long_fade_in, R.anim.long_fade_out, 80 R.anim.long_fade_in, R.anim.long_fade_out) 81 .replace(R.id.content, fragmentB) 82 .addToBackStack(null) 83 .commit(); 84 } 85 }); 86 // Wait for the middle of the animation 87 Thread.sleep(150); 88 runTestOnUiThread(new Runnable() { 89 @Override 90 public void run() { 91 mActivity.getSupportFragmentManager().beginTransaction() 92 .setCustomAnimations(R.anim.long_fade_in, R.anim.long_fade_out, 93 R.anim.long_fade_in, R.anim.long_fade_out) 94 .replace(R.id.content, fragmentA) 95 .addToBackStack(null) 96 .commit(); 97 } 98 }); 99 // Wait for the middle of the animation 100 Thread.sleep(150); 101 getInstrumentation().waitForIdleSync(); 102 runTestOnUiThread(new Runnable() { 103 @Override 104 public void run() { 105 mActivity.getSupportFragmentManager().popBackStack(); 106 } 107 }); 108 // Wait for the middle of the animation 109 Thread.sleep(150); 110 getInstrumentation().waitForIdleSync(); 111 runTestOnUiThread(new Runnable() { 112 @Override 113 public void run() { 114 mActivity.getSupportFragmentManager().popBackStack(); 115 } 116 }); 117 } 118 119 @MediumTest 120 @UiThreadTest 121 public void testViewOrder() throws Throwable { 122 FragmentA fragmentA = new FragmentA(); 123 FragmentB fragmentB = new FragmentB(); 124 FragmentC fragmentC = new FragmentC(); 125 mActivity.getSupportFragmentManager() 126 .beginTransaction() 127 .add(R.id.content, fragmentA) 128 .add(R.id.content, fragmentB) 129 .add(R.id.content, fragmentC) 130 .commitNow(); 131 ViewGroup content = (ViewGroup) mActivity.findViewById(R.id.content); 132 assertEquals(3, content.getChildCount()); 133 assertNotNull(content.getChildAt(0).findViewById(R.id.textA)); 134 assertNotNull(content.getChildAt(1).findViewById(R.id.textB)); 135 assertNotNull(content.getChildAt(2).findViewById(R.id.textC)); 136 } 137 138 public static class OrderFragment extends Fragment { 139 private static AtomicInteger sOrder = new AtomicInteger(); 140 public int createOrder = -1; 141 142 public OrderFragment() {} 143 144 @Override 145 public void onCreate(Bundle savedInstanceState) { 146 createOrder = sOrder.getAndIncrement(); 147 super.onCreate(savedInstanceState); 148 } 149 } 150 151 public static class FragmentA extends Fragment { 152 @Override 153 public View onCreateView(LayoutInflater inflater, ViewGroup container, 154 Bundle savedInstanceState) { 155 return inflater.inflate(R.layout.fragment_a, container, false); 156 } 157 } 158 159 public static class FragmentB extends Fragment { 160 @Override 161 public View onCreateView(LayoutInflater inflater, ViewGroup container, 162 Bundle savedInstanceState) { 163 return inflater.inflate(R.layout.fragment_b, container, false); 164 } 165 } 166 167 public static class FragmentC extends Fragment { 168 @Override 169 public View onCreateView(LayoutInflater inflater, ViewGroup container, 170 Bundle savedInstanceState) { 171 return inflater.inflate(R.layout.fragment_c, container, false); 172 } 173 } 174} 175