Craig Donner | dc4796c | 2017-03-08 09:51:47 -0800 | [diff] [blame] | 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 | package android.os; |
| 17 | |
| 18 | import android.app.ActivityManager; |
| 19 | import android.app.VrManager; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.pm.PackageManager; |
| 23 | import android.os.Process; |
| 24 | import android.provider.Settings; |
| 25 | import android.test.ActivityInstrumentationTestCase2; |
| 26 | import android.test.suitebuilder.annotation.SmallTest; |
Craig Donner | dc4796c | 2017-03-08 09:51:47 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Tests ActivityManager#setPersistentVrThread and ActivityManager#setVrThread's |
| 30 | * interaction with persistent VR mode. |
| 31 | */ |
| 32 | public class SetPersistentVrThreadTest extends ActivityInstrumentationTestCase2<TestVrActivity> { |
| 33 | private TestVrActivity mActivity; |
| 34 | private ActivityManager mActivityManager; |
| 35 | private VrManager mVrManager; |
| 36 | private Context mContext; |
| 37 | private String mOldVrListener; |
| 38 | private ComponentName mRequestedComponent; |
| 39 | private static final int SCHED_OTHER = 0; |
| 40 | private static final int SCHED_FIFO = 1; |
| 41 | private static final int SCHED_RESET_ON_FORK = 0x40000000; |
| 42 | public static final String ENABLED_VR_LISTENERS = "enabled_vr_listeners"; |
| 43 | private static final String TAG = "VrSetPersistentFIFOThreadTest"; |
| 44 | |
| 45 | public SetPersistentVrThreadTest() { |
| 46 | super(TestVrActivity.class); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | protected void setUp() throws Exception { |
| 51 | super.setUp(); |
| 52 | mContext = getInstrumentation().getTargetContext(); |
| 53 | mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
| 54 | mVrManager = (VrManager) mContext.getSystemService(Context.VR_SERVICE); |
| 55 | |
| 56 | mRequestedComponent = new ComponentName(mContext, |
| 57 | TestVrActivity.TestVrListenerService.class); |
| 58 | mOldVrListener = Settings.Secure.getString(mContext.getContentResolver(), |
| 59 | ENABLED_VR_LISTENERS); |
| 60 | Settings.Secure.putString(mContext.getContentResolver(), ENABLED_VR_LISTENERS, |
| 61 | mRequestedComponent.flattenToString()); |
| 62 | mActivity = getActivity(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected void tearDown() throws Exception { |
| 67 | try { |
| 68 | setPersistentVrModeEnabled(false); |
| 69 | } catch (Throwable e) { |
| 70 | // pass |
| 71 | } |
| 72 | Settings.Secure.putString(mContext.getContentResolver(), ENABLED_VR_LISTENERS, |
| 73 | mOldVrListener); |
| 74 | super.tearDown(); |
| 75 | } |
| 76 | |
| 77 | private void setPersistentVrModeEnabled(boolean enable) throws Throwable { |
Rick Yiu | 47a17eb | 2018-09-08 18:57:04 +0800 | [diff] [blame] | 78 | if (mVrManager != null) { |
| 79 | mVrManager.setPersistentVrModeEnabled(enable); |
| 80 | // Allow the system time to send out callbacks for persistent VR mode. |
| 81 | Thread.sleep(200); |
| 82 | } |
Craig Donner | dc4796c | 2017-03-08 09:51:47 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @SmallTest |
| 86 | public void testSetPersistentVrThreadAPISuccess() throws Throwable { |
| 87 | if (!mActivity.getPackageManager().hasSystemFeature( |
| 88 | PackageManager.FEATURE_VR_MODE_HIGH_PERFORMANCE)) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | int vr_thread = 0, policy = 0; |
| 93 | vr_thread = Process.myTid(); |
| 94 | |
| 95 | setPersistentVrModeEnabled(true); |
| 96 | mActivityManager.setPersistentVrThread(vr_thread); |
| 97 | policy = (int) Process.getThreadScheduler(vr_thread); |
| 98 | assertEquals((SCHED_FIFO | SCHED_RESET_ON_FORK), policy); |
| 99 | |
| 100 | // Check that the thread loses priority when persistent mode is disabled. |
| 101 | setPersistentVrModeEnabled(false); |
| 102 | policy = (int) Process.getThreadScheduler(vr_thread); |
| 103 | assertEquals(SCHED_OTHER, policy); |
| 104 | |
| 105 | // Check that disabling VR mode when in persistent mode does not affect the persistent |
| 106 | // thread. |
| 107 | mActivity.setVrModeEnabled(true, mRequestedComponent); |
| 108 | Thread.sleep(200); |
| 109 | setPersistentVrModeEnabled(true); |
| 110 | Thread.sleep(200); |
| 111 | mActivityManager.setPersistentVrThread(vr_thread); |
| 112 | policy = (int) Process.getThreadScheduler(vr_thread); |
| 113 | assertEquals((SCHED_FIFO | SCHED_RESET_ON_FORK), policy); |
| 114 | mActivity.setVrModeEnabled(false, mRequestedComponent); |
| 115 | Thread.sleep(200); |
| 116 | assertEquals((SCHED_FIFO | SCHED_RESET_ON_FORK), policy); |
| 117 | setPersistentVrModeEnabled(false); |
| 118 | policy = (int) Process.getThreadScheduler(vr_thread); |
| 119 | assertEquals(SCHED_OTHER, policy); |
| 120 | } |
| 121 | |
| 122 | @SmallTest |
| 123 | public void testSetPersistentVrThreadAPIFailure() throws Throwable { |
| 124 | if (!mActivity.getPackageManager().hasSystemFeature( |
| 125 | PackageManager.FEATURE_VR_MODE_HIGH_PERFORMANCE)) { |
| 126 | return; |
| 127 | } |
| 128 | int vr_thread = 0, policy = 0; |
| 129 | vr_thread = Process.myTid(); |
| 130 | mActivityManager.setPersistentVrThread(vr_thread); |
| 131 | policy = (int) Process.getThreadScheduler(vr_thread); |
| 132 | assertEquals(SCHED_OTHER, policy); |
| 133 | } |
| 134 | |
| 135 | @SmallTest |
| 136 | public void testSetVrThreadAPIFailsInPersistentMode() throws Throwable { |
| 137 | if (!mActivity.getPackageManager().hasSystemFeature( |
| 138 | PackageManager.FEATURE_VR_MODE_HIGH_PERFORMANCE)) { |
| 139 | return; |
| 140 | } |
| 141 | int vr_thread = 0, policy = 0; |
| 142 | mActivity.setVrModeEnabled(true, mRequestedComponent); |
| 143 | vr_thread = Process.myTid(); |
| 144 | |
| 145 | setPersistentVrModeEnabled(true); |
| 146 | mActivityManager.setVrThread(vr_thread); |
| 147 | policy = (int) Process.getThreadScheduler(vr_thread); |
| 148 | assertEquals(SCHED_OTHER, policy); |
| 149 | setPersistentVrModeEnabled(false); |
| 150 | |
| 151 | // When not in persistent mode the API works again. |
| 152 | mActivity.setVrModeEnabled(true, mRequestedComponent); |
| 153 | mActivityManager.setVrThread(vr_thread); |
| 154 | policy = (int) Process.getThreadScheduler(vr_thread); |
| 155 | assertEquals((SCHED_FIFO | SCHED_RESET_ON_FORK), policy); |
| 156 | |
| 157 | // The thread loses priority when persistent mode is disabled. |
| 158 | setPersistentVrModeEnabled(true); |
| 159 | policy = (int) Process.getThreadScheduler(vr_thread); |
| 160 | assertEquals(SCHED_OTHER, policy); |
| 161 | setPersistentVrModeEnabled(false); |
| 162 | } |
| 163 | } |