1 /*
2  * Copyright 2018 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.recyclerview.widget;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.view.WindowManager;
22 
23 import androidx.test.core.app.ActivityScenario;
24 import androidx.testutils.ActivityScenarioResetRule;
25 import androidx.testutils.Resettable;
26 
27 import kotlin.Unit;
28 import kotlin.jvm.functions.Function1;
29 
30 public class TestActivity extends Activity implements Resettable {
31     private volatile TestedFrameLayout mContainer;
32 
33     @Override
onCreate(Bundle savedInstanceState)34     protected void onCreate(Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36         reset();
37 
38         // disable enter animation.
39         overridePendingTransition(0, 0);
40     }
41 
reset()42     public void reset() {
43         mContainer = new TestedFrameLayout(this);
44         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
45         getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
46         setContentView(mContainer);
47         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
48     }
49 
getContainer()50     public TestedFrameLayout getContainer() {
51         return mContainer;
52     }
53 
54     @Override
finish()55     public void finish() {
56         if (!mFinishEnabled) {
57             return;
58         }
59         super.finish();
60 
61         // disable exit animation.
62         overridePendingTransition(0, 0);
63     }
64 
65     private boolean mFinishEnabled;
66 
67     @Override
setFinishEnabled(boolean finishEnabled)68     public void setFinishEnabled(boolean finishEnabled) {
69         mFinishEnabled = finishEnabled;
70     }
71 
72     static class ResetRule extends ActivityScenarioResetRule<TestActivity> {
ResetRule(ActivityScenario<TestActivity> scenario)73         ResetRule(ActivityScenario<TestActivity> scenario) {
74             super(scenario, new Function1<TestActivity, Unit>() {
75                 @Override
76                 public Unit invoke(TestActivity activity) {
77                     activity.reset();
78                     return null;
79                 }
80             });
81         }
82     }
83 }
84