diff --git a/packages/SystemUI/src/com/android/systemui/process/ProcessWrapper.java b/packages/SystemUI/src/com/android/systemui/process/ProcessWrapper.java new file mode 100644 index 0000000000000..7db293d96a503 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/process/ProcessWrapper.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.process; + +/** + * A simple wrapper that provides access to process-related details. This facilitates testing by + * providing a mockable target around these details. + */ +public class ProcessWrapper { + public int getUserHandleIdentifier() { + return android.os.Process.myUserHandle().getIdentifier(); + } +} diff --git a/packages/SystemUI/src/com/android/systemui/process/condition/UserProcessCondition.java b/packages/SystemUI/src/com/android/systemui/process/condition/UserProcessCondition.java new file mode 100644 index 0000000000000..5a21ea075ea3b --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/process/condition/UserProcessCondition.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.process.condition; + +import com.android.systemui.process.ProcessWrapper; +import com.android.systemui.settings.UserTracker; +import com.android.systemui.shared.condition.Condition; + +import javax.inject.Inject; + +/** + * {@link UserProcessCondition} provides a signal when the process handle belongs to the current + * user. + */ +public class UserProcessCondition extends Condition { + private final ProcessWrapper mProcessWrapper; + private final UserTracker mUserTracker; + + @Inject + public UserProcessCondition(ProcessWrapper processWrapper, UserTracker userTracker) { + mProcessWrapper = processWrapper; + mUserTracker = userTracker; + } + + @Override + protected void start() { + updateCondition(mUserTracker.getUserId() + == mProcessWrapper.getUserHandleIdentifier()); + } + + @Override + protected void stop() { + } +} diff --git a/packages/SystemUI/tests/src/com/android/systemui/process/condition/UserProcessConditionTest.java b/packages/SystemUI/tests/src/com/android/systemui/process/condition/UserProcessConditionTest.java new file mode 100644 index 0000000000000..2293fc5770293 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/process/condition/UserProcessConditionTest.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.process.condition; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.testing.AndroidTestingRunner; +import android.testing.TestableLooper; + +import androidx.test.filters.SmallTest; + +import com.android.systemui.SysuiTestCase; +import com.android.systemui.process.ProcessWrapper; +import com.android.systemui.settings.UserTracker; +import com.android.systemui.shared.condition.Condition; +import com.android.systemui.shared.condition.Monitor; +import com.android.systemui.util.concurrency.FakeExecutor; +import com.android.systemui.util.time.FakeSystemClock; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +@RunWith(AndroidTestingRunner.class) +@TestableLooper.RunWithLooper +@SmallTest +public class UserProcessConditionTest extends SysuiTestCase { + @Mock + UserTracker mUserTracker; + + @Mock + ProcessWrapper mProcessWrapper; + + @Mock + Monitor.Callback mCallback; + + private final FakeExecutor mExecutor = new FakeExecutor(new FakeSystemClock()); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + /** + * Verifies condition reports false when tracker reports a different user id than the + * identifier from the process handle. + */ + @Test + public void testConditionFailsWithDifferentIds() { + + final Condition condition = new UserProcessCondition(mProcessWrapper, mUserTracker); + when(mProcessWrapper.getUserHandleIdentifier()).thenReturn(0); + when(mUserTracker.getUserId()).thenReturn(1); + + final Monitor monitor = new Monitor(mExecutor); + + monitor.addSubscription(new Monitor.Subscription.Builder(mCallback) + .addCondition(condition) + .build()); + + mExecutor.runAllReady(); + + verify(mCallback).onConditionsChanged(false); + } + + /** + * Verifies condition reports false when tracker reports a different user id than the + * identifier from the process handle. + */ + @Test + public void testConditionSucceedsWithSameIds() { + + final Condition condition = new UserProcessCondition(mProcessWrapper, mUserTracker); + when(mProcessWrapper.getUserHandleIdentifier()).thenReturn(0); + when(mUserTracker.getUserId()).thenReturn(0); + + final Monitor monitor = new Monitor(mExecutor); + + monitor.addSubscription(new Monitor.Subscription.Builder(mCallback) + .addCondition(condition) + .build()); + + mExecutor.runAllReady(); + + verify(mCallback).onConditionsChanged(true); + } + +}