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