Introduce SelfExecutingMonitor for tests.

A number of tested components rely on monitors for pretext conditions.
In these cases, a mocked monitor will not execute the callback in order
for the logic to proceed. This change introduces and integrates
SelfExecutingMonitor to handle this case.

Bug: 261420432
Test: atest ComplicationTypesUpdaterTest
Test: atest DreamClockTimeComplicationTest
Test: atest DreamHomeControlsComplicationTest
Test: atest SmartSpaceComplicationTest
Change-Id: I2368bdcaa8a8a064484c160ddd95bf73882b733d
This commit is contained in:
Bryce Lee
2023-02-13 12:25:28 -08:00
parent ffbc4a29c9
commit 71bf33c6b9
5 changed files with 71 additions and 4 deletions

View File

@@ -32,6 +32,7 @@ import androidx.test.filters.SmallTest;
import com.android.settingslib.dream.DreamBackend;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.condition.SelfExecutingMonitor;
import com.android.systemui.dreams.DreamOverlayStateController;
import com.android.systemui.shared.condition.Monitor;
import com.android.systemui.util.concurrency.FakeExecutor;
@@ -67,7 +68,6 @@ public class ComplicationTypesUpdaterTest extends SysuiTestCase {
private ComplicationTypesUpdater mController;
@Mock
private Monitor mMonitor;
@Before
@@ -75,6 +75,7 @@ public class ComplicationTypesUpdaterTest extends SysuiTestCase {
MockitoAnnotations.initMocks(this);
when(mDreamBackend.getEnabledComplications()).thenReturn(new HashSet<>());
mMonitor = SelfExecutingMonitor.createInstance();
mController = new ComplicationTypesUpdater(mDreamBackend, mExecutor,
mSecureSettings, mDreamOverlayStateController, mMonitor);
}

View File

@@ -29,6 +29,7 @@ import android.view.View;
import androidx.test.filters.SmallTest;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.condition.SelfExecutingMonitor;
import com.android.systemui.dreams.DreamOverlayStateController;
import com.android.systemui.shared.condition.Monitor;
@@ -70,13 +71,13 @@ public class DreamClockTimeComplicationTest extends SysuiTestCase {
@Mock
private ComplicationLayoutParams mLayoutParams;
@Mock
private Monitor mMonitor;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(mDreamClockTimeViewHolderProvider.get()).thenReturn(mDreamClockTimeViewHolder);
mMonitor = SelfExecutingMonitor.createInstance();
}
/**

View File

@@ -38,6 +38,7 @@ import com.android.internal.logging.UiEventLogger;
import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.animation.view.LaunchableImageView;
import com.android.systemui.condition.SelfExecutingMonitor;
import com.android.systemui.controls.ControlsServiceInfo;
import com.android.systemui.controls.controller.ControlsController;
import com.android.systemui.controls.controller.StructureInfo;
@@ -102,7 +103,6 @@ public class DreamHomeControlsComplicationTest extends SysuiTestCase {
@Captor
private ArgumentCaptor<DreamOverlayStateController.Callback> mStateCallbackCaptor;
@Mock
private Monitor mMonitor;
@Before
@@ -116,6 +116,8 @@ public class DreamHomeControlsComplicationTest extends SysuiTestCase {
Optional.of(mControlsListingController));
when(mControlsComponent.getVisibility()).thenReturn(AVAILABLE);
when(mView.findViewById(R.id.home_controls_chip)).thenReturn(mHomeControlsView);
mMonitor = SelfExecutingMonitor.createInstance();
}
@Test

View File

@@ -30,6 +30,7 @@ import android.view.View;
import androidx.test.filters.SmallTest;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.condition.SelfExecutingMonitor;
import com.android.systemui.dreams.DreamOverlayStateController;
import com.android.systemui.dreams.smartspace.DreamSmartspaceController;
import com.android.systemui.plugins.BcSmartspaceDataPlugin;
@@ -64,7 +65,6 @@ public class SmartSpaceComplicationTest extends SysuiTestCase {
@Mock
private View mBcSmartspaceView;
@Mock
private Monitor mMonitor;
private final Set<Condition> mPreconditions = new HashSet<>();
@@ -72,6 +72,7 @@ public class SmartSpaceComplicationTest extends SysuiTestCase {
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mMonitor = SelfExecutingMonitor.createInstance();
}
/**

View File

@@ -0,0 +1,62 @@
/*
* 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.condition;
import androidx.annotation.NonNull;
import com.android.systemui.shared.condition.Monitor;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;
/**
* {@link SelfExecutingMonitor} creates a monitor that independently executes its logic through
* a {@link FakeExecutor}, which is ran at when a subscription is added and removed.
*/
public class SelfExecutingMonitor extends Monitor {
private final FakeExecutor mExecutor;
/**
* Default constructor that allows specifying the FakeExecutor to use.
*/
public SelfExecutingMonitor(FakeExecutor executor) {
super(executor);
mExecutor = executor;
}
@Override
public Subscription.Token addSubscription(@NonNull Subscription subscription) {
final Subscription.Token result = super.addSubscription(subscription);
mExecutor.runAllReady();
return result;
}
@Override
public void removeSubscription(@NonNull Subscription.Token token) {
super.removeSubscription(token);
mExecutor.runNextReady();
}
/**
* Creates a {@link SelfExecutingMonitor} with a self-managed {@link FakeExecutor}. Use only
* for cases where condition state only will be set at when a subscription is added.
*/
public static SelfExecutingMonitor createInstance() {
final FakeSystemClock mFakeSystemClock = new FakeSystemClock();
final FakeExecutor mExecutor = new FakeExecutor(mFakeSystemClock);
return new SelfExecutingMonitor(mExecutor);
}
}