• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.support.test.runner.MonitoringInstrumentation;
22 import android.view.WindowManager;
23 
24 public class TestActivity extends Activity {
25     // This is not great but the only way to do this until test runner adds support to not kill
26     // activities after tests.
27     private static final String TEST_RUNNER =
28             MonitoringInstrumentation.class.getCanonicalName() + "$" + MonitoringInstrumentation
29                     .ActivityFinisher.class.getSimpleName();
30     private volatile TestedFrameLayout mContainer;
31     boolean mVisible;
32     boolean mAllowFinish;
33 
34     @Override
onCreate(Bundle savedInstanceState)35     protected void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         overridePendingTransition(0, 0);
38 
39         mContainer = new TestedFrameLayout(this);
40         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
41         getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
42         setContentView(mContainer);
43         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
44     }
45 
getContainer()46     public TestedFrameLayout getContainer() {
47         return mContainer;
48     }
49 
50     @Override
onPause()51     protected void onPause() {
52         super.onPause();
53         mVisible = false;
54     }
55 
56     @Override
onResume()57     public void onResume() {
58         super.onResume();
59         mVisible = true;
60     }
61 
62     @Override
finish()63     public void finish() {
64         if (!mAllowFinish) {
65             StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
66             // this is terrible but easy workaround for selective finishing
67             for (StackTraceElement element : stackTrace) {
68 
69                 if (TEST_RUNNER.equals(element.getClassName())) {
70                     // don't allow activity finisher to finish this.
71                     return;
72                 }
73             }
74         }
75         super.finish();
76     }
77 }
78