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 org.mockito.Mockito.*; 19 20 import android.app.Activity; 21 import android.app.Fragment; 22 import android.app.PendingIntent; 23 import android.app.stubs.FragmentResultActivity; 24 import android.app.stubs.FragmentTestActivity; 25 import android.app.stubs.R; 26 import android.content.Intent; 27 import android.content.IntentSender; 28 import android.test.ActivityInstrumentationTestCase2; 29 import android.test.suitebuilder.annotation.SmallTest; 30 31 import org.mockito.ArgumentCaptor; 32 33 import java.util.concurrent.TimeUnit; 34 35 /** 36 * Tests Fragment's startActivityForResult and startIntentSenderForResult. 37 */ 38 public class FragmentReceiveResultTest extends 39 ActivityInstrumentationTestCase2<FragmentTestActivity> { 40 41 private FragmentTestActivity mActivity; 42 private Fragment mFragment; 43 FragmentReceiveResultTest()44 public FragmentReceiveResultTest() { 45 super(FragmentTestActivity.class); 46 } 47 48 @Override setUp()49 protected void setUp() throws Exception { 50 super.setUp(); 51 mActivity = getActivity(); 52 mFragment = attachTestFragment(); 53 } 54 55 @SmallTest testStartActivityForResultOk()56 public void testStartActivityForResultOk() { 57 startActivityForResult(10, Activity.RESULT_OK, "content 10"); 58 59 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); 60 asyncVerifyOnce(mFragment) 61 .onActivityResult(eq(10), eq(Activity.RESULT_OK), captor.capture()); 62 final String data = captor.getValue() 63 .getStringExtra(FragmentResultActivity.EXTRA_RESULT_CONTENT); 64 assertEquals("content 10", data); 65 } 66 67 @SmallTest testStartActivityForResultCanceled()68 public void testStartActivityForResultCanceled() { 69 startActivityForResult(20, Activity.RESULT_CANCELED, "content 20"); 70 71 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); 72 asyncVerifyOnce(mFragment) 73 .onActivityResult(eq(20), eq(Activity.RESULT_CANCELED), captor.capture()); 74 final String data = captor.getValue() 75 .getStringExtra(FragmentResultActivity.EXTRA_RESULT_CONTENT); 76 assertEquals("content 20", data); 77 } 78 79 @SmallTest testStartIntentSenderForResultOk()80 public void testStartIntentSenderForResultOk() { 81 startIntentSenderForResult(30, Activity.RESULT_OK, "content 30"); 82 83 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); 84 asyncVerifyOnce(mFragment) 85 .onActivityResult(eq(30), eq(Activity.RESULT_OK), captor.capture()); 86 final String data = captor.getValue() 87 .getStringExtra(FragmentResultActivity.EXTRA_RESULT_CONTENT); 88 assertEquals("content 30", data); 89 } 90 91 @SmallTest testStartIntentSenderForResultCanceled()92 public void testStartIntentSenderForResultCanceled() { 93 startIntentSenderForResult(40, Activity.RESULT_CANCELED, "content 40"); 94 95 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); 96 asyncVerifyOnce(mFragment) 97 .onActivityResult(eq(40), eq(Activity.RESULT_CANCELED), captor.capture()); 98 final String data = captor.getValue() 99 .getStringExtra(FragmentResultActivity.EXTRA_RESULT_CONTENT); 100 assertEquals("content 40", data); 101 } 102 attachTestFragment()103 private Fragment attachTestFragment() { 104 final Fragment fragment = spy(new Fragment()); 105 getInstrumentation().waitForIdleSync(); 106 getInstrumentation().runOnMainSync(new Runnable() { 107 @Override 108 public void run() { 109 mActivity.getFragmentManager().beginTransaction() 110 .add(R.id.content, fragment) 111 .addToBackStack(null) 112 .commitAllowingStateLoss(); 113 mActivity.getFragmentManager().executePendingTransactions(); 114 } 115 }); 116 getInstrumentation().waitForIdleSync(); 117 return fragment; 118 } 119 startActivityForResult(final int requestCode, final int resultCode, final String content)120 private void startActivityForResult(final int requestCode, final int resultCode, 121 final String content) { 122 getInstrumentation().runOnMainSync(new Runnable() { 123 @Override 124 public void run() { 125 Intent intent = new Intent(mActivity, FragmentResultActivity.class); 126 intent.putExtra(FragmentResultActivity.EXTRA_RESULT_CODE, resultCode); 127 intent.putExtra(FragmentResultActivity.EXTRA_RESULT_CONTENT, content); 128 129 mFragment.startActivityForResult(intent, requestCode); 130 } 131 }); 132 getInstrumentation().waitForIdleSync(); 133 } 134 asyncVerifyOnce(T mock)135 private static <T> T asyncVerifyOnce(T mock) { 136 return verify(mock, timeout(TimeUnit.SECONDS.toMillis(10)).times(1)); 137 } 138 startIntentSenderForResult(final int requestCode, final int resultCode, final String content)139 private void startIntentSenderForResult(final int requestCode, final int resultCode, 140 final String content) { 141 getInstrumentation().runOnMainSync(new Runnable() { 142 @Override 143 public void run() { 144 Intent intent = new Intent(mActivity, FragmentResultActivity.class); 145 intent.putExtra(FragmentResultActivity.EXTRA_RESULT_CODE, resultCode); 146 intent.putExtra(FragmentResultActivity.EXTRA_RESULT_CONTENT, content); 147 148 PendingIntent pendingIntent = PendingIntent.getActivity(mActivity, 149 requestCode, intent, PendingIntent.FLAG_IMMUTABLE); 150 151 try { 152 mFragment.startIntentSenderForResult(pendingIntent.getIntentSender(), 153 requestCode, null, 0, 0, 0, null); 154 } catch (IntentSender.SendIntentException e) { 155 fail("IntentSender failed"); 156 } 157 } 158 }); 159 getInstrumentation().waitForIdleSync(); 160 } 161 162 } 163