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 android.support.v7.testutils; 18 19import android.os.Bundle; 20import android.support.annotation.NonNull; 21import android.support.annotation.Nullable; 22import android.support.v4.view.MenuItemCompat; 23import android.support.v7.app.AppCompatActivity; 24import android.support.v7.app.AppCompatCallback; 25import android.support.v7.appcompat.test.R; 26import android.support.v7.view.ActionMode; 27import android.view.KeyEvent; 28import android.view.Menu; 29import android.view.MenuItem; 30import android.view.WindowManager; 31 32public abstract class BaseTestActivity extends AppCompatActivity { 33 34 private Menu mMenu; 35 36 private KeyEvent mOnKeyDownEvent; 37 private KeyEvent mOnKeyUpEvent; 38 private KeyEvent mOnKeyShortcutEvent; 39 40 private MenuItem mOptionsItemSelected; 41 42 private boolean mOnMenuOpenedCalled; 43 private boolean mOnPanelClosedCalled; 44 45 private boolean mShouldPopulateOptionsMenu = true; 46 47 private boolean mOnBackPressedCalled; 48 private boolean mDestroyed; 49 50 private AppCompatCallback mAppCompatCallback; 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 overridePendingTransition(0, 0); 56 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 57 final int contentView = getContentViewLayoutResId(); 58 if (contentView > 0) { 59 setContentView(contentView); 60 } 61 onContentViewSet(); 62 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 63 } 64 65 @Override 66 public void finish() { 67 super.finish(); 68 overridePendingTransition(0, 0); 69 } 70 71 protected abstract int getContentViewLayoutResId(); 72 73 protected void onContentViewSet() { 74 } 75 76 @Override 77 public boolean onOptionsItemSelected(MenuItem item) { 78 mOptionsItemSelected = item; 79 return super.onOptionsItemSelected(item); 80 } 81 82 @Override 83 public boolean onMenuOpened(int featureId, Menu menu) { 84 mOnMenuOpenedCalled = true; 85 return super.onMenuOpened(featureId, menu); 86 } 87 88 @Override 89 public void onPanelClosed(int featureId, Menu menu) { 90 mOnPanelClosedCalled = true; 91 super.onPanelClosed(featureId, menu); 92 } 93 94 @Override 95 public boolean onKeyDown(int keyCode, KeyEvent event) { 96 mOnKeyDownEvent = event; 97 return super.onKeyDown(keyCode, event); 98 } 99 100 @Override 101 public boolean onKeyUp(int keyCode, KeyEvent event) { 102 mOnKeyUpEvent = event; 103 return super.onKeyUp(keyCode, event); 104 } 105 106 @Override 107 public boolean onKeyShortcut(int keyCode, KeyEvent event) { 108 mOnKeyShortcutEvent = event; 109 return super.onKeyShortcut(keyCode, event); 110 } 111 112 public KeyEvent getInvokedKeyShortcutEvent() { 113 return mOnKeyShortcutEvent; 114 } 115 116 public boolean wasOnMenuOpenedCalled() { 117 return mOnMenuOpenedCalled; 118 } 119 120 public boolean wasOnPanelClosedCalled() { 121 return mOnPanelClosedCalled; 122 } 123 124 public KeyEvent getInvokedKeyDownEvent() { 125 return mOnKeyDownEvent; 126 } 127 128 public KeyEvent getInvokedKeyUpEvent() { 129 return mOnKeyUpEvent; 130 } 131 132 @Override 133 public boolean onCreateOptionsMenu(Menu menu) { 134 mMenu = menu; 135 if (mShouldPopulateOptionsMenu) { 136 getMenuInflater().inflate(R.menu.sample_actions, menu); 137 return true; 138 } else { 139 menu.clear(); 140 return super.onCreateOptionsMenu(menu); 141 } 142 } 143 144 public boolean expandSearchView() { 145 return MenuItemCompat.expandActionView(mMenu.findItem(R.id.action_search)); 146 } 147 148 public boolean collapseSearchView() { 149 return MenuItemCompat.collapseActionView(mMenu.findItem(R.id.action_search)); 150 } 151 152 public boolean isSearchViewExpanded() { 153 return MenuItemCompat.isActionViewExpanded(mMenu.findItem(R.id.action_search)); 154 } 155 156 public MenuItem getOptionsItemSelected() { 157 return mOptionsItemSelected; 158 } 159 160 public void reset() { 161 mOnKeyUpEvent = null; 162 mOnKeyDownEvent = null; 163 mOnKeyShortcutEvent = null; 164 mOnMenuOpenedCalled = false; 165 mOnPanelClosedCalled = false; 166 mMenu = null; 167 mOptionsItemSelected = null; 168 } 169 170 public void setShouldPopulateOptionsMenu(boolean populate) { 171 mShouldPopulateOptionsMenu = populate; 172 if (mMenu != null) { 173 supportInvalidateOptionsMenu(); 174 } 175 } 176 177 @Override 178 protected void onDestroy() { 179 super.onDestroy(); 180 mDestroyed = true; 181 } 182 183 @Override 184 public void onBackPressed() { 185 super.onBackPressed(); 186 mOnBackPressedCalled = true; 187 } 188 189 public boolean wasOnBackPressedCalled() { 190 return mOnBackPressedCalled; 191 } 192 193 public Menu getMenu() { 194 return mMenu; 195 } 196 197 @Override 198 public boolean isDestroyed() { 199 return mDestroyed; 200 } 201 202 @Override 203 public void onSupportActionModeStarted(@NonNull ActionMode mode) { 204 if (mAppCompatCallback != null) { 205 mAppCompatCallback.onSupportActionModeStarted(mode); 206 } 207 } 208 209 @Override 210 public void onSupportActionModeFinished(@NonNull ActionMode mode) { 211 if (mAppCompatCallback != null) { 212 mAppCompatCallback.onSupportActionModeFinished(mode); 213 } 214 } 215 216 @Nullable 217 @Override 218 public ActionMode onWindowStartingSupportActionMode(@NonNull ActionMode.Callback callback) { 219 if (mAppCompatCallback != null) { 220 return mAppCompatCallback.onWindowStartingSupportActionMode(callback); 221 } 222 return super.onWindowStartingSupportActionMode(callback); 223 } 224 225 public void setAppCompatCallback(AppCompatCallback callback) { 226 mAppCompatCallback = callback; 227 } 228} 229