Monitor add callback uniqueness.

Only allow the same instance of callback registered once in the
conditions monitor.

Test: atest ConditionMonitorTest#addCallback_withMultipleInstancesOfTheSameCallback_registerOnlyOne
Fix: 218921598
Change-Id: Ibbce271446e4039759e27bf889c4c672ac026c16
This commit is contained in:
Darrell Shi
2022-02-11 01:50:23 +00:00
parent 52dbc0d234
commit 59594bee0e
2 changed files with 19 additions and 0 deletions

View File

@@ -145,6 +145,10 @@ public class Monitor implements CallbackController<Monitor.Callback> {
}
private void addCallbackLocked(@NotNull Callback callback) {
if (mCallbacks.contains(callback)) {
return;
}
if (shouldLog()) Log.d(mTag, "adding callback");
mCallbacks.add(callback);

View File

@@ -194,6 +194,21 @@ public class ConditionMonitorTest extends SysuiTestCase {
verify(callback).onConditionsChanged(true);
}
@Test
public void addCallback_withMultipleInstancesOfTheSameCallback_registerOnlyOne() {
final Monitor monitor = new Monitor(mExecutor, new HashSet<>(), null /*callbacks*/);
final Monitor.Callback callback = mock(Monitor.Callback.class);
// Adds the same instance multiple times.
monitor.addCallback(callback);
monitor.addCallback(callback);
monitor.addCallback(callback);
mExecutor.runAllReady();
// Callback should only be triggered once.
verify(callback, times(1)).onConditionsChanged(true);
}
@Test
public void removeCallback_shouldNoLongerReceiveUpdate() {
final Condition condition = mock(Condition.class);