1 /* 2 * Copyright 2024 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 17 package androidx.camera.testing.impl.activity; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.os.Bundle; 22 23 import androidx.test.espresso.idling.CountingIdlingResource; 24 25 import org.jspecify.annotations.NonNull; 26 import org.jspecify.annotations.Nullable; 27 28 /** 29 * A intermediate activity to launch another activity and wait for result. 30 */ 31 public class RequestResultTestActivity extends Activity { 32 33 /** Used to pass the action intent of the target activity. */ 34 public static final String INTENT_EXTRA_INTENT_ACTION = "intent_action"; 35 /** Used to pass the extra bundle info to launch the target activity. */ 36 public static final String INTENT_EXTRA_BUNDLE = "intent_extra_bundle"; 37 private static final String INTENT_EXTRA_RESULT_ERROR_MESSAGE = "result_error_message"; 38 private static final int REQUEST_CODE_DEFAULT = 0; 39 private final CountingIdlingResource mRequestResultReady = new CountingIdlingResource( 40 "RequestResultReady"); 41 42 private int mResultErrorCode = 0; 43 private @Nullable String mResultErrorMessage = null; 44 45 /** 46 * Retrieves the CountingIdlingResource to know whether the result has been returned or not. 47 */ getRequestResultReadyIdlingResource()48 public @NonNull CountingIdlingResource getRequestResultReadyIdlingResource() { 49 return mRequestResultReady; 50 } 51 52 /** 53 * Retrieves the result error code. 54 */ getResultErrorCode()55 public int getResultErrorCode() { 56 return mResultErrorCode; 57 } 58 59 /** 60 * Retrieves the result error message. 61 */ getResultErrorMessage()62 public @Nullable String getResultErrorMessage() { 63 return mResultErrorMessage; 64 } 65 66 @SuppressWarnings("deprecation") // Bundle.get 67 @Override onCreate(@ullable Bundle savedInstanceState)68 protected void onCreate(@Nullable Bundle savedInstanceState) { 69 super.onCreate(savedInstanceState); 70 mRequestResultReady.increment(); 71 String intentAction = getIntent().getStringExtra(INTENT_EXTRA_INTENT_ACTION); 72 Intent intent = new Intent(intentAction); 73 Bundle bundle = getIntent().getBundleExtra(INTENT_EXTRA_BUNDLE); 74 // Uses Bundle to bring the extra settings to the target activity. instanceof is used to 75 // correctly cast the extra values. Needs to add new types and verify the results when new 76 // types are needed here. 77 if (bundle != null) { 78 for (String key : bundle.keySet()) { 79 Object value = bundle.get(key); 80 if (value instanceof String) { 81 intent.putExtra(key, (String) value); 82 } else if (value instanceof Boolean) { 83 intent.putExtra(key, (Boolean) value); 84 } else if (value instanceof Integer) { 85 intent.putExtra(key, (Integer) value); 86 } 87 } 88 } 89 startActivityForResult(intent, REQUEST_CODE_DEFAULT); 90 } 91 92 @Override onActivityResult(int requestCode, int resultCode, Intent data)93 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 94 super.onActivityResult(requestCode, resultCode, data); 95 mResultErrorCode = resultCode; 96 mResultErrorMessage = data.getStringExtra(INTENT_EXTRA_RESULT_ERROR_MESSAGE); 97 mRequestResultReady.decrement(); 98 } 99 } 100