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.app.Activity; 19import android.app.Instrumentation; 20import android.app.PendingIntent; 21import android.content.Intent; 22import android.content.IntentSender; 23import android.support.test.InstrumentationRegistry; 24import android.support.test.runner.AndroidJUnit4; 25import android.support.v4.BaseInstrumentationTestCase; 26import android.support.v4.app.test.FragmentResultActivity; 27import android.support.v4.app.test.FragmentTestActivity; 28import android.support.v4.test.R; 29import android.test.suitebuilder.annotation.SmallTest; 30 31import static junit.framework.TestCase.assertEquals; 32import static junit.framework.TestCase.assertTrue; 33import static junit.framework.TestCase.fail; 34 35import org.junit.Before; 36import org.junit.Test; 37import org.junit.runner.RunWith; 38 39/** 40 * Tests for Fragment startActivityForResult and startIntentSenderForResult. 41 */ 42@RunWith(AndroidJUnit4.class) 43public class FragmentReceiveResultTest extends BaseInstrumentationTestCase<FragmentTestActivity> { 44 private FragmentTestActivity mActivity; 45 private TestFragment mFragment; 46 47 public FragmentReceiveResultTest() { 48 super(FragmentTestActivity.class); 49 } 50 51 @Before 52 public void setUp() { 53 mActivity = mActivityTestRule.getActivity(); 54 mFragment = attachTestFragment(); 55 } 56 57 @Test 58 @SmallTest 59 public void testStartActivityForResultOk() { 60 startActivityForResult(10, Activity.RESULT_OK, "content 10"); 61 62 assertTrue("Fragment should receive result", mFragment.mHasResult); 63 assertEquals(10, mFragment.mRequestCode); 64 assertEquals(Activity.RESULT_OK, mFragment.mResultCode); 65 assertEquals("content 10", mFragment.mResultContent); 66 } 67 68 @Test 69 @SmallTest 70 public void testStartActivityForResultCanceled() { 71 startActivityForResult(20, Activity.RESULT_CANCELED, "content 20"); 72 73 assertTrue("Fragment should receive result", mFragment.mHasResult); 74 assertEquals(20, mFragment.mRequestCode); 75 assertEquals(Activity.RESULT_CANCELED, mFragment.mResultCode); 76 assertEquals("content 20", mFragment.mResultContent); 77 } 78 79 @Test 80 @SmallTest 81 public void testStartIntentSenderForResultOk() { 82 startIntentSenderForResult(30, Activity.RESULT_OK, "content 30"); 83 84 assertTrue("Fragment should receive result", mFragment.mHasResult); 85 assertEquals(30, mFragment.mRequestCode); 86 assertEquals(Activity.RESULT_OK, mFragment.mResultCode); 87 assertEquals("content 30", mFragment.mResultContent); 88 } 89 90 @Test 91 @SmallTest 92 public void testStartIntentSenderForResultCanceled() { 93 startIntentSenderForResult(40, Activity.RESULT_CANCELED, "content 40"); 94 95 assertTrue("Fragment should receive result", mFragment.mHasResult); 96 assertEquals(40, mFragment.mRequestCode); 97 assertEquals(Activity.RESULT_CANCELED, mFragment.mResultCode); 98 assertEquals("content 40", mFragment.mResultContent); 99 } 100 101 private TestFragment attachTestFragment() { 102 final TestFragment fragment = new TestFragment(); 103 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 104 @Override 105 public void run() { 106 mActivity.getSupportFragmentManager().beginTransaction() 107 .add(R.id.content, fragment) 108 .addToBackStack(null) 109 .commitAllowingStateLoss(); 110 mActivity.getFragmentManager().executePendingTransactions(); 111 } 112 }); 113 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 114 return fragment; 115 } 116 117 private void startActivityForResult(final int requestCode, final int resultCode, 118 final String content) { 119 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 120 @Override 121 public void run() { 122 Intent intent = new Intent(mActivity, FragmentResultActivity.class); 123 intent.putExtra(FragmentResultActivity.EXTRA_RESULT_CODE, resultCode); 124 intent.putExtra(FragmentResultActivity.EXTRA_RESULT_CONTENT, content); 125 126 mFragment.startActivityForResult(intent, requestCode); 127 } 128 }); 129 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 130 } 131 132 private void startIntentSenderForResult(final int requestCode, final int resultCode, 133 final String content) { 134 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 135 @Override 136 public void run() { 137 Intent intent = new Intent(mActivity, FragmentResultActivity.class); 138 intent.putExtra(FragmentResultActivity.EXTRA_RESULT_CODE, resultCode); 139 intent.putExtra(FragmentResultActivity.EXTRA_RESULT_CONTENT, content); 140 141 PendingIntent pendingIntent = PendingIntent.getActivity(mActivity, 142 requestCode, intent, 0); 143 144 try { 145 mFragment.startIntentSenderForResult(pendingIntent.getIntentSender(), 146 requestCode, null, 0, 0, 0, null); 147 } catch (IntentSender.SendIntentException e) { 148 fail("IntentSender failed"); 149 } 150 } 151 }); 152 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 153 } 154 155 private static class TestFragment extends Fragment { 156 boolean mHasResult = false; 157 int mRequestCode = -1; 158 int mResultCode = 100; 159 String mResultContent; 160 161 @Override 162 public void onActivityResult(int requestCode, int resultCode, Intent data) { 163 mHasResult = true; 164 mRequestCode = requestCode; 165 mResultCode = resultCode; 166 mResultContent = data.getStringExtra(FragmentResultActivity.EXTRA_RESULT_CONTENT); 167 } 168 } 169} 170