1 /* 2 * Copyright (C) 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 android.autofillservice.cts.commontests; 18 19 import android.autofillservice.cts.activities.GridActivity; 20 import android.autofillservice.cts.testcore.AutofillActivityTestRule; 21 import android.autofillservice.cts.testcore.Timeouts; 22 import android.autofillservice.cts.testcore.WindowChangeTimeoutException; 23 import android.view.accessibility.AccessibilityEvent; 24 25 import java.util.concurrent.TimeoutException; 26 27 /** 28 * Base class for test cases using {@link GridActivity}. 29 */ 30 public abstract class AbstractGridActivityTestCase 31 extends AutoFillServiceTestCase.AutoActivityLaunch<GridActivity> { 32 33 protected GridActivity mActivity; 34 35 @Override getActivityRule()36 protected AutofillActivityTestRule<GridActivity> getActivityRule() { 37 return new AutofillActivityTestRule<GridActivity>(GridActivity.class) { 38 @Override 39 protected void afterActivityLaunched() { 40 mActivity = getActivity(); 41 postActivityLaunched(); 42 } 43 }; 44 } 45 46 /** 47 * Hook for subclass to customize activity after it's launched. 48 */ 49 protected void postActivityLaunched() { 50 } 51 52 /** 53 * Focus to a cell and expect window event 54 */ 55 protected void focusCell(int row, int column) throws TimeoutException { 56 mUiBot.waitForWindowChange(() -> mActivity.focusCell(row, column)); 57 } 58 59 /** 60 * Focus to a cell and expect no window event. 61 */ 62 protected void focusCellNoWindowChange(int row, int column) { 63 final AccessibilityEvent event; 64 try { 65 event = mUiBot.waitForWindowChange(() -> mActivity.focusCell(row, column), 66 Timeouts.WINDOW_CHANGE_NOT_GENERATED_NAPTIME_MS); 67 } catch (WindowChangeTimeoutException ex) { 68 // no window events! looking good 69 return; 70 } 71 throw new IllegalStateException(String.format("Expect no window event when focusing to" 72 + " column %d row %d, but event happened: %s", row, column, event)); 73 } 74 } 75