1 /* 2 * Copyright (C) 2023 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 com.android.documentsui; 18 19 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 20 21 import static java.util.Objects.requireNonNull; 22 23 import android.app.Instrumentation; 24 import android.content.Context; 25 import android.view.KeyEvent; 26 27 import androidx.test.uiautomator.UiDevice; 28 29 import com.android.documentsui.bots.Bots; 30 31 import java.io.IOException; 32 33 34 /** Base class for instrumentation tests for DocumentsUI Activities. */ 35 class DocumentsUiTestBase { 36 private static final int BOTS_TIMEOUT = 5000; // 5 seconds 37 38 protected Context targetContext; 39 protected Context context; 40 protected UiDevice device; 41 protected Bots bots; 42 43 private String initialScreenOffTimeoutValue = null; 44 private String initialSleepTimeoutValue = null; 45 setUp()46 protected void setUp() throws Exception { 47 final Instrumentation instrumentation = getInstrumentation(); 48 targetContext = instrumentation.getTargetContext(); 49 context = instrumentation.getContext(); 50 device = UiDevice.getInstance(instrumentation); 51 52 disableScreenOffAndSleepTimeouts(); 53 54 device.setOrientationNatural(); 55 56 // "Wake-up" the device and navigate to the home page. 57 device.pressKeyCode(KeyEvent.KEYCODE_WAKEUP); 58 device.pressKeyCode(KeyEvent.KEYCODE_MENU); 59 60 bots = new Bots(device, instrumentation.getUiAutomation(), targetContext, BOTS_TIMEOUT); 61 } 62 tearDown()63 protected void tearDown() throws Exception { 64 restoreScreenOffAndSleepTimeouts(); 65 } 66 disableScreenOffAndSleepTimeouts()67 private void disableScreenOffAndSleepTimeouts() throws IOException { 68 initialScreenOffTimeoutValue = device.executeShellCommand( 69 "settings get system screen_off_timeout"); 70 initialSleepTimeoutValue = device.executeShellCommand( 71 "settings get secure sleep_timeout"); 72 device.executeShellCommand("settings put system screen_off_timeout -1"); 73 device.executeShellCommand("settings put secure sleep_timeout -1"); 74 } 75 restoreScreenOffAndSleepTimeouts()76 private void restoreScreenOffAndSleepTimeouts() throws IOException { 77 requireNonNull(initialScreenOffTimeoutValue); 78 requireNonNull(initialSleepTimeoutValue); 79 try { 80 device.executeShellCommand( 81 "settings put system screen_off_timeout " + initialScreenOffTimeoutValue); 82 device.executeShellCommand( 83 "settings put secure sleep_timeout " + initialSleepTimeoutValue); 84 } finally { 85 initialScreenOffTimeoutValue = null; 86 initialSleepTimeoutValue = null; 87 } 88 } 89 } 90