1 /* 2 * Copyright (C) 2016 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 package androidx.leanback.app; 17 18 import android.app.Activity; 19 import android.content.Intent; 20 import android.os.Bundle; 21 import android.support.test.rule.ActivityTestRule; 22 23 import androidx.annotation.CallSuper; 24 25 import java.util.HashMap; 26 27 /** 28 * A general Activity that allows test set a Provider to custom activity's behavior in life 29 * cycle events. 30 */ 31 public class TestActivity extends Activity { 32 33 public static class Provider { 34 35 TestActivity mActivity; 36 37 /** 38 * @return Currently attached activity. 39 */ getActivity()40 public TestActivity getActivity() { 41 return mActivity; 42 } 43 44 @CallSuper onCreate(TestActivity activity, Bundle savedInstanceState)45 public void onCreate(TestActivity activity, Bundle savedInstanceState) { 46 mActivity = activity; 47 } 48 onAttachedToWindow(TestActivity activity)49 public void onAttachedToWindow(TestActivity activity) { 50 } 51 onStart(TestActivity activity)52 public void onStart(TestActivity activity) { 53 } 54 onStop(TestActivity activity)55 public void onStop(TestActivity activity) { 56 } 57 onPause(TestActivity activity)58 public void onPause(TestActivity activity) { 59 } 60 onResume(TestActivity activity)61 public void onResume(TestActivity activity) { 62 } 63 onDestroy(TestActivity activity)64 public void onDestroy(TestActivity activity) { 65 } 66 } 67 68 public static class TestActivityTestRule extends ActivityTestRule<TestActivity> { 69 70 String mProviderName; TestActivityTestRule(TestActivity.Provider provider, String providerName)71 public TestActivityTestRule(TestActivity.Provider provider, String providerName) { 72 super(TestActivity.class, false, false); 73 mProviderName = providerName; 74 provider.mActivity = null; 75 TestActivity.setProvider(mProviderName, provider); 76 } 77 launchActivity()78 public TestActivity launchActivity() { 79 Intent intent = new Intent(); 80 intent.putExtra(TestActivity.EXTRA_PROVIDER, mProviderName); 81 return launchActivity(intent); 82 } 83 } 84 85 public static final String EXTRA_PROVIDER = "testActivityProvider"; 86 87 static HashMap<String, Provider> sProviders = new HashMap<>(); 88 89 String mProviderName; 90 Provider mProvider; 91 boolean mStarted; 92 TestActivity()93 public TestActivity() { 94 } 95 setProvider(String name, Provider provider)96 public static void setProvider(String name, Provider provider) { 97 sProviders.put(name, provider); 98 } 99 100 @Override onCreate(Bundle savedInstanceState)101 protected void onCreate(Bundle savedInstanceState) { 102 super.onCreate(savedInstanceState); 103 mProviderName = getIntent().getStringExtra(EXTRA_PROVIDER); 104 mProvider = sProviders.get(mProviderName); 105 if (mProvider != null) { 106 mProvider.onCreate(this, savedInstanceState); 107 } 108 } 109 110 @Override onAttachedToWindow()111 public void onAttachedToWindow() { 112 super.onAttachedToWindow(); 113 if (mProvider != null) { 114 mProvider.onAttachedToWindow(this); 115 } 116 } 117 isStarted()118 public boolean isStarted() { 119 return mStarted; 120 } 121 122 @Override onStart()123 protected void onStart() { 124 super.onStart(); 125 mStarted = true; 126 if (mProvider != null) { 127 mProvider.onStart(this); 128 } 129 } 130 131 @Override onPause()132 protected void onPause() { 133 if (mProvider != null) { 134 mProvider.onPause(this); 135 } 136 super.onPause(); 137 } 138 139 @Override onResume()140 protected void onResume() { 141 super.onResume(); 142 if (mProvider != null) { 143 mProvider.onResume(this); 144 } 145 } 146 147 @Override onStop()148 protected void onStop() { 149 mStarted = false; 150 if (mProvider != null) { 151 mProvider.onStop(this); 152 } 153 super.onStop(); 154 } 155 156 @Override onDestroy()157 protected void onDestroy() { 158 if (mProvider != null) { 159 mProvider.onDestroy(this); 160 setProvider(mProviderName, null); 161 } 162 super.onDestroy(); 163 } 164 } 165