• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 com.android.bedstead.testapp;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.os.PersistableBundle;
23 
24 import com.android.bedstead.nene.utils.Poll;
25 import com.android.eventlib.premade.EventLibActivity;
26 
27 import java.util.Map;
28 import java.util.WeakHashMap;
29 
30 /**
31  * An {@link Activity} which logs events for all lifecycle events and supports TestApp Features.
32  */
33 public class BaseTestAppActivity extends EventLibActivity {
34 
35     public static final String ACTIVITY_RESULT_KEY = "ACTIVITY_RESULT";
36 
37     private static Map<String, BaseTestAppActivity> sActivities = new WeakHashMap<>();
38 
39     /**
40      * Find an activity for the given class name.
41      *
42      * <p>This will throw an {@link IllegalStateException} if there is no existing activity for the
43      * class name.
44      *
45      * <p>This method is thread-safe
46      */
findActivity(Context context, String activityClassName)47     public static BaseTestAppActivity findActivity(Context context, String activityClassName) {
48         return Poll.forValue("activity for className", () -> {
49             synchronized (BaseTestAppActivity.class) {
50                 return sActivities.get(activityClassName);
51             }
52         }).toNotBeNull()
53                 .errorOnFail("No existing activity named " + activityClassName
54                         + " in package " + context.getPackageName() + ". ")
55                 .await();
56     }
57 
58     @Override
59     protected void onCreate(Bundle savedInstanceState) {
60         synchronized (BaseTestAppActivity.class) {
61             sActivities.put(getClassName(), this);
62         }
63         super.onCreate(savedInstanceState);
64 
65         forwardIntentResult();
66     }
67 
68     @Override
69     public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
70         synchronized (BaseTestAppActivity.class) {
71             sActivities.put(getClassName(), this);
72         }
73         super.onCreate(savedInstanceState, persistentState);
74 
75         forwardIntentResult();
76     }
77 
78     @Override
79     protected void onStart() {
80         super.onStart();
81     }
82 
83     @Override
84     protected void onRestart() {
85         super.onRestart();
86     }
87 
88     @Override
89     protected void onResume() {
90         super.onResume();
91     }
92 
93     @Override
94     protected void onPause() {
95         super.onPause();
96     }
97 
98     @Override
99     protected void onStop() {
100         super.onStop();
101     }
102 
103     @Override
104     protected void onDestroy() {
105         super.onDestroy();
106     }
107 
108     // TODO(b/198280332): Remove this temporary solution to set return values for methods
109     private void forwardIntentResult() {
110         int result = getIntent().getIntExtra(ACTIVITY_RESULT_KEY, -1);
111 
112         if (result != -1) {
113             setResult(result);
114             finish();
115         }
116     }
117 }
118