1 /* 2 * Copyright (C) 2017 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.os; 18 19 import android.content.ComponentName; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.provider.Settings; 23 import android.service.dreams.DreamService; 24 import android.service.dreams.IDreamManager; 25 import android.test.ActivityInstrumentationTestCase2; 26 27 import androidx.test.filters.MediumTest; 28 import androidx.test.filters.SmallTest; 29 30 /** 31 * Tests dream aspects of PowerManager. 32 */ 33 @MediumTest 34 public class PowerManagerVrTest extends ActivityInstrumentationTestCase2<TestVrActivity> { 35 private PowerManager mPm; 36 private IDreamManager mDm; 37 private String mOldVrListener; 38 PowerManagerVrTest()39 public PowerManagerVrTest() { 40 super(TestVrActivity.class); 41 } 42 43 /** 44 * Setup any common data for the upcoming tests. 45 */ 46 @Override setUp()47 public void setUp() throws Exception { 48 super.setUp(); 49 Context context = getInstrumentation().getTargetContext(); 50 mPm = (PowerManager) getInstrumentation().getTargetContext().getSystemService( 51 Context.POWER_SERVICE); 52 mDm = IDreamManager.Stub.asInterface( 53 ServiceManager.getService(DreamService.DREAM_SERVICE)); 54 55 mOldVrListener = setTestVrListener(new ComponentName( 56 context, TestVrActivity.TestVrListenerService.class).flattenToString()); 57 } 58 59 @Override tearDown()60 public void tearDown() throws Exception { 61 if (mDm != null) { 62 mDm.awaken(); // Don't leave the device in the dream state. 63 } 64 65 setTestVrListener(mOldVrListener); 66 } 67 68 /** 69 * Confirm that the setup is good. 70 * 71 * @throws Exception 72 */ 73 @SmallTest testPreconditions()74 public void testPreconditions() throws Exception { 75 assertNotNull(mPm); 76 assertNotNull(mDm); 77 } 78 79 /** 80 * Confirm that the system prevents napping while in VR. 81 * Dreaming is controlled by PowerManager, but we use dreamManager to access those features 82 * in order to not require DEVICE_POWER permissions which other tests expect not to have. 83 * 84 * @throws Exception 85 */ 86 @SmallTest testNap()87 public void testNap() throws Exception { 88 // For dream to work, we need to wake up the system 89 wakeUpDevice(); 90 91 mDm.dream(); 92 waitForDreamState(true); 93 assertTrue(mDm.isDreaming()); 94 mDm.awaken(); 95 96 // awaken() is not immediate so we have to wait for dreaming to stop 97 // before continuing with the test. 98 waitForDreamState(false); 99 100 // set VR Mode to true by starting our VR Activity, then retest the dream. 101 TestVrActivity activity = getActivity(); 102 assertTrue(activity.waitForActivityStart()); 103 104 try { 105 mDm.dream(); 106 waitForDreamState(true); // wait for dream to turn true with a timeout 107 assertFalse(mDm.isDreaming()); // ensure dream is still false after waiting. 108 mDm.awaken(); 109 } finally { 110 activity.finish(); 111 } 112 } 113 114 /** 115 * Waits synchronously for the system to be set to the specified dream state. 116 */ waitForDreamState(boolean isDreaming)117 private void waitForDreamState(boolean isDreaming) throws Exception { 118 final int MAX_ATTEMPTS = 10; 119 final int WAIT_TIME_PER_ATTEMPT_MILLIS = 100; 120 for (int i = 0; i < MAX_ATTEMPTS; i++) { 121 if (mDm.isDreaming() == isDreaming) { 122 break; 123 } 124 Thread.sleep(WAIT_TIME_PER_ATTEMPT_MILLIS); 125 } 126 } 127 wakeUpDevice()128 private void wakeUpDevice() { 129 PowerManager.WakeLock wl = mPm.newWakeLock( 130 PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP 131 | PowerManager.ON_AFTER_RELEASE, "FULL_WAKE_LOCK"); 132 wl.acquire(); 133 wl.release(); 134 } 135 136 /** 137 * Sets a new value for the enabled VrListenerService and returns the previous value. 138 */ setTestVrListener(String newValue)139 private String setTestVrListener(String newValue) { 140 final String ENABLED_VR_LISTENERS = "enabled_vr_listeners"; 141 Context context = getInstrumentation().getTargetContext(); 142 ContentResolver cr = context.getContentResolver(); 143 String oldVrListeners = Settings.Secure.getString(cr, ENABLED_VR_LISTENERS); 144 Settings.Secure.putString(cr, ENABLED_VR_LISTENERS, newValue); 145 return oldVrListeners; 146 } 147 } 148