1/*
2 * Copyright (C) 2015 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 com.android.messaging.ui;
18
19import android.app.Fragment;
20import android.os.Bundle;
21import android.support.v4.app.FragmentActivity;
22import android.view.View;
23import android.view.WindowManager;
24import android.widget.FrameLayout;
25
26import com.android.messaging.R;
27import com.android.messaging.util.LogUtil;
28import com.google.common.annotations.VisibleForTesting;
29
30/**
31 * An empty activity that can be used to host a fragment or view for unit testing purposes. Lives in
32 * app code vs test code due to requirement of ActivityInstrumentationTestCase2.
33 */
34public class TestActivity extends FragmentActivity {
35    private FragmentEventListener mFragmentEventListener;
36
37    public interface FragmentEventListener {
38        public void onAttachFragment(Fragment fragment);
39    }
40
41    @Override
42    protected void onCreate(final Bundle bundle) {
43        super.onCreate(bundle);
44
45        if (bundle != null) {
46            // The test case may have configured the fragment, and recreating the activity will
47            // lose that configuration. The real activity is the only activity that would know
48            // how to reapply that configuration.
49            throw new IllegalStateException("TestActivity cannot get recreated");
50        }
51
52        // There is a race condition, but this often makes it possible for tests to run with the
53        // key guard up
54        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
55                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
56                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
57                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
58
59        setContentView(R.layout.test_activity);
60    }
61
62    @VisibleForTesting
63    public void setFragment(final Fragment fragment) {
64        LogUtil.i(LogUtil.BUGLE_TAG, "TestActivity.setFragment");
65        getFragmentManager()
66                .beginTransaction()
67                .replace(R.id.test_content, fragment)
68                .commit();
69        getFragmentManager().executePendingTransactions();
70    }
71
72    @VisibleForTesting
73    public void setView(final View view) {
74        LogUtil.i(LogUtil.BUGLE_TAG, "TestActivity.setView");
75        ((FrameLayout) findViewById(R.id.test_content)).addView(view);
76    }
77
78    @Override
79    public void onAttachFragment(final Fragment fragment) {
80        if (mFragmentEventListener != null) {
81            mFragmentEventListener.onAttachFragment(fragment);
82        }
83    }
84
85    public void setFragmentEventListener(final FragmentEventListener fragmentEventListener) {
86        mFragmentEventListener = fragmentEventListener;
87    }
88}
89