1/*
2 * Copyright (C) 2014 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
17package android.support.v7.widget;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.os.Handler;
22import android.os.Looper;
23import android.support.test.runner.MonitoringInstrumentation;
24import android.view.WindowManager;
25
26import java.util.concurrent.CountDownLatch;
27import java.util.concurrent.TimeUnit;
28
29public class TestActivity extends Activity {
30    // This is not great but the only way to do this until test runner adds support to not kill
31    // activities after tests.
32    private static final String TEST_RUNNER =
33            MonitoringInstrumentation.class.getCanonicalName() + "$" + MonitoringInstrumentation
34                    .ActivityFinisher.class.getSimpleName();
35    private volatile TestedFrameLayout mContainer;
36    boolean mVisible;
37    boolean mAllowFinish;
38    private Handler mHandler;
39
40    @Override
41    protected void onCreate(Bundle savedInstanceState) {
42        super.onCreate(savedInstanceState);
43        overridePendingTransition(0, 0);
44
45        mContainer = new TestedFrameLayout(this);
46        mHandler = new Handler(Looper.getMainLooper());
47
48        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
49        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
50        setContentView(mContainer);
51        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
52    }
53
54    public TestedFrameLayout getContainer() {
55        return mContainer;
56    }
57
58    public void resetContent() throws InterruptedException {
59        final CountDownLatch done = new CountDownLatch(1);
60        mHandler.post(new Runnable() {
61            @Override
62            public void run() {
63                mContainer = new TestedFrameLayout(TestActivity.this);
64                setContentView(mContainer);
65                done.countDown();
66            }
67        });
68        if (!done.await(5, TimeUnit.SECONDS)) {
69            throw new AssertionError("could not cleanup activity contents in 5 seconds");
70        }
71    }
72
73    @Override
74    protected void onPause() {
75        super.onPause();
76        mVisible = false;
77    }
78
79    @Override
80    public void onResume() {
81        super.onResume();
82        mVisible = true;
83    }
84
85    @Override
86    public void finish() {
87        if (!mAllowFinish) {
88            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
89            // this is terrible but easy workaround for selective finishing
90            for (StackTraceElement element : stackTrace) {
91
92                if (TEST_RUNNER.equals(element.getClassName())) {
93                    // don't allow activity finisher to finish this.
94                    return;
95                }
96            }
97        }
98        super.finish();
99    }
100
101    public void setAllowFinish(boolean allowFinish) {
102        mAllowFinish = allowFinish;
103    }
104
105    public boolean canBeReUsed() {
106        return getWindow() != null && mVisible && !mAllowFinish;
107    }
108}
109