1c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas/* 2c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * Copyright (C) 2016 The Android Open Source Project 3c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * 4c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * Licensed under the Apache License, Version 2.0 (the "License"); 5c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * you may not use this file except in compliance with the License. 6c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * You may obtain a copy of the License at 7c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * 8c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * http://www.apache.org/licenses/LICENSE-2.0 9c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * 10c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * Unless required by applicable law or agreed to in writing, software 11c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * distributed under the License is distributed on an "AS IS" BASIS, 12c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * See the License for the specific language governing permissions and 14c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * limitations under the License. 15c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas */ 16c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikaspackage android.support.v4.app; 17c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas 1864bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banesimport android.app.Fragment; 1964bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banesimport android.support.test.filters.SdkSuppress; 20c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikasimport android.support.v4.app.test.FragmentTestActivity; 21c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikasimport android.support.v4.app.test.FragmentTestActivity.TestFragment; 22c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikasimport android.support.v4.test.R; 23c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikasimport android.test.ActivityInstrumentationTestCase2; 24c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikasimport android.test.UiThreadTest; 2564bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banesimport android.view.KeyEvent; 26c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas 27c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas/** 28c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas * Test to prevent regressions in SupportFragmentManager fragment replace method. See b/24693644 29c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas */ 30c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikaspublic class FragmentReplaceTest extends 31c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas ActivityInstrumentationTestCase2<FragmentTestActivity> { 32c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas private FragmentTestActivity mActivity; 33c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas 34c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas 35c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas public FragmentReplaceTest() { 36c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas super(FragmentTestActivity.class); 37c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas } 38c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas 39c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas @Override 40c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas protected void setUp() throws Exception { 41c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas super.setUp(); 42c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas mActivity = getActivity(); 43c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas } 44c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas 45c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas @UiThreadTest 46c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas public void testReplaceFragment() throws Throwable { 47c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas mActivity.getSupportFragmentManager().beginTransaction() 48c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas .add(R.id.content, TestFragment.create(R.layout.fragment_a)) 49c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas .addToBackStack(null) 50c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas .commit(); 51c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas mActivity.getSupportFragmentManager().executePendingTransactions(); 52c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas assertNotNull(mActivity.findViewById(R.id.textA)); 53c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas assertNull(mActivity.findViewById(R.id.textB)); 54c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas assertNull(mActivity.findViewById(R.id.textC)); 55c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas 56c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas 57c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas mActivity.getSupportFragmentManager().beginTransaction() 58c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas .add(R.id.content, TestFragment.create(R.layout.fragment_b)) 59c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas .addToBackStack(null) 60c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas .commit(); 61c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas mActivity.getSupportFragmentManager().executePendingTransactions(); 62c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas assertNotNull(mActivity.findViewById(R.id.textA)); 63c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas assertNotNull(mActivity.findViewById(R.id.textB)); 64c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas assertNull(mActivity.findViewById(R.id.textC)); 65c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas 66c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas mActivity.getSupportFragmentManager().beginTransaction() 67c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas .replace(R.id.content, TestFragment.create(R.layout.fragment_c)) 68c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas .addToBackStack(null) 69c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas .commit(); 70c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas mActivity.getSupportFragmentManager().executePendingTransactions(); 71c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas assertNull(mActivity.findViewById(R.id.textA)); 72c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas assertNull(mActivity.findViewById(R.id.textB)); 73c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas assertNotNull(mActivity.findViewById(R.id.textC)); 74c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas } 7564bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes 7664bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes @SdkSuppress(minSdkVersion = 11) 7764bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes @UiThreadTest 7864bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes public void testBackPressWithFrameworkFragment() throws Throwable { 7964bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes mActivity.getFragmentManager().beginTransaction() 8064bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes .add(R.id.content, new Fragment()) 8164bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes .addToBackStack(null) 8264bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes .commit(); 8364bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes mActivity.getFragmentManager().executePendingTransactions(); 8464bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes assertEquals(1, mActivity.getFragmentManager().getBackStackEntryCount()); 8564bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes 8664bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK); 8764bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes 8864bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes assertEquals(0, mActivity.getFragmentManager().getBackStackEntryCount()); 8964bea6abcefe2bb6f2de56aed795c138f6d70e65Chris Banes } 90c5cea33d2092d265a2b0806ca94400dc85ab5499Aurimas Liutikas} 91